Search in sources :

Example 1 with ResultIterator

use of org.apache.cayenne.ResultIterator in project cayenne by apache.

the class MixedResultIncrementalFaultList method fillIn.

@Override
protected void fillIn(final Query query, List<Object> elementsList) {
    elementsList.clear();
    try (ResultIterator it = dataContext.performIteratedQuery(query)) {
        while (it.hasNextRow()) {
            elementsList.add(it.nextRow());
        }
    }
    unfetchedObjects = elementsList.size();
}
Also used : ResultIterator(org.apache.cayenne.ResultIterator)

Example 2 with ResultIterator

use of org.apache.cayenne.ResultIterator in project cayenne by apache.

the class BatchAction method processGeneratedKeys.

/**
 * Implements generated keys extraction supported in JDBC 3.0 specification.
 *
 * @since 4.0
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
protected void processGeneratedKeys(Statement statement, OperationObserver observer, BatchQueryRow row) throws SQLException, CayenneException {
    ResultSet keysRS = statement.getGeneratedKeys();
    // TODO: andrus, 7/4/2007 - (1) get the type of meaningful PK's from
    // their
    // ObjAttributes; (2) use a different form of Statement.execute -
    // "execute(String,String[])" to be able to map generated column names
    // (this way
    // we can support multiple columns.. although need to check how well
    // this works
    // with most common drivers)
    RowDescriptorBuilder builder = new RowDescriptorBuilder();
    if (this.keyRowDescriptor == null) {
        // attempt to figure out the right descriptor from the mapping...
        Collection<DbAttribute> generated = query.getDbEntity().getGeneratedAttributes();
        if (generated.size() == 1) {
            DbAttribute key = generated.iterator().next();
            ColumnDescriptor[] columns = new ColumnDescriptor[1];
            // use column name from result set, but type and Java class from
            // DB
            // attribute
            columns[0] = new ColumnDescriptor(keysRS.getMetaData(), 1);
            columns[0].setJdbcType(key.getType());
            columns[0].setJavaClass(TypesMapping.getJavaBySqlType(key.getType()));
            builder.setColumns(columns);
        } else {
            builder.setResultSet(keysRS);
        }
        this.keyRowDescriptor = builder.getDescriptor(dataNode.getAdapter().getExtendedTypes());
    }
    RowReader<?> rowReader = dataNode.rowReader(keyRowDescriptor, query.getMetaData(dataNode.getEntityResolver()), Collections.<ObjAttribute, ColumnDescriptor>emptyMap());
    ResultIterator iterator = new JDBCResultIterator(null, keysRS, rowReader);
    observer.nextGeneratedRows(query, iterator, row.getObjectId());
}
Also used : DbAttribute(org.apache.cayenne.map.DbAttribute) ResultIterator(org.apache.cayenne.ResultIterator)

Example 3 with ResultIterator

use of org.apache.cayenne.ResultIterator in project cayenne by apache.

the class DataNodeQueryAction method runQuery.

public void runQuery(Connection connection, final Query originalQuery) throws SQLException, Exception {
    // wrap to ensure that the result is mapped back to the original query, even if
    // the underlying SQLAction uses query substitute...
    OperationObserver wrapper = new OperationObserver() {

        @Override
        public void nextBatchCount(Query query, int[] resultCount) {
            observer.nextBatchCount(originalQuery, resultCount);
        }

        @Override
        public void nextCount(Query query, int resultCount) {
            observer.nextCount(originalQuery, resultCount);
        }

        @Override
        public void nextRows(Query query, List<?> dataRows) {
            observer.nextRows(originalQuery, dataRows);
        }

        @Override
        public void nextRows(Query q, ResultIterator it) {
            observer.nextRows(originalQuery, it);
        }

        @Override
        public void nextGeneratedRows(Query query, ResultIterator keys, ObjectId idToUpdate) {
            observer.nextGeneratedRows(originalQuery, keys, idToUpdate);
        }

        @Override
        public void nextGlobalException(Exception ex) {
            observer.nextGlobalException(ex);
        }

        @Override
        public void nextQueryException(Query query, Exception ex) {
            observer.nextQueryException(originalQuery, ex);
        }

        @Override
        public boolean isIteratedResult() {
            return observer.isIteratedResult();
        }
    };
    SQLAction action = node.getAdapter().getAction(originalQuery, node);
    action.performAction(connection, wrapper);
}
Also used : Query(org.apache.cayenne.query.Query) ObjectId(org.apache.cayenne.ObjectId) ResultIterator(org.apache.cayenne.ResultIterator) SQLAction(org.apache.cayenne.query.SQLAction) List(java.util.List) SQLException(java.sql.SQLException)

Example 4 with ResultIterator

use of org.apache.cayenne.ResultIterator in project cayenne by apache.

the class IncrementalFaultList method fillIn.

/**
 * Performs initialization of the list of objects. Only the first page is
 * fully resolved. For the rest of the list, only ObjectIds are read.
 *
 * @since 3.0
 */
