Search in sources :

Example 16 with JdbcAdapter

use of org.apache.cayenne.dba.JdbcAdapter in project cayenne by apache.

the class SoftDeleteBatchTranslatorIT method testUpdate.

@Test
public void testUpdate() throws Exception {
    final DbEntity entity = context.getEntityResolver().getObjEntity(SoftDelete.class).getDbEntity();
    JdbcAdapter adapter = (JdbcAdapter) this.adapter;
    BatchTranslatorFactory oldFactory = dataNode.getBatchTranslatorFactory();
    try {
        dataNode.setBatchTranslatorFactory(new SoftDeleteTranslatorFactory());
        final SoftDelete test = context.newObject(SoftDelete.class);
        test.setName("SoftDeleteBatchQueryBuilderTest");
        context.commitChanges();
        final SelectQuery query = new SelectQuery(SoftDelete.class);
        new ParallelTestContainer() {

            @Override
            protected void assertResult() throws Exception {
                query.setQualifier(ExpressionFactory.matchExp("name", test.getName()));
                assertEquals(1, context.performQuery(query).size());
                query.andQualifier(ExpressionFactory.matchDbExp("DELETED", true));
                assertEquals(0, context.performQuery(query).size());
            }
        }.runTest(200);
        context.deleteObjects(test);
        assertEquals(test.getPersistenceState(), PersistenceState.DELETED);
        context.commitChanges();
        new ParallelTestContainer() {

            @Override
            protected void assertResult() throws Exception {
                query.setQualifier(ExpressionFactory.matchExp("name", test.getName()));
                assertEquals(0, context.performQuery(query).size());
                SQLTemplate template = new SQLTemplate(entity, "SELECT * FROM SOFT_DELETE");
                template.setFetchingDataRows(true);
                assertEquals(1, context.performQuery(template).size());
            }
        }.runTest(200);
    } finally {
        context.performQuery(new SQLTemplate(entity, "DELETE FROM SOFT_DELETE"));
        dataNode.setBatchTranslatorFactory(oldFactory);
    }
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) SQLTemplate(org.apache.cayenne.query.SQLTemplate) JdbcAdapter(org.apache.cayenne.dba.JdbcAdapter) DbEntity(org.apache.cayenne.map.DbEntity) SoftDelete(org.apache.cayenne.testdo.soft_delete.SoftDelete) ParallelTestContainer(org.apache.cayenne.test.parallel.ParallelTestContainer) Test(org.junit.Test)

Example 17 with JdbcAdapter

use of org.apache.cayenne.dba.JdbcAdapter in project cayenne by apache.

the class SoftDeleteBatchTranslatorIT method testCreateSqlStringWithIdentifiersQuote.

@Test
public void testCreateSqlStringWithIdentifiersQuote() throws Exception {
    DbEntity entity = context.getEntityResolver().getObjEntity(SoftDelete.class).getDbEntity();
    try {
        entity.getDataMap().setQuotingSQLIdentifiers(true);
        List<DbAttribute> idAttributes = Collections.singletonList(entity.getAttribute("ID"));
        DeleteBatchQuery deleteQuery = new DeleteBatchQuery(entity, idAttributes, Collections.<String>emptySet(), 1);
        JdbcAdapter adapter = (JdbcAdapter) this.adapter;
        DeleteBatchTranslator builder = createTranslator(deleteQuery, adapter);
        String generatedSql = builder.getSql();
        String charStart = unitAdapter.getIdentifiersStartQuote();
        String charEnd = unitAdapter.getIdentifiersEndQuote();
        assertNotNull(generatedSql);
        assertEquals("UPDATE " + charStart + entity.getName() + charEnd + " SET " + charStart + "DELETED" + charEnd + " = ? WHERE " + charStart + "ID" + charEnd + " = ?", generatedSql);
    } finally {
        entity.getDataMap().setQuotingSQLIdentifiers(false);
    }
}
Also used : JdbcAdapter(org.apache.cayenne.dba.JdbcAdapter) DbEntity(org.apache.cayenne.map.DbEntity) SoftDelete(org.apache.cayenne.testdo.soft_delete.SoftDelete) DeleteBatchQuery(org.apache.cayenne.query.DeleteBatchQuery) DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

Example 18 with JdbcAdapter

use of org.apache.cayenne.dba.JdbcAdapter in project cayenne by apache.

the class UpdateBatchTranslatorIT method testCreateSqlStringWithIdentifiersQuote.

@Test
public void testCreateSqlStringWithIdentifiersQuote() throws Exception {
    DbEntity entity = runtime.getDataDomain().getEntityResolver().getObjEntity(SimpleLockingTestEntity.class).getDbEntity();
    try {
        entity.getDataMap().setQuotingSQLIdentifiers(true);
        List idAttributes = Collections.singletonList(entity.getAttribute("LOCKING_TEST_ID"));
        List updatedAttributes = Collections.singletonList(entity.getAttribute("DESCRIPTION"));
        UpdateBatchQuery updateQuery = new UpdateBatchQuery(entity, idAttributes, updatedAttributes, Collections.<String>emptySet(), 1);
        JdbcAdapter adapter = (JdbcAdapter) this.adapter;
        UpdateBatchTranslator builder = new UpdateBatchTranslator(updateQuery, adapter, null);
        String generatedSql = builder.getSql();
        String charStart = unitAdapter.getIdentifiersStartQuote();
        String charEnd = unitAdapter.getIdentifiersEndQuote();
        assertNotNull(generatedSql);
        assertEquals("UPDATE " + charStart + entity.getName() + charEnd + " SET " + charStart + "DESCRIPTION" + 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) UpdateBatchQuery(org.apache.cayenne.query.UpdateBatchQuery) SimpleLockingTestEntity(org.apache.cayenne.testdo.locking.SimpleLockingTestEntity) List(java.util.List) Test(org.junit.Test)

Aggregations

JdbcAdapter (org.apache.cayenne.dba.JdbcAdapter)18 Test (org.junit.Test)15 DbEntity (org.apache.cayenne.map.DbEntity)9 SimpleLockingTestEntity (org.apache.cayenne.testdo.locking.SimpleLockingTestEntity)7 DataNode (org.apache.cayenne.access.DataNode)6 RowReaderFactory (org.apache.cayenne.access.jdbc.reader.RowReaderFactory)6 DbAttribute (org.apache.cayenne.map.DbAttribute)6 EntityResolver (org.apache.cayenne.map.EntityResolver)6 DeleteBatchQuery (org.apache.cayenne.query.DeleteBatchQuery)5 InsertBatchQuery (org.apache.cayenne.query.InsertBatchQuery)5 PreparedStatementResultSetHandler (com.mockrunner.jdbc.PreparedStatementResultSetHandler)2 MockConnection (com.mockrunner.mock.jdbc.MockConnection)2 HashMap (java.util.HashMap)2 List (java.util.List)2 MockOperationObserver (org.apache.cayenne.access.MockOperationObserver)2 DeleteBatchTranslator (org.apache.cayenne.access.translator.batch.DeleteBatchTranslator)2 UpdateBatchQuery (org.apache.cayenne.query.UpdateBatchQuery)2 SoftDelete (org.apache.cayenne.testdo.soft_delete.SoftDelete)2 Collection (java.util.Collection)1 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)1