Search in sources :

Example 11 with UpdateBatchQuery

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

Example 12 with UpdateBatchQuery

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());
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Query(org.apache.cayenne.query.Query) UpdateBatchQuery(org.apache.cayenne.query.UpdateBatchQuery) DbAttribute(org.apache.cayenne.map.DbAttribute) Persistent(org.apache.cayenne.Persistent) LinkedHashMap(java.util.LinkedHashMap) ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) UpdateBatchQuery(org.apache.cayenne.query.UpdateBatchQuery) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 13 with UpdateBatchQuery

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;
}
Also used : UpdateBatchQuery(org.apache.cayenne.query.UpdateBatchQuery) ArrayList(java.util.ArrayList) DbAttribute(org.apache.cayenne.map.DbAttribute)

Example 14 with UpdateBatchQuery

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;
}
Also used : UpdateBatchQuery(org.apache.cayenne.query.UpdateBatchQuery) DbAttribute(org.apache.cayenne.map.DbAttribute) ExtendedType(org.apache.cayenne.access.types.ExtendedType)

Example 15 with UpdateBatchQuery

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);
}
Also used : UpdateBatchQuery(org.apache.cayenne.query.UpdateBatchQuery) DbAttribute(org.apache.cayenne.map.DbAttribute) UpdateBuilder(org.apache.cayenne.access.sqlbuilder.UpdateBuilder)

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