Search in sources :

Example 6 with BinaryType

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

the class TestSQLXMLProcessing method testXmlSerializeBinary.

@Test
public void testXmlSerializeBinary() throws Exception {
    // $NON-NLS-1$
    String sql = "SELECT xmlserialize(document xmlelement(parent) as varbinary version '1.2' including xmldeclaration)";
    List<?>[] expected = new List<?>[] { Arrays.asList(new BinaryType("<?xml version=\"1.2\" encoding=\"UTF-8\"?><parent></parent>".getBytes(Charset.forName("UTF-8")))) };
    process(sql, expected);
}
Also used : BinaryType(org.teiid.core.types.BinaryType) List(java.util.List) Test(org.junit.Test)

Example 7 with BinaryType

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

the class TestSQLXMLProcessing method testXmlSerializeBinary1.

@Test
public void testXmlSerializeBinary1() throws Exception {
    // $NON-NLS-1$
    String sql = "SELECT xmlserialize(document xmlelement(parent) as varbinary encoding \"UTF-16\" version '1.2' including xmldeclaration)";
    List<?>[] expected = new List<?>[] { Arrays.asList(new BinaryType("<?xml version=\"1.2\" encoding=\"UTF-16\"?><parent></parent>".getBytes(Charset.forName("UTF-16")))) };
    process(sql, expected);
}
Also used : BinaryType(org.teiid.core.types.BinaryType) List(java.util.List) Test(org.junit.Test)

Example 8 with BinaryType

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

the class TestSQLXMLProcessing method testXmlTableBinary.

@Test
public void testXmlTableBinary() throws Exception {
    // $NON-NLS-1$
    String sql = "select * from xmltable('/a/b' passing convert('<a xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><b xsi:type=\"xs:hexBinary\">0FAB</b><b>1F1C</b></a>', xml) columns val varbinary path '/.') as x";
    List<?>[] expected = new List<?>[] { Arrays.asList(new BinaryType(new byte[] { 0xf, (byte) 0xab })), Arrays.asList(new BinaryType(new byte[] { 0x1F, 0x1C })) };
    process(sql, expected);
}
Also used : BinaryType(org.teiid.core.types.BinaryType) List(java.util.List) Test(org.junit.Test)

Example 9 with BinaryType

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

the class MongoDBExecutionFactory method convertToMongoType.

/**
 * Mongodb only supports certain data types, Teiid need to serialize them in other compatible
 * formats, and convert them back while reading them.
 * @param value
 * @return
 */
public Object convertToMongoType(Object value, DB mongoDB, String fqn) throws TranslatorException {
    if (value == null) {
        return null;
    }
    try {
        if (value instanceof BigDecimal) {
            return ((BigDecimal) value).doubleValue();
        } else if (value instanceof BigInteger) {
            return ((BigInteger) value).doubleValue();
        } else if (value instanceof Character) {
            return ((Character) value).toString();
        } else if (value instanceof java.sql.Date) {
            return new java.util.Date(((java.sql.Date) value).getTime());
        } else if (value instanceof java.sql.Time) {
            return new java.util.Date(((java.sql.Time) value).getTime());
        } else if (value instanceof java.sql.Timestamp) {
            return new java.util.Date(((java.sql.Timestamp) value).getTime());
        } else if (value instanceof BinaryType) {
            return new Binary(((BinaryType) value).getBytes());
        } else if (value instanceof byte[]) {
            return new Binary((byte[]) value);
        } else if (!(value instanceof GeometryType) && value instanceof Blob) {
            String uuid = UUID.randomUUID().toString();
            GridFS gfs = new GridFS(mongoDB, fqn);
            GridFSInputFile gfsFile = gfs.createFile(((Blob) value).getBinaryStream());
            gfsFile.setFilename(uuid);
            gfsFile.save();
            return uuid;
        } else if (value instanceof Clob) {
            String uuid = UUID.randomUUID().toString();
            GridFS gfs = new GridFS(mongoDB, fqn);
            GridFSInputFile gfsFile = gfs.createFile(((Clob) value).getAsciiStream());
            gfsFile.setFilename(uuid);
            gfsFile.save();
            return uuid;
        } else if (value instanceof SQLXML) {
            String uuid = UUID.randomUUID().toString();
            GridFS gfs = new GridFS(mongoDB, fqn);
            GridFSInputFile gfsFile = gfs.createFile(((SQLXML) value).getBinaryStream());
            gfsFile.setFilename(uuid);
            gfsFile.save();
            return uuid;
        } else if (value instanceof Object[]) {
            BasicDBList list = new BasicDBList();
            for (Object obj : (Object[]) value) {
                list.add(obj);
            }
            return list;
        }
        return value;
    } catch (SQLException e) {
        throw new TranslatorException(e);
    }
}
Also used : SQLException(java.sql.SQLException) GeometryType(org.teiid.core.types.GeometryType) Blob(java.sql.Blob) BinaryType(org.teiid.core.types.BinaryType) GridFS(com.mongodb.gridfs.GridFS) BigDecimal(java.math.BigDecimal) GridFSInputFile(com.mongodb.gridfs.GridFSInputFile) BasicDBList(com.mongodb.BasicDBList) SQLXML(java.sql.SQLXML) BigInteger(java.math.BigInteger) BasicDBObject(com.mongodb.BasicDBObject) Binary(org.bson.types.Binary) Clob(java.sql.Clob)

