use of org.adbcj.Field in project adbcj by mheath.
the class ProtocolHandler method doRowDescription.
private void doRowDescription(AbstractConnection connection, RowDescriptionMessage rowDescriptionMessage) {
AbstractDbSession.Request<Object> request = connection.getActiveRequest();
if (request == null) {
throw new IllegalStateException("Received a row description without an active request");
}
//logger.debug("Received row description for request {}", request);
request.getEventHandler().startFields(request.getAccumulator());
for (Field field : rowDescriptionMessage.getFields()) {
request.getEventHandler().field(field, request.getAccumulator());
}
request.getEventHandler().endFields(request.getAccumulator());
request.getEventHandler().startResults(request.getAccumulator());
}
use of org.adbcj.Field 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();
}
}
}
});
}
use of org.adbcj.Field in project adbcj by mheath.
the class DefaultResultSet method getField.
public Field getField(Object key) {
if (key == null) {
return null;
}
if (key instanceof Field) {
return (Field) key;
}
Field field = fieldMapping.get(key);
if (field != null) {
return field;
}
if (key instanceof Number) {
int index = ((Number) key).intValue();
field = fields.get(index);
fieldMapping.put(key, field);
return field;
}
String stringKey = key.toString();
if (stringKey == null) {
return null;
}
// Search by column label
for (Field f : fields) {
if (stringKey.equals(f.getColumnLabel())) {
fieldMapping.put(key, f);
return f;
}
}
// Search by column name
for (Field f : fields) {
if (stringKey.equals(f.getColumnName())) {
fieldMapping.put(key, f);
return f;
}
}
// Search by table label.column label
for (Field f : fields) {
if (stringKey.equals(f.getTableLabel() + "." + f.getColumnLabel())) {
fieldMapping.put(key, f);
return f;
}
}
// Search by table name.column name
for (Field f : fields) {
if (stringKey.equals(f.getTableName() + "." + f.getColumnName())) {
fieldMapping.put(key, f);
return f;
}
}
return null;
}
use of org.adbcj.Field in project adbcj by mheath.
the class DefaultRow method get.
@Override
public Value get(Object key) {
Field field = resultSet.getField(key);
Value value = values[field.getIndex()];
return value;
}
Aggregations