Search in sources :

Example 1 with UpdateBatchQuery

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

the class UpdateBatchTranslator method createSql.

@Override
protected String createSql() {
    UpdateBatchQuery updateBatch = (UpdateBatchQuery) query;
    QuotingStrategy strategy = adapter.getQuotingStrategy();
    List<DbAttribute> qualifierAttributes = updateBatch.getQualifierAttributes();
    List<DbAttribute> updatedDbAttributes = updateBatch.getUpdatedAttributes();
    StringBuilder buffer = new StringBuilder("UPDATE ");
    buffer.append(strategy.quotedFullyQualifiedName(query.getDbEntity()));
    buffer.append(" SET ");
    int len = updatedDbAttributes.size();
    for (int i = 0; i < len; i++) {
        if (i > 0) {
            buffer.append(", ");
        }
        DbAttribute attribute = updatedDbAttributes.get(i);
        buffer.append(strategy.quotedName(attribute));
        buffer.append(" = ?");
    }
    buffer.append(" WHERE ");
    Iterator<DbAttribute> i = qualifierAttributes.iterator();
    while (i.hasNext()) {
        DbAttribute attribute = i.next();
        appendDbAttribute(buffer, attribute);
        buffer.append(updateBatch.isNull(attribute) ? " IS NULL" : " = ?");
        if (i.hasNext()) {
            buffer.append(" AND ");
        }
    }
    return buffer.toString();
}
Also used : UpdateBatchQuery(org.apache.cayenne.query.UpdateBatchQuery) DbAttribute(org.apache.cayenne.map.DbAttribute) QuotingStrategy(org.apache.cayenne.dba.QuotingStrategy)

Example 2 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 3 with UpdateBatchQuery

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

the class UpdateBatchTranslator method createBindings.

@Override
protected DbAttributeBinding[] createBindings() {
    UpdateBatchQuery updateBatch = (UpdateBatchQuery) query;
    List<DbAttribute> updatedDbAttributes = updateBatch.getUpdatedAttributes();
    List<DbAttribute> qualifierAttributes = updateBatch.getQualifierAttributes();
    int ul = updatedDbAttributes.size();
    int ql = qualifierAttributes.size();
    DbAttributeBinding[] bindings = new DbAttributeBinding[ul + ql];
    for (int i = 0; i < ul; i++) {
        bindings[i] = new DbAttributeBinding(updatedDbAttributes.get(i));
    }
    for (int i = 0; i < ql; i++) {
        bindings[ul + i] = new DbAttributeBinding(qualifierAttributes.get(i));
    }
    return bindings;
}
Also used : UpdateBatchQuery(org.apache.cayenne.query.UpdateBatchQuery) DbAttribute(org.apache.cayenne.map.DbAttribute) DbAttributeBinding(org.apache.cayenne.access.translator.DbAttributeBinding)

Example 4 with UpdateBatchQuery

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

the class QueryCreatorVisitor method visitUpdate.

@Override
public Void visitUpdate(UpdateDbRowOp dbRow) {
    // skip empty update..
    if (dbRow.getValues().isEmpty()) {
        return null;
    }
    UpdateBatchQuery query;
    if (lastRow == null || !lastRow.isSameBatch(dbRow)) {
        query = new UpdateBatchQuery(dbRow.getEntity(), dbRow.getQualifier().getQualifierAttributes(), dbRow.getValues().getUpdatedAttributes(), dbRow.getQualifier().getNullQualifierNames(), batchSize);
        query.setUsingOptimisticLocking(dbRow.getQualifier().isUsingOptimisticLocking());
        queryList.add(query);
        lastBatch = query;
    } else {
        query = (UpdateBatchQuery) lastBatch;
    }
    query.add(dbRow.getQualifier().getSnapshot(), dbRow.getValues().getSnapshot(), dbRow.getChangeId());
    lastRow = dbRow;
    return null;
}
Also used : UpdateBatchQuery(org.apache.cayenne.query.UpdateBatchQuery)

Example 5 with UpdateBatchQuery

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

the class Oracle8LOBBatchAction method performAction.

@Override
public void performAction(Connection connection, OperationObserver observer) throws SQLException, Exception {
    Oracle8LOBBatchTranslator translator;
    if (query instanceof InsertBatchQuery) {
        translator = new Oracle8LOBInsertBatchTranslator((InsertBatchQuery) query, adapter, OracleAdapter.TRIM_FUNCTION);
    } else if (query instanceof UpdateBatchQuery) {
        translator = new Oracle8LOBUpdateBatchTranslator((UpdateBatchQuery) query, adapter, OracleAdapter.TRIM_FUNCTION);
    } else {
        throw new CayenneRuntimeException("Unsupported batch type for special LOB processing: " + query);
    }
    translator.setNewBlobFunction(OracleAdapter.NEW_BLOB_FUNCTION);
    translator.setNewClobFunction(OracleAdapter.NEW_CLOB_FUNCTION);
    // no batching is done, queries are translated
    // for each batch set, since prepared statements
    // may be different depending on whether LOBs are NULL or not..
    Oracle8LOBBatchQueryWrapper selectQuery = new Oracle8LOBBatchQueryWrapper(query);
    List<DbAttribute> qualifierAttributes = selectQuery.getDbAttributesForLOBSelectQualifier();
    for (BatchQueryRow row : query.getRows()) {
        selectQuery.indexLOBAttributes(row);
        int updated;
        String updateStr = translator.createSql(row);
        // 1. run row update
        logger.log(updateStr);
        try (PreparedStatement statement = connection.prepareStatement(updateStr)) {
            DbAttributeBinding[] bindings = translator.updateBindings(row);
            logger.logQueryParameters("bind", bindings);
            bind(adapter, statement, bindings);
            updated = statement.executeUpdate();
            logger.logUpdateCount(updated);
        }
        // 2. run row LOB update (SELECT...FOR UPDATE and writing out LOBs)
        processLOBRow(connection, translator, selectQuery, qualifierAttributes, row);
        // finally, notify delegate that the row was updated
        observer.nextCount(query, updated);
    }
}
Also used : InsertBatchQuery(org.apache.cayenne.query.InsertBatchQuery) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) DbAttribute(org.apache.cayenne.map.DbAttribute) PreparedStatement(java.sql.PreparedStatement) DbAttributeBinding(org.apache.cayenne.access.translator.DbAttributeBinding) BatchQueryRow(org.apache.cayenne.query.BatchQueryRow) UpdateBatchQuery(org.apache.cayenne.query.UpdateBatchQuery)

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