Search in sources :

Example 6 with UpdateBatchQuery

use of org.apache.cayenne.query.UpdateBatchQuery 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 UpdateBatchQuery

use of org.apache.cayenne.query.UpdateBatchQuery 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)

Example 8 with UpdateBatchQuery

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

the class UpdateBatchTranslatorIT method testCreateSqlStringWithNulls.

@Test
public void testCreateSqlStringWithNulls() throws Exception {
    DbEntity entity = runtime.getDataDomain().getEntityResolver().getObjEntity(SimpleLockingTestEntity.class).getDbEntity();
    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);
    DbAdapter adapter = objectFactory.newInstance(DbAdapter.class, JdbcAdapter.class.getName());
    UpdateBatchTranslator builder = new UpdateBatchTranslator(updateQuery, adapter, null);
    String generatedSql = builder.getSql();
    assertNotNull(generatedSql);
    assertEquals("UPDATE " + entity.getName() + " SET DESCRIPTION = ? 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) 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 9 with UpdateBatchQuery

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

the class UpdateBatchTranslatorIT method testCreateSqlStringWithNullsWithIdentifiersQuote.

@Test
public void testCreateSqlStringWithNullsWithIdentifiersQuote() {
    DbEntity entity = runtime.getDataDomain().getEntityResolver().getObjEntity(SimpleLockingTestEntity.class).getDbEntity();
    try {
        entity.getDataMap().setQuotingSQLIdentifiers(true);
        List<DbAttribute> idAttributes = Arrays.asList(entity.getAttribute("LOCKING_TEST_ID"), entity.getAttribute("NAME"));
        List<DbAttribute> updatedAttributes = Collections.singletonList(entity.getAttribute("DESCRIPTION"));
        Collection<String> nullAttributes = Collections.singleton("NAME");
        UpdateBatchQuery updateQuery = new UpdateBatchQuery(entity, idAttributes, updatedAttributes, nullAttributes, 1);
        JdbcAdapter adapter = (JdbcAdapter) this.adapter;
        UpdateBatchTranslator builder = new UpdateBatchTranslator(updateQuery, adapter);
        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) DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

Example 10 with UpdateBatchQuery

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

the class UpdateBatchTranslatorIT method testCreateSqlStringWithNulls.

@Test
public void testCreateSqlStringWithNulls() {
    DbEntity entity = runtime.getDataDomain().getEntityResolver().getObjEntity(SimpleLockingTestEntity.class).getDbEntity();
    List<DbAttribute> idAttributes = Arrays.asList(entity.getAttribute("LOCKING_TEST_ID"), entity.getAttribute("NAME"));
    List<DbAttribute> updatedAttributes = Collections.singletonList(entity.getAttribute("DESCRIPTION"));
    Collection<String> nullAttributes = Collections.singleton("NAME");
    UpdateBatchQuery updateQuery = new UpdateBatchQuery(entity, idAttributes, updatedAttributes, nullAttributes, 1);
    DbAdapter adapter = objectFactory.newInstance(DbAdapter.class, JdbcAdapter.class.getName());
    UpdateBatchTranslator builder = new UpdateBatchTranslator(updateQuery, adapter);
    String generatedSql = builder.getSql();
    assertNotNull(generatedSql);
    assertEquals("UPDATE " + entity.getName() + " SET DESCRIPTION = ? 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) UpdateBatchQuery(org.apache.cayenne.query.UpdateBatchQuery) SimpleLockingTestEntity(org.apache.cayenne.testdo.locking.SimpleLockingTestEntity) DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

Aggregations

UpdateBatchQuery (org.apache.cayenne.query.UpdateBatchQuery)24 DbAttribute (org.apache.cayenne.map.DbAttribute)18 DbEntity (org.apache.cayenne.map.DbEntity)10 Test (org.junit.Test)10 JdbcAdapter (org.apache.cayenne.dba.JdbcAdapter)9 SimpleLockingTestEntity (org.apache.cayenne.testdo.locking.SimpleLockingTestEntity)8 List (java.util.List)5 DbAdapter (org.apache.cayenne.dba.DbAdapter)5 UnitDbAdapter (org.apache.cayenne.unit.UnitDbAdapter)5 DbAttributeBinding (org.apache.cayenne.access.translator.DbAttributeBinding)3 QuotingStrategy (org.apache.cayenne.dba.QuotingStrategy)3 Collection (java.util.Collection)2 ExtendedType (org.apache.cayenne.access.types.ExtendedType)2 InsertBatchQuery (org.apache.cayenne.query.InsertBatchQuery)2 PreparedStatement (java.sql.PreparedStatement)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Set (java.util.Set)1