Search in sources :

Example 1 with Field

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());
}
Also used : Field(org.adbcj.Field) AbstractDbSession(org.adbcj.support.AbstractDbSession)

Example 2 with Field

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();
                }
            }
        }
    });
}
Also used : PreparedStatement(org.adbcj.PreparedStatement) Statement(java.sql.Statement) DbException(org.adbcj.DbException) SQLException(java.sql.SQLException) DbSessionClosedException(org.adbcj.DbSessionClosedException) ResultSetMetaData(java.sql.ResultSetMetaData) DefaultField(org.adbcj.support.DefaultField) Field(org.adbcj.Field) DefaultValue(org.adbcj.support.DefaultValue) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) DefaultField(org.adbcj.support.DefaultField)

Example 3 with Field

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;
}
Also used : Field(org.adbcj.Field)

Example 4 with Field

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;
}
Also used : Field(org.adbcj.Field) Value(org.adbcj.Value)

Aggregations

Field (org.adbcj.Field)4 ResultSetMetaData (java.sql.ResultSetMetaData)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 DbException (org.adbcj.DbException)1 DbSessionClosedException (org.adbcj.DbSessionClosedException)1 PreparedStatement (org.adbcj.PreparedStatement)1 Value (org.adbcj.Value)1 AbstractDbSession (org.adbcj.support.AbstractDbSession)1 DefaultField (org.adbcj.support.DefaultField)1 DefaultValue (org.adbcj.support.DefaultValue)1