Search in sources :

Example 6 with DeleteBatchQuery

use of org.apache.cayenne.query.DeleteBatchQuery 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());
}
Also used : JdbcAdapter(org.apache.cayenne.dba.JdbcAdapter) HashMap(java.util.HashMap) DeleteBatchQuery(org.apache.cayenne.query.DeleteBatchQuery) SimpleLockingTestEntity(org.apache.cayenne.testdo.locking.SimpleLockingTestEntity) DbAttribute(org.apache.cayenne.map.DbAttribute) RowReaderFactory(org.apache.cayenne.access.jdbc.reader.RowReaderFactory) OptimisticLockException(org.apache.cayenne.access.OptimisticLockException) EntityResolver(org.apache.cayenne.map.EntityResolver) MockConnection(com.mockrunner.mock.jdbc.MockConnection) DbEntity(org.apache.cayenne.map.DbEntity) PreparedStatementResultSetHandler(com.mockrunner.jdbc.PreparedStatementResultSetHandler) DataNode(org.apache.cayenne.access.DataNode) DeleteBatchTranslator(org.apache.cayenne.access.translator.batch.DeleteBatchTranslator) MockOperationObserver(org.apache.cayenne.access.MockOperationObserver) Test(org.junit.Test)

Example 7 with DeleteBatchQuery

use of org.apache.cayenne.query.DeleteBatchQuery 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());
}
Also used : JdbcAdapter(org.apache.cayenne.dba.JdbcAdapter) HashMap(java.util.HashMap) DeleteBatchQuery(org.apache.cayenne.query.DeleteBatchQuery) SimpleLockingTestEntity(org.apache.cayenne.testdo.locking.SimpleLockingTestEntity) DbAttribute(org.apache.cayenne.map.DbAttribute) RowReaderFactory(org.apache.cayenne.access.jdbc.reader.RowReaderFactory) EntityResolver(org.apache.cayenne.map.EntityResolver) MockConnection(com.mockrunner.mock.jdbc.MockConnection) DbEntity(org.apache.cayenne.map.DbEntity) PreparedStatementResultSetHandler(com.mockrunner.jdbc.PreparedStatementResultSetHandler) DataNode(org.apache.cayenne.access.DataNode) DeleteBatchTranslator(org.apache.cayenne.access.translator.batch.DeleteBatchTranslator) MockOperationObserver(org.apache.cayenne.access.MockOperationObserver) Test(org.junit.Test)

Example 8 with DeleteBatchQuery

use of org.apache.cayenne.query.DeleteBatchQuery in project cayenne by apache.

the class DeleteBatchTranslatorIT method testCreateSqlString.

@Test
public void testCreateSqlString() throws Exception {
    DbEntity entity = runtime.getDataDomain().getEntityResolver().getObjEntity(SimpleLockingTestEntity.class).getDbEntity();
    List<DbAttribute> idAttributes = Collections.singletonList(entity.getAttribute("LOCKING_TEST_ID"));
    DeleteBatchQuery deleteQuery = new DeleteBatchQuery(entity, idAttributes, Collections.<String>emptySet(), 1);
    DbAdapter adapter = objectFactory.newInstance(DbAdapter.class, JdbcAdapter.class.getName());
    DeleteBatchTranslator builder = new DeleteBatchTranslator(deleteQuery, adapter, null);
    String generatedSql = builder.getSql();
    assertNotNull(generatedSql);
    assertEquals("DELETE FROM " + entity.getName() + " WHERE LOCKING_TEST_ID = ?", generatedSql);
}
Also used : JdbcAdapter(org.apache.cayenne.dba.JdbcAdapter) DbEntity(org.apache.cayenne.map.DbEntity) UnitDbAdapter(org.apache.cayenne.unit.UnitDbAdapter) DbAdapter(org.apache.cayenne.dba.DbAdapter) DeleteBatchQuery(org.apache.cayenne.query.DeleteBatchQuery) SimpleLockingTestEntity(org.apache.cayenne.testdo.locking.SimpleLockingTestEntity) DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

