use of org.apache.cayenne.query.BatchQueryRow in project cayenne by apache.
the class BatchAction method runAsBatch.
protected void runAsBatch(Connection con, BatchTranslator translator, OperationObserver delegate) throws SQLException, Exception {
String sql = translator.getSql();
JdbcEventLogger logger = dataNode.getJdbcEventLogger();
boolean isLoggable = logger.isLoggable();
// log batch SQL execution
logger.log(sql);
// run batch
DbAdapter adapter = dataNode.getAdapter();
try (PreparedStatement statement = con.prepareStatement(sql)) {
for (BatchQueryRow row : query.getRows()) {
DbAttributeBinding[] bindings = translator.updateBindings(row);
logger.logQueryParameters("batch bind", bindings);
bind(adapter, statement, bindings);
statement.addBatch();
}
// execute the whole batch
int[] results = statement.executeBatch();
delegate.nextBatchCount(query, results);
if (isLoggable) {
int totalUpdateCount = 0;
for (int result : results) {
// Statement.EXECUTE_FAILED
if (result < 0) {
totalUpdateCount = Statement.SUCCESS_NO_INFO;
break;
}
totalUpdateCount += result;
}
logger.logUpdateCount(totalUpdateCount);
}
}
}
use of org.apache.cayenne.query.BatchQueryRow in project cayenne by apache.
the class CryptoBatchTranslatorFactoryDecorator method translator.
@Override
public BatchTranslator translator(BatchQuery query, final DbAdapter adapter, String trimFunction) {
final BatchTranslator delegateTranslator = delegate.translator(query, adapter, trimFunction);
return new BatchTranslator() {
private boolean encryptorCompiled;
private BindingsTransformer encryptor;
private void ensureEncryptorCompiled() {
if (!encryptorCompiled) {
encryptor = cryptoFactory.encryptor(getBindings(), adapter.getExtendedTypes());
encryptorCompiled = true;
}
}
@Override
public String getSql() {
return delegateTranslator.getSql();
}
@Override
public DbAttributeBinding[] getBindings() {
return delegateTranslator.getBindings();
}
@Override
public DbAttributeBinding[] updateBindings(BatchQueryRow row) {
ensureEncryptorCompiled();
DbAttributeBinding[] bindings = delegateTranslator.updateBindings(row);
if (encryptor != null) {
encryptor.transform(bindings);
}
return bindings;
}
};
}
use of org.apache.cayenne.query.BatchQueryRow in project cayenne by apache.
the class DefaultBatchTranslatorIT method testAppendDbAttribute1.
@Test
public void testAppendDbAttribute1() throws Exception {
DbAdapter adapter = objectFactory.newInstance(DbAdapter.class, JdbcAdapter.class.getName());
String trimFunction = "testTrim";
DefaultBatchTranslator builder = new DefaultBatchTranslator(mock(BatchQuery.class), adapter, trimFunction) {
@Override
protected String createSql() {
return null;
}
@Override
protected DbAttributeBinding[] createBindings() {
return new DbAttributeBinding[0];
}
@Override
protected DbAttributeBinding[] doUpdateBindings(BatchQueryRow row) {
return new DbAttributeBinding[0];
}
};
StringBuilder buf = new StringBuilder();
DbEntity entity = new DbEntity("Test");
DbAttribute attr = new DbAttribute("testAttr", Types.CHAR, null);
attr.setEntity(entity);
builder.appendDbAttribute(buf, attr);
assertEquals("testTrim(testAttr)", buf.toString());
buf = new StringBuilder();
attr = new DbAttribute("testAttr", Types.VARCHAR, null);
attr.setEntity(entity);
builder.appendDbAttribute(buf, attr);
assertEquals("testAttr", buf.toString());
}
use of org.apache.cayenne.query.BatchQueryRow 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 CayenneException("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);
}
}
use of org.apache.cayenne.query.BatchQueryRow in project cayenne by apache.
the class DefaultBatchTranslatorIT method testConstructor.
@Test
public void testConstructor() throws Exception {
DbAdapter adapter = objectFactory.newInstance(DbAdapter.class, JdbcAdapter.class.getName());
DefaultBatchTranslator builder = new DefaultBatchTranslator(mock(BatchQuery.class), adapter, null) {
@Override
protected String createSql() {
return null;
}
@Override
protected DbAttributeBinding[] createBindings() {
return new DbAttributeBinding[0];
}
@Override
protected DbAttributeBinding[] doUpdateBindings(BatchQueryRow row) {
return new DbAttributeBinding[0];
}
};
assertSame(adapter, builder.adapter);
}
Aggregations