use of org.teiid.core.types.GeometryType 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);
}
}
use of org.teiid.core.types.GeometryType in project teiid by teiid.
the class JDBCExecutionFactory method retrieveGeometryValue.
public Object retrieveGeometryValue(ResultSet results, int paramIndex) throws SQLException {
GeometryType geom = null;
Blob val = results.getBlob(paramIndex);
if (val != null) {
geom = new GeometryType(val);
}
return geom;
}
use of org.teiid.core.types.GeometryType 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.GeometryType in project teiid by teiid.
the class TestJDBCUpdateExecution method testPreparedInsertWithGeometry.
@Test
public void testPreparedInsertWithGeometry() throws Exception {
// $NON-NLS-1$
Insert command = (Insert) TranslationHelper.helpTranslate(TranslationHelper.BQT_VDB, "insert into cola_markets(name,shape) values('foo124', ST_GeomFromText('POINT (300 100)', 8307))");
Parameter param = new Parameter();
param.setType(DataTypeManager.DefaultDataClasses.STRING);
param.setValueIndex(0);
List<Expression> values = ((ExpressionValueSource) command.getValueSource()).getValues();
values.set(0, param);
param = new Parameter();
param.setType(DataTypeManager.DefaultDataClasses.GEOMETRY);
param.setValueIndex(1);
values.set(1, param);
GeometryType value = new GeometryType();
value.setSrid(123);
command.setParameterValues(Arrays.asList(Arrays.asList("a", value)).iterator());
Connection connection = Mockito.mock(Connection.class);
PreparedStatement p = Mockito.mock(PreparedStatement.class);
Mockito.stub(p.executeBatch()).toReturn(new int[] { 1 });
// $NON-NLS-1$
Mockito.stub(connection.prepareStatement("INSERT INTO COLA_MARKETS (NAME, SHAPE) VALUES (?, st_geomfromwkb(?, ?))")).toReturn(p);
JDBCExecutionFactory config = new JDBCExecutionFactory();
JDBCUpdateExecution updateExecution = new JDBCUpdateExecution(command, connection, new FakeExecutionContextImpl(), config);
updateExecution.execute();
Mockito.verify(p, Mockito.times(1)).addBatch();
Mockito.verify(p, Mockito.times(1)).setObject(1, "a", Types.VARCHAR);
Mockito.verify(p, Mockito.times(1)).setInt(3, 123);
}
use of org.teiid.core.types.GeometryType in project teiid by teiid.
the class MySQLExecutionFactory method toGeometryType.
/**
* It appears that mysql will actually return a byte array or a blob backed by a byte array
* but just to be safe we'll assume that there may be true blob and that we should back the
* geometry value with that blob.
* @param val
* @return
* @throws SQLException
*/
GeometryType toGeometryType(final Blob val) throws SQLException {
if (val == null) {
return null;
}
// create a wrapper for that will handle the srid
long length = val.length() - 4;
InputStreamFactory streamFactory = new InputStreamFactory() {
@Override
public InputStream getInputStream() throws IOException {
InputStream is;
try {
is = val.getBinaryStream();
} catch (SQLException e) {
throw new IOException(e);
}
for (int i = 0; i < 4; i++) {
is.read();
}
return is;
}
};
// read the little endian srid
InputStream is = val.getBinaryStream();
int srid = 0;
try {
for (int i = 0; i < 4; i++) {
try {
int b = is.read();
srid += (b << i * 8);
} catch (IOException e) {
// could not determine srid
srid = GeometryType.UNKNOWN_SRID;
}
}
} finally {
try {
is.close();
} catch (IOException e) {
// i
}
}
streamFactory.setLength(length);
Blob b = new BlobImpl(streamFactory);
GeometryType geom = new GeometryType(b);
geom.setSrid(srid);
return geom;
}
Aggregations