1

I am new to apache metamodel. I am using it for getting sum of any numeric column in a database table using following code:

        Object object = null;
        long sum = 0;
        String columnName = table.getColumn(iColumnNumber).getName();
        Query query = dataContext.query().from(table).select(FunctionType.SUM, table.getColumnByName(sColumnName)).toQuery();
        org.eobjects.metamodel.data.DataSet ds = dataContext.executeQuery(query);
        try {
            ds.next();
            org.eobjects.metamodel.data.Row row = ds.getRow();
            try {
                object = row.getValue(0);
            } catch (Exception ex) {
                return 0;
            }
            if (object instanceof java.lang.Long) {
                sum = (long) object;
            } else if (object instanceof BigDecimal) {
                sum = ((BigDecimal) object).longValue();
            } else if (object instanceof java.lang.Integer) {
                sum = ((java.lang.Integer) object).longValue();
            }

        } catch (Exception ex) {
            return 0;
        } finally {
            if (ds != null) {
                ds.close();
            }
        }

The code works fine for small data but when I try the same code for very large data containing hundreds of thousands of rows. The code throws an exception: Could not get next record in resultset: Arithmetic overflow error converting expression to data type int.

When I run same query using sum function on the database table directly in the dbms script window, I get same error. If I cast the result of sum() function in bigint or numeric datatype the query returns the sum without giving any error. Therefore I think this error can be resolved by casting the result of sum() function in a suitable datatype in apache metamodel also. So can anyone let me know how can I cast query result into another datatype such as decimal instead of default type int.

TGiri
  • 131
  • 6

1 Answers1

0

First a few small remarks on your code:

  • Be careful with swallowing exceptions. You should at minimum log the exceptions that occur - right now you just return 0 which is misleading.
  • Instead of handling integer, long etc. differently, why not just let your method return Number?

With regards to the arithmethic overflow issue, if your database really returns BigDecimal (pretty unusual, but databases are sometimes like that) then you should probably look at questions/answers such as Converting BigDecimal to Integer

Community
  • 1
  • 1