Example 9 with DeleteBatchQuery

use of org.apache.cayenne.query.DeleteBatchQuery in project cayenne by apache.

the class DeleteBatchTranslatorIT method testCreateSqlStringWithNulls.

@Test
public void testCreateSqlStringWithNulls() throws Exception {
    DbEntity entity = runtime.getDataDomain().getEntityResolver().getObjEntity(SimpleLockingTestEntity.class).getDbEntity();
    List<DbAttribute> idAttributes = Arrays.asList(entity.getAttribute("LOCKING_TEST_ID"), entity.getAttribute("NAME"));
    Collection<String> nullAttributes = Collections.singleton("NAME");
    DeleteBatchQuery deleteQuery = new DeleteBatchQuery(entity, idAttributes, nullAttributes, 1);
    DbAdapter adapter = objectFactory.newInstance(DbAdapter.class, JdbcAdapter.class.getName());
    DeleteBatchTranslator builder = new DeleteBatchTranslator(deleteQuery, adapter, null);
    String generatedSql = builder.getSql();
    assertNotNull(generatedSql);
    assertEquals("DELETE FROM " + entity.getName() + " WHERE LOCKING_TEST_ID = ? AND NAME IS NULL", generatedSql);
}
Also used : JdbcAdapter(org.apache.cayenne.dba.JdbcAdapter) DbEntity(org.apache.cayenne.map.DbEntity) UnitDbAdapter(org.apache.cayenne.unit.UnitDbAdapter) DbAdapter(org.apache.cayenne.dba.DbAdapter) DeleteBatchQuery(org.apache.cayenne.query.DeleteBatchQuery) SimpleLockingTestEntity(org.apache.cayenne.testdo.locking.SimpleLockingTestEntity) DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

Example 10 with DeleteBatchQuery

use of org.apache.cayenne.query.DeleteBatchQuery 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);
    }
}
Also used : JdbcAdapter(org.apache.cayenne.dba.JdbcAdapter) DbEntity(org.apache.cayenne.map.DbEntity) DeleteBatchQuery(org.apache.cayenne.query.DeleteBatchQuery) SimpleLockingTestEntity(org.apache.cayenne.testdo.locking.SimpleLockingTestEntity) DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

Aggregations

DeleteBatchQuery (org.apache.cayenne.query.DeleteBatchQuery)15 DbAttribute (org.apache.cayenne.map.DbAttribute)12 DbEntity (org.apache.cayenne.map.DbEntity)10 Test (org.junit.Test)9 JdbcAdapter (org.apache.cayenne.dba.JdbcAdapter)7 SimpleLockingTestEntity (org.apache.cayenne.testdo.locking.SimpleLockingTestEntity)6 HashMap (java.util.HashMap)3 DbAttributeBinding (org.apache.cayenne.access.translator.DbAttributeBinding)3 SoftDelete (org.apache.cayenne.testdo.soft_delete.SoftDelete)3 PreparedStatementResultSetHandler (com.mockrunner.jdbc.PreparedStatementResultSetHandler)2 MockConnection (com.mockrunner.mock.jdbc.MockConnection)2 Map (java.util.Map)2 DataNode (org.apache.cayenne.access.DataNode)2 MockOperationObserver (org.apache.cayenne.access.MockOperationObserver)2 RowReaderFactory (org.apache.cayenne.access.jdbc.reader.RowReaderFactory)2 DeleteBatchTranslator (org.apache.cayenne.access.translator.batch.DeleteBatchTranslator)2 ExtendedType (org.apache.cayenne.access.types.ExtendedType)2 DbAdapter (org.apache.cayenne.dba.DbAdapter)2 EntityResolver (org.apache.cayenne.map.EntityResolver)2 UnitDbAdapter (org.apache.cayenne.unit.UnitDbAdapter)2