Search in sources :

Example 41 with Blob

use of java.sql.Blob in project teiid by teiid.

the class S3ProcedureExecution method next.

@Override
public List<?> next() throws TranslatorException, DataNotAvailableException {
    if (this.command.getProcedureName().equalsIgnoreCase(S3ExecutionFactory.SAVEFILE) || this.command.getProcedureName().equalsIgnoreCase(S3ExecutionFactory.DELETEFILE)) {
        return null;
    }
    if (this.execution == null || this.execution.getResponseCode() < 200 || this.execution.getResponseCode() > 300) {
        return null;
    }
    Blob contents = (Blob) execution.getOutputParameterValues().get(0);
    BlobInputStreamFactory isf = new BlobInputStreamFactory(contents);
    String length = getHeader("Content-Length");
    if (length != null) {
        isf.setLength(Long.parseLong(length));
    }
    Object value = null;
    if (isText) {
        ClobImpl clob = new ClobImpl(isf, -1);
        clob.setCharset(Charset.forName(this.ef.getEncoding()));
        value = new ClobType(clob);
        if (!streaming) {
            value = new InputStreamFactory.ClobInputStreamFactory(clob);
        }
    } else {
        if (streaming) {
            value = new BlobType(contents);
        } else {
            value = isf;
        }
    }
    String lastModified = getHeader("Last-Modified");
    ArrayList<Object> result = new ArrayList<Object>(2);
    result.add(value);
    if (!isList) {
        result.add(endpoint);
        try {
            SimpleDateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss zzz");
            result.add(lastModified == null ? null : new Timestamp(df.parse(lastModified).getTime()));
        } catch (ParseException e) {
            result.add(null);
        }
        result.add(getHeader("ETag"));
        result.add(length);
    }
    this.execution = null;
    return result;
}
Also used : Blob(java.sql.Blob) ArrayList(java.util.ArrayList) InputStreamFactory(org.teiid.core.types.InputStreamFactory) BlobInputStreamFactory(org.teiid.core.types.InputStreamFactory.BlobInputStreamFactory) Timestamp(java.sql.Timestamp) BlobInputStreamFactory(org.teiid.core.types.InputStreamFactory.BlobInputStreamFactory) ClobType(org.teiid.core.types.ClobType) BlobType(org.teiid.core.types.BlobType) ParseException(java.text.ParseException) ClobImpl(org.teiid.core.types.ClobImpl) SimpleDateFormat(java.text.SimpleDateFormat)

Example 42 with Blob

use of java.sql.Blob in project teiid by teiid.

the class Evaluator method evaluateParameter.

private Object evaluateParameter(List<?> tuple, DerivedColumn passing) throws ExpressionEvaluationException, BlockedException, TeiidComponentException {
    if (passing.getExpression() instanceof Function) {
        Function f = (Function) passing.getExpression();
        // narrow optimization of json based documents to allow for lower overhead streaming
        if (f.getName().equalsIgnoreCase(SourceSystemFunctions.JSONTOXML)) {
            String rootName = (String) this.evaluate(f.getArg(0), tuple);
            Object lob = this.evaluate(f.getArg(1), tuple);
            if (rootName == null || lob == null) {
                return null;
            }
            try {
                if (lob instanceof Blob) {
                    return XMLSystemFunctions.jsonToXml(context, rootName, (Blob) lob, true);
                }
                return XMLSystemFunctions.jsonToXml(context, rootName, (Clob) lob, true);
            } catch (IOException e) {
                throw new FunctionExecutionException(QueryPlugin.Event.TEIID30384, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30384, f.getFunctionDescriptor().getName()));
            } catch (SQLException e) {
                throw new FunctionExecutionException(QueryPlugin.Event.TEIID30384, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30384, f.getFunctionDescriptor().getName()));
            } catch (TeiidProcessingException e) {
                throw new FunctionExecutionException(QueryPlugin.Event.TEIID30384, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30384, f.getFunctionDescriptor().getName()));
            }
        }
    } else if (passing.getExpression() instanceof XMLParse) {
        XMLParse xmlParse = (XMLParse) passing.getExpression();
        xmlParse.setWellFormed(true);
    }
    Object value = this.evaluate(passing.getExpression(), tuple);
    return value;
}
Also used : Blob(java.sql.Blob) FunctionExecutionException(org.teiid.api.exception.query.FunctionExecutionException) TeiidSQLException(org.teiid.jdbc.TeiidSQLException) SQLException(java.sql.SQLException) LanguageObject(org.teiid.query.sql.LanguageObject) IOException(java.io.IOException) TeiidProcessingException(org.teiid.core.TeiidProcessingException)

Example 43 with Blob

use of java.sql.Blob in project teiid by teiid.

the class GeometryUtils method geometryToEwkb.

/**
 * We'll take the wkb format and add the extended flag/srid
 * @param geometry
 * @return
 */
public static BlobType geometryToEwkb(final GeometryType geometry) {
    final Blob b = geometry.getReference();
    BlobImpl blobImpl = new BlobImpl(new InputStreamFactory() {

        @Override
        public InputStream getInputStream() throws IOException {
            PushbackInputStream pbis;
            try {
                pbis = new PushbackInputStream(b.getBinaryStream(), 9);
            } catch (SQLException e) {
                throw new IOException(e);
            }
            int byteOrder = pbis.read();
            if (byteOrder == -1) {
                return pbis;
            }
            byte[] typeInt = new byte[4];
            int bytesRead = pbis.read(typeInt);
            if (bytesRead == 4) {
                int srid = geometry.getSrid();
                byte[] sridInt = new byte[4];
                ByteOrderValues.putInt(srid, sridInt, byteOrder == 0 ? ByteOrderValues.BIG_ENDIAN : ByteOrderValues.LITTLE_ENDIAN);
                pbis.unread(sridInt);
                typeInt[byteOrder == 0 ? 0 : 3] |= 0x20;
            }
            pbis.unread(typeInt, 0, bytesRead);
            pbis.unread(byteOrder);
            return pbis;
        }
    });
    return new BlobType(blobImpl);
}
Also used : Blob(java.sql.Blob) BlobType(org.teiid.core.types.BlobType) PushbackInputStream(java.io.PushbackInputStream) SQLException(java.sql.SQLException) PushbackInputStream(java.io.PushbackInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) InputStreamFactory(org.teiid.core.types.InputStreamFactory) BlobImpl(org.teiid.core.types.BlobImpl)

