Search in sources :

Example 6 with DbAttributeBinding

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

the class SoftDeleteBatchTranslator method createBindings.

@Override
protected DbAttributeBinding[] createBindings() {
    DbAttributeBinding[] superBindings = super.createBindings();
    int slen = superBindings.length;
    DbAttributeBinding[] bindings = new DbAttributeBinding[slen + 1];
    DbAttribute deleteAttribute = query.getDbEntity().getAttribute(deletedFieldName);
    String typeName = TypesMapping.getJavaBySqlType(deleteAttribute.getType());
    ExtendedType extendedType = adapter.getExtendedTypes().getRegisteredType(typeName);
    bindings[0] = new DbAttributeBinding(deleteAttribute);
    bindings[0].include(1, true, extendedType);
    System.arraycopy(superBindings, 0, bindings, 1, slen);
    return bindings;
}
Also used : DbAttribute(org.apache.cayenne.map.DbAttribute) DbAttributeBinding(org.apache.cayenne.access.translator.DbAttributeBinding) ExtendedType(org.apache.cayenne.access.types.ExtendedType)

Example 7 with DbAttributeBinding

use of org.apache.cayenne.access.translator.DbAttributeBinding 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;
}
Also used : UpdateBatchQuery(org.apache.cayenne.query.UpdateBatchQuery) DbAttribute(org.apache.cayenne.map.DbAttribute) DbAttributeBinding(org.apache.cayenne.access.translator.DbAttributeBinding)

Example 8 with DbAttributeBinding

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

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

the class DefaultBindingsTransformer method transform.

@Override
public void transform(DbAttributeBinding[] bindings) {
    int len = positions.length;
    for (int i = 0; i < len; i++) {
        DbAttributeBinding b = bindings[positions[i]];
        Object transformed = transformers[i].encrypt(encryptor, b.getValue());
        b.setValue(transformed);
        ExtendedType extendedType = transformed != null ? extendedTypeMap.getRegisteredType(transformed.getClass()) : extendedTypeMap.getDefaultType();
        b.setExtendedType(extendedType);
    }
}
Also used : DbAttributeBinding(org.apache.cayenne.access.translator.DbAttributeBinding) ExtendedType(org.apache.cayenne.access.types.ExtendedType)

Example 10 with DbAttributeBinding

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

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