Search in sources :

Example 21 with InputStreamFactory

use of org.teiid.core.types.InputStreamFactory 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 22 with InputStreamFactory

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

the class CouchbaseDirectQueryExecution method next.

@Override
public List<?> next() throws TranslatorException, DataNotAvailableException {
    ArrayList<Object[]> returns = new ArrayList<>(1);
    ArrayList<Object> result = new ArrayList<>(1);
    if (this.results != null && this.results.hasNext()) {
        final N1qlQueryRow row = this.results.next();
        InputStreamFactory isf = new InputStreamFactory() {

            @Override
            public InputStream getInputStream() throws IOException {
                return new ByteArrayInputStream(row.byteValue());
            }
        };
        result.add(new BlobType(new BlobImpl(isf)));
        returns.add(result.toArray());
        return returns;
    } else {
        return null;
    }
}
Also used : N1qlQueryRow(com.couchbase.client.java.query.N1qlQueryRow) BlobType(org.teiid.core.types.BlobType) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) InputStreamFactory(org.teiid.core.types.InputStreamFactory) BlobImpl(org.teiid.core.types.BlobImpl)

Example 23 with InputStreamFactory

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

the class ObjectDecoderInputStream method readObjectOverride.

@Override
protected final Object readObjectOverride() throws IOException, ClassNotFoundException {
    if (result == null) {
        if (!foundLength) {
            clearRemaining();
            remaining = dis.readInt();
            foundLength = true;
            if (remaining <= 0) {
                // $NON-NLS-1$
                throw new StreamCorruptedException("invalid data length: " + remaining);
            }
            if (remaining > maxObjectSize) {
                throw new StreamCorruptedException(JDBCPlugin.Util.gs(JDBCPlugin.Event.TEIID20028, remaining, maxObjectSize));
            }
        }
        foundLength = false;
        CompactObjectInputStream cois = new CompactObjectInputStream(subStream, classLoader);
        result = cois.readObject();
        streams = ExternalizeUtil.readList(cois, StreamFactoryReference.class);
        streamIndex = 0;
    }
    while (streamIndex < streams.size()) {
        if (!foundLength) {
            clearRemaining();
            // convert to unsigned
            remaining = 0xffff & dis.readShort();
            foundLength = true;
            if (remaining < 0) {
                // $NON-NLS-1$
                throw new StreamCorruptedException("Invalid stream chunk length");
            }
        }
        if (stream == null) {
            // $NON-NLS-1$
            final File f = File.createTempFile("teiid", null);
            StreamFactoryReference sfr = streams.get(streamIndex);
            sfr.setStreamFactory(new InputStreamFactory() {

                @Override
                public InputStream getInputStream() throws IOException {
                    return new BufferedInputStream(new FileInputStream(f));
                }

                @Override
                protected void finalize() throws Throwable {
                    super.finalize();
                    f.delete();
                }
            });
            this.stream = new FileOutputStream(f);
        }
        foundLength = false;
        if (remaining != 0) {
            int available = Math.min(remaining, in.getCount() - in.getPosition());
            if (available > 0) {
                this.stream.write(in.getBuffer(), in.getPosition(), available);
                in.setPosition(in.getPosition() + available);
                remaining -= available;
            }
            if (remaining > 0) {
                ObjectConverterUtil.write(this.stream, in, in.getBuffer(), remaining, false);
                remaining = 0;
            }
            continue;
        }
        stream.close();
        stream = null;
        streamIndex++;
    }
    Object toReturn = result;
    result = null;
    streams = null;
    stream = null;
    return toReturn;
}
Also used : AccessibleBufferedInputStream(org.teiid.core.util.AccessibleBufferedInputStream) InputStreamFactory(org.teiid.core.types.InputStreamFactory) AccessibleBufferedInputStream(org.teiid.core.util.AccessibleBufferedInputStream) StreamFactoryReference(org.teiid.core.types.InputStreamFactory.StreamFactoryReference)

Example 24 with InputStreamFactory

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

the class CouchbaseProcedureExecution method next.

@Override
public List<?> next() throws TranslatorException, DataNotAvailableException {
    if (this.results != null && this.results.hasNext()) {
        final N1qlQueryRow row = this.results.next();
        String procName = this.call.getProcedureName();
        if (procName.equalsIgnoreCase(GETDOCUMENTS) || procName.equalsIgnoreCase(GETDOCUMENT)) {
            ArrayList<Object> result = new ArrayList<>(1);
            InputStreamFactory isf = new InputStreamFactory() {

                @Override
                public InputStream getInputStream() throws IOException {
                    return new ByteArrayInputStream(row.byteValue());
                }
            };
            Object value = new BlobType(new BlobImpl(isf));
            result.add(value);
            return result;
        }
    }
    return null;
}
Also used : N1qlQueryRow(com.couchbase.client.java.query.N1qlQueryRow) BlobType(org.teiid.core.types.BlobType) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) InputStreamFactory(org.teiid.core.types.InputStreamFactory) BlobImpl(org.teiid.core.types.BlobImpl)

Example 25 with InputStreamFactory

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

the class ODataSQLBuilder method updateStreamProperty.

public Update updateStreamProperty(EdmProperty edmProperty, final InputStream content) throws TeiidException {
    Update update = new Update();
    update.setGroup(this.context.getGroupSymbol());
    Column column = this.context.getColumnByName(edmProperty.getName());
    ElementSymbol symbol = new ElementSymbol(column.getName(), this.context.getGroupSymbol());
    update.addChange(symbol, new Reference(0));
    Class<?> lobType = DataTypeManager.getDataTypeClass(column.getRuntimeType());
    int sqlType = JDBCSQLTypeInfo.getSQLType(column.getRuntimeType());
    if (content == null) {
        this.params.add(new SQLParameter(null, sqlType));
    } else {
        Object value = null;
        InputStreamFactory isf = new InputStreamFactory() {

            @Override
            public InputStream getInputStream() throws IOException {
                return content;
            }
        };
        if (lobType.isAssignableFrom(SQLXML.class)) {
            value = new SQLXMLImpl(isf);
        } else if (lobType.isAssignableFrom(ClobType.class)) {
            value = new ClobImpl(isf, -1);
        } else if (lobType.isAssignableFrom(BlobType.class)) {
            value = new BlobImpl(isf);
        } else {
            throw new TeiidException(ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16031, column.getName()));
        }
        this.params.add(new SQLParameter(value, sqlType));
    }
    update.setCriteria(this.context.getCriteria());
    return update;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) SQLXMLImpl(org.teiid.core.types.SQLXMLImpl) Reference(org.teiid.query.sql.symbol.Reference) SQLParameter(org.teiid.odata.api.SQLParameter) InputStreamFactory(org.teiid.core.types.InputStreamFactory) SubqueryHint(org.teiid.query.sql.lang.ExistsCriteria.SubqueryHint) TeiidException(org.teiid.core.TeiidException) ClobType(org.teiid.core.types.ClobType) Column(org.teiid.metadata.Column) ClobImpl(org.teiid.core.types.ClobImpl) BlobImpl(org.teiid.core.types.BlobImpl)

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