Search in sources :

Example 6 with Struct

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

the class BootstrapAuditTester method compareField.

private boolean compareField(Field f, Object databaseFieldValue, Object avroField) {
    // NULL condition handled
    if (databaseFieldValue == avroField) {
        return true;
    }
    if (databaseFieldValue == null) {
        // avroField cannot also be null or first conditional would have triggered
        LOG.error("compareField error: " + " field=" + f.name() + " null databaseFieldValue but non-null avroField ");
        return false;
    }
    if (avroField == null) {
        // databaseFieldValue cannot also be null or first conditional would have triggered
        LOG.error("compareField error: " + " field=" + f.name() + " non-null databaseFieldValue but null avroField ");
        return false;
    }
    try {
        // == f.schema() if f is not a union
        Schema fieldSchema = SchemaHelper.unwindUnionSchema(f);
        Type avroFieldType = fieldSchema.getType();
        if (_sDebug) {
            LOG.debug("Checking for type:" + avroFieldType + ", Field:" + f.name() + ", Exp:" + databaseFieldValue + ", Got:" + avroField);
        }
        switch(avroFieldType) {
            case BOOLEAN:
                assertEquals(f.name(), databaseFieldValue, avroField);
                break;
            case BYTES:
                byte[] byteArr = null;
                if (databaseFieldValue instanceof Blob) {
                    Blob b = (Blob) databaseFieldValue;
                    byteArr = b.getBytes(1, (int) b.length());
                } else {
                    byteArr = (byte[]) databaseFieldValue;
                }
                assertEquals(f.name(), byteArr, avroField);
                break;
            case DOUBLE:
                assertEquals(f.name(), new Double(((Number) databaseFieldValue).doubleValue()), (avroField));
                break;
            case FLOAT:
                assertEquals(f.name(), new Float(((Number) databaseFieldValue).floatValue()), (avroField));
                break;
            case INT:
                assertEquals(f.name(), Integer.valueOf(((Number) databaseFieldValue).intValue()), (avroField));
                break;
            case LONG:
                if (databaseFieldValue instanceof Number) {
                    long lvalue = ((Number) databaseFieldValue).longValue();
                    assertEquals(f.name(), lvalue, ((Long) avroField).longValue());
                } else if (databaseFieldValue instanceof Timestamp) {
                    long time = ((Timestamp) databaseFieldValue).getTime();
                    assertEquals(f.name(), time, ((Long) avroField).longValue());
                } else if (databaseFieldValue instanceof Date) {
                    long time = ((Date) databaseFieldValue).getTime();
                    assertEquals(f.name(), time, ((Long) avroField).longValue());
                } else {
                    Class timestampClass = null, dateClass = null;
                    try {
                        timestampClass = OracleJarUtils.loadClass("oracle.sql.TIMESTAMP");
                        dateClass = OracleJarUtils.loadClass("oracle.sql.DATE");
                    } catch (Exception e) {
                        String errMsg = "Cannot convert " + databaseFieldValue.getClass() + " to long. Unable to get Oracle datatypes " + e.getMessage();
                        LOG.error(errMsg);
                        throw new EventCreationException(errMsg);
                    }
                    if (timestampClass.isInstance(databaseFieldValue)) {
                        try {
                            Object tsc = timestampClass.cast(databaseFieldValue);
                            Method dateValueMethod = timestampClass.getMethod("dateValue");
                            Date dateValue = (Date) dateValueMethod.invoke(tsc);
                            long time = dateValue.getTime();
                            assertEquals(f.name(), time, ((Long) avroField).longValue());
                        } catch (Exception ex) {
                            String errMsg = "SQLException reading oracle.sql.TIMESTAMP value for field " + f.name();
                            LOG.error(errMsg);
                            throw new RuntimeException(errMsg, ex);
                        }
                    } else if (dateClass.isInstance(databaseFieldValue)) {
                        try {
                            Object dsc = dateClass.cast(databaseFieldValue);
                            Method dateValueMethod = dateClass.getMethod("dateValue");
                            Date dateValue = (Date) dateValueMethod.invoke(dsc);
                            long time = dateValue.getTime();
                            assertEquals(f.name(), time, ((Long) avroField).longValue());
                        } catch (Exception ex) {
                            String errMsg = "SQLException reading oracle.sql.DATE value for field " + f.name();
                            LOG.error(errMsg);
                            throw new RuntimeException(errMsg, ex);
                        }
                    } else {
                        String errMsg = "Cannot convert " + databaseFieldValue.getClass() + " to long for field " + f.name();
                        LOG.error(errMsg);
                        throw new RuntimeException();
                    }
                }
                break;
            case STRING:
                if (databaseFieldValue instanceof Clob) {
                    String text = null;
                    try {
                        text = OracleAvroGenericEventFactory.extractClobText((Clob) databaseFieldValue, f.name());
                    } catch (EventCreationException ex) {
                        LOG.error("compareField error: " + ex.getMessage(), ex);
                    }
                    assertEquals(f.name(), text, ((Utf8) avroField).toString());
                } else {
                    String text = databaseFieldValue.toString();
                    assertEquals(f.name(), text, ((Utf8) avroField).toString());
                }
                break;
            case NULL:
                assertNull(f.name(), databaseFieldValue);
                assertNull(f.name(), avroField);
                break;
            case ARRAY:
                GenericArray<GenericRecord> avroArray = (GenericArray<GenericRecord>) avroField;
                Schema elementSchema = fieldSchema.getElementType();
                Array array = (Array) databaseFieldValue;
                ResultSet arrayResultSet = array.getResultSet();
                int i = 0;
                while (arrayResultSet.next()) {
                    // 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);
                    Object[] attributes = struct.getAttributes();
                    GenericRecord avroElement = avroArray.get(i++);
                    // have to use dbFieldPosition recorded in the schema definition.
                    for (Field field : elementSchema.getFields()) {
                        int dbFieldPosition = Integer.valueOf(SchemaHelper.getMetaField(field, "dbFieldPosition"));
                        Object dbFieldValue = attributes[dbFieldPosition];
                        Object avroFieldValue = avroElement.get(field.name());
                        compareField(field, dbFieldValue, avroFieldValue);
                    }
                }
                break;
            case RECORD:
                assert (compareRecord(fieldSchema, (Struct) databaseFieldValue, (GenericRecord) avroField)) : "comparison of Avro 'record' type failed";
                break;
            case ENUM:
            case FIXED:
            case MAP:
            case UNION:
            default:
                String msg = "Audit for these fields not yet implemented for: " + fieldSchema.getName() + ", Avro type: " + avroFieldType;
                LOG.error(msg);
                throw new RuntimeException(msg);
        }
    } catch (AssertionError err) {
        LOG.error("compareField error: " + err.getMessage() + " field= " + f.name());
        return false;
    } catch (ClassCastException ce) {
        LOG.error("compareField error: " + ce.getMessage() + " field=" + f.name(), ce);
        return false;
    } catch (Exception ex) {
        LOG.error("compareField error: " + ex.getMessage() + " field=" + f.name(), ex);
        return false;
    }
    return true;
}
Also used : Schema(org.apache.avro.Schema) Timestamp(java.sql.Timestamp) Struct(java.sql.Struct) Field(org.apache.avro.Schema.Field) ResultSet(java.sql.ResultSet) GenericArray(org.apache.avro.generic.GenericArray) GenericRecord(org.apache.avro.generic.GenericRecord) Blob(java.sql.Blob) EventCreationException(com.linkedin.databus2.producers.EventCreationException) Method(java.lang.reflect.Method) Date(java.sql.Date) SQLException(java.sql.SQLException) EventCreationException(com.linkedin.databus2.producers.EventCreationException) GenericArray(org.apache.avro.generic.GenericArray) Array(java.sql.Array) Type(org.apache.avro.Schema.Type) Clob(java.sql.Clob)

Example 7 with Struct

use of java.sql.Struct in project dbeaver by serge-rider.

the class JDBCStructValueHandler method getValueFromObject.

@Override
public Object getValueFromObject(@NotNull DBCSession session, @NotNull DBSTypedObject type, Object object, boolean copy) throws DBCException {
    String typeName;
    try {
        if (object instanceof Struct) {
            typeName = ((Struct) object).getSQLTypeName();
        } else {
            typeName = type.getTypeName();
        }
    } catch (SQLException e) {
        throw new DBCException(e, session.getDataSource());
    }
    DBSDataType dataType = null;
    try {
        dataType = DBUtils.resolveDataType(session.getProgressMonitor(), session.getDataSource(), typeName);
    } catch (DBException e) {
        log.debug("Error resolving data type '" + typeName + "'", e);
    }
    if (dataType == null) {
        if (object instanceof Struct) {
            return new JDBCCompositeDynamic(session, (Struct) object, null);
        } else {
            return new JDBCCompositeUnknown(session, object);
        }
    }
    if (object == null) {
        return new JDBCCompositeStatic(session, dataType, new JDBCStructImpl(dataType.getTypeName(), null));
    } else if (object instanceof JDBCCompositeStatic) {
        return copy ? ((JDBCCompositeStatic) object).cloneValue(session.getProgressMonitor()) : object;
    } else if (object instanceof Struct) {
        return new JDBCCompositeStatic(session, dataType, (Struct) object);
    } else {
        return new JDBCCompositeUnknown(session, object);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) JDBCCompositeStatic(org.jkiss.dbeaver.model.impl.jdbc.data.JDBCCompositeStatic) SQLException(java.sql.SQLException) DBSDataType(org.jkiss.dbeaver.model.struct.DBSDataType) JDBCCompositeDynamic(org.jkiss.dbeaver.model.impl.jdbc.data.JDBCCompositeDynamic) JDBCStructImpl(org.jkiss.dbeaver.model.impl.jdbc.JDBCStructImpl) JDBCCompositeUnknown(org.jkiss.dbeaver.model.impl.jdbc.data.JDBCCompositeUnknown) DBCException(org.jkiss.dbeaver.model.exec.DBCException) Struct(java.sql.Struct)

Example 8 with Struct

use of java.sql.Struct in project druid by alibaba.

the class ConnectionProxyImpl method createStruct.

@Override
public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
    FilterChainImpl chain = createChain();
    Struct value = chain.connection_createStruct(this, typeName, attributes);
    recycleFilterChain(chain);
    return value;
}
Also used : FilterChainImpl(com.alibaba.druid.filter.FilterChainImpl) Struct(java.sql.Struct)

Example 9 with Struct

use of java.sql.Struct in project calcite-avatica by apache.

the class ArrayImplTest method arraysOfStructs.

@Test
public void arraysOfStructs() 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"));
    ArrayType arrayType = ColumnMetaData.array(structType, "OBJECT", Rep.STRUCT);
    ColumnMetaData arrayMetaData = MetaImpl.columnMetaData("MY_ARRAY", 1, arrayType, false);
    ArrayImpl.Factory factory = new ArrayFactoryImpl(Unsafe.localCalendar().getTimeZone());
    // Create some arrays from the structs
    Array array1 = factory.createArray(structType, Arrays.<Object>asList(struct1, struct2));
    Array array2 = factory.createArray(structType, Arrays.<Object>asList(struct3, struct4));
    List<List<Object>> rows = Arrays.asList(Collections.<Object>singletonList(array1), Collections.<Object>singletonList(array2));
    // Create two rows, each with one (array) column
    try (Cursor cursor = new ListIteratorCursor(rows.iterator())) {
        List<Accessor> accessors = cursor.createAccessors(Collections.singletonList(arrayMetaData), Unsafe.localCalendar(), factory);
        assertEquals(1, accessors.size());
        Accessor accessor = accessors.get(0);
        assertTrue(cursor.next());
        Array actualArray = accessor.getArray();
        // Avoiding explicit use of the getResultSet() method for now..
        Object[] arrayData = (Object[]) actualArray.getArray();
        assertEquals(2, arrayData.length);
        Struct actualStruct = (Struct) arrayData[0];
        Object[] o = actualStruct.getAttributes();
        assertEquals(2, o.length);
        assertEquals(1, o[0]);
        assertEquals("one", o[1]);
        actualStruct = (Struct) arrayData[1];
        o = actualStruct.getAttributes();
        assertEquals(2, o.length);
        assertEquals(2, o[0]);
        assertEquals("two", o[1]);
        assertTrue(cursor.next());
        actualArray = accessor.getArray();
        arrayData = (Object[]) actualArray.getArray();
        assertEquals(2, arrayData.length);
        actualStruct = (Struct) arrayData[0];
        o = actualStruct.getAttributes();
        assertEquals(1, o.length);
        assertEquals(3, o[0]);
        actualStruct = (Struct) arrayData[1];
        o = actualStruct.getAttributes();
        assertEquals(2, o.length);
        assertEquals(4, o[0]);
        assertEquals("four", o[1]);
    }
}
Also used : StructType(org.apache.calcite.avatica.ColumnMetaData.StructType) Accessor(org.apache.calcite.avatica.util.Cursor.Accessor) Struct(java.sql.Struct) ArrayType(org.apache.calcite.avatica.ColumnMetaData.ArrayType) Array(java.sql.Array) List(java.util.List) ColumnMetaData(org.apache.calcite.avatica.ColumnMetaData) Test(org.junit.Test)

Example 10 with Struct

use of java.sql.Struct in project calcite-avatica by apache.

the class JdbcResultSet method getValue.

private static Object getValue(ResultSet resultSet, int type, int j, Calendar calendar) throws SQLException {
    switch(type) {
        case Types.BIGINT:
            final long aLong = resultSet.getLong(j + 1);
            return aLong == 0 && resultSet.wasNull() ? null : aLong;
        case Types.INTEGER:
            final int anInt = resultSet.getInt(j + 1);
            return anInt == 0 && resultSet.wasNull() ? null : anInt;
        case Types.SMALLINT:
            final short aShort = resultSet.getShort(j + 1);
            return aShort == 0 && resultSet.wasNull() ? null : aShort;
        case Types.TINYINT:
            final byte aByte = resultSet.getByte(j + 1);
            return aByte == 0 && resultSet.wasNull() ? null : aByte;
        case Types.DOUBLE:
        case Types.FLOAT:
            final double aDouble = resultSet.getDouble(j + 1);
            return aDouble == 0D && resultSet.wasNull() ? null : aDouble;
        case Types.REAL:
            final float aFloat = resultSet.getFloat(j + 1);
            return aFloat == 0D && resultSet.wasNull() ? null : aFloat;
        case Types.DATE:
            final Date aDate = resultSet.getDate(j + 1, calendar);
            return aDate == null ? null : (int) (aDate.getTime() / DateTimeUtils.MILLIS_PER_DAY);
        case Types.TIME:
            final Time aTime = resultSet.getTime(j + 1, calendar);
            return aTime == null ? null : (int) (aTime.getTime() % DateTimeUtils.MILLIS_PER_DAY);
        case Types.TIMESTAMP:
            final Timestamp aTimestamp = resultSet.getTimestamp(j + 1, calendar);
            return aTimestamp == null ? null : aTimestamp.getTime();
        case Types.ARRAY:
            final Array array = resultSet.getArray(j + 1);
            if (null == array) {
                return null;
            }
            try {
                // Recursively extracts an Array using its ResultSet-representation
                return extractUsingResultSet(array, calendar);
            } catch (UnsupportedOperationException | SQLFeatureNotSupportedException e) {
                // assumes a non-nested array (depends on the db if that's a valid assumption)
                return extractUsingArray(array, calendar);
            }
        case Types.STRUCT:
            Struct struct = resultSet.getObject(j + 1, Struct.class);
            Object[] attrs = struct.getAttributes();
            List<Object> list = new ArrayList<>(attrs.length);
            for (Object o : attrs) {
                list.add(o);
            }
            return list;
        default:
            return resultSet.getObject(j + 1);
    }
}
Also used : SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) ArrayList(java.util.ArrayList) Time(java.sql.Time) Timestamp(java.sql.Timestamp) Date(java.sql.Date) Struct(java.sql.Struct) Array(java.sql.Array)

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