Search in sources :

Example 1 with DefaultField

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();
                }
            }
        }
    });
}
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)

Aggregations

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 Field (org.adbcj.Field)1 PreparedStatement (org.adbcj.PreparedStatement)1 DefaultField (org.adbcj.support.DefaultField)1 DefaultValue (org.adbcj.support.DefaultValue)1