Search in sources :

Example 26 with DbAdapter

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

the class DefaultBatchTranslatorIT method testAppendDbAttribute2.

@Test
public void testAppendDbAttribute2() throws Exception {
    DbAdapter adapter = objectFactory.newInstance(DbAdapter.class, JdbcAdapter.class.getName());
    DefaultBatchTranslator builder = new DefaultBatchTranslator(mock(BatchQuery.class), adapter, null) {

        @Override
        protected String createSql() {
            return null;
        }

        @Override
        protected DbAttributeBinding[] createBindings() {
            return new DbAttributeBinding[0];
        }

        @Override
        protected DbAttributeBinding[] doUpdateBindings(BatchQueryRow row) {
            return new DbAttributeBinding[0];
        }
    };
    StringBuilder buf = new StringBuilder();
    DbEntity entity = new DbEntity("Test");
    DbAttribute attr = new DbAttribute("testAttr", Types.CHAR, null);
    attr.setEntity(entity);
    builder.appendDbAttribute(buf, attr);
    assertEquals("testAttr", buf.toString());
    buf = new StringBuilder();
    attr = new DbAttribute("testAttr", Types.VARCHAR, null);
    attr.setEntity(entity);
    builder.appendDbAttribute(buf, attr);
    assertEquals("testAttr", buf.toString());
}
Also used : BatchQueryRow(org.apache.cayenne.query.BatchQueryRow) JdbcAdapter(org.apache.cayenne.dba.JdbcAdapter) DbAdapter(org.apache.cayenne.dba.DbAdapter) DbEntity(org.apache.cayenne.map.DbEntity) DbAttribute(org.apache.cayenne.map.DbAttribute) DbAttributeBinding(org.apache.cayenne.access.translator.DbAttributeBinding) BatchQuery(org.apache.cayenne.query.BatchQuery) Test(org.junit.Test)

Example 27 with DbAdapter

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

the class InsertBatchTranslatorIT method testConstructor.

@Test
public void testConstructor() throws Exception {
    DbAdapter adapter = objectFactory.newInstance(DbAdapter.class, JdbcAdapter.class.getName());
    InsertBatchTranslator builder = new InsertBatchTranslator(mock(InsertBatchQuery.class), adapter);
    assertSame(adapter, builder.adapter);
}
Also used : JdbcAdapter(org.apache.cayenne.dba.JdbcAdapter) UnitDbAdapter(org.apache.cayenne.unit.UnitDbAdapter) DbAdapter(org.apache.cayenne.dba.DbAdapter) InsertBatchQuery(org.apache.cayenne.query.InsertBatchQuery) Test(org.junit.Test)

Example 28 with DbAdapter

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

the class UpdateBatchTranslatorIT method testConstructor.

@Test
public void testConstructor() throws Exception {
    DbAdapter adapter = objectFactory.newInstance(DbAdapter.class, JdbcAdapter.class.getName());
    UpdateBatchTranslator builder = new UpdateBatchTranslator(mock(UpdateBatchQuery.class), adapter, null);
    assertSame(adapter, builder.adapter);
}
Also used : JdbcAdapter(org.apache.cayenne.dba.JdbcAdapter) UnitDbAdapter(org.apache.cayenne.unit.UnitDbAdapter) DbAdapter(org.apache.cayenne.dba.DbAdapter) UpdateBatchQuery(org.apache.cayenne.query.UpdateBatchQuery) Test(org.junit.Test)

Example 29 with DbAdapter

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

the class SchemaBuilder method tableCreateQueries.

/**
 * Returns iterator of preprocessed table create queries.
 */
private Collection<String> tableCreateQueries(DataNode node, DataMap map) throws Exception {
    DbAdapter adapter = node.getAdapter();
    DbGenerator gen = new DbGenerator(adapter, map, null, domain, jdbcEventLogger);
    List<DbEntity> orderedEnts = dbEntitiesInInsertOrder(node, map);
    List<String> queries = new ArrayList<String>();
    // table definitions
    for (DbEntity ent : orderedEnts) {
        queries.add(adapter.createTable(ent));
    }
    // FK constraints
    for (DbEntity ent : orderedEnts) {
        if (!unitDbAdapter.supportsFKConstraints(ent)) {
            continue;
        }
        List<String> qs = gen.createConstraintsQueries(ent);
        queries.addAll(qs);
    }
    return queries;
}
Also used : UnitDbAdapter(org.apache.cayenne.unit.UnitDbAdapter) DbAdapter(org.apache.cayenne.dba.DbAdapter) DbEntity(org.apache.cayenne.map.DbEntity) ArrayList(java.util.ArrayList) DbGenerator(org.apache.cayenne.access.DbGenerator)

Example 30 with DbAdapter

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

the class FlattenedArcKey method buildJoinSnapshotForInsert.

/**
 * Returns a snapshot for join record for the single-step flattened
 * relationship, generating value for the primary key column if it is not
 * propagated via the relationships.
 */
Map<String, Object> buildJoinSnapshotForInsert(DataNode node) {
    Map<String, Object> snapshot = lazyJoinSnapshot();
    boolean autoPkDone = false;
    DbEntity joinEntity = getJoinEntity();
    for (DbAttribute dbAttr : joinEntity.getPrimaryKeys()) {
        String dbAttrName = dbAttr.getName();
        if (snapshot.containsKey(dbAttrName)) {
            continue;
        }
        DbAdapter adapter = node.getAdapter();
        // value here, so no need to retrieve db-generated pk back to Java.
        if (adapter.supportsGeneratedKeys() && dbAttr.isGenerated()) {
            continue;
        }
        if (autoPkDone) {
            throw new CayenneRuntimeException("Primary Key autogeneration only works for a single attribute.");
        }
        // finally, use database generation mechanism
        try {
            Object pkValue = adapter.getPkGenerator().generatePk(node, dbAttr);
            snapshot.put(dbAttrName, pkValue);
            autoPkDone = true;
        } catch (Exception ex) {
            throw new CayenneRuntimeException("Error generating PK: %s", ex, ex.getMessage());
        }
    }
    return snapshot;
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbAdapter(org.apache.cayenne.dba.DbAdapter) DbAttribute(org.apache.cayenne.map.DbAttribute) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException)

Aggregations

DbAdapter (org.apache.cayenne.dba.DbAdapter)44 Test (org.junit.Test)17 DbEntity (org.apache.cayenne.map.DbEntity)16 JdbcAdapter (org.apache.cayenne.dba.JdbcAdapter)11 QuotingStrategy (org.apache.cayenne.dba.QuotingStrategy)11 UnitDbAdapter (org.apache.cayenne.unit.UnitDbAdapter)10 Injector (org.apache.cayenne.di.Injector)8 DataNodeDescriptor (org.apache.cayenne.configuration.DataNodeDescriptor)7 List (java.util.List)6 AdhocObjectFactory (org.apache.cayenne.di.AdhocObjectFactory)6 JdbcEventLogger (org.apache.cayenne.log.JdbcEventLogger)6 ArrayList (java.util.ArrayList)5 DbAttributeBinding (org.apache.cayenne.access.translator.DbAttributeBinding)5 AutoAdapter (org.apache.cayenne.dba.AutoAdapter)5 DataMap (org.apache.cayenne.map.DataMap)5 BatchQueryRow (org.apache.cayenne.query.BatchQueryRow)5 SimpleLockingTestEntity (org.apache.cayenne.testdo.locking.SimpleLockingTestEntity)5 MockConnection (com.mockrunner.mock.jdbc.MockConnection)4 MockDataSource (com.mockrunner.mock.jdbc.MockDataSource)4 DatabaseMetaData (java.sql.DatabaseMetaData)4