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.