Example 44 with Blob

use of java.sql.Blob in project teiid by teiid.

the class StringAgg method writeValue.

private void writeValue(Object val) throws TeiidProcessingException {
    try {
        if (binary) {
            if (val instanceof BinaryType) {
                result.getOuputStream().write(((BinaryType) val).getBytesDirect());
                return;
            }
            Blob b = (Blob) val;
            InputStream binaryStream = b.getBinaryStream();
            try {
                ObjectConverterUtil.write(result.getOuputStream(), binaryStream, -1, false);
            } finally {
                binaryStream.close();
            }
        } else {
            if (val instanceof String) {
                result.getWriter().write((String) val);
                return;
            }
            Clob c = (Clob) val;
            Reader characterStream = c.getCharacterStream();
            try {
                ObjectConverterUtil.write(result.getWriter(), characterStream, -1, false);
            } finally {
                characterStream.close();
            }
        }
    } catch (IOException e) {
        throw new TeiidProcessingException(QueryPlugin.Event.TEIID30422, e);
    } catch (SQLException e) {
        throw new TeiidProcessingException(QueryPlugin.Event.TEIID30423, e);
    }
}
Also used : SerialBlob(javax.sql.rowset.serial.SerialBlob) Blob(java.sql.Blob) BinaryType(org.teiid.core.types.BinaryType) SQLException(java.sql.SQLException) InputStream(java.io.InputStream) Reader(java.io.Reader) IOException(java.io.IOException) Clob(java.sql.Clob) TeiidProcessingException(org.teiid.core.TeiidProcessingException)

Example 45 with Blob

use of java.sql.Blob in project teiid by teiid.

the class EntityCollectionResponse method getPropertyValueInternal.

private static Object getPropertyValueInternal(SingletonPrimitiveType expectedType, boolean isArray, Object value) throws TransformationException, SQLException, IOException {
    Class<?> sourceType = DataTypeManager.getRuntimeType(value.getClass());
    if (sourceType.isAssignableFrom(expectedType.getDefaultType())) {
        return value;
    }
    if (expectedType instanceof EdmDate && sourceType == Date.class) {
        return value;
    } else if (expectedType instanceof EdmDateTimeOffset && sourceType == Timestamp.class) {
        return value;
    } else if (expectedType instanceof EdmTimeOfDay && sourceType == Time.class) {
        return value;
    } else if (expectedType instanceof EdmBinary) {
        // there could be memory implications here, should have been modeled as EdmStream
        // $NON-NLS-1$
        LogManager.logDetail(LogConstants.CTX_ODATA, "Possible OOM when inlining the stream based values");
        if (sourceType == ClobType.class) {
            return ClobType.getString((Clob) value).getBytes();
        }
        if (sourceType == SQLXML.class) {
            return ((SQLXML) value).getString().getBytes();
        }
        if (sourceType == BlobType.class) {
            return ObjectConverterUtil.convertToByteArray(((Blob) value).getBinaryStream());
        }
        if (value instanceof Serializable) {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(value);
            oos.close();
            bos.close();
            return bos.toByteArray();
        }
    }
    Class<?> targetType = DataTypeManager.getDataTypeClass(ODataTypeManager.teiidType(expectedType, isArray));
    if (sourceType != targetType) {
        Transform t = DataTypeManager.getTransform(sourceType, targetType);
        value = t != null ? t.transform(value, targetType) : value;
    }
    return value;
}
Also used : Blob(java.sql.Blob) Serializable(java.io.Serializable) EdmBinary(org.apache.olingo.commons.core.edm.primitivetype.EdmBinary) EdmDateTimeOffset(org.apache.olingo.commons.core.edm.primitivetype.EdmDateTimeOffset) Time(java.sql.Time) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) EdmDate(org.apache.olingo.commons.core.edm.primitivetype.EdmDate) EdmDate(org.apache.olingo.commons.core.edm.primitivetype.EdmDate) Date(java.sql.Date) ClobType(org.teiid.core.types.ClobType) SQLXML(java.sql.SQLXML) BlobType(org.teiid.core.types.BlobType) Transform(org.teiid.core.types.Transform) EdmTimeOfDay(org.apache.olingo.commons.core.edm.primitivetype.EdmTimeOfDay)

Aggregations

Blob (java.sql.Blob)337 ResultSet (java.sql.ResultSet)130 SQLException (java.sql.SQLException)109 PreparedStatement (java.sql.PreparedStatement)106 InputStream (java.io.InputStream)97 Clob (java.sql.Clob)81 ByteArrayInputStream (java.io.ByteArrayInputStream)59 IOException (java.io.IOException)54 Statement (java.sql.Statement)52 Connection (java.sql.Connection)36 Test (org.junit.Test)34 BigDecimal (java.math.BigDecimal)25 Timestamp (java.sql.Timestamp)25 SQLXML (java.sql.SQLXML)22 OutputStream (java.io.OutputStream)21 NClob (java.sql.NClob)21 Reader (java.io.Reader)19 Date (java.sql.Date)18 ArrayList (java.util.ArrayList)17 List (java.util.List)17