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);
}
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);
}
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);
}
}
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);
}
}
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());
}
}
Aggregations