Search in sources :

Example 1 with RowDescriptor

use of org.apache.cayenne.access.jdbc.RowDescriptor in project cayenne by apache.

the class CryptoRowReaderFactoryDecorator method encryptedRowDescriptor.

protected RowDescriptor encryptedRowDescriptor(RowDescriptor descriptor, ExtendedTypeMap typeMap) {
    // need to tweak the original descriptor to ensure binary columns are read as binary, eben if the plain Java
    // type is not a byte[]
    ColumnDescriptor[] originalColumns = descriptor.getColumns();
    int len = originalColumns.length;
    ExtendedType[] originalConverters = descriptor.getConverters();
    ExtendedType[] encryptedConverters = new ExtendedType[len];
    for (int i = 0; i < len; i++) {
        DbAttribute attribute = originalColumns[i].getAttribute();
        ExtendedType t = originalConverters[i];
        if (attribute != null && columnMapper.isEncrypted(attribute)) {
            // only char or binary columns can store encrypted data
            if (TypesMapping.isBinary(attribute.getType())) {
                t = typeMap.getRegisteredType(byte[].class);
            } else if (TypesMapping.isCharacter(attribute.getType())) {
                t = typeMap.getRegisteredType(String.class);
            }
        // else - warning?
        }
        encryptedConverters[i] = t;
    }
    return new RowDescriptor(originalColumns, encryptedConverters);
}
Also used : ColumnDescriptor(org.apache.cayenne.access.jdbc.ColumnDescriptor) DbAttribute(org.apache.cayenne.map.DbAttribute) ExtendedType(org.apache.cayenne.access.types.ExtendedType) RowDescriptor(org.apache.cayenne.access.jdbc.RowDescriptor)

Example 2 with RowDescriptor

use of org.apache.cayenne.access.jdbc.RowDescriptor in project cayenne by apache.

the class MySQLProcedureAction method processResultSet.

private void processResultSet(CallableStatement statement, OperationObserver observer) throws Exception {
    ResultSet rs = Objects.requireNonNull(statement.getResultSet());
    try {
        RowDescriptor descriptor = describeResultSet(rs, processedResultSets++);
        readResultSet(rs, descriptor, query, observer);
    } finally {
        try {
            rs.close();
        } catch (SQLException ex) {
        }
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) RowDescriptor(org.apache.cayenne.access.jdbc.RowDescriptor)

Example 3 with RowDescriptor

use of org.apache.cayenne.access.jdbc.RowDescriptor in project cayenne by apache.

the class SQLServerProcedureAction method performAction.

@Override
public void performAction(Connection connection, OperationObserver observer) throws SQLException, Exception {
    ProcedureTranslator transl = createTranslator(connection);
    try (CallableStatement statement = (CallableStatement) transl.createStatement()) {
        // stored procedure may contain a mixture of update counts and
        // result sets,
        // and out parameters. Read out parameters first, then
        // iterate until we exhaust all results
        boolean hasResultSet = statement.execute();
        // local observer to cache results and provide them to the external
        // observer
        // in the order consistent with other adapters.
        Observer localObserver = new Observer(observer);
        while (true) {
            if (hasResultSet) {
                try (ResultSet rs = statement.getResultSet()) {
                    RowDescriptor descriptor = describeResultSet(rs, processedResultSets++);
                    readResultSet(rs, descriptor, query, localObserver);
                }
            } else {
                int updateCount = statement.getUpdateCount();
                if (updateCount == -1) {
                    break;
                }
                dataNode.getJdbcEventLogger().logUpdateCount(updateCount);
                localObserver.nextCount(query, updateCount);
            }
            hasResultSet = statement.getMoreResults();
        }
        // read out parameters to the main observer ... AFTER the main
        // result set
        // TODO: I hope SQLServer does not support ResultSets as OUT
        // parameters,
        // otherwise
        // the order of custom result descriptors will be messed up
        readProcedureOutParameters(statement, observer);
        // add results back to main observer
        localObserver.flushResults(query);
    }
}
Also used : CallableStatement(java.sql.CallableStatement) OperationObserver(org.apache.cayenne.access.OperationObserver) ResultSet(java.sql.ResultSet) ProcedureTranslator(org.apache.cayenne.access.translator.procedure.ProcedureTranslator) RowDescriptor(org.apache.cayenne.access.jdbc.RowDescriptor)

Example 4 with RowDescriptor

use of org.apache.cayenne.access.jdbc.RowDescriptor in project cayenne by apache.

the class DB2ProcedureAction method performAction.

@Override
public void performAction(Connection connection, OperationObserver observer) throws SQLException, Exception {
    // cloned from super except for result processing consistent with
    // CAY-1874
    processedResultSets = 0;
    ProcedureTranslator transl = createTranslator(connection);
    try (CallableStatement statement = (CallableStatement) transl.createStatement()) {
        initStatement(statement);
        boolean hasResultSet = statement.execute();
        // read out parameters
        readProcedureOutParameters(statement, observer);
        // read the rest of the query
        while (true) {
            if (hasResultSet) {
                try (ResultSet rs = statement.getResultSet()) {
                    RowDescriptor descriptor = describeResultSet(rs, processedResultSets++);
                    readResultSet(rs, descriptor, query, observer);
                }
            } else {
                int updateCount = statement.getUpdateCount();
                if (updateCount == -1) {
                    break;
                }
                dataNode.getJdbcEventLogger().logUpdateCount(updateCount);
                observer.nextCount(query, updateCount);
            }
            hasResultSet = statement.getMoreResults();
        }
    }
}
Also used : CallableStatement(java.sql.CallableStatement) ResultSet(java.sql.ResultSet) ProcedureTranslator(org.apache.cayenne.access.translator.procedure.ProcedureTranslator) RowDescriptor(org.apache.cayenne.access.jdbc.RowDescriptor)

Example 5 with RowDescriptor

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

Aggregations

RowDescriptor (org.apache.cayenne.access.jdbc.RowDescriptor)5 ResultSet (java.sql.ResultSet)4 CallableStatement (java.sql.CallableStatement)2 ColumnDescriptor (org.apache.cayenne.access.jdbc.ColumnDescriptor)2 ProcedureTranslator (org.apache.cayenne.access.translator.procedure.ProcedureTranslator)2 ExtendedType (org.apache.cayenne.access.types.ExtendedType)2 SQLException (java.sql.SQLException)1 DataRow (org.apache.cayenne.DataRow)1 OperationObserver (org.apache.cayenne.access.OperationObserver)1 DbAttribute (org.apache.cayenne.map.DbAttribute)1 ProcedureParameter (org.apache.cayenne.map.ProcedureParameter)1