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