Example 10 with BinaryType

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

the class MongoDBExecutionFactory method retrieveValue.

/**
 * @param field
 * @param expectedClass
 * @return
 * @throws TranslatorException
 */
public Object retrieveValue(Object value, Class<?> expectedClass, DB mongoDB, String fqn, String colName) throws TranslatorException {
    if (value == null) {
        return null;
    }
    if (value.getClass().equals(expectedClass)) {
        return value;
    }
    if (value instanceof DBRef) {
        Object obj = ((DBRef) value).getId();
        if (obj instanceof BasicDBObject) {
            BasicDBObject bdb = (BasicDBObject) obj;
            return bdb.get(colName);
        }
        return obj;
    } else if (value instanceof java.util.Date && expectedClass.equals(java.sql.Date.class)) {
        return new java.sql.Date(((java.util.Date) value).getTime());
    } else if (value instanceof java.util.Date && expectedClass.equals(java.sql.Timestamp.class)) {
        return new java.sql.Timestamp(((java.util.Date) value).getTime());
    } else if (value instanceof java.util.Date && expectedClass.equals(java.sql.Time.class)) {
        return new java.sql.Time(((java.util.Date) value).getTime());
    } else if (value instanceof String && expectedClass.equals(BigDecimal.class)) {
        return new BigDecimal((String) value);
    } else if (value instanceof String && expectedClass.equals(BigInteger.class)) {
        return new BigInteger((String) value);
    } else if (value instanceof String && expectedClass.equals(Character.class)) {
        return new Character(((String) value).charAt(0));
    } else if (value instanceof String && expectedClass.equals(BinaryType.class)) {
        return new BinaryType(((String) value).getBytes());
    } else if (value instanceof String && expectedClass.equals(Blob.class)) {
        GridFS gfs = new GridFS(mongoDB, fqn);
        final GridFSDBFile resource = gfs.findOne((String) value);
        if (resource == null) {
            return null;
        }
        return new BlobImpl(new InputStreamFactory() {

            @Override
            public InputStream getInputStream() throws IOException {
                return resource.getInputStream();
            }
        });
    } else if (value instanceof String && expectedClass.equals(Clob.class)) {
        GridFS gfs = new GridFS(mongoDB, fqn);
        final GridFSDBFile resource = gfs.findOne((String) value);
        if (resource == null) {
            return null;
        }
        return new ClobImpl(new InputStreamFactory() {

            @Override
            public InputStream getInputStream() throws IOException {
                return resource.getInputStream();
            }
        }, -1);
    } else if (value instanceof String && expectedClass.equals(SQLXML.class)) {
        GridFS gfs = new GridFS(mongoDB, fqn);
        final GridFSDBFile resource = gfs.findOne((String) value);
        if (resource == null) {
            return null;
        }
        return new SQLXMLImpl(new InputStreamFactory() {

            @Override
            public InputStream getInputStream() throws IOException {
                return resource.getInputStream();
            }
        });
    } else if (value instanceof BasicDBList) {
        BasicDBList arrayValues = (BasicDBList) value;
        // array
        if (expectedClass.isArray() && !(arrayValues.get(0) instanceof BasicDBObject)) {
            Class arrayType = expectedClass.getComponentType();
            Object array = Array.newInstance(arrayType, arrayValues.size());
            for (int i = 0; i < arrayValues.size(); i++) {
                Object arrayItem = retrieveValue(arrayValues.get(i), arrayType, mongoDB, fqn, colName);
                Array.set(array, i, arrayItem);
            }
            value = array;
        }
    } else if (value instanceof org.bson.types.ObjectId) {
        org.bson.types.ObjectId id = (org.bson.types.ObjectId) value;
        value = id.toHexString();
    } else {
        Transform transform = DataTypeManager.getTransform(value.getClass(), expectedClass);
        if (transform != null) {
            try {
                value = transform.transform(value, expectedClass);
            } catch (TransformationException e) {
                throw new TranslatorException(e);
            }
        }
    }
    return value;
}
Also used : InputStreamFactory(org.teiid.core.types.InputStreamFactory) BasicDBObject(com.mongodb.BasicDBObject) BlobImpl(org.teiid.core.types.BlobImpl) ClobImpl(org.teiid.core.types.ClobImpl) Blob(java.sql.Blob) SQLXMLImpl(org.teiid.core.types.SQLXMLImpl) TransformationException(org.teiid.core.types.TransformationException) BinaryType(org.teiid.core.types.BinaryType) InputStream(java.io.InputStream) DBRef(com.mongodb.DBRef) IOException(java.io.IOException) GridFS(com.mongodb.gridfs.GridFS) BigDecimal(java.math.BigDecimal) BasicDBList(com.mongodb.BasicDBList) SQLXML(java.sql.SQLXML) GridFSDBFile(com.mongodb.gridfs.GridFSDBFile) BigInteger(java.math.BigInteger) BasicDBObject(com.mongodb.BasicDBObject) Transform(org.teiid.core.types.Transform)

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