Search in sources :

Example 6 with JdbcAdapter

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

the class UpdateBatchTranslatorIT method testCreateSqlStringWithNullsWithIdentifiersQuote.

@Test
public void testCreateSqlStringWithNullsWithIdentifiersQuote() throws Exception {
    DbEntity entity = runtime.getDataDomain().getEntityResolver().getObjEntity(SimpleLockingTestEntity.class).getDbEntity();
    try {
        entity.getDataMap().setQuotingSQLIdentifiers(true);
        List idAttributes = Arrays.asList(entity.getAttribute("LOCKING_TEST_ID"), entity.getAttribute("NAME"));
        List updatedAttributes = Collections.singletonList(entity.getAttribute("DESCRIPTION"));
        Collection nullAttributes = Collections.singleton("NAME");
        UpdateBatchQuery updateQuery = new UpdateBatchQuery(entity, idAttributes, updatedAttributes, nullAttributes, 1);
        JdbcAdapter adapter = (JdbcAdapter) this.adapter;
        UpdateBatchTranslator builder = new UpdateBatchTranslator(updateQuery, adapter, null);
        String generatedSql = builder.getSql();
        assertNotNull(generatedSql);
        String charStart = unitAdapter.getIdentifiersStartQuote();
        String charEnd = unitAdapter.getIdentifiersEndQuote();
        assertEquals("UPDATE " + charStart + entity.getName() + charEnd + " SET " + charStart + "DESCRIPTION" + charEnd + " = ? WHERE " + charStart + "LOCKING_TEST_ID" + charEnd + " = ? AND " + charStart + "NAME" + charEnd + " IS NULL", 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) Collection(java.util.Collection) List(java.util.List) Test(org.junit.Test)

Example 7 with JdbcAdapter

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

the class DataNodeIT method testAdapter.

@Test
public void testAdapter() throws Exception {
    DataNode node = new DataNode();
    assertNull(node.getAdapter());
    JdbcAdapter a1 = objectFactory.newInstance(JdbcAdapter.class, JdbcAdapter.class.getName());
    node.setAdapter(a1);
    assertSame(a1, node.getAdapter());
    JdbcAdapter a2 = objectFactory.newInstance(JdbcAdapter.class, JdbcAdapter.class.getName());
    node.setAdapter(a2);
    assertSame(a2, node.getAdapter());
}
Also used : JdbcAdapter(org.apache.cayenne.dba.JdbcAdapter) Test(org.junit.Test)

Example 8 with JdbcAdapter

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

the class DataContextExtrasIT method testCommitChangesError.

@Test
public void testCommitChangesError() {
    DataDomain domain = context.getParentDataDomain();
    // setup mockup PK generator that will blow on PK request
    // to emulate an exception
    JdbcAdapter jdbcAdapter = objectFactory.newInstance(JdbcAdapter.class, JdbcAdapter.class.getName());
    PkGenerator newGenerator = new JdbcPkGenerator(jdbcAdapter) {

        @Override
        public Object generatePk(DataNode node, DbAttribute pk) throws Exception {
            throw new CayenneRuntimeException("Intentional");
        }
    };
    PkGenerator oldGenerator = domain.getDataNodes().iterator().next().getAdapter().getPkGenerator();
    JdbcAdapter adapter = (JdbcAdapter) domain.getDataNodes().iterator().next().getAdapter();
    adapter.setPkGenerator(newGenerator);
    try {
        Artist newArtist = context.newObject(Artist.class);
        newArtist.setArtistName("aaa");
        context.commitChanges();
        fail("Exception expected but not thrown due to missing PK generation routine.");
    } catch (CayenneRuntimeException ex) {
    // exception expected
    } finally {
        adapter.setPkGenerator(oldGenerator);
    }
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) JdbcAdapter(org.apache.cayenne.dba.JdbcAdapter) DbAttribute(org.apache.cayenne.map.DbAttribute) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) JdbcPkGenerator(org.apache.cayenne.dba.JdbcPkGenerator) PkGenerator(org.apache.cayenne.dba.PkGenerator) JdbcPkGenerator(org.apache.cayenne.dba.JdbcPkGenerator) Test(org.junit.Test)

Example 9 with JdbcAdapter

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

the class BatchActionGeneratedIT method testHasGeneratedKeys1.

@Test
public void testHasGeneratedKeys1() throws Exception {
    EntityResolver resolver = runtime.getChannel().getEntityResolver();
    // test with adapter that supports keys
    JdbcAdapter adapter = buildAdapter(true);
    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));
    assertTrue(new BatchAction(batch1, node, false).hasGeneratedKeys());
}
Also used : JdbcAdapter(org.apache.cayenne.dba.JdbcAdapter) InsertBatchQuery(org.apache.cayenne.query.InsertBatchQuery) DataNode(org.apache.cayenne.access.DataNode) RowReaderFactory(org.apache.cayenne.access.jdbc.reader.RowReaderFactory) EntityResolver(org.apache.cayenne.map.EntityResolver) Test(org.junit.Test)

Example 10 with JdbcAdapter

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

the class BatchActionGeneratedIT method buildAdapter.

JdbcAdapter buildAdapter(boolean supportGeneratedKeys) {
    JdbcAdapter adapter = objectFactory.newInstance(JdbcAdapter.class, JdbcAdapter.class.getName());
    adapter.setSupportsGeneratedKeys(supportGeneratedKeys);
    return adapter;
}
Also used : JdbcAdapter(org.apache.cayenne.dba.JdbcAdapter)

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