Search in sources :

Example 21 with DbAttributeBinding

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

the class DefaultTransformerFactory method encryptor.

@Override
public BindingsTransformer encryptor(DbAttributeBinding[] bindings, ExtendedTypeMap extendedTypeMap) {
    int len = bindings.length;
    List<Integer> cryptoColumns = null;
    for (int i = 0; i < len; i++) {
        DbAttribute a = bindings[i].getAttribute();
        if (columnMapper.isEncrypted(a)) {
            if (cryptoColumns == null) {
                cryptoColumns = new ArrayList<>(len - i);
            }
            cryptoColumns.add(i);
        }
    }
    if (cryptoColumns != null) {
        int dlen = cryptoColumns.size();
        int[] positions = new int[dlen];
        ValueEncryptor[] transformers = new ValueEncryptor[dlen];
        for (int i = 0; i < dlen; i++) {
            int pos = cryptoColumns.get(i);
            DbAttributeBinding b = bindings[pos];
            positions[i] = pos;
            transformers[i] = transformerFactory.encryptor(b.getAttribute());
        }
        return new DefaultBindingsTransformer(positions, transformers, bytesTransformerFactory.encryptor(), extendedTypeMap);
    }
    return null;
}
Also used : DbAttribute(org.apache.cayenne.map.DbAttribute) DbAttributeBinding(org.apache.cayenne.access.translator.DbAttributeBinding) ValueEncryptor(org.apache.cayenne.crypto.transformer.value.ValueEncryptor)

Example 22 with DbAttributeBinding

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

the class BatchAction method runAsIndividualQueries.

/**
 * Executes batch as individual queries over the same prepared statement.
 */
protected void runAsIndividualQueries(Connection connection, BatchTranslator translator, OperationObserver delegate, boolean generatesKeys) throws SQLException, Exception {
    JdbcEventLogger logger = dataNode.getJdbcEventLogger();
    boolean useOptimisticLock = query.isUsingOptimisticLocking();
    String queryStr = translator.getSql();
    // log batch SQL execution
    logger.log(queryStr);
    // run batch queries one by one
    DbAdapter adapter = dataNode.getAdapter();
    try (PreparedStatement statement = prepareStatement(connection, queryStr, adapter, generatesKeys)) {
        for (BatchQueryRow row : query.getRows()) {
            DbAttributeBinding[] bindings = translator.updateBindings(row);
            logger.logQueryParameters("bind", bindings);
            bind(adapter, statement, bindings);
            int updated = statement.executeUpdate();
            if (useOptimisticLock && updated != 1) {
                throw new OptimisticLockException(row.getObjectId(), query.getDbEntity(), queryStr, row.getQualifier());
            }
            delegate.nextCount(query, updated);
            if (generatesKeys) {
                processGeneratedKeys(statement, delegate, row);
            }
            logger.logUpdateCount(updated);
        }
    }
}
Also used : BatchQueryRow(org.apache.cayenne.query.BatchQueryRow) DbAdapter(org.apache.cayenne.dba.DbAdapter) JdbcEventLogger(org.apache.cayenne.log.JdbcEventLogger) OptimisticLockException(org.apache.cayenne.access.OptimisticLockException) DbAttributeBinding(org.apache.cayenne.access.translator.DbAttributeBinding)

Example 23 with DbAttributeBinding

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

the class SelectAction method performAction.

@SuppressWarnings({ "unchecked", "rawtypes", "resource" })
@Override
public void performAction(Connection connection, OperationObserver observer) throws SQLException, Exception {
    final long t1 = System.currentTimeMillis();
    JdbcEventLogger logger = dataNode.getJdbcEventLogger();
    SelectTranslator translator = dataNode.selectTranslator(query);
    final String sql = translator.getSql();
    final DbAttributeBinding[] bindings = translator.getBindings();
    logger.logQuery(sql, bindings);
    PreparedStatement statement = connection.prepareStatement(sql);
    bind(dataNode.getAdapter(), statement, bindings);
    int fetchSize = queryMetadata.getStatementFetchSize();
    if (fetchSize != 0) {
        statement.setFetchSize(fetchSize);
    }
    ResultSet rs;
    // exception happens
    try {
        rs = statement.executeQuery();
    } catch (Exception ex) {
        statement.close();
        throw ex;
    }
    RowDescriptor descriptor = new RowDescriptorBuilder().setColumns(translator.getResultColumns()).getDescriptor(dataNode.getAdapter().getExtendedTypes());
    RowReader<?> rowReader = dataNode.rowReader(descriptor, queryMetadata, translator.getAttributeOverrides());
    ResultIterator it = new JDBCResultIterator(statement, rs, rowReader);
    it = forIteratedResult(it, observer, connection, t1, sql);
    it = forSuppressedDistinct(it, translator);
    it = forFetchLimit(it, translator);
    if (observer.isIteratedResult()) {
        try {
            observer.nextRows(query, it);
        } catch (Exception ex) {
            it.close();
            throw ex;
        }
    } else {
        List<DataRow> resultRows;
        try {
            resultRows = it.allRows();
        } finally {
            it.close();
        }
        dataNode.getJdbcEventLogger().logSelectCount(resultRows.size(), System.currentTimeMillis() - t1, sql);
        observer.nextRows(query, resultRows);
    }
}
Also used : ResultIterator(org.apache.cayenne.ResultIterator) DbAttributeBinding(org.apache.cayenne.access.translator.DbAttributeBinding) PreparedStatement(java.sql.PreparedStatement) DataRow(org.apache.cayenne.DataRow) SQLException(java.sql.SQLException) JdbcEventLogger(org.apache.cayenne.log.JdbcEventLogger) SelectTranslator(org.apache.cayenne.access.translator.select.SelectTranslator) ResultSet(java.sql.ResultSet)

Example 24 with DbAttributeBinding

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

the class InsertBatchTranslator method createBindings.

@Override
protected DbAttributeBinding[] createBindings() {
    List<DbAttribute> attributes = query.getDbAttributes();
    int len = attributes.size();
    DbAttributeBinding[] bindings = new DbAttributeBinding[len];
    for (int i = 0; i < len; i++) {
        DbAttribute a = attributes.get(i);
        bindings[i] = new DbAttributeBinding(a);
        // precompiled here
        if (includeInBatch(a)) {
            // setting fake position here... all we care about is that it is
            // > -1
            bindings[i].include(1, null, null);
        } else {
            bindings[i].exclude();
        }
    }
    return bindings;
}
Also used : DbAttribute(org.apache.cayenne.map.DbAttribute) DbAttributeBinding(org.apache.cayenne.access.translator.DbAttributeBinding)

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