Search in sources :

Example 16 with BlobType

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

the class TestFunctionLibrary method testToChars1.

@Test
public void testToChars1() throws Exception {
    // $NON-NLS-1$
    Clob result = (Clob) helpInvokeMethod("to_chars", new Class<?>[] { DefaultDataClasses.BLOB, DefaultDataClasses.STRING }, new Object[] { new BlobType(new SerialBlob("hello world".getBytes("ASCII"))), "BASE64" }, null);
    String string = result.getSubString(1, (int) result.length());
    assertEquals("hello world", new String(Base64.decode(string), "ASCII"));
}
Also used : BlobType(org.teiid.core.types.BlobType) SerialBlob(javax.sql.rowset.serial.SerialBlob) Clob(java.sql.Clob) SerialClob(javax.sql.rowset.serial.SerialClob) Test(org.junit.Test)

Example 17 with BlobType

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

the class TestFunctionLibrary method testCastWithNonRuntimeTypes.

@Test
public void testCastWithNonRuntimeTypes() throws Exception {
    // $NON-NLS-1$
    helpInvokeMethod("cast", new Object[] { new java.util.Date(0), "time" }, new Time(24 * 60 * 60 * 1000));
    // $NON-NLS-1$
    helpInvokeMethod("cast", new Object[] { new byte[0], "blob" }, new BlobType(BlobType.createBlob(new byte[0])));
}
Also used : BlobType(org.teiid.core.types.BlobType) Time(java.sql.Time) Test(org.junit.Test)

Example 18 with BlobType

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

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

the class S3ProcedureExecution method saveFile.

// should use chunking based save, but it is little complex
// see http://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-examples-using-sdks.html
// for example.
private BinaryWSProcedureExecution saveFile(List<Argument> arguments) throws TranslatorException {
    String name = (String) arguments.get(0).getArgumentValue().getValue();
    String bucket = (String) arguments.get(1).getArgumentValue().getValue();
    String region = (String) arguments.get(2).getArgumentValue().getValue();
    this.endpoint = (String) arguments.get(3).getArgumentValue().getValue();
    String accessKey = (String) arguments.get(4).getArgumentValue().getValue();
    String secretKey = (String) arguments.get(5).getArgumentValue().getValue();
    if (bucket == null) {
        bucket = this.ef.getBucket();
    }
    if (region == null) {
        region = this.ef.getRegion();
    }
    if (endpoint == null) {
        if (region.equals("us-east-1")) {
            endpoint = "https://s3.amazonaws.com/" + bucket + "/" + name;
        } else {
            endpoint = "https://s3-" + region + ".amazonaws.com/" + bucket + "/" + name;
        }
    }
    if (accessKey == null) {
        accessKey = this.ef.getAccesskey();
    }
    if (secretKey == null) {
        secretKey = this.ef.getSecretkey();
    }
    Object file = command.getArguments().get(6).getArgumentValue().getValue();
    if (file == null) {
        // $NON-NLS-1$
        throw new TranslatorException(S3ExecutionFactory.UTIL.getString("non_null"));
    }
    try {
        long length = 0;
        byte[] contents = null;
        if (file instanceof XMLType) {
            length = ((XMLType) file).length();
            contents = ObjectConverterUtil.convertToByteArray(((XMLType) file).getBinaryStream());
        } else if (file instanceof Clob) {
            length = ((Clob) file).length();
            contents = ObjectConverterUtil.convertToByteArray(((Clob) file).getAsciiStream());
        } else if (file instanceof Blob) {
            length = ((Blob) file).length();
            contents = ObjectConverterUtil.convertToByteArray(((Blob) file).getBinaryStream());
        } else if (file instanceof String) {
            length = ((String) file).length();
            contents = ((String) file).getBytes();
        } else {
            // $NON-NLS-1$
            throw new TranslatorException(S3ExecutionFactory.UTIL.getString("unknown_type"));
        }
        byte[] contentHash = AWS4SignerBase.hash(contents);
        String contentHashString = BinaryUtils.toHex(contentHash);
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("x-amz-content-sha256", contentHashString);
        headers.put("content-length", "" + length);
        headers.put("x-amz-storage-class", "STANDARD");
        if (accessKey != null) {
            AWS4SignerForAuthorizationHeader signer = new AWS4SignerForAuthorizationHeader(new URL(endpoint), "PUT", "s3", region);
            String authorization = signer.computeSignature(headers, null, contentHashString, accessKey, secretKey);
            headers.put("Authorization", authorization);
        }
        headers.put("Content-Type", "application/octet-stream");
        // $NON-NLS-1$
        LogManager.logDetail(LogConstants.CTX_WS, "Saving", endpoint);
        return invokeHTTP("PUT", endpoint, new BlobType(contents), headers);
    } catch (SQLException | IOException e) {
        throw new TranslatorException(e);
    }
}
Also used : Blob(java.sql.Blob) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) IOException(java.io.IOException) URL(java.net.URL) XMLType(org.teiid.core.types.XMLType) BlobType(org.teiid.core.types.BlobType) TranslatorException(org.teiid.translator.TranslatorException) Clob(java.sql.Clob)

Example 20 with BlobType

use of org.teiid.core.types.BlobType 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)

Aggregations

BlobType (org.teiid.core.types.BlobType)23 BlobImpl (org.teiid.core.types.BlobImpl)11 InputStreamFactory (org.teiid.core.types.InputStreamFactory)10 Test (org.junit.Test)9 ByteArrayInputStream (java.io.ByteArrayInputStream)7 IOException (java.io.IOException)7 SerialBlob (javax.sql.rowset.serial.SerialBlob)7 Clob (java.sql.Clob)6 SQLException (java.sql.SQLException)6 ClobImpl (org.teiid.core.types.ClobImpl)6 ClobType (org.teiid.core.types.ClobType)6 XMLType (org.teiid.core.types.XMLType)6 InputStream (java.io.InputStream)5 Blob (java.sql.Blob)5 ArrayList (java.util.ArrayList)4 ReaderInputStream (org.teiid.core.util.ReaderInputStream)4 SerialClob (javax.sql.rowset.serial.SerialClob)3 FileStoreOutputStream (org.teiid.common.buffer.FileStore.FileStoreOutputStream)3 SQLXMLImpl (org.teiid.core.types.SQLXMLImpl)3 N1qlQueryRow (com.couchbase.client.java.query.N1qlQueryRow)2