Search in sources :

Example 6 with ResultIterator

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

the class SelectAction method performAction.

@SuppressWarnings({ "unchecked", "rawtypes", "resource" })
@Override
public void performAction(Connection connection, OperationObserver observer) throws SQLException, Exception {
    final long t1 = System.currentTimeMillis();
    JdbcEventLogger logger = dataNode.getJdbcEventLogger();
    SelectTranslator translator = dataNode.selectTranslator(query);
    final String sql = translator.getSql();
    final DbAttributeBinding[] bindings = translator.getBindings();
    logger.logQuery(sql, bindings);
    PreparedStatement statement = connection.prepareStatement(sql);
    bind(dataNode.getAdapter(), statement, bindings);
    int fetchSize = queryMetadata.getStatementFetchSize();
    if (fetchSize != 0) {
        statement.setFetchSize(fetchSize);
    }
    ResultSet rs;
    // exception happens
    try {
        rs = statement.executeQuery();
    } catch (Exception ex) {
        statement.close();
        throw ex;
    }
    RowDescriptor descriptor = new RowDescriptorBuilder().setColumns(translator.getResultColumns()).getDescriptor(dataNode.getAdapter().getExtendedTypes());
    RowReader<?> rowReader = dataNode.rowReader(descriptor, queryMetadata, translator.getAttributeOverrides());
    ResultIterator it = new JDBCResultIterator(statement, rs, rowReader);
    it = forIteratedResult(it, observer, connection, t1, sql);
    it = forSuppressedDistinct(it, translator);
    it = forFetchLimit(it, translator);
    if (observer.isIteratedResult()) {
        try {
            observer.nextRows(query, it);
        } catch (Exception ex) {
            it.close();
            throw ex;
        }
    } else {
        List<DataRow> resultRows;
        try {
            resultRows = it.allRows();
        } finally {
            it.close();
        }
        dataNode.getJdbcEventLogger().logSelectCount(resultRows.size(), System.currentTimeMillis() - t1, sql);
        observer.nextRows(query, resultRows);
    }
}
Also used : ResultIterator(org.apache.cayenne.ResultIterator) DbAttributeBinding(org.apache.cayenne.access.translator.DbAttributeBinding) PreparedStatement(java.sql.PreparedStatement) DataRow(org.apache.cayenne.DataRow) SQLException(java.sql.SQLException) JdbcEventLogger(org.apache.cayenne.log.JdbcEventLogger) SelectTranslator(org.apache.cayenne.access.translator.select.SelectTranslator) ResultSet(java.sql.ResultSet)

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