Search in sources :

Example 1 with OperationObserver

use of org.apache.cayenne.access.OperationObserver in project cayenne by apache.

the class SchemaUpdateStrategyIT method testCreateIfNoSchemaStrategy.

@Test
public void testCreateIfNoSchemaStrategy() throws Exception {
    setStrategy(CreateIfNoSchemaStrategy.class);
    String template = "SELECT #result('id' 'int') FROM SUS1";
    SQLTemplate query = new SQLTemplate(Object.class, template);
    OperationObserver observer = new MockOperationObserver();
    node.performQueries(Collections.singletonList((Query) query), observer);
    Map<String, Boolean> nameTables = tablesMap();
    assertTrue(nameTables.get("SUS1"));
    assertEquals(2, existingTables().size());
    node.performQueries(Collections.singletonList(query), observer);
    assertEquals(2, existingTables().size());
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) Query(org.apache.cayenne.query.Query) MockOperationObserver(org.apache.cayenne.access.MockOperationObserver) OperationObserver(org.apache.cayenne.access.OperationObserver) MockOperationObserver(org.apache.cayenne.access.MockOperationObserver) Test(org.junit.Test)

Example 2 with OperationObserver

use of org.apache.cayenne.access.OperationObserver in project cayenne by apache.

the class FrontBasePkGenerator method longPkFromDatabase.

/**
 * @since 3.0
 */
@Override
protected long longPkFromDatabase(DataNode node, DbEntity entity) throws Exception {
    String template = "SELECT #result('UNIQUE' 'long') FROM " + entity.getName();
    final long[] pkHolder = new long[1];
    SQLTemplate query = new SQLTemplate(entity, template);
    OperationObserver observer = new DoNothingOperationObserver() {

        @Override
        public void nextRows(Query query, List<?> dataRows) {
            if (dataRows.size() != 1) {
                throw new CayenneRuntimeException("Error fetching PK. Expected one row, got %d", dataRows.size());
            }
            DataRow row = (DataRow) dataRows.get(0);
            Number pk = (Number) row.get("UNIQUE");
            pkHolder[0] = pk.longValue();
        }
    };
    node.performQueries(Collections.singleton((Query) query), observer);
    return pkHolder[0];
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) Query(org.apache.cayenne.query.Query) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) OperationObserver(org.apache.cayenne.access.OperationObserver) DoNothingOperationObserver(org.apache.cayenne.access.util.DoNothingOperationObserver) ArrayList(java.util.ArrayList) List(java.util.List) DoNothingOperationObserver(org.apache.cayenne.access.util.DoNothingOperationObserver) DataRow(org.apache.cayenne.DataRow)

Example 3 with OperationObserver

use of org.apache.cayenne.access.OperationObserver in project cayenne by apache.

the class SQLServerProcedureAction method performAction.

@Override
public void performAction(Connection connection, OperationObserver observer) throws SQLException, Exception {
    ProcedureTranslator transl = createTranslator(connection);
    try (CallableStatement statement = (CallableStatement) transl.createStatement()) {
        // stored procedure may contain a mixture of update counts and
        // result sets,
        // and out parameters. Read out parameters first, then
        // iterate until we exhaust all results
        boolean hasResultSet = statement.execute();
        // local observer to cache results and provide them to the external
        // observer
        // in the order consistent with other adapters.
        Observer localObserver = new Observer(observer);
        while (true) {
            if (hasResultSet) {
                try (ResultSet rs = statement.getResultSet()) {
                    RowDescriptor descriptor = describeResultSet(rs, processedResultSets++);
                    readResultSet(rs, descriptor, query, localObserver);
                }
            } else {
                int updateCount = statement.getUpdateCount();
                if (updateCount == -1) {
                    break;
                }
                dataNode.getJdbcEventLogger().logUpdateCount(updateCount);
                localObserver.nextCount(query, updateCount);
            }
            hasResultSet = statement.getMoreResults();
        }
        // read out parameters to the main observer ... AFTER the main
        // result set
        // TODO: I hope SQLServer does not support ResultSets as OUT
        // parameters,
        // otherwise
        // the order of custom result descriptors will be messed up
        readProcedureOutParameters(statement, observer);
        // add results back to main observer
        localObserver.flushResults(query);
    }
}
Also used : CallableStatement(java.sql.CallableStatement) OperationObserver(org.apache.cayenne.access.OperationObserver) ResultSet(java.sql.ResultSet) ProcedureTranslator(org.apache.cayenne.access.translator.procedure.ProcedureTranslator) RowDescriptor(org.apache.cayenne.access.jdbc.RowDescriptor)

Aggregations

OperationObserver (org.apache.cayenne.access.OperationObserver)3 Query (org.apache.cayenne.query.Query)2 SQLTemplate (org.apache.cayenne.query.SQLTemplate)2 CallableStatement (java.sql.CallableStatement)1 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)1 DataRow (org.apache.cayenne.DataRow)1 MockOperationObserver (org.apache.cayenne.access.MockOperationObserver)1 RowDescriptor (org.apache.cayenne.access.jdbc.RowDescriptor)1 ProcedureTranslator (org.apache.cayenne.access.translator.procedure.ProcedureTranslator)1 DoNothingOperationObserver (org.apache.cayenne.access.util.DoNothingOperationObserver)1 Test (org.junit.Test)1