Search in sources :

Example 26 with InputStreamFactory

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

the class TestEmbeddedServer method testSourceLobUnderTxn.

@Test
public void testSourceLobUnderTxn() throws Exception {
    EmbeddedConfiguration ec = new EmbeddedConfiguration();
    ec.setMaxResultSetCacheStaleness(0);
    MockTransactionManager tm = new MockTransactionManager();
    ec.setTransactionManager(tm);
    ec.setUseDisk(false);
    es.start(ec);
    final AtomicBoolean closed = new AtomicBoolean();
    es.addTranslator("foo", new ExecutionFactory() {

        @Override
        public boolean isSourceRequired() {
            return false;
        }

        @Override
        public ResultSetExecution createResultSetExecution(QueryExpression command, ExecutionContext executionContext, RuntimeMetadata metadata, Object connection) throws TranslatorException {
            return new ResultSetExecution() {

                private boolean returned;

                @Override
                public void execute() throws TranslatorException {
                }

                @Override
                public void close() {
                    closed.set(true);
                }

                @Override
                public void cancel() throws TranslatorException {
                }

                @Override
                public List<?> next() throws TranslatorException, DataNotAvailableException {
                    if (returned) {
                        return null;
                    }
                    returned = true;
                    ArrayList<Object> result = new ArrayList<Object>(1);
                    result.add(new SQLXMLImpl(new InputStreamFactory() {

                        @Override
                        public InputStream getInputStream() throws IOException {
                            // need to make it of a sufficient size to not be inlined
                            return new ByteArrayInputStream(new byte[DataTypeManager.MAX_LOB_MEMORY_BYTES + 1]);
                        }
                    }));
                    return result;
                }
            };
        }
    });
    es.deployVDB(new ByteArrayInputStream("<vdb name=\"test\" version=\"1\"><model name=\"test\"><source name=\"foo\" translator-name=\"foo\"/><metadata type=\"DDL\"><![CDATA[CREATE foreign table x (y xml);]]> </metadata></model></vdb>".getBytes()));
    Connection c = es.getDriver().connect("jdbc:teiid:test", null);
    c.setAutoCommit(false);
    Statement s = c.createStatement();
    ResultSet rs = s.executeQuery("select * from x");
    rs.next();
    assertFalse(closed.get());
    s.close();
    assertTrue(closed.get());
}
Also used : SQLXMLImpl(org.teiid.core.types.SQLXMLImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) ExecutionFactory(org.teiid.translator.ExecutionFactory) IOException(java.io.IOException) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) InputStreamFactory(org.teiid.core.types.InputStreamFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ResultSetExecution(org.teiid.translator.ResultSetExecution) ExecutionContext(org.teiid.translator.ExecutionContext) ByteArrayInputStream(java.io.ByteArrayInputStream) TranslatorException(org.teiid.translator.TranslatorException) List(java.util.List) ArrayList(java.util.ArrayList) DataNotAvailableException(org.teiid.translator.DataNotAvailableException) QueryExpression(org.teiid.language.QueryExpression) Test(org.junit.Test)

Example 27 with InputStreamFactory

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

the class TestJDBCSocketTransport method testGeometryStreaming.

@Test
public void testGeometryStreaming() throws Exception {
    StringBuilder geomString = new StringBuilder();
    for (int i = 0; i < 600; i++) {
        geomString.append("100 100,");
    }
    geomString.append("100 100");
    final GeometryType geo = GeometryUtils.geometryFromClob(new ClobType(new ClobImpl("POLYGON ((" + geomString + "))")));
    long length = geo.length();
    PreparedStatement s = conn.prepareStatement("select st_geomfrombinary(?)");
    s.setBlob(1, new BlobImpl(new InputStreamFactory() {

        @Override
        public InputStream getInputStream() throws IOException {
            try {
                return geo.getBinaryStream();
            } catch (SQLException e) {
                throw new IOException(e);
            }
        }
    }));
    ResultSet rs = s.executeQuery();
    rs.next();
    Blob b = rs.getBlob(1);
    assertEquals(length, b.length());
    b.getBytes(1, (int) b.length());
    toggleInline(false);
    rs = s.executeQuery();
    rs.next();
    b = rs.getBlob(1);
    assertEquals(length, b.length());
    b.getBytes(1, (int) b.length());
}
Also used : Blob(java.sql.Blob) TeiidSQLException(org.teiid.jdbc.TeiidSQLException) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) InputStreamFactory(org.teiid.core.types.InputStreamFactory) ClobType(org.teiid.core.types.ClobType) GeometryType(org.teiid.core.types.GeometryType) ResultSet(java.sql.ResultSet) ClobImpl(org.teiid.core.types.ClobImpl) BlobImpl(org.teiid.core.types.BlobImpl) Test(org.junit.Test)

Aggregations

InputStreamFactory (org.teiid.core.types.InputStreamFactory)27 ByteArrayInputStream (java.io.ByteArrayInputStream)13 IOException (java.io.IOException)12 BlobImpl (org.teiid.core.types.BlobImpl)12 InputStream (java.io.InputStream)11 ClobImpl (org.teiid.core.types.ClobImpl)11 Test (org.junit.Test)10 BlobType (org.teiid.core.types.BlobType)8 SQLException (java.sql.SQLException)7 SQLXMLImpl (org.teiid.core.types.SQLXMLImpl)7 ClobType (org.teiid.core.types.ClobType)6 Blob (java.sql.Blob)5 ArrayList (java.util.ArrayList)5 FileStoreInputStreamFactory (org.teiid.common.buffer.FileStoreInputStreamFactory)4 StringReader (java.io.StringReader)3 BinaryType (org.teiid.core.types.BinaryType)3 ReaderInputStream (org.teiid.core.util.ReaderInputStream)3 N1qlQueryRow (com.couchbase.client.java.query.N1qlQueryRow)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 BigInteger (java.math.BigInteger)2