Search in sources :

Example 1 with Struct

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);
    }
}
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 2 with Struct

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

Example 3 with Struct

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);
}
Also used : DBException(org.jkiss.dbeaver.DBException) JDBCCompositeStatic(org.jkiss.dbeaver.model.impl.jdbc.data.JDBCCompositeStatic) PostgreDataTypeAttribute(org.jkiss.dbeaver.ext.postgresql.model.PostgreDataTypeAttribute) JDBCStructImpl(org.jkiss.dbeaver.model.impl.jdbc.JDBCStructImpl) DBSTypedObject(org.jkiss.dbeaver.model.struct.DBSTypedObject) Struct(java.sql.Struct)

Example 4 with Struct

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));
}
Also used : SQLInputImpl(javax.sql.rowset.serial.SQLInputImpl) StubStruct(util.StubStruct) StubStruct(util.StubStruct) Struct(java.sql.Struct) Test(org.testng.annotations.Test) BaseTest(util.BaseTest)

Example 5 with Struct

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]);
    }
}
Also used : StructType(org.apache.calcite.avatica.ColumnMetaData.StructType) List(java.util.List) ColumnMetaData(org.apache.calcite.avatica.ColumnMetaData) Accessor(org.apache.calcite.avatica.util.Cursor.Accessor) Struct(java.sql.Struct) Test(org.junit.Test)

Aggregations

Struct (java.sql.Struct)11 Array (java.sql.Array)5 SQLException (java.sql.SQLException)4 ResultSet (java.sql.ResultSet)3 EventCreationException (com.linkedin.databus2.producers.EventCreationException)2 Date (java.sql.Date)2 Timestamp (java.sql.Timestamp)2 List (java.util.List)2 Schema (org.apache.avro.Schema)2 GenericArray (org.apache.avro.generic.GenericArray)2 GenericRecord (org.apache.avro.generic.GenericRecord)2 ColumnMetaData (org.apache.calcite.avatica.ColumnMetaData)2 StructType (org.apache.calcite.avatica.ColumnMetaData.StructType)2 Accessor (org.apache.calcite.avatica.util.Cursor.Accessor)2 DBException (org.jkiss.dbeaver.DBException)2 JDBCStructImpl (org.jkiss.dbeaver.model.impl.jdbc.JDBCStructImpl)2 JDBCCompositeStatic (org.jkiss.dbeaver.model.impl.jdbc.data.JDBCCompositeStatic)2 Test (org.junit.Test)2 Test (org.testng.annotations.Test)2 BaseTest (util.BaseTest)2