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