use of org.apache.cayenne.query.UpdateBatchQuery in project cayenne by apache.
the class UpdateBatchTranslatorIT method testCreateSqlString.
@Test
public void testCreateSqlString() {
DbEntity entity = runtime.getDataDomain().getEntityResolver().getObjEntity(SimpleLockingTestEntity.class).getDbEntity();
List<DbAttribute> idAttributes = Collections.singletonList(entity.getAttribute("LOCKING_TEST_ID"));
List<DbAttribute> updatedAttributes = Collections.singletonList(entity.getAttribute("DESCRIPTION"));
UpdateBatchQuery updateQuery = new UpdateBatchQuery(entity, idAttributes, updatedAttributes, Collections.emptySet(), 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 = ?", generatedSql);
}
use of org.apache.cayenne.query.UpdateBatchQuery in project cayenne by apache.
the class DataDomainUpdateBucket method appendQueriesInternal.
@Override
void appendQueriesInternal(Collection<Query> queries) {
DataDomainDBDiffBuilder diffBuilder = new DataDomainDBDiffBuilder();
DataNodeSyncQualifierDescriptor qualifierBuilder = new DataNodeSyncQualifierDescriptor();
for (DbEntity dbEntity : dbEntities) {
Collection<DbEntityClassDescriptor> descriptors = descriptorsByDbEntity.get(dbEntity);
Map<Object, Query> batches = new LinkedHashMap<>();
for (DbEntityClassDescriptor descriptor : descriptors) {
ObjEntity entity = descriptor.getEntity();
diffBuilder.reset(descriptor);
qualifierBuilder.reset(descriptor);
boolean isRootDbEntity = entity.getDbEntity() == dbEntity;
for (Persistent o : objectsByDescriptor.get(descriptor.getClassDescriptor())) {
ObjectDiff diff = parent.objectDiff(o.getObjectId());
Map<String, Object> snapshot = diffBuilder.buildDBDiff(diff);
// check whether MODIFIED object has real db-level modifications
if (snapshot == null) {
continue;
}
// after we filtered out "fake" modifications, check if an
// attempt is made to modify a read only entity
checkReadOnly(entity);
Map<String, Object> qualifierSnapshot = qualifierBuilder.createQualifierSnapshot(diff);
// organize batches by the updated columns + nulls in qualifier
Set<String> snapshotSet = snapshot.keySet();
Set<String> nullQualifierNames = new HashSet<>();
for (Map.Entry<String, Object> entry : qualifierSnapshot.entrySet()) {
if (entry.getValue() == null) {
nullQualifierNames.add(entry.getKey());
}
}
List<Set<String>> batchKey = Arrays.asList(snapshotSet, nullQualifierNames);
UpdateBatchQuery batch = (UpdateBatchQuery) batches.get(batchKey);
if (batch == null) {
batch = new UpdateBatchQuery(dbEntity, qualifierBuilder.getAttributes(), updatedAttributes(dbEntity, snapshot), nullQualifierNames, 10);
batch.setUsingOptimisticLocking(qualifierBuilder.isUsingOptimisticLocking());
batches.put(batchKey, batch);
}
batch.add(qualifierSnapshot, snapshot, o.getObjectId());
// update replacement id with meaningful PK changes
if (isRootDbEntity) {
Map<String, Object> replacementId = o.getObjectId().getReplacementIdMap();
for (DbAttribute pk : dbEntity.getPrimaryKeys()) {
String name = pk.getName();
if (snapshot.containsKey(name) && !replacementId.containsKey(name)) {
replacementId.put(name, snapshot.get(name));
}
}
}
}
}
queries.addAll(batches.values());
}
}
use of org.apache.cayenne.query.UpdateBatchQuery in project cayenne by apache.
the class Oracle8LOBUpdateBatchTranslator method getValuesForLOBUpdateParameters.
@Override
List<Object> getValuesForLOBUpdateParameters(BatchQueryRow row) {
int len = query.getDbAttributes().size();
UpdateBatchQuery updateBatch = (UpdateBatchQuery) query;
List<Object> values = new ArrayList<>(len);
List<DbAttribute> qualifierAttributes = updateBatch.getQualifierAttributes();
List<DbAttribute> updatedDbAttributes = updateBatch.getUpdatedAttributes();
int updatedLen = updatedDbAttributes.size();
int qualifierLen = qualifierAttributes.size();
for (int i = 0; i < updatedLen; i++) {
DbAttribute attribute = updatedDbAttributes.get(i);
Object value = row.getValue(i);
if (isUpdateableColumn(value, attribute.getType())) {
values.add(value);
}
}
for (int i = 0; i < qualifierLen; i++) {
values.add(row.getValue(updatedLen + i));
}
return values;
}
use of org.apache.cayenne.query.UpdateBatchQuery in project cayenne by apache.
the class UpdateBatchTranslator method doUpdateBindings.
@Override
protected DbAttributeBinding[] doUpdateBindings(BatchQueryRow row) {
UpdateBatchQuery updateBatch = (UpdateBatchQuery) query;
List<DbAttribute> updatedDbAttributes = updateBatch.getUpdatedAttributes();
List<DbAttribute> qualifierAttributes = updateBatch.getQualifierAttributes();
int ul = updatedDbAttributes.size();
int ql = qualifierAttributes.size();
int j = 1;
for (int i = 0; i < ul; i++) {
Object value = row.getValue(i);
ExtendedType extendedType = value != null ? adapter.getExtendedTypes().getRegisteredType(value.getClass()) : adapter.getExtendedTypes().getDefaultType();
bindings[i].include(j++, value, extendedType);
}
for (int i = 0; i < ql; i++) {
DbAttribute a = qualifierAttributes.get(i);
// skip null attributes... they are translated as "IS NULL"
if (updateBatch.isNull(a)) {
continue;
}
Object value = row.getValue(ul + i);
ExtendedType extendedType = value != null ? adapter.getExtendedTypes().getRegisteredType(value.getClass()) : adapter.getExtendedTypes().getDefaultType();
bindings[ul + i].include(j++, value, extendedType);
}
return bindings;
}
use of org.apache.cayenne.query.UpdateBatchQuery in project cayenne by apache.
the class UpdateBatchTranslator method getSql.
@Override
public String getSql() {
UpdateBatchQuery query = context.getQuery();
UpdateBuilder updateBuilder = SQLBuilder.update(context.getRootDbEntity());
for (DbAttribute attr : query.getUpdatedAttributes()) {
updateBuilder.set(SQLBuilder.column(attr.getName()).attribute(attr).eq(SQLBuilder.value(1).attribute(attr)));
}
updateBuilder.where(buildQualifier(query.getQualifierAttributes()));
return doTranslate(updateBuilder);
}
Aggregations