use of org.apache.cayenne.dba.JdbcAdapter in project cayenne by apache.
the class BatchActionGeneratedIT method testHasGeneratedKeys2.
@Test
public void testHasGeneratedKeys2() throws Exception {
EntityResolver resolver = runtime.getChannel().getEntityResolver();
// test with adapter that does not support keys...
JdbcAdapter adapter = buildAdapter(false);
InsertBatchQuery batch1 = new InsertBatchQuery(resolver.getObjEntity(GeneratedColumnTestEntity.class).getDbEntity(), 5);
DataNode node = new DataNode();
node.setAdapter(adapter);
node.setEntityResolver(resolver);
node.setRowReaderFactory(mock(RowReaderFactory.class));
assertFalse(new BatchAction(batch1, node, false).hasGeneratedKeys());
}
use of org.apache.cayenne.dba.JdbcAdapter in project cayenne by apache.
the class BatchActionIT method buildAdapter.
JdbcAdapter buildAdapter(boolean supportGeneratedKeys) {
JdbcAdapter adapter = objectFactory.newInstance(JdbcAdapter.class, JdbcAdapter.class.getName());
adapter.setSupportsGeneratedKeys(supportGeneratedKeys);
return adapter;
}
use of org.apache.cayenne.dba.JdbcAdapter in project cayenne by apache.
the class BatchActionLockingIT method testRunAsIndividualQueriesOptimisticLockingFailure.
@Test
public void testRunAsIndividualQueriesOptimisticLockingFailure() throws Exception {
EntityResolver resolver = runtime.getDataDomain().getEntityResolver();
// test with adapter that supports keys...
JdbcAdapter adapter = buildAdapter(true);
DbEntity dbEntity = resolver.getObjEntity(SimpleLockingTestEntity.class).getDbEntity();
List<DbAttribute> qualifierAttributes = Arrays.asList(dbEntity.getAttribute("LOCKING_TEST_ID"), dbEntity.getAttribute("NAME"));
Collection<String> nullAttributeNames = Collections.singleton("NAME");
Map<String, Object> qualifierSnapshot = new HashMap<>();
qualifierSnapshot.put("LOCKING_TEST_ID", 1);
DeleteBatchQuery batchQuery = new DeleteBatchQuery(dbEntity, qualifierAttributes, nullAttributeNames, 5);
batchQuery.setUsingOptimisticLocking(true);
batchQuery.add(qualifierSnapshot);
DeleteBatchTranslator batchQueryBuilder = new DeleteBatchTranslator(batchQuery, adapter, null);
MockConnection mockConnection = new MockConnection();
PreparedStatementResultSetHandler preparedStatementResultSetHandler = mockConnection.getPreparedStatementResultSetHandler();
preparedStatementResultSetHandler.setExactMatch(false);
preparedStatementResultSetHandler.setCaseSensitive(false);
preparedStatementResultSetHandler.prepareUpdateCount("DELETE", 0);
boolean generatesKeys = false;
DataNode node = new DataNode();
node.setAdapter(adapter);
node.setEntityResolver(resolver);
node.setRowReaderFactory(mock(RowReaderFactory.class));
BatchAction action = new BatchAction(batchQuery, node, false);
try {
action.runAsIndividualQueries(mockConnection, batchQueryBuilder, new MockOperationObserver(), generatesKeys);
fail("No OptimisticLockingFailureException thrown.");
} catch (OptimisticLockException e) {
}
assertEquals(0, mockConnection.getNumberCommits());
assertEquals(0, mockConnection.getNumberRollbacks());
}
use of org.apache.cayenne.dba.JdbcAdapter in project cayenne by apache.
the class BatchActionLockingIT method testRunAsIndividualQueriesSuccess.
@Test
public void testRunAsIndividualQueriesSuccess() throws Exception {
EntityResolver resolver = runtime.getDataDomain().getEntityResolver();
// test with adapter that supports keys...
JdbcAdapter adapter = buildAdapter(true);
DbEntity dbEntity = resolver.getObjEntity(SimpleLockingTestEntity.class).getDbEntity();
List<DbAttribute> qualifierAttributes = Arrays.asList(dbEntity.getAttribute("LOCKING_TEST_ID"), dbEntity.getAttribute("NAME"));
Collection<String> nullAttributeNames = Collections.singleton("NAME");
Map<String, Object> qualifierSnapshot = new HashMap<>();
qualifierSnapshot.put("LOCKING_TEST_ID", new Integer(1));
DeleteBatchQuery batchQuery = new DeleteBatchQuery(dbEntity, qualifierAttributes, nullAttributeNames, 5);
batchQuery.setUsingOptimisticLocking(true);
batchQuery.add(qualifierSnapshot);
DeleteBatchTranslator batchQueryBuilder = new DeleteBatchTranslator(batchQuery, adapter, null);
MockConnection mockConnection = new MockConnection();
PreparedStatementResultSetHandler preparedStatementResultSetHandler = mockConnection.getPreparedStatementResultSetHandler();
preparedStatementResultSetHandler.setExactMatch(false);
preparedStatementResultSetHandler.setCaseSensitive(false);
preparedStatementResultSetHandler.prepareUpdateCount("DELETE", 1);
boolean generatesKeys = false;
DataNode node = new DataNode();
node.setAdapter(adapter);
node.setEntityResolver(resolver);
node.setRowReaderFactory(mock(RowReaderFactory.class));
BatchAction action = new BatchAction(batchQuery, node, false);
action.runAsIndividualQueries(mockConnection, batchQueryBuilder, new MockOperationObserver(), generatesKeys);
assertEquals(0, mockConnection.getNumberCommits());
assertEquals(0, mockConnection.getNumberRollbacks());
}
use of org.apache.cayenne.dba.JdbcAdapter in project cayenne by apache.
the class DeleteBatchTranslatorIT method testCreateSqlStringWithIdentifiersQuote.
@Test
public void testCreateSqlStringWithIdentifiersQuote() throws Exception {
DbEntity entity = runtime.getDataDomain().getEntityResolver().getObjEntity(SimpleLockingTestEntity.class).getDbEntity();
try {
entity.getDataMap().setQuotingSQLIdentifiers(true);
List<DbAttribute> idAttributes = Collections.singletonList(entity.getAttribute("LOCKING_TEST_ID"));
DeleteBatchQuery deleteQuery = new DeleteBatchQuery(entity, idAttributes, Collections.<String>emptySet(), 1);
JdbcAdapter adapter = (JdbcAdapter) this.adapter;
DeleteBatchTranslator builder = new DeleteBatchTranslator(deleteQuery, adapter, null);
String generatedSql = builder.getSql();
String charStart = unitAdapter.getIdentifiersStartQuote();
String charEnd = unitAdapter.getIdentifiersEndQuote();
assertNotNull(generatedSql);
assertEquals("DELETE FROM " + charStart + entity.getName() + charEnd + " WHERE " + charStart + "LOCKING_TEST_ID" + charEnd + " = ?", generatedSql);
} finally {
entity.getDataMap().setQuotingSQLIdentifiers(false);
}
}
Aggregations