protected void fillIn(final Query query, List<Object> elementsList) {
    elementsList.clear();
    try (ResultIterator it = dataContext.performIteratedQuery(query)) {
        while (it.hasNextRow()) {
            elementsList.add(it.nextRow());
        }
    }
    unfetchedObjects = elementsList.size();
}
Also used : ResultIterator(org.apache.cayenne.ResultIterator)

Example 5 with ResultIterator

use of org.apache.cayenne.ResultIterator in project cayenne by apache.

the class SQLTemplateAction method processSelectResult.

@SuppressWarnings({ "unchecked", "rawtypes" })
protected void processSelectResult(SQLStatement compiled, Connection connection, Statement statement, ResultSet resultSet, OperationObserver callback, final long startTime) throws Exception {
    boolean iteratedResult = callback.isIteratedResult();
    ExtendedTypeMap types = dataNode.getAdapter().getExtendedTypes();
    RowDescriptorBuilder builder = configureRowDescriptorBuilder(compiled, resultSet);
    RowReader<?> rowReader = dataNode.rowReader(builder.getDescriptor(types), queryMetadata);
    ResultIterator it = new JDBCResultIterator(statement, resultSet, rowReader);
    if (iteratedResult) {
        it = new ConnectionAwareResultIterator(it, connection) {

            @Override
            protected void doClose() {
                dataNode.getJdbcEventLogger().logSelectCount(rowCounter, System.currentTimeMillis() - startTime);
                super.doClose();
            }
        };
    }
    it = new LimitResultIterator(it, getFetchOffset(), query.getFetchLimit());
    if (iteratedResult) {
        try {
            callback.nextRows(query, it);
        } catch (Exception ex) {
            it.close();
            throw ex;
        }
    } else {
        // note that we are not closing the iterator here, relying on caller
        // to close the underlying ResultSet on its own... this is a hack,
        // maybe a cleaner flow is due here.
        List<DataRow> resultRows = (List<DataRow>) it.allRows();
        dataNode.getJdbcEventLogger().logSelectCount(resultRows.size(), System.currentTimeMillis() - startTime);
        callback.nextRows(query, resultRows);
    }
}
Also used : ResultIterator(org.apache.cayenne.ResultIterator) DataRow(org.apache.cayenne.DataRow) SQLException(java.sql.SQLException) CayenneException(org.apache.cayenne.CayenneException) ExtendedTypeMap(org.apache.cayenne.access.types.ExtendedTypeMap) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

ResultIterator (org.apache.cayenne.ResultIterator)6 SQLException (java.sql.SQLException)3 List (java.util.List)2 DataRow (org.apache.cayenne.DataRow)2 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1 CayenneException (org.apache.cayenne.CayenneException)1 ObjectId (org.apache.cayenne.ObjectId)1 DbAttributeBinding (org.apache.cayenne.access.translator.DbAttributeBinding)1 SelectTranslator (org.apache.cayenne.access.translator.select.SelectTranslator)1 ExtendedTypeMap (org.apache.cayenne.access.types.ExtendedTypeMap)1 JdbcEventLogger (org.apache.cayenne.log.JdbcEventLogger)1 DbAttribute (org.apache.cayenne.map.DbAttribute)1 Query (org.apache.cayenne.query.Query)1 SQLAction (org.apache.cayenne.query.SQLAction)1