Search in sources :

Example 1 with BatchQueryRow

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);
        }
    }
}
Also used : BatchQueryRow(org.apache.cayenne.query.BatchQueryRow) DbAdapter(org.apache.cayenne.dba.DbAdapter) JdbcEventLogger(org.apache.cayenne.log.JdbcEventLogger) DbAttributeBinding(org.apache.cayenne.access.translator.DbAttributeBinding)

Example 2 with BatchQueryRow

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;
        }
    };
}
Also used : BatchTranslator(org.apache.cayenne.access.translator.batch.BatchTranslator) BatchQueryRow(org.apache.cayenne.query.BatchQueryRow) DbAttributeBinding(org.apache.cayenne.access.translator.DbAttributeBinding) BindingsTransformer(org.apache.cayenne.crypto.transformer.BindingsTransformer)

Example 3 with BatchQueryRow

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());
}
Also used : BatchQueryRow(org.apache.cayenne.query.BatchQueryRow) JdbcAdapter(org.apache.cayenne.dba.JdbcAdapter) DbAdapter(org.apache.cayenne.dba.DbAdapter) DbEntity(org.apache.cayenne.map.DbEntity) DbAttribute(org.apache.cayenne.map.DbAttribute) DbAttributeBinding(org.apache.cayenne.access.translator.DbAttributeBinding) BatchQuery(org.apache.cayenne.query.BatchQuery) Test(org.junit.Test)

Example 4 with BatchQueryRow

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);
    }
}
Also used : InsertBatchQuery(org.apache.cayenne.query.InsertBatchQuery) 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) CayenneException(org.apache.cayenne.CayenneException)

Example 5 with BatchQueryRow

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);
}
Also used : BatchQueryRow(org.apache.cayenne.query.BatchQueryRow) JdbcAdapter(org.apache.cayenne.dba.JdbcAdapter) DbAdapter(org.apache.cayenne.dba.DbAdapter) DbAttributeBinding(org.apache.cayenne.access.translator.DbAttributeBinding) BatchQuery(org.apache.cayenne.query.BatchQuery) Test(org.junit.Test)

Aggregations

BatchQueryRow (org.apache.cayenne.query.BatchQueryRow)8 DbAttributeBinding (org.apache.cayenne.access.translator.DbAttributeBinding)7 DbAdapter (org.apache.cayenne.dba.DbAdapter)5 DbAttribute (org.apache.cayenne.map.DbAttribute)4 JdbcAdapter (org.apache.cayenne.dba.JdbcAdapter)3 DbEntity (org.apache.cayenne.map.DbEntity)3 BatchQuery (org.apache.cayenne.query.BatchQuery)3 Test (org.junit.Test)3 JdbcEventLogger (org.apache.cayenne.log.JdbcEventLogger)2 InsertBatchQuery (org.apache.cayenne.query.InsertBatchQuery)2 PreparedStatement (java.sql.PreparedStatement)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 CayenneException (org.apache.cayenne.CayenneException)1 ObjectId (org.apache.cayenne.ObjectId)1 OptimisticLockException (org.apache.cayenne.access.OptimisticLockException)1 BatchTranslator (org.apache.cayenne.access.translator.batch.BatchTranslator)1 BindingsTransformer (org.apache.cayenne.crypto.transformer.BindingsTransformer)1