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