Search in sources :

Example 1 with DbAttributeBinding

use of org.apache.cayenne.access.translator.DbAttributeBinding 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 DbAttributeBinding

use of org.apache.cayenne.access.translator.DbAttributeBinding in project cayenne by apache.

the class DeleteBatchTranslator method createBindings.

@Override
protected DbAttributeBinding[] createBindings() {
    DeleteBatchQuery deleteBatch = (DeleteBatchQuery) query;
    List<DbAttribute> attributes = deleteBatch.getDbAttributes();
    int len = attributes.size();
    DbAttributeBinding[] bindings = new DbAttributeBinding[len];
    for (int i = 0; i < len; i++) {
        bindings[i] = new DbAttributeBinding(attributes.get(i));
    }
    return bindings;
}
Also used : DeleteBatchQuery(org.apache.cayenne.query.DeleteBatchQuery) DbAttribute(org.apache.cayenne.map.DbAttribute) DbAttributeBinding(org.apache.cayenne.access.translator.DbAttributeBinding)

Example 3 with DbAttributeBinding

use of org.apache.cayenne.access.translator.DbAttributeBinding in project cayenne by apache.

the class DeleteBatchTranslator method doUpdateBindings.

@Override
protected DbAttributeBinding[] doUpdateBindings(BatchQueryRow row) {
    int len = bindings.length;
    DeleteBatchQuery deleteBatch = (DeleteBatchQuery) query;
    for (int i = 0, j = 1; i < len; i++) {
        DbAttributeBinding b = bindings[i];
        // skip null attributes... they are translated as "IS NULL"
        if (deleteBatch.isNull(b.getAttribute())) {
            b.exclude();
        } else {
            Object value = row.getValue(i);
            ExtendedType extendedType = value != null ? adapter.getExtendedTypes().getRegisteredType(value.getClass()) : adapter.getExtendedTypes().getDefaultType();
            b.include(j++, value, extendedType);
        }
    }
    return bindings;
}
Also used : DeleteBatchQuery(org.apache.cayenne.query.DeleteBatchQuery) DbAttributeBinding(org.apache.cayenne.access.translator.DbAttributeBinding) ExtendedType(org.apache.cayenne.access.types.ExtendedType)

Example 4 with DbAttributeBinding

use of org.apache.cayenne.access.translator.DbAttributeBinding in project cayenne by apache.

the class InsertBatchTranslator method doUpdateBindings.

@Override
protected DbAttributeBinding[] doUpdateBindings(BatchQueryRow row) {
    int len = bindings.length;
    for (int i = 0, j = 1; i < len; i++) {
        DbAttributeBinding b = bindings[i];
        // exclusions are permanent
        if (!b.isExcluded()) {
            Object value = row.getValue(i);
            ExtendedType extendedType = value != null ? adapter.getExtendedTypes().getRegisteredType(value.getClass()) : adapter.getExtendedTypes().getDefaultType();
            b.include(j++, value, extendedType);
        }
    }
    return bindings;
}
Also used : DbAttributeBinding(org.apache.cayenne.access.translator.DbAttributeBinding) ExtendedType(org.apache.cayenne.access.types.ExtendedType)

Example 5 with DbAttributeBinding

use of org.apache.cayenne.access.translator.DbAttributeBinding in project cayenne by apache.

the class SoftDeleteBatchTranslator method doUpdateBindings.

@Override
protected DbAttributeBinding[] doUpdateBindings(BatchQueryRow row) {
    int len = bindings.length;
    DeleteBatchQuery deleteBatch = (DeleteBatchQuery) query;
    // skip position 0... Otherwise follow super algorithm
    for (int i = 1, j = 2; i < len; i++) {
        DbAttributeBinding b = bindings[i];
        // skip null attributes... they are translated as "IS NULL"
        if (deleteBatch.isNull(b.getAttribute())) {
            b.exclude();
        } else {
            Object value = row.getValue(i - 1);
            ExtendedType extendedType = value != null ? adapter.getExtendedTypes().getRegisteredType(value.getClass()) : adapter.getExtendedTypes().getDefaultType();
            b.include(j++, value, extendedType);
        }
    }
    return bindings;
}
Also used : DeleteBatchQuery(org.apache.cayenne.query.DeleteBatchQuery) DbAttributeBinding(org.apache.cayenne.access.translator.DbAttributeBinding) ExtendedType(org.apache.cayenne.access.types.ExtendedType)

Aggregations

DbAttributeBinding (org.apache.cayenne.access.translator.DbAttributeBinding)24 DbAttribute (org.apache.cayenne.map.DbAttribute)12 ExtendedType (org.apache.cayenne.access.types.ExtendedType)8 BatchQueryRow (org.apache.cayenne.query.BatchQueryRow)7 DbAdapter (org.apache.cayenne.dba.DbAdapter)5 PreparedStatement (java.sql.PreparedStatement)3 JdbcAdapter (org.apache.cayenne.dba.JdbcAdapter)3 JdbcEventLogger (org.apache.cayenne.log.JdbcEventLogger)3 BatchQuery (org.apache.cayenne.query.BatchQuery)3 DeleteBatchQuery (org.apache.cayenne.query.DeleteBatchQuery)3 Test (org.junit.Test)3 ResultSet (java.sql.ResultSet)2 DbEntity (org.apache.cayenne.map.DbEntity)2 UpdateBatchQuery (org.apache.cayenne.query.UpdateBatchQuery)2 Blob (java.sql.Blob)1 Clob (java.sql.Clob)1 SQLException (java.sql.SQLException)1 CayenneException (org.apache.cayenne.CayenneException)1 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)1 DataRow (org.apache.cayenne.DataRow)1