Search in sources :

Example 1 with DeleteBatchQuery

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

the class DataDomainFlattenedBucket method addFlattenedDelete.

void addFlattenedDelete(DbEntity flattenedEntity, FlattenedArcKey flattenedDeleteInfo) {
    DeleteBatchQuery relationDeleteQuery = flattenedDeleteQueries.get(flattenedEntity);
    if (relationDeleteQuery == null) {
        Collection<DbAttribute> pk = flattenedEntity.getPrimaryKeys();
        List<DbAttribute> pkList = pk instanceof List ? (List<DbAttribute>) pk : new ArrayList<>(pk);
        relationDeleteQuery = new DeleteBatchQuery(flattenedEntity, pkList, Collections.<String>emptySet(), 50);
        relationDeleteQuery.setUsingOptimisticLocking(false);
        flattenedDeleteQueries.put(flattenedEntity, relationDeleteQuery);
    }
    DataNode node = parent.getDomain().lookupDataNode(flattenedEntity.getDataMap());
    List<Map<String, Object>> flattenedSnapshots = flattenedDeleteInfo.buildJoinSnapshotsForDelete(node);
    if (!flattenedSnapshots.isEmpty()) {
        for (Map<String, Object> flattenedSnapshot : flattenedSnapshots) {
            relationDeleteQuery.add(flattenedSnapshot);
        }
    }
}
Also used : DeleteBatchQuery(org.apache.cayenne.query.DeleteBatchQuery) DbAttribute(org.apache.cayenne.map.DbAttribute) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with DeleteBatchQuery

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

the class DeleteBatchTranslator method createBindings.

@Override
protected DbAttributeBinding[] createBindings() {
    DeleteBatchQuery deleteBatch = (DeleteBatchQuery) query;
    List<DbAttribute> attributes = deleteBatch.getDbAttributes();
    int len = attributes.size();
    DbAttributeBinding[] bindings = new DbAttributeBinding[len];
    for (int i = 0; i < len; i++) {
        bindings[i] = new DbAttributeBinding(attributes.get(i));
    }
    return bindings;
}
Also used : DeleteBatchQuery(org.apache.cayenne.query.DeleteBatchQuery) DbAttribute(org.apache.cayenne.map.DbAttribute) DbAttributeBinding(org.apache.cayenne.access.translator.DbAttributeBinding)

Example 3 with DeleteBatchQuery

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

the class DeleteBatchTranslator method applyQualifier.

/**
 * Appends WHERE clause to SQL string
 */
protected void applyQualifier(StringBuilder buffer) {
    buffer.append(" WHERE ");
    DeleteBatchQuery deleteBatch = (DeleteBatchQuery) query;
    Iterator<DbAttribute> i = deleteBatch.getDbAttributes().iterator();
    while (i.hasNext()) {
        DbAttribute attribute = i.next();
        appendDbAttribute(buffer, attribute);
        buffer.append(deleteBatch.isNull(attribute) ? " IS NULL" : " = ?");
        if (i.hasNext()) {
            buffer.append(" AND ");
        }
    }
}
Also used : DeleteBatchQuery(org.apache.cayenne.query.DeleteBatchQuery) DbAttribute(org.apache.cayenne.map.DbAttribute)

Example 4 with DeleteBatchQuery

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

the class DeleteBatchTranslator method doUpdateBindings.

@Override
protected DbAttributeBinding[] doUpdateBindings(BatchQueryRow row) {
    int len = bindings.length;
    DeleteBatchQuery deleteBatch = (DeleteBatchQuery) query;
    for (int i = 0, j = 1; i < len; i++) {
        DbAttributeBinding b = bindings[i];
        // skip null attributes... they are translated as "IS NULL"
        if (deleteBatch.isNull(b.getAttribute())) {
            b.exclude();
        } else {
            Object value = row.getValue(i);
            ExtendedType extendedType = value != null ? adapter.getExtendedTypes().getRegisteredType(value.getClass()) : adapter.getExtendedTypes().getDefaultType();
            b.include(j++, value, extendedType);
        }
    }
    return bindings;
}
Also used : DeleteBatchQuery(org.apache.cayenne.query.DeleteBatchQuery) DbAttributeBinding(org.apache.cayenne.access.translator.DbAttributeBinding) ExtendedType(org.apache.cayenne.access.types.ExtendedType)

Example 5 with DeleteBatchQuery

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

the class SoftDeleteBatchTranslator method doUpdateBindings.

@Override
protected DbAttributeBinding[] doUpdateBindings(BatchQueryRow row) {
    int len = bindings.length;
    DeleteBatchQuery deleteBatch = (DeleteBatchQuery) query;
    // skip position 0... Otherwise follow super algorithm
    for (int i = 1, j = 2; i < len; i++) {
        DbAttributeBinding b = bindings[i];
        // skip null attributes... they are translated as "IS NULL"
        if (deleteBatch.isNull(b.getAttribute())) {
            b.exclude();
        } else {
            Object value = row.getValue(i - 1);
            ExtendedType extendedType = value != null ? adapter.getExtendedTypes().getRegisteredType(value.getClass()) : adapter.getExtendedTypes().getDefaultType();
            b.include(j++, value, extendedType);
        }
    }
    return bindings;
}
Also used : DeleteBatchQuery(org.apache.cayenne.query.DeleteBatchQuery) DbAttributeBinding(org.apache.cayenne.access.translator.DbAttributeBinding) ExtendedType(org.apache.cayenne.access.types.ExtendedType)

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