Search in sources :

Example 6 with GeometryType

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

Example 7 with GeometryType

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;
}
Also used : GeometryType(org.teiid.core.types.GeometryType)

Example 8 with GeometryType

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);
    }
}
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 9 with GeometryType

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);
}
Also used : GeometryType(org.teiid.core.types.GeometryType) FakeExecutionContextImpl(org.teiid.dqp.internal.datamgr.FakeExecutionContextImpl) Expression(org.teiid.language.Expression) Connection(java.sql.Connection) Parameter(org.teiid.language.Parameter) PreparedStatement(java.sql.PreparedStatement) Insert(org.teiid.language.Insert) ExpressionValueSource(org.teiid.language.ExpressionValueSource) Test(org.junit.Test)

Example 10 with GeometryType

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;
}
Also used : GeometryType(org.teiid.core.types.GeometryType) Blob(java.sql.Blob) SQLException(java.sql.SQLException) InputStream(java.io.InputStream) IOException(java.io.IOException) InputStreamFactory(org.teiid.core.types.InputStreamFactory) BlobImpl(org.teiid.core.types.BlobImpl)

Aggregations

GeometryType (org.teiid.core.types.GeometryType)17 Test (org.junit.Test)8 Blob (java.sql.Blob)4 BinaryType (org.teiid.core.types.BinaryType)4 BasicDBList (com.mongodb.BasicDBList)3 BasicDBObject (com.mongodb.BasicDBObject)3 IOException (java.io.IOException)3 BigInteger (java.math.BigInteger)3 SQLException (java.sql.SQLException)3 BlobImpl (org.teiid.core.types.BlobImpl)3 ClobImpl (org.teiid.core.types.ClobImpl)3 ClobType (org.teiid.core.types.ClobType)3 InputStreamFactory (org.teiid.core.types.InputStreamFactory)3 BasicDBObjectBuilder (com.mongodb.BasicDBObjectBuilder)2 DBObject (com.mongodb.DBObject)2 InputStream (java.io.InputStream)2 BigDecimal (java.math.BigDecimal)2 PreparedStatement (java.sql.PreparedStatement)2 GridFS (com.mongodb.gridfs.GridFS)1 GridFSInputFile (com.mongodb.gridfs.GridFSInputFile)1