Search in sources :

Example 16 with DbAttributeBinding

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

the class Oracle8LOBBatchAction method processLOBRow.

void processLOBRow(Connection con, Oracle8LOBBatchTranslator queryBuilder, Oracle8LOBBatchQueryWrapper selectQuery, List<DbAttribute> qualifierAttributes, BatchQueryRow row) throws SQLException, Exception {
    List<DbAttribute> lobAttributes = selectQuery.getDbAttributesForUpdatedLOBColumns();
    if (lobAttributes.size() == 0) {
        return;
    }
    final boolean isLoggable = logger.isLoggable();
    List<Object> qualifierValues = selectQuery.getValuesForLOBSelectQualifier(row);
    List<Object> lobValues = selectQuery.getValuesForUpdatedLOBColumns();
    int parametersSize = qualifierValues.size();
    int lobSize = lobAttributes.size();
    String selectStr = queryBuilder.createLOBSelectString(lobAttributes, qualifierAttributes);
    try (PreparedStatement selectStatement = con.prepareStatement(selectStr)) {
        DbAttributeBinding[] attributeBindings = null;
        if (isLoggable) {
            attributeBindings = new DbAttributeBinding[parametersSize];
        }
        for (int i = 0; i < parametersSize; i++) {
            DbAttribute attribute = qualifierAttributes.get(i);
            Object value = qualifierValues.get(i);
            ExtendedType extendedType = value != null ? adapter.getExtendedTypes().getRegisteredType(value.getClass()) : adapter.getExtendedTypes().getDefaultType();
            DbAttributeBinding binding = new DbAttributeBinding(attribute);
            binding.setStatementPosition(i + 1);
            binding.setValue(value);
            binding.setExtendedType(extendedType);
            adapter.bindParameter(selectStatement, binding);
            if (isLoggable) {
                attributeBindings[i] = binding;
            }
        }
        if (isLoggable) {
            logger.logQuery(selectStr, attributeBindings);
        }
        try (ResultSet result = selectStatement.executeQuery()) {
            if (!result.next()) {
                throw new CayenneRuntimeException("Missing LOB row.");
            }
            // read the only expected row
            for (int i = 0; i < lobSize; i++) {
                DbAttribute attribute = lobAttributes.get(i);
                int type = attribute.getType();
                if (type == Types.CLOB) {
                    Clob clob = result.getClob(i + 1);
                    Object clobVal = lobValues.get(i);
                    if (clobVal instanceof char[]) {
                        writeClob(clob, (char[]) clobVal);
                    } else {
                        writeClob(clob, clobVal.toString());
                    }
                } else if (type == Types.BLOB) {
                    Blob blob = result.getBlob(i + 1);
                    Object blobVal = lobValues.get(i);
                    if (blobVal instanceof byte[]) {
                        writeBlob(blob, (byte[]) blobVal);
                    } else {
                        String className = (blobVal != null) ? blobVal.getClass().getName() : null;
                        throw new CayenneRuntimeException("Unsupported class of BLOB value: %s", className);
                    }
                } else {
                    throw new CayenneRuntimeException("Only BLOB or CLOB is expected here, got: %s", type);
                }
            }
            if (result.next()) {
                throw new CayenneRuntimeException("More than one LOB row found.");
            }
        }
    }
}
Also used : Blob(java.sql.Blob) DbAttribute(org.apache.cayenne.map.DbAttribute) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) PreparedStatement(java.sql.PreparedStatement) DbAttributeBinding(org.apache.cayenne.access.translator.DbAttributeBinding) ResultSet(java.sql.ResultSet) ExtendedType(org.apache.cayenne.access.types.ExtendedType) Clob(java.sql.Clob)

Example 17 with DbAttributeBinding

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

the class Oracle8LOBBatchTranslator 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];
        Object value = row.getValue(i);
        DbAttribute attribute = b.getAttribute();
        int type = attribute.getType();
        // qualifier
        if (isUpdateableColumn(value, type)) {
            ExtendedType extendedType = value != null ? adapter.getExtendedTypes().getRegisteredType(value.getClass()) : adapter.getExtendedTypes().getDefaultType();
            b.include(j++, value, extendedType);
        } else {
            b.exclude();
        }
    }
    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 18 with DbAttributeBinding

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

the class Slf4jJdbcEventLogger method appendParameters.

@SuppressWarnings("unchecked")
private void appendParameters(StringBuilder buffer, String label, ParameterBinding[] bindings) {
    int len = bindings.length;
    if (len > 0) {
        boolean hasIncluded = false;
        for (int i = 0, j = 1; i < len; i++) {
            ParameterBinding b = bindings[i];
            if (b.isExcluded()) {
                continue;
            }
            if (hasIncluded) {
                buffer.append(", ");
            } else {
                hasIncluded = true;
                buffer.append("[").append(label).append(": ");
            }
            buffer.append(j++);
            if (b instanceof DbAttributeBinding) {
                DbAttribute attribute = ((DbAttributeBinding) b).getAttribute();
                if (attribute != null) {
                    buffer.append("->");
                    buffer.append(attribute.getName());
                }
            }
            buffer.append(":");
            if (b.getExtendedType() != null) {
                buffer.append(b.getExtendedType().toString(b.getValue()));
            } else if (b.getValue() == null) {
                buffer.append("NULL");
            } else {
                buffer.append(b.getValue().getClass().getName()).append("@").append(System.identityHashCode(b.getValue()));
            }
        }
        if (hasIncluded) {
            buffer.append("]");
        }
    }
}
Also used : DbAttribute(org.apache.cayenne.map.DbAttribute) DbAttributeBinding(org.apache.cayenne.access.translator.DbAttributeBinding) ParameterBinding(org.apache.cayenne.access.translator.ParameterBinding)

Example 19 with DbAttributeBinding

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

Example 20 with DbAttributeBinding

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

the class DefaultBatchTranslatorIT method testAppendDbAttribute2.

@Test
public void testAppendDbAttribute2() 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];
        }
    };
    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("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