Search in sources :

Example 11 with ExtendedType

use of org.apache.cayenne.access.types.ExtendedType in project cayenne by apache.

the class OracleAdapter method bindParameter.

@Override
public void bindParameter(PreparedStatement statement, ParameterBinding binding) throws SQLException, Exception {
    // NULL Boolean here, as super doesn't pass it through ExtendedType...
    if (binding.getValue() == null && binding.getJdbcType() == Types.BOOLEAN) {
        ExtendedType typeProcessor = getExtendedTypes().getRegisteredType(Boolean.class);
        typeProcessor.setJdbcObject(statement, binding.getValue(), binding.getStatementPosition(), binding.getJdbcType(), binding.getScale());
    } else {
        super.bindParameter(statement, binding);
    }
}
Also used : ExtendedType(org.apache.cayenne.access.types.ExtendedType)

Example 12 with ExtendedType

use of org.apache.cayenne.access.types.ExtendedType 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 13 with ExtendedType

use of org.apache.cayenne.access.types.ExtendedType 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 14 with ExtendedType

use of org.apache.cayenne.access.types.ExtendedType in project cayenne by apache.

the class OracleProcedureAction method readProcedureOutParameters.

/**
 * Helper method that reads OUT parameters of a CallableStatement.
 */
@Override
protected void readProcedureOutParameters(CallableStatement statement, OperationObserver delegate) throws SQLException, Exception {
    long t1 = System.currentTimeMillis();
    // build result row...
    DataRow result = null;
    List<ProcedureParameter> parameters = getProcedure().getCallParameters();
    for (int i = 0; i < parameters.size(); i++) {
        ProcedureParameter parameter = parameters.get(i);
        if (!parameter.isOutParam()) {
            continue;
        }
        // ==== start Oracle-specific part
        if (parameter.getType() == OracleAdapter.getOracleCursorType()) {
            try (ResultSet rs = (ResultSet) statement.getObject(i + 1)) {
                RowDescriptor rsDescriptor = describeResultSet(rs, processedResultSets++);
                readResultSet(rs, rsDescriptor, query, delegate);
            }
        } else // ==== end Oracle-specific part
        {
            if (result == null) {
                result = new DataRow(2);
            }
            ColumnDescriptor descriptor = new ColumnDescriptor(parameter);
            ExtendedType type = dataNode.getAdapter().getExtendedTypes().getRegisteredType(descriptor.getJavaClass());
            Object val = type.materializeObject(statement, i + 1, descriptor.getJdbcType());
            result.put(descriptor.getDataRowKey(), val);
        }
    }
    if (result != null && !result.isEmpty()) {
        // treat out parameters as a separate data row set
        dataNode.getJdbcEventLogger().logSelectCount(1, System.currentTimeMillis() - t1);
        delegate.nextRows(query, Collections.singletonList(result));
    }
}
Also used : ProcedureParameter(org.apache.cayenne.map.ProcedureParameter) ColumnDescriptor(org.apache.cayenne.access.jdbc.ColumnDescriptor) ResultSet(java.sql.ResultSet) ExtendedType(org.apache.cayenne.access.types.ExtendedType) DataRow(org.apache.cayenne.DataRow) RowDescriptor(org.apache.cayenne.access.jdbc.RowDescriptor)

Example 15 with ExtendedType

use of org.apache.cayenne.access.types.ExtendedType in project cayenne by apache.

the class PerAdapterProviderTest method before.

@Before
public void before() {
    ResourceLocator locator = new ClassLoaderResourceLocator(new DefaultClassLoaderManager());
    RuntimeProperties runtimeProperties = mock(RuntimeProperties.class);
    ValueObjectTypeRegistry valueObjectTypeRegistry = mock(ValueObjectTypeRegistry.class);
    this.oracleAdapter = new OracleAdapter(runtimeProperties, Collections.<ExtendedType>emptyList(), Collections.<ExtendedType>emptyList(), Collections.<ExtendedTypeFactory>emptyList(), locator, valueObjectTypeRegistry);
    this.derbyAdapter = new DerbyAdapter(runtimeProperties, Collections.<ExtendedType>emptyList(), Collections.<ExtendedType>emptyList(), Collections.<ExtendedTypeFactory>emptyList(), locator, valueObjectTypeRegistry);
    this.autoDerbyAdapter = new AutoAdapter(new Provider<DbAdapter>() {

        @Override
        public DbAdapter get() throws DIRuntimeException {
            return derbyAdapter;
        }
    }, new Slf4jJdbcEventLogger(runtimeProperties));
}
Also used : OracleAdapter(org.apache.cayenne.dba.oracle.OracleAdapter) DerbyAdapter(org.apache.cayenne.dba.derby.DerbyAdapter) Slf4jJdbcEventLogger(org.apache.cayenne.log.Slf4jJdbcEventLogger) ExtendedTypeFactory(org.apache.cayenne.access.types.ExtendedTypeFactory) ClassLoaderResourceLocator(org.apache.cayenne.resource.ClassLoaderResourceLocator) ValueObjectTypeRegistry(org.apache.cayenne.access.types.ValueObjectTypeRegistry) ExtendedType(org.apache.cayenne.access.types.ExtendedType) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) RuntimeProperties(org.apache.cayenne.configuration.RuntimeProperties) ResourceLocator(org.apache.cayenne.resource.ResourceLocator) ClassLoaderResourceLocator(org.apache.cayenne.resource.ClassLoaderResourceLocator) Provider(org.apache.cayenne.di.Provider) Before(org.junit.Before)

Aggregations

ExtendedType (org.apache.cayenne.access.types.ExtendedType)17 DbAttributeBinding (org.apache.cayenne.access.translator.DbAttributeBinding)8 DbAttribute (org.apache.cayenne.map.DbAttribute)5 ResultSet (java.sql.ResultSet)2 DataRow (org.apache.cayenne.DataRow)2 ColumnDescriptor (org.apache.cayenne.access.jdbc.ColumnDescriptor)2 RowDescriptor (org.apache.cayenne.access.jdbc.RowDescriptor)2 ExtendedTypeFactory (org.apache.cayenne.access.types.ExtendedTypeFactory)2 ProcedureParameter (org.apache.cayenne.map.ProcedureParameter)2 DeleteBatchQuery (org.apache.cayenne.query.DeleteBatchQuery)2 Blob (java.sql.Blob)1 Clob (java.sql.Clob)1 PreparedStatement (java.sql.PreparedStatement)1 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)1 ParameterBinding (org.apache.cayenne.access.translator.ParameterBinding)1 ProcedureParameterBinding (org.apache.cayenne.access.translator.ProcedureParameterBinding)1 ValueObjectTypeFactory (org.apache.cayenne.access.types.ValueObjectTypeFactory)1 ValueObjectTypeRegistry (org.apache.cayenne.access.types.ValueObjectTypeRegistry)1 RuntimeProperties (org.apache.cayenne.configuration.RuntimeProperties)1 DerbyAdapter (org.apache.cayenne.dba.derby.DerbyAdapter)1