Search in sources :

Example 16 with BinaryType

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

the class TestPreparedStatementBatchedUpdate method testBulkBytePushdown.

@Test
public void testBulkBytePushdown() throws Exception {
    // $NON-NLS-1$
    String preparedSql = "insert into g1 (e1) values (?)";
    TransformationMetadata tm = RealMetadataFactory.fromDDL("create foreign table g1 (e1 varbinary) options (updatable true)", "y", "z");
    // Create a testable prepared plan cache
    SessionAwareCache<PreparedPlan> prepPlanCache = new SessionAwareCache<PreparedPlan>("preparedplan", DefaultCacheFactory.INSTANCE, SessionAwareCache.Type.PREPAREDPLAN, 0);
    // Construct data manager with data
    HardcodedDataManager dataManager = new HardcodedDataManager();
    // $NON-NLS-1$
    dataManager.addData("INSERT INTO g1 (e1) VALUES (?)", new List[] { Arrays.asList(1), Arrays.asList(1) });
    // Source capabilities must support batched updates
    FakeCapabilitiesFinder capFinder = new FakeCapabilitiesFinder();
    BasicSourceCapabilities caps = TestOptimizer.getTypicalCapabilities();
    caps.setCapabilitySupport(Capability.BULK_UPDATE, true);
    // $NON-NLS-1$
    capFinder.addCapabilities("z", caps);
    ArrayList<List<?>> values = new ArrayList<List<?>>(2);
    // $NON-NLS-1$
    values.add(Arrays.asList(new byte[1]));
    // $NON-NLS-1$
    values.add(Arrays.asList(new byte[1]));
    List<?>[] expected = new List[] { Arrays.asList(1), Arrays.asList(1) };
    // Create the plan and process the query
    TestPreparedStatement.helpTestProcessing(preparedSql, values, expected, dataManager, capFinder, tm, prepPlanCache, false, false, false, tm.getVdbMetaData());
    Insert insert = (Insert) dataManager.getCommandHistory().iterator().next();
    Constant c = (Constant) insert.getValues().get(0);
    assertTrue(c.isMultiValued());
    assertTrue(((List<?>) c.getValue()).get(0) instanceof BinaryType);
}
Also used : FakeCapabilitiesFinder(org.teiid.query.optimizer.capabilities.FakeCapabilitiesFinder) TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) BinaryType(org.teiid.core.types.BinaryType) HardcodedDataManager(org.teiid.query.processor.HardcodedDataManager) BasicSourceCapabilities(org.teiid.query.optimizer.capabilities.BasicSourceCapabilities) Constant(org.teiid.query.sql.symbol.Constant) ArrayList(java.util.ArrayList) Insert(org.teiid.query.sql.lang.Insert) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 17 with BinaryType

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

the class ResultSetImpl method getObjectDirect.

/**
 * Get the value of the current row at the column index specified.
 * @param column Column index
 * @return Value at column, which may be null
 * @throws SQLException if this result set has an exception
 */
public Object getObjectDirect(int column) throws SQLException {
    checkClosed();
    if (column < 1 || column > columnCount) {
        // $NON-NLS-1$
        throw new IllegalArgumentException(JDBCPlugin.Util.getString("ResultsImpl.Invalid_col_index", column));
    }
    List<?> cursorRow = batchResults.getCurrentRow();
    if (cursorRow == null) {
        // $NON-NLS-1$
        throw new TeiidSQLException(JDBCPlugin.Util.getString("ResultsImpl.The_cursor_is_not_on_a_valid_row._1"));
    }
    // defect 13539 - set the currentValue (defined in MMResultSet) so that wasNull() accurately returns whether this value was null
    currentValue = cursorRow.get(column - 1);
    if (currentValue instanceof Streamable<?>) {
        Object reference = ((Streamable<?>) currentValue).getReference();
        if (reference != null) {
            return reference;
        }
        if (currentValue instanceof ClobType) {
            return new ClobImpl(createInputStreamFactory((ClobType) currentValue), ((ClobType) currentValue).getLength());
        } else if (currentValue instanceof BlobType) {
            InputStreamFactory isf = createInputStreamFactory((BlobType) currentValue);
            isf.setLength(((BlobType) currentValue).getLength());
            return new BlobImpl(isf);
        } else if (currentValue instanceof XMLType) {
            XMLType val = (XMLType) currentValue;
            SQLXMLImpl impl = new SQLXMLImpl(createInputStreamFactory(val));
            impl.setEncoding(val.getEncoding());
            return impl;
        }
    } else if (currentValue instanceof java.util.Date) {
        return TimestampWithTimezone.create((java.util.Date) currentValue, serverTimeZone, getDefaultCalendar(), currentValue.getClass());
    } else if (maxFieldSize > 0 && currentValue instanceof String) {
        String val = (String) currentValue;
        return val.substring(0, Math.min(maxFieldSize / 2, val.length()));
    } else if (currentValue instanceof BinaryType) {
        BinaryType val = (BinaryType) currentValue;
        return val.getBytesDirect();
    }
    return currentValue;
}
Also used : SQLXMLImpl(org.teiid.core.types.SQLXMLImpl) BinaryType(org.teiid.core.types.BinaryType) InputStreamFactory(org.teiid.core.types.InputStreamFactory) ClobType(org.teiid.core.types.ClobType) XMLType(org.teiid.core.types.XMLType) BlobType(org.teiid.core.types.BlobType) Streamable(org.teiid.core.types.Streamable) ClobImpl(org.teiid.core.types.ClobImpl) BlobImpl(org.teiid.core.types.BlobImpl)

