use of java.sql.Struct 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.Struct 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);
}
use of java.sql.Struct in project dbeaver by serge-rider.
the class PostgreStructValueHandler method convertStringToStruct.
private JDBCCompositeStatic convertStringToStruct(@NotNull DBCSession session, @NotNull PostgreDataType compType, @NotNull String value) throws DBException {
if (value.startsWith("(") && value.endsWith(")")) {
value = value.substring(1, value.length() - 1);
}
final Collection<PostgreDataTypeAttribute> attributes = compType.getAttributes(session.getProgressMonitor());
if (attributes == null) {
throw new DBException("Composite type '" + compType.getTypeName() + "' has no attributes");
}
String[] parsedValues = PostgreUtils.parseObjectString(value);
if (parsedValues.length != attributes.size()) {
log.debug("Number o attributes (" + attributes.size() + ") doesn't match actual number of parsed strings (" + parsedValues.length + ")");
}
Object[] attrValues = new Object[attributes.size()];
Iterator<PostgreDataTypeAttribute> attrIter = attributes.iterator();
for (int i = 0; i < parsedValues.length && attrIter.hasNext(); i++) {
final PostgreDataTypeAttribute itemAttr = attrIter.next();
attrValues[i] = PostgreUtils.convertStringToValue(itemAttr, parsedValues[i], true);
}
Struct contents = new JDBCStructImpl(compType.getTypeName(), attrValues);
return new JDBCCompositeStatic(session, compType, contents);
}
use of java.sql.Struct in project jdk8u_jdk by JetBrains.
the class SQLInputImplTests method test11.
/*
* Validate that readObject returns the correct value when a Struct is
* next on the stream
*/
@Test()
public void test11() throws Exception {
Object[] attributes = new Object[] { "Bruce", "Wayne", 1939, "Batman" };
map.put(sqlType, Class.forName("util.SuperHero"));
Struct struct = new StubStruct(sqlType, attributes);
Object[] values = { struct };
SQLInputImpl sqli = new SQLInputImpl(values, map);
Object o = sqli.readObject();
assertTrue(hero.equals(o));
}
use of java.sql.Struct in project calcite-avatica by apache.
the class StructImplTest method structAccessor.
@Test
public void structAccessor() throws Exception {
// Define the struct type we're creating
ColumnMetaData intMetaData = MetaImpl.columnMetaData("MY_INT", 1, int.class, false);
ColumnMetaData stringMetaData = MetaImpl.columnMetaData("MY_STRING", 2, String.class, true);
StructType structType = ColumnMetaData.struct(Arrays.asList(intMetaData, stringMetaData));
// Create some structs
Struct struct1 = new StructImpl(Arrays.<Object>asList(1, "one"));
Struct struct2 = new StructImpl(Arrays.<Object>asList(2, "two"));
Struct struct3 = new StructImpl(Arrays.<Object>asList(3));
Struct struct4 = new StructImpl(Arrays.<Object>asList(4, "four", "ignored"));
ColumnMetaData structMetaData = MetaImpl.columnMetaData("MY_STRUCT", 1, structType, false);
List<List<Object>> rows = Arrays.asList(Collections.<Object>singletonList(struct1), Collections.<Object>singletonList(struct2), Collections.<Object>singletonList(struct3), Collections.<Object>singletonList(struct4));
// Create four rows, each with one (struct) column
try (Cursor cursor = new ListIteratorCursor(rows.iterator())) {
List<Accessor> accessors = cursor.createAccessors(Collections.singletonList(structMetaData), Unsafe.localCalendar(), null);
assertEquals(1, accessors.size());
Accessor accessor = accessors.get(0);
assertTrue(cursor.next());
Struct s = accessor.getObject(Struct.class);
Object[] structData = s.getAttributes();
assertEquals(2, structData.length);
assertEquals(1, structData[0]);
assertEquals("one", structData[1]);
assertTrue(cursor.next());
s = accessor.getObject(Struct.class);
structData = s.getAttributes();
assertEquals(2, structData.length);
assertEquals(2, structData[0]);
assertEquals("two", structData[1]);
assertTrue(cursor.next());
s = accessor.getObject(Struct.class);
structData = s.getAttributes();
assertEquals(1, structData.length);
assertEquals(3, structData[0]);
assertTrue(cursor.next());
s = accessor.getObject(Struct.class);
structData = s.getAttributes();
assertEquals(3, structData.length);
assertEquals(4, structData[0]);
assertEquals("four", structData[1]);
// We didn't provide metadata, but we still expect to see it.
assertEquals("ignored", structData[2]);
}
}
Aggregations