Search in sources :

Example 1 with ExtendedTypeMap

use of org.apache.cayenne.access.types.ExtendedTypeMap in project cayenne by apache.

the class JDBCResultIteratorTest method testNextDataRow.

@Test
public void testNextDataRow() throws Exception {
    Connection c = new MockConnection();
    Statement s = new MockStatement(c);
    MockResultSet rs = new MockResultSet("rs");
    rs.addColumn("a", new Object[] { "1", "2", "3" });
    RowDescriptor descriptor = new RowDescriptorBuilder().setResultSet(rs).getDescriptor(new ExtendedTypeMap());
    RowReader<?> rowReader = new DefaultRowReaderFactory().rowReader(descriptor, new MockQueryMetadata(), mock(DbAdapter.class), Collections.<ObjAttribute, ColumnDescriptor>emptyMap());
    JDBCResultIterator it = new JDBCResultIterator(s, rs, rowReader);
    DataRow row = (DataRow) it.nextRow();
    assertNotNull(row);
    assertEquals(1, row.size());
    assertEquals("1", row.get("a"));
}
Also used : MockStatement(com.mockrunner.mock.jdbc.MockStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) MockConnection(com.mockrunner.mock.jdbc.MockConnection) MockResultSet(com.mockrunner.mock.jdbc.MockResultSet) MockConnection(com.mockrunner.mock.jdbc.MockConnection) DataRow(org.apache.cayenne.DataRow) ExtendedTypeMap(org.apache.cayenne.access.types.ExtendedTypeMap) DefaultRowReaderFactory(org.apache.cayenne.access.jdbc.reader.DefaultRowReaderFactory) MockQueryMetadata(org.apache.cayenne.query.MockQueryMetadata) DbAdapter(org.apache.cayenne.dba.DbAdapter) MockStatement(com.mockrunner.mock.jdbc.MockStatement) Test(org.junit.Test)

Example 2 with ExtendedTypeMap

use of org.apache.cayenne.access.types.ExtendedTypeMap 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

DataRow (org.apache.cayenne.DataRow)2 ExtendedTypeMap (org.apache.cayenne.access.types.ExtendedTypeMap)2 MockConnection (com.mockrunner.mock.jdbc.MockConnection)1 MockResultSet (com.mockrunner.mock.jdbc.MockResultSet)1 MockStatement (com.mockrunner.mock.jdbc.MockStatement)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 CayenneException (org.apache.cayenne.CayenneException)1 ResultIterator (org.apache.cayenne.ResultIterator)1 DefaultRowReaderFactory (org.apache.cayenne.access.jdbc.reader.DefaultRowReaderFactory)1 DbAdapter (org.apache.cayenne.dba.DbAdapter)1 MockQueryMetadata (org.apache.cayenne.query.MockQueryMetadata)1 Test (org.junit.Test)1