Example 18 with BinaryType

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

the class CassandraUpdateExecution method internalExecute.

private void internalExecute() throws TranslatorException {
    if (this.command instanceof BatchedUpdates) {
        handleBatchedUpdates();
        return;
    }
    CassandraSQLVisitor visitor = new CassandraSQLVisitor();
    visitor.translateSQL(this.command);
    String cql = visitor.getTranslatedSQL();
    // $NON-NLS-1$
    LogManager.logDetail(LogConstants.CTX_CONNECTOR, "Source-Query:", cql);
    this.executionContext.logCommand(cql);
    if (this.command instanceof BulkCommand) {
        BulkCommand bc = (BulkCommand) this.command;
        if (bc.getParameterValues() != null) {
            int count = 0;
            List<Object[]> newValues = new ArrayList<Object[]>();
            Iterator<? extends List<?>> values = bc.getParameterValues();
            while (values.hasNext()) {
                Object[] bindValues = values.next().toArray();
                for (int i = 0; i < bindValues.length; i++) {
                    if (bindValues[i] instanceof Blob) {
                        Blob blob = (Blob) bindValues[i];
                        try {
                            if (blob.length() > Integer.MAX_VALUE) {
                                // $NON-NLS-1$
                                throw new AssertionError("Blob is too large");
                            }
                            byte[] bytes = ((Blob) bindValues[i]).getBytes(0, (int) blob.length());
                            bindValues[i] = ByteBuffer.wrap(bytes);
                        } catch (SQLException e) {
                            throw new TranslatorException(e);
                        }
                    } else if (bindValues[i] instanceof BinaryType) {
                        bindValues[i] = ByteBuffer.wrap(((BinaryType) bindValues[i]).getBytesDirect());
                    }
                }
                newValues.add(bindValues);
                count++;
            }
            updateCount = count;
            resultSetFuture = connection.executeBatch(cql, newValues);
            return;
        }
    }
    resultSetFuture = connection.executeQuery(cql);
}
Also used : Blob(java.sql.Blob) BinaryType(org.teiid.core.types.BinaryType) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) BulkCommand(org.teiid.language.BulkCommand) TranslatorException(org.teiid.translator.TranslatorException) BatchedUpdates(org.teiid.language.BatchedUpdates)

Example 19 with BinaryType

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

the class TestGeometry method testRoundTrip.

@Test
public void testRoundTrip() throws Exception {
    Expression ex = TestFunctionResolving.getExpression("ST_GeomFromText('POLYGON ((40 0, 50 50, 0 50, 0 0, 40 0))')");
    GeometryType geom = (GeometryType) Evaluator.evaluate(ex);
    assertEquals(0, geom.getSrid());
    byte[] bytes = geom.getBytes(1, (int) geom.length());
    Expression ex1 = TestFunctionResolving.getExpression("ST_GeomFromBinary(X'" + new BinaryType(bytes) + "', 8307)");
    GeometryType geom1 = (GeometryType) Evaluator.evaluate(ex1);
    assertEquals(8307, geom1.getSrid());
    assertEquals(geom, geom1);
}
Also used : GeometryType(org.teiid.core.types.GeometryType) BinaryType(org.teiid.core.types.BinaryType) Expression(org.teiid.query.sql.symbol.Expression) Test(org.junit.Test)

Example 20 with BinaryType

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

the class TestGeometry method testEwkbZCooridinate.

@Test(expected = ExpressionEvaluationException.class)
public void testEwkbZCooridinate() throws Exception {
    WKBWriter writer = new WKBWriter(3, true);
    GeometryFactory gf = new GeometryFactory();
    Point point = gf.createPoint(new Coordinate(0, 0, 0));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    writer.write(point, new OutputStreamOutStream(baos));
    Expression ex1 = TestFunctionResolving.getExpression("ST_GeomFromBinary(X'" + new BinaryType(baos.toByteArray()) + "', 8307)");
    Evaluator.evaluate(ex1);
}
Also used : WKBWriter(com.vividsolutions.jts.io.WKBWriter) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) BinaryType(org.teiid.core.types.BinaryType) Coordinate(com.vividsolutions.jts.geom.Coordinate) Expression(org.teiid.query.sql.symbol.Expression) Point(com.vividsolutions.jts.geom.Point) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStreamOutStream(com.vividsolutions.jts.io.OutputStreamOutStream) Test(org.junit.Test)

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