use of org.adbcj.support.DefaultField in project adbcj by mheath.
the class JdbcConnection method executeQuery.
public <T> DbSessionFuture<T> executeQuery(final String sql, final ResultEventHandler<T> eventHandler, final T accumulator) {
checkClosed();
logger.trace("Scheduling query '{}'", sql);
return enqueueTransactionalRequest(new CallableRequest<T>() {
@Override
protected T doCall() throws Exception {
logger.debug("Executing query '{}'", sql);
Statement jdbcStatement = jdbcConnection.createStatement();
java.sql.ResultSet jdbcResultSet = null;
try {
// Execute query
jdbcResultSet = jdbcStatement.executeQuery(sql);
// Fetch meta data
ResultSetMetaData metaData = jdbcResultSet.getMetaData();
int columnCount = metaData.getColumnCount();
List<Field> fields = new ArrayList<Field>(columnCount);
eventHandler.startFields(accumulator);
for (int i = 1; i <= columnCount; i++) {
Field field = new DefaultField(i - 1, metaData.getCatalogName(i), metaData.getSchemaName(i), metaData.getTableName(i), metaData.getTableName(i), Type.fromJdbcType(metaData.getColumnType(i)), metaData.getColumnLabel(i), metaData.getCatalogName(i), metaData.getPrecision(i), metaData.getScale(i), metaData.isAutoIncrement(i), metaData.isCaseSensitive(i), metaData.isCurrency(i), metaData.isDefinitelyWritable(i), metaData.isNullable(i) == 1, metaData.isReadOnly(i), metaData.isSearchable(i), metaData.isSigned(i), metaData.isWritable(i), metaData.getColumnClassName(i));
fields.add(field);
eventHandler.field(field, accumulator);
}
eventHandler.endFields(accumulator);
eventHandler.startResults(accumulator);
while (jdbcResultSet.next()) {
eventHandler.startRow(accumulator);
for (int i = 1; i <= columnCount; i++) {
Field field = fields.get(i - 1);
Object value = null;
switch(field.getColumnType()) {
case BIGINT:
value = jdbcResultSet.getLong(i);
break;
case INTEGER:
value = jdbcResultSet.getInt(i);
break;
case VARCHAR:
value = jdbcResultSet.getString(i);
break;
default:
throw new IllegalStateException("Don't know how to handle field to type " + field.getColumnType());
}
if (jdbcResultSet.wasNull()) {
value = null;
}
eventHandler.value(new DefaultValue(field, value), accumulator);
}
eventHandler.endRow(accumulator);
}
eventHandler.endResults(accumulator);
return accumulator;
} finally {
if (jdbcResultSet != null) {
jdbcResultSet.close();
}
if (jdbcStatement != null) {
jdbcStatement.close();
}
}
}
});
}
Aggregations