Search in sources :

Example 1 with Array

use of java.sql.Array in project jOOQ by jOOQ.

the class DefaultBinding method pgGetArray.

/**
     * Workarounds for the unimplemented Postgres JDBC driver features
     */
@SuppressWarnings("unchecked")
private static final <T> T pgGetArray(Scope ctx, ResultSet rs, Class<T> type, int index) throws SQLException {
    // Get the JDBC Array and check for null. If null, that's OK
    Array array = null;
    try {
        array = rs.getArray(index);
        if (array == null) {
            return null;
        }
        // Try fetching a Java Object[]. That's gonna work for non-UDT types
        try {
            // [#5586] [#5613] TODO: Improve PostgreSQL array deserialisation.
            if (byte[][].class == type)
                throw new ControlFlowSignal("GOTO the next array deserialisation strategy");
            else
                return (T) convertArray(array, (Class<? extends Object[]>) type);
        }// This might be a UDT (not implemented exception...)
         catch (Exception e) {
            List<Object> result = new ArrayList<Object>();
            ResultSet arrayRs = null;
            // Try fetching the array as a JDBC ResultSet
            try {
                arrayRs = array.getResultSet();
                while (arrayRs.next()) {
                    DefaultBindingGetResultSetContext<T> out = new DefaultBindingGetResultSetContext<T>(ctx.configuration(), ctx.data(), arrayRs, 2);
                    new DefaultBinding<T, T>(Converters.identity((Class<T>) type.getComponentType()), false).get(out);
                    result.add(out.value());
                }
            }// That might fail too, then we don't know any further...
             catch (Exception fatal) {
                String string = null;
                try {
                    string = rs.getString(index);
                } catch (SQLException ignore) {
                }
                log.error("Cannot parse array", string, fatal);
                return null;
            } finally {
                safeClose(arrayRs);
            }
            return (T) convertArray(result.toArray(), (Class<? extends Object[]>) type);
        }
    } finally {
        safeFree(array);
    }
}
Also used : SQLException(java.sql.SQLException) PostgresUtils.toPGArrayString(org.jooq.util.postgres.PostgresUtils.toPGArrayString) DataTypeException(org.jooq.exception.DataTypeException) SQLDialectNotSupportedException(org.jooq.exception.SQLDialectNotSupportedException) MappingException(org.jooq.exception.MappingException) SQLException(java.sql.SQLException) Array(java.sql.Array) MockArray(org.jooq.tools.jdbc.MockArray) ResultSet(java.sql.ResultSet) MockResultSet(org.jooq.tools.jdbc.MockResultSet) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ArrayList(java.util.ArrayList) ControlFlowSignal(org.jooq.exception.ControlFlowSignal)

Example 2 with Array

use of java.sql.Array in project jOOQ by jOOQ.

the class DefaultExecuteContext method clean.

/**
     * Clean up blobs, clobs and the local configuration.
     * <p>
     * <h5>BLOBS and CLOBS</h5>
     * <p>
     * [#1326] This is necessary in those dialects that have long-lived
     * temporary lob objects, which can cause memory leaks in certain contexts,
     * where the lobs' underlying session / connection is long-lived as well.
     * Specifically, Oracle and ojdbc have some trouble when streaming temporary
     * lobs to UDTs:
     * <ol>
     * <li>The lob cannot have a call-scoped life time with UDTs</li>
     * <li>Freeing the lob after binding will cause an ORA-22275</li>
     * <li>Not freeing the lob after execution will cause an
     * {@link OutOfMemoryError}</li>
     * </ol>
     * <p>
     * <h5>Local configuration</h5>
     * <p>
     * [#1544] There exist some corner-cases regarding the {@link SQLOutput}
     * API, used for UDT serialisation / deserialisation, which have no elegant
     * solutions of obtaining a {@link Configuration} and thus a JDBC
     * {@link Connection} object short of:
     * <ul>
     * <li>Making assumptions about the JDBC driver and using proprietary API,
     * e.g. that of ojdbc</li>
     * <li>Dealing with this problem globally by using such a local
     * configuration</li>
     * </ul>
     *
     * @see <a
     *      href="http://stackoverflow.com/q/11439543/521799">http://stackoverflow.com/q/11439543/521799</a>
     */
static final void clean() {
    List<Blob> blobs = BLOBS.get();
    List<Clob> clobs = CLOBS.get();
    List<SQLXML> xmls = SQLXMLS.get();
    List<Array> arrays = ARRAYS.get();
    if (blobs != null) {
        for (Blob blob : blobs) {
            JDBCUtils.safeFree(blob);
        }
        BLOBS.remove();
    }
    if (clobs != null) {
        for (Clob clob : clobs) {
            JDBCUtils.safeFree(clob);
        }
        CLOBS.remove();
    }
    if (xmls != null) {
        for (SQLXML xml : xmls) {
            JDBCUtils.safeFree(xml);
        }
        SQLXMLS.remove();
    }
    if (arrays != null) {
        for (Array array : arrays) {
            JDBCUtils.safeFree(array);
        }
        SQLXMLS.remove();
    }
    LOCAL_CONFIGURATION.remove();
    LOCAL_DATA.remove();
    LOCAL_CONNECTION.remove();
}
Also used : Array(java.sql.Array) Blob(java.sql.Blob) SQLXML(java.sql.SQLXML) Clob(java.sql.Clob)

Example 3 with Array

use of java.sql.Array in project jOOQ by jOOQ.

the class MockStatement method getArray.

@Override
public Array getArray(int parameterIndex) throws SQLException {
    Array value = outParameters().get(translate(parameterIndex), Array.class);
    resultWasNull = value == null;
    return value;
}
Also used : Array(java.sql.Array)

Example 4 with Array

use of java.sql.Array in project databus by linkedin.

the class DataReader method doIt.

public void doIt() {
    Connection con = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        con = getConnection();
        stmt = con.prepareStatement("SELECT * FROM " + _table + " WHERE ROWNUM < 2");
        rs = stmt.executeQuery();
        rs.next();
        ResultSetMetaData rsmd = rs.getMetaData();
        for (int column = 1; column <= rsmd.getColumnCount(); column++) {
            System.out.println(column + ": " + rsmd.getColumnName(column) + " --> " + rs.getObject(column).getClass().getName());
            if (rs.getObject(column) instanceof Array) {
                Array arr = rs.getArray(column);
                System.out.println("\t" + arr.getBaseTypeName());
                ResultSet rs2 = arr.getResultSet();
                while (rs2.next()) {
                    System.out.println("\t" + rs2.getObject(1));
                    Struct struct = (Struct) rs2.getObject(2);
                    System.out.println(Arrays.toString(struct.getAttributes()));
                }
                rs2.close();
            }
        }
    } catch (SQLException ex) {
        System.out.println(ex);
        ex.printStackTrace();
    } finally {
        SchemaUtils.close(rs);
        SchemaUtils.close(stmt);
        SchemaUtils.close(con);
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) Array(java.sql.Array) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Struct(java.sql.Struct)

Example 5 with Array

use of java.sql.Array in project databus by linkedin.

the class OracleAvroGenericEventFactory method putArray.

private void putArray(GenericRecord record, String arrayFieldName, Schema schema, Array array) throws EventCreationException {
    // Make sure this is an array type
    if (schema.getType() != Type.ARRAY) {
        throw new EventCreationException("Not an array type. " + schema.getName());
    }
    Schema elementSchema = schema.getElementType();
    GenericArray<GenericRecord> avroArray = new GenericData.Array<GenericRecord>(0, schema);
    try {
        ResultSet arrayResultSet = array.getResultSet();
        try {
            while (arrayResultSet.next()) {
                // Create the avro record and add it to the array
                GenericRecord elemRecord = new GenericData.Record(elementSchema);
                avroArray.add(elemRecord);
                // Get the underlying structure from the database. Oracle returns the structure in the
                // second column of the array's ResultSet
                Struct struct = (Struct) arrayResultSet.getObject(2);
                putOracleRecord(elemRecord, elementSchema, struct);
            }
        } finally {
            arrayResultSet.close();
        }
    } catch (SQLException e) {
        throw new EventCreationException("putArray error: " + e.getMessage(), e);
    }
    record.put(arrayFieldName, avroArray);
}
Also used : GenericArray(org.apache.avro.generic.GenericArray) Array(java.sql.Array) SQLException(java.sql.SQLException) EventCreationException(com.linkedin.databus2.producers.EventCreationException) Schema(org.apache.avro.Schema) ResultSet(java.sql.ResultSet) GenericRecord(org.apache.avro.generic.GenericRecord) GenericRecord(org.apache.avro.generic.GenericRecord) Struct(java.sql.Struct)

Aggregations

Array (java.sql.Array)122 Test (org.junit.Test)84 Connection (java.sql.Connection)74 ResultSet (java.sql.ResultSet)69 PreparedStatement (java.sql.PreparedStatement)46 Properties (java.util.Properties)29 PhoenixArray (org.apache.phoenix.schema.types.PhoenixArray)25 ArrayList (java.util.ArrayList)23 BaseTest (org.apache.phoenix.query.BaseTest)22 SQLException (java.sql.SQLException)19 ScalarType (org.apache.calcite.avatica.ColumnMetaData.ScalarType)16 Timestamp (java.sql.Timestamp)10 Date (java.sql.Date)8 Random (java.util.Random)7 Time (java.sql.Time)6 Statement (java.sql.Statement)5 Struct (java.sql.Struct)5 HashMap (java.util.HashMap)5 List (java.util.List)5 BigDecimal (java.math.BigDecimal)4