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);
}
}
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();
}
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;
}
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);
}
}
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);
}
Aggregations