Search in sources :

Example 1 with BinaryType

use of org.teiid.core.types.BinaryType in project teiid by teiid.

the class AccumuloDataTypeManager method deserialize.

public static Object deserialize(final byte[] value, final Class<?> expectedType) {
    if (value == null || Arrays.equals(value, EMPTY_BYTES)) {
        return null;
    }
    try {
        if (expectedType.isAssignableFrom(Clob.class)) {
            return new ClobImpl(new InputStreamFactory() {

                @Override
                public InputStream getInputStream() throws IOException {
                    return ObjectConverterUtil.convertToInputStream(value);
                }
            }, -1);
        } else if (expectedType.isAssignableFrom(Blob.class)) {
            return new BlobType(new BlobImpl(new InputStreamFactory() {

                @Override
                public InputStream getInputStream() throws IOException {
                    return ObjectConverterUtil.convertToInputStream(value);
                }
            }));
        } else if (expectedType.isAssignableFrom(SQLXML.class)) {
            return new SQLXMLImpl(new InputStreamFactory() {

                @Override
                public InputStream getInputStream() throws IOException {
                    return ObjectConverterUtil.convertToInputStream(value);
                }
            });
        } else if (expectedType.isAssignableFrom(BinaryType.class)) {
            return new BinaryType(value);
        } else if (expectedType.isAssignableFrom(GeometryType.class)) {
            GeometryType result = new GeometryType(Arrays.copyOf(value, value.length - 4));
            int srid = (((value[value.length - 4] & 0xff) << 24) + ((value[value.length - 3] & 0xff) << 16) + ((value[value.length - 2] & 0xff) << 8) + ((value[value.length - 1] & 0xff) << 0));
            result.setSrid(srid);
            return result;
        } else if (expectedType.isAssignableFrom(byte[].class)) {
            return value;
        } else if (expectedType.isAssignableFrom(String.class) || expectedType.isAssignableFrom(Boolean.class) || expectedType.isAssignableFrom(Boolean.class) || expectedType.isAssignableFrom(Byte.class) || expectedType.isAssignableFrom(Short.class) || expectedType.isAssignableFrom(Character.class) || expectedType.isAssignableFrom(Integer.class) || expectedType.isAssignableFrom(Long.class) || expectedType.isAssignableFrom(BigInteger.class) || expectedType.isAssignableFrom(BigDecimal.class) || expectedType.isAssignableFrom(Float.class) || expectedType.isAssignableFrom(Double.class) || expectedType.isAssignableFrom(Date.class) || expectedType.isAssignableFrom(Time.class) || expectedType.isAssignableFrom(Timestamp.class)) {
            return DataTypeManager.transformValue(new String(value, UTF_8), expectedType);
        } else {
            ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(value));
            Object obj = ois.readObject();
            ois.close();
            return obj;
        }
    } catch (ClassNotFoundException e) {
        throw new TeiidRuntimeException(e);
    } catch (IOException e) {
        throw new TeiidRuntimeException(e);
    } catch (TransformationException e) {
        throw new TeiidRuntimeException(e);
    }
}
Also used : TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) InputStreamFactory(org.teiid.core.types.InputStreamFactory) Timestamp(java.sql.Timestamp) GeometryType(org.teiid.core.types.GeometryType) ClobImpl(org.teiid.core.types.ClobImpl) BlobImpl(org.teiid.core.types.BlobImpl) Blob(java.sql.Blob) SQLXMLImpl(org.teiid.core.types.SQLXMLImpl) TransformationException(org.teiid.core.types.TransformationException) BinaryType(org.teiid.core.types.BinaryType) ObjectInputStream(java.io.ObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) Date(java.sql.Date) BigInteger(java.math.BigInteger) BlobType(org.teiid.core.types.BlobType) ByteArrayInputStream(java.io.ByteArrayInputStream) BigInteger(java.math.BigInteger) ObjectInputStream(java.io.ObjectInputStream)

Example 2 with BinaryType

use of org.teiid.core.types.BinaryType in project teiid by teiid.

the class StringAgg method writeValue.

private void writeValue(Object val) throws TeiidProcessingException {
    try {
        if (binary) {
            if (val instanceof BinaryType) {
                result.getOuputStream().write(((BinaryType) val).getBytesDirect());
                return;
            }
            Blob b = (Blob) val;
            InputStream binaryStream = b.getBinaryStream();
            try {
                ObjectConverterUtil.write(result.getOuputStream(), binaryStream, -1, false);
            } finally {
                binaryStream.close();
            }
        } else {
            if (val instanceof String) {
                result.getWriter().write((String) val);
                return;
            }
            Clob c = (Clob) val;
            Reader characterStream = c.getCharacterStream();
            try {
                ObjectConverterUtil.write(result.getWriter(), characterStream, -1, false);
            } finally {
                characterStream.close();
            }
        }
    } catch (IOException e) {
        throw new TeiidProcessingException(QueryPlugin.Event.TEIID30422, e);
    } catch (SQLException e) {
        throw new TeiidProcessingException(QueryPlugin.Event.TEIID30423, e);
    }
}
Also used : SerialBlob(javax.sql.rowset.serial.SerialBlob) Blob(java.sql.Blob) BinaryType(org.teiid.core.types.BinaryType) SQLException(java.sql.SQLException) InputStream(java.io.InputStream) Reader(java.io.Reader) IOException(java.io.IOException) Clob(java.sql.Clob) TeiidProcessingException(org.teiid.core.TeiidProcessingException)

Example 3 with BinaryType

use of org.teiid.core.types.BinaryType in project teiid by teiid.

the class TestFunctionMethods method testEncryptDecrypt.

@Test
public void testEncryptDecrypt() throws Exception {
    // $NON-NLS-1$
    String key = "redhat";
    // $NON-NLS-1$
    String data = "jboss teiid";
    // $NON-NLS-1$ //$NON-NLS-2$
    BinaryType encryptedBytes = FunctionMethods.aes_encrypt(new BinaryType(data.getBytes("UTF-8")), new BinaryType(key.getBytes("UTF-8")));
    // $NON-NLS-1$
    BinaryType decryptedBytes = FunctionMethods.aes_decrypt(encryptedBytes, new BinaryType(key.getBytes("UTF-8")));
    // $NON-NLS-1$
    assertArrayEquals(data.getBytes("UTF-8"), decryptedBytes.getBytesDirect());
}
Also used : BinaryType(org.teiid.core.types.BinaryType) Test(org.junit.Test)

Example 4 with BinaryType

use of org.teiid.core.types.BinaryType in project teiid by teiid.

the class TestFunctionTree method testVarbinary.

@Test
public void testVarbinary() throws Exception {
    FunctionMethod method = new FunctionMethod(// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    "dummy", // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    null, // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    null, // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    PushDown.CANNOT_PUSHDOWN, // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    TestFunctionTree.class.getName(), // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    "toString", // $NON-NLS-1$
    Arrays.asList(new FunctionParameter("in", DataTypeManager.DefaultDataTypes.VARBINARY)), // $NON-NLS-1$
    new FunctionParameter("output", DataTypeManager.DefaultDataTypes.STRING), true, Determinism.DETERMINISTIC);
    FunctionTree sys = RealMetadataFactory.SFM.getSystemFunctions();
    FunctionLibrary fl = new FunctionLibrary(sys, new FunctionTree("foo", new UDFSource(Arrays.asList(method)), true));
    FunctionDescriptor fd = fl.findFunction("dummy", new Class<?>[] { DataTypeManager.DefaultDataClasses.VARBINARY });
    String hello = "hello";
    assertEquals(hello, fd.invokeFunction(new Object[] { new BinaryType(hello.getBytes()) }, null, null));
}
Also used : BinaryType(org.teiid.core.types.BinaryType) FunctionMethod(org.teiid.metadata.FunctionMethod) FunctionParameter(org.teiid.metadata.FunctionParameter) Test(org.junit.Test)

Example 5 with BinaryType

use of org.teiid.core.types.BinaryType in project teiid by teiid.

the class JDBCExecutionFactory method bindValue.

/**
 * Sets prepared statement parameter i with param.
 *
 * Performs special handling to translate dates using the database time zone and to
 * translate biginteger, float, and char to JDBC safe objects.
 *
 * @param stmt
 * @param param
 * @param paramType
 * @param i
 * @param cal
 * @throws SQLException
 */
public void bindValue(PreparedStatement stmt, Object param, Class<?> paramType, int i) throws SQLException {
    int type = TypeFacility.getSQLTypeFromRuntimeType(paramType);
    if (param == null) {
        if (type == Types.JAVA_OBJECT) {
            stmt.setNull(i, Types.OTHER);
        } else {
            stmt.setNull(i, type);
        }
        return;
    }
    // if this is a Date object, then use the database calendar
    if (paramType.equals(TypeFacility.RUNTIME_TYPES.DATE)) {
        stmt.setDate(i, (java.sql.Date) param, getDatabaseCalendar());
        return;
    }
    if (paramType.equals(TypeFacility.RUNTIME_TYPES.TIME)) {
        stmt.setTime(i, (java.sql.Time) param, getDatabaseCalendar());
        return;
    }
    if (paramType.equals(TypeFacility.RUNTIME_TYPES.TIMESTAMP)) {
        stmt.setTimestamp(i, (java.sql.Timestamp) param, getDatabaseCalendar());
        return;
    }
    // not all drivers handle the setObject call with BigDecimal correctly (namely jConnect 6.05)
    if (TypeFacility.RUNTIME_TYPES.BIG_DECIMAL.equals(paramType)) {
        stmt.setBigDecimal(i, (BigDecimal) param);
        return;
    }
    // special handling for the srid parameter
    if (paramType == TypeFacility.RUNTIME_TYPES.INTEGER && param instanceof GeometryType) {
        stmt.setInt(i, ((GeometryType) param).getSrid());
        return;
    }
    if (useStreamsForLobs()) {
        if (param instanceof Blob) {
            Blob blob = (Blob) param;
            long length = blob.length();
            if (length <= Integer.MAX_VALUE) {
                stmt.setBinaryStream(i, blob.getBinaryStream(), (int) length);
            } else {
                stmt.setBinaryStream(i, blob.getBinaryStream(), length);
            }
            return;
        }
        if (param instanceof Clob) {
            Clob clob = (Clob) param;
            long length = clob.length();
            if (length <= Integer.MAX_VALUE) {
                stmt.setCharacterStream(i, clob.getCharacterStream(), (int) clob.length());
            } else {
                stmt.setCharacterStream(i, clob.getCharacterStream(), length);
            }
            return;
        }
    }
    // convert these the following to jdbc safe values
    if (TypeFacility.RUNTIME_TYPES.BIG_INTEGER.equals(paramType)) {
        param = new BigDecimal((BigInteger) param);
    } else if (TypeFacility.RUNTIME_TYPES.FLOAT.equals(paramType)) {
        param = new Double(((Float) param).doubleValue());
    } else if (TypeFacility.RUNTIME_TYPES.CHAR.equals(paramType)) {
        param = ((Character) param).toString();
    } else if (paramType.equals(TypeFacility.RUNTIME_TYPES.VARBINARY)) {
        param = ((BinaryType) param).getBytesDirect();
    }
    if (type != Types.JAVA_OBJECT) {
        if (this.removePushdownCharacters != null && param instanceof String) {
            // TODO: this only accounts for strings and not strings embedded in arrays
            // $NON-NLS-1$
            param = this.removePushdownCharacters.matcher((String) param).replaceAll("");
        }
        stmt.setObject(i, param, type);
    } else {
        stmt.setObject(i, param);
    }
}
Also used : GeometryType(org.teiid.core.types.GeometryType) BinaryType(org.teiid.core.types.BinaryType) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal) java.sql(java.sql)

Aggregations

BinaryType (org.teiid.core.types.BinaryType)21 Test (org.junit.Test)12 Blob (java.sql.Blob)6 List (java.util.List)6 IOException (java.io.IOException)4 BigInteger (java.math.BigInteger)4 SQLException (java.sql.SQLException)4 SQLXML (java.sql.SQLXML)4 InputStream (java.io.InputStream)3 BigDecimal (java.math.BigDecimal)3 Clob (java.sql.Clob)3 ArrayList (java.util.ArrayList)3 BlobImpl (org.teiid.core.types.BlobImpl)3 GeometryType (org.teiid.core.types.GeometryType)3 BasicDBList (com.mongodb.BasicDBList)2 BasicDBObject (com.mongodb.BasicDBObject)2 GridFS (com.mongodb.gridfs.GridFS)2 Coordinate (com.vividsolutions.jts.geom.Coordinate)2 GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)2 Point (com.vividsolutions.jts.geom.Point)2