Search in sources :

Example 6 with InsertBatchQuery

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

the class OracleAdapter method updatesLOBColumns.

/**
 * Utility method that returns <code>true</code> if the query will update at
 * least one BLOB or CLOB DbAttribute.
 *
 * @since 1.2
 */
static boolean updatesLOBColumns(BatchQuery query) {
    boolean isInsert = query instanceof InsertBatchQuery;
    boolean isUpdate = query instanceof UpdateBatchQuery;
    if (!isInsert && !isUpdate) {
        return false;
    }
    List<DbAttribute> updatedAttributes = (isInsert) ? query.getDbAttributes() : ((UpdateBatchQuery) query).getUpdatedAttributes();
    for (DbAttribute attr : updatedAttributes) {
        int type = attr.getType();
        if (type == Types.CLOB || type == Types.BLOB) {
            return true;
        }
    }
    return false;
}
Also used : InsertBatchQuery(org.apache.cayenne.query.InsertBatchQuery) UpdateBatchQuery(org.apache.cayenne.query.UpdateBatchQuery) DbAttribute(org.apache.cayenne.map.DbAttribute)

Example 7 with InsertBatchQuery

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

the class InsertBatchTranslatorIT method testCreateSqlStringWithIdentifiersQuote.

@Test
public void testCreateSqlStringWithIdentifiersQuote() throws Exception {
    DbEntity entity = runtime.getDataDomain().getEntityResolver().getObjEntity(SimpleLockingTestEntity.class).getDbEntity();
    try {
        entity.getDataMap().setQuotingSQLIdentifiers(true);
        JdbcAdapter adapter = (JdbcAdapter) this.adapter;
        InsertBatchQuery insertQuery = new InsertBatchQuery(entity, 1);
        InsertBatchTranslator builder = new InsertBatchTranslator(insertQuery, adapter);
        String generatedSql = builder.getSql();
        String charStart = unitAdapter.getIdentifiersStartQuote();
        String charEnd = unitAdapter.getIdentifiersEndQuote();
        assertNotNull(generatedSql);
        assertEquals("INSERT INTO " + charStart + entity.getName() + charEnd + " (" + charStart + "DESCRIPTION" + charEnd + ", " + charStart + "INT_COLUMN_NOTNULL" + charEnd + ", " + charStart + "INT_COLUMN_NULL" + charEnd + ", " + charStart + "LOCKING_TEST_ID" + charEnd + ", " + charStart + "NAME" + charEnd + ") VALUES (?, ?, ?, ?, ?)", generatedSql);
    } finally {
        entity.getDataMap().setQuotingSQLIdentifiers(false);
    }
}
Also used : JdbcAdapter(org.apache.cayenne.dba.JdbcAdapter) DbEntity(org.apache.cayenne.map.DbEntity) InsertBatchQuery(org.apache.cayenne.query.InsertBatchQuery) SimpleLockingTestEntity(org.apache.cayenne.testdo.locking.SimpleLockingTestEntity) Test(org.junit.Test)

Example 8 with InsertBatchQuery

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

the class OracleAdapterIT method testUpdatesLOBColumns.

@Test
public void testUpdatesLOBColumns() throws Exception {
    DataMap map = runtime.getDataDomain().getDataMap("lob");
    assertTrue(OracleAdapter.updatesLOBColumns(new InsertBatchQuery(map.getDbEntity("BLOB_TEST"), 1)));
    assertTrue(OracleAdapter.updatesLOBColumns(new InsertBatchQuery(map.getDbEntity("CLOB_TEST"), 1)));
    assertFalse(OracleAdapter.updatesLOBColumns(new InsertBatchQuery(map.getDbEntity("TEST"), 1)));
}
Also used : InsertBatchQuery(org.apache.cayenne.query.InsertBatchQuery) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Example 9 with InsertBatchQuery

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

the class InsertBatchTranslator method getSql.

@Override
public String getSql() {
    InsertBatchQuery query = context.getQuery();
    InsertBuilder insertBuilder = SQLBuilder.insert(context.getRootDbEntity());
    for (DbAttribute attribute : query.getDbAttributes()) {
        // skip generated attributes, if needed
        if (excludeInBatch(attribute)) {
            continue;
        }
        insertBuilder.column(SQLBuilder.column(attribute.getName()).attribute(attribute)).value(SQLBuilder.value(1).attribute(attribute));
    }
    return doTranslate(insertBuilder);
}
Also used : InsertBuilder(org.apache.cayenne.access.sqlbuilder.InsertBuilder) InsertBatchQuery(org.apache.cayenne.query.InsertBatchQuery) DbAttribute(org.apache.cayenne.map.DbAttribute)

Example 10 with InsertBatchQuery

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

the class InsertBatchTranslator method updateBindings.

@Override
public DbAttributeBinding[] updateBindings(BatchQueryRow row) {
    InsertBatchQuery query = context.getQuery();
    int i = 0;
    int j = 0;
    for (DbAttribute attribute : query.getDbAttributes()) {
        if (excludeInBatch(attribute)) {
            i++;
            continue;
        }
        Object value = row.getValue(i++);
        ExtendedType<?> extendedType = value != null ? context.getAdapter().getExtendedTypes().getRegisteredType(value.getClass()) : context.getAdapter().getExtendedTypes().getDefaultType();
        bindings[j].include(++j, value, extendedType);
    }
    return bindings;
}
Also used : InsertBatchQuery(org.apache.cayenne.query.InsertBatchQuery) DbAttribute(org.apache.cayenne.map.DbAttribute)

Aggregations

InsertBatchQuery (org.apache.cayenne.query.InsertBatchQuery)21 Test (org.junit.Test)11 JdbcAdapter (org.apache.cayenne.dba.JdbcAdapter)9 DbAttribute (org.apache.cayenne.map.DbAttribute)8 DbEntity (org.apache.cayenne.map.DbEntity)7 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)4 ObjectId (org.apache.cayenne.ObjectId)4 DataNode (org.apache.cayenne.access.DataNode)4 RowReaderFactory (org.apache.cayenne.access.jdbc.reader.RowReaderFactory)4 EntityResolver (org.apache.cayenne.map.EntityResolver)4 SimpleLockingTestEntity (org.apache.cayenne.testdo.locking.SimpleLockingTestEntity)4 DataRow (org.apache.cayenne.DataRow)3 DbAdapter (org.apache.cayenne.dba.DbAdapter)3 BatchQuery (org.apache.cayenne.query.BatchQuery)3 UnitDbAdapter (org.apache.cayenne.unit.UnitDbAdapter)3 ArrayList (java.util.ArrayList)2 BatchQueryRow (org.apache.cayenne.query.BatchQueryRow)2 UpdateBatchQuery (org.apache.cayenne.query.UpdateBatchQuery)2 PreparedStatement (java.sql.PreparedStatement)1 HashMap (java.util.HashMap)1