Search in sources :

Example 11 with DeleteBatchQuery

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

the class SoftDeleteBatchTranslatorIT method testCreateSqlString.

@Test
public void testCreateSqlString() throws Exception {
    DbEntity entity = context.getEntityResolver().getObjEntity(SoftDelete.class).getDbEntity();
    List<DbAttribute> idAttributes = Collections.singletonList(entity.getAttribute("ID"));
    DeleteBatchQuery deleteQuery = new DeleteBatchQuery(entity, idAttributes, Collections.<String>emptySet(), 1);
    DeleteBatchTranslator builder = createTranslator(deleteQuery);
    String generatedSql = builder.getSql();
    assertNotNull(generatedSql);
    assertEquals("UPDATE " + entity.getName() + " SET DELETED = ? WHERE ID = ?", generatedSql);
}
Also used : 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 12 with DeleteBatchQuery

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

the class SoftDeleteBatchTranslatorIT method testCreateSqlStringWithNulls.

@Test
public void testCreateSqlStringWithNulls() throws Exception {
    DbEntity entity = context.getEntityResolver().getObjEntity(SoftDelete.class).getDbEntity();
    List<DbAttribute> idAttributes = Arrays.asList(entity.getAttribute("ID"), entity.getAttribute("NAME"));
    Collection<String> nullAttributes = Collections.singleton("NAME");
    DeleteBatchQuery deleteQuery = new DeleteBatchQuery(entity, idAttributes, nullAttributes, 1);
    DeleteBatchTranslator builder = createTranslator(deleteQuery);
    String generatedSql = builder.getSql();
    assertNotNull(generatedSql);
    assertEquals("UPDATE " + entity.getName() + " SET DELETED = ? WHERE ID = ? AND NAME IS NULL", generatedSql);
}
Also used : 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 13 with DeleteBatchQuery

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

the class DeleteBatchTranslatorIT method testCreateSqlStringWithNullsWithIdentifiersQuote.

@Test
public void testCreateSqlStringWithNullsWithIdentifiersQuote() throws Exception {
    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"));
        Collection<String> nullAttributes = Collections.singleton("NAME");
        DeleteBatchQuery deleteQuery = new DeleteBatchQuery(entity, idAttributes, nullAttributes, 1);
        JdbcAdapter adapter = (JdbcAdapter) this.adapter;
        DeleteBatchTranslator builder = new DeleteBatchTranslator(deleteQuery, adapter, null);
        String generatedSql = builder.getSql();
        String charStart = unitAdapter.getIdentifiersStartQuote();
        String charEnd = unitAdapter.getIdentifiersEndQuote();
        assertNotNull(generatedSql);
        assertEquals("DELETE FROM " + charStart + entity.getName() + 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) DeleteBatchQuery(org.apache.cayenne.query.DeleteBatchQuery) SimpleLockingTestEntity(org.apache.cayenne.testdo.locking.SimpleLockingTestEntity) DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

Example 14 with DeleteBatchQuery

use of org.apache.cayenne.query.DeleteBatchQuery 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 15 with DeleteBatchQuery

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

the class DataDomainDeleteBucket method appendQueriesInternal.

@Override
void appendQueriesInternal(Collection<Query> queries) {
    DataNodeSyncQualifierDescriptor qualifierBuilder = new DataNodeSyncQualifierDescriptor();
    EntitySorter sorter = parent.getDomain().getEntitySorter();
    sorter.sortDbEntities(dbEntities, true);
    for (DbEntity dbEntity : dbEntities) {
        Collection<DbEntityClassDescriptor> descriptors = descriptorsByDbEntity.get(dbEntity);
        Map<Object, Query> batches = new LinkedHashMap<>();
        for (DbEntityClassDescriptor descriptor : descriptors) {
            qualifierBuilder.reset(descriptor);
            boolean isRootDbEntity = descriptor.isMaster();
            // remove object set for dependent entity, so that it does not show up
            // on post processing
            List<Persistent> objects = objectsByDescriptor.get(descriptor.getClassDescriptor());
            if (objects.isEmpty()) {
                continue;
            }
            checkReadOnly(descriptor.getEntity());
            if (isRootDbEntity) {
                sorter.sortObjectsForEntity(descriptor.getEntity(), objects, true);
            }
            for (Persistent o : objects) {
                ObjectDiff diff = parent.objectDiff(o.getObjectId());
                Map<String, Object> qualifierSnapshot = qualifierBuilder.createQualifierSnapshot(diff);
                // organize batches by the nulls in qualifier
                Set<String> nullQualifierNames = new HashSet<String>();
                for (Map.Entry<String, ?> entry : qualifierSnapshot.entrySet()) {
                    if (entry.getValue() == null) {
                        nullQualifierNames.add(entry.getKey());
                    }
                }
                Object batchKey = Arrays.asList(nullQualifierNames);
                DeleteBatchQuery batch = (DeleteBatchQuery) batches.get(batchKey);
                if (batch == null) {
                    batch = new DeleteBatchQuery(dbEntity, qualifierBuilder.getAttributes(), nullQualifierNames, 27);
                    batch.setUsingOptimisticLocking(qualifierBuilder.isUsingOptimisticLocking());
                    batches.put(batchKey, batch);
                }
                batch.add(qualifierSnapshot);
            }
        }
        queries.addAll(batches.values());
    }
}
Also used : EntitySorter(org.apache.cayenne.map.EntitySorter) DeleteBatchQuery(org.apache.cayenne.query.DeleteBatchQuery) Query(org.apache.cayenne.query.Query) DeleteBatchQuery(org.apache.cayenne.query.DeleteBatchQuery) Persistent(org.apache.cayenne.Persistent) LinkedHashMap(java.util.LinkedHashMap) DbEntity(org.apache.cayenne.map.DbEntity) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Aggregations

DeleteBatchQuery (org.apache.cayenne.query.DeleteBatchQuery)15 DbAttribute (org.apache.cayenne.map.DbAttribute)12 DbEntity (org.apache.cayenne.map.DbEntity)10 Test (org.junit.Test)9 JdbcAdapter (org.apache.cayenne.dba.JdbcAdapter)7 SimpleLockingTestEntity (org.apache.cayenne.testdo.locking.SimpleLockingTestEntity)6 HashMap (java.util.HashMap)3 DbAttributeBinding (org.apache.cayenne.access.translator.DbAttributeBinding)3 SoftDelete (org.apache.cayenne.testdo.soft_delete.SoftDelete)3 PreparedStatementResultSetHandler (com.mockrunner.jdbc.PreparedStatementResultSetHandler)2 MockConnection (com.mockrunner.mock.jdbc.MockConnection)2 Map (java.util.Map)2 DataNode (org.apache.cayenne.access.DataNode)2 MockOperationObserver (org.apache.cayenne.access.MockOperationObserver)2 RowReaderFactory (org.apache.cayenne.access.jdbc.reader.RowReaderFactory)2 DeleteBatchTranslator (org.apache.cayenne.access.translator.batch.DeleteBatchTranslator)2 ExtendedType (org.apache.cayenne.access.types.ExtendedType)2 DbAdapter (org.apache.cayenne.dba.DbAdapter)2 EntityResolver (org.apache.cayenne.map.EntityResolver)2 UnitDbAdapter (org.apache.cayenne.unit.UnitDbAdapter)2