Search in sources :

Example 16 with XMLType

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

the class XMLTableNode method processRow.

private List<?> processRow() throws ExpressionEvaluationException, BlockedException, TeiidComponentException, TeiidProcessingException {
    List<Object> tuple = new ArrayList<Object>(projectedColumns.size());
    for (XMLColumn proColumn : projectedColumns) {
        if (proColumn.isOrdinal()) {
            if (rowCount > Integer.MAX_VALUE) {
                throw new TeiidRuntimeException(new TeiidProcessingException(QueryPlugin.Event.TEIID31174, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31174)));
            }
            tuple.add((int) rowCount);
        } else {
            try {
                XPathExpression path = proColumn.getPathExpression();
                XPathDynamicContext dynamicContext = path.createDynamicContext(item);
                final SequenceIterator pathIter = path.iterate(dynamicContext);
                Item colItem = pathIter.next();
                if (colItem == null) {
                    if (proColumn.getDefaultExpression() != null) {
                        tuple.add(getEvaluator(Collections.emptyMap()).evaluate(proColumn.getDefaultExpression(), null));
                    } else {
                        tuple.add(null);
                    }
                    continue;
                }
                if (proColumn.getSymbol().getType() == DataTypeManager.DefaultDataClasses.XML) {
                    SequenceIterator pushBack = new PushBackSequenceIterator(pathIter, colItem);
                    XMLType value = table.getXQueryExpression().createXMLType(pushBack, this.getBufferManager(), false, getContext());
                    tuple.add(value);
                    continue;
                }
                if (proColumn.getSymbol().getType().isArray()) {
                    ArrayList<Object> vals = new ArrayList<Object>();
                    vals.add(getValue(proColumn.getSymbol().getType().getComponentType(), colItem, this.table.getXQueryExpression().getConfig(), getContext()));
                    Item next = null;
                    while ((next = pathIter.next()) != null) {
                        vals.add(getValue(proColumn.getSymbol().getType().getComponentType(), next, this.table.getXQueryExpression().getConfig(), getContext()));
                    }
                    Object value = new ArrayImpl(vals.toArray((Object[]) Array.newInstance(proColumn.getSymbol().getType().getComponentType(), vals.size())));
                    tuple.add(value);
                    continue;
                } else if (pathIter.next() != null) {
                    throw new TeiidProcessingException(QueryPlugin.Event.TEIID30171, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30171, proColumn.getName()));
                }
                Object value = getValue(proColumn.getSymbol().getType(), colItem, this.table.getXQueryExpression().getConfig(), getContext());
                tuple.add(value);
            } catch (XPathException e) {
                throw new TeiidProcessingException(QueryPlugin.Event.TEIID30172, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30172, proColumn.getName()));
            }
        }
    }
    item = null;
    return tuple;
}
Also used : XPathExpression(net.sf.saxon.sxpath.XPathExpression) XPathException(net.sf.saxon.trans.XPathException) ArrayImpl(org.teiid.core.types.ArrayImpl) ArrayList(java.util.ArrayList) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) TeiidProcessingException(org.teiid.core.TeiidProcessingException) XMLColumn(org.teiid.query.sql.lang.XMLTable.XMLColumn) Item(net.sf.saxon.om.Item) XMLType(org.teiid.core.types.XMLType) PushBackSequenceIterator(org.teiid.query.xquery.saxon.PushBackSequenceIterator) SequenceIterator(net.sf.saxon.om.SequenceIterator) LanguageObject(org.teiid.query.sql.LanguageObject) PushBackSequenceIterator(org.teiid.query.xquery.saxon.PushBackSequenceIterator) XPathDynamicContext(net.sf.saxon.sxpath.XPathDynamicContext)

Example 17 with XMLType

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

the class XQueryEvaluator method evaluateXMLQuery.

/**
 * @param tuple
 * @param xmlQuery
 * @param exists - check only for the existence of a non-empty result
 * @return Boolean if exists is true, otherwise an XMLType value
 * @throws BlockedException
 * @throws TeiidComponentException
 * @throws FunctionExecutionException
 */
public static Object evaluateXMLQuery(List<?> tuple, XMLQuery xmlQuery, boolean exists, Map<String, Object> parameters, CommandContext context) throws BlockedException, TeiidComponentException, FunctionExecutionException {
    boolean emptyOnEmpty = xmlQuery.getEmptyOnEmpty() == null || xmlQuery.getEmptyOnEmpty();
    Result result = null;
    try {
        XMLQueryRowProcessor rp = null;
        if (xmlQuery.getXQueryExpression().isStreaming()) {
            rp = new XMLQueryRowProcessor(exists, context);
        }
        try {
            Object contextItem = null;
            if (parameters.containsKey(null)) {
                contextItem = parameters.remove(null);
                if (contextItem == null) {
                    return null;
                }
            }
            result = evaluateXQuery(xmlQuery.getXQueryExpression(), contextItem, parameters, rp, context);
            if (result == null) {
                return null;
            }
            if (exists) {
                if (result.iter.next() == null) {
                    return false;
                }
                return true;
            }
        } catch (TeiidRuntimeException e) {
            if (e.getCause() instanceof XPathException) {
                throw (XPathException) e.getCause();
            }
            throw e;
        }
        if (rp != null) {
            if (exists) {
                return rp.hasItem;
            }
            XMLType.Type type = rp.type;
            if (type == null) {
                if (!emptyOnEmpty) {
                    return null;
                }
                type = Type.CONTENT;
            }
            XMLType val = rp.concat.close(context);
            val.setType(rp.type);
            return val;
        }
        return xmlQuery.getXQueryExpression().createXMLType(result.iter, context.getBufferManager(), emptyOnEmpty, context);
    } catch (TeiidProcessingException e) {
        throw new FunctionExecutionException(QueryPlugin.Event.TEIID30333, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30333, e.getMessage()));
    } catch (XPathException e) {
        throw new FunctionExecutionException(QueryPlugin.Event.TEIID30333, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30333, e.getMessage()));
    } finally {
        if (result != null) {
            result.close();
        }
    }
}
Also used : XMLType(org.teiid.core.types.XMLType) FunctionExecutionException(org.teiid.api.exception.query.FunctionExecutionException) XPathException(net.sf.saxon.trans.XPathException) Type(org.teiid.core.types.XMLType.Type) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) StreamResult(javax.xml.transform.stream.StreamResult) Result(org.teiid.query.xquery.saxon.SaxonXQueryExpression.Result) QueryResult(net.sf.saxon.query.QueryResult) TeiidProcessingException(org.teiid.core.TeiidProcessingException)

Example 18 with XMLType

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

the class TestFunctionLibrary method testInvokeXmlComment.

@Test
public void testInvokeXmlComment() throws Exception {
    CommandContext c = new CommandContext();
    c.setBufferManager(BufferManagerFactory.getStandaloneBufferManager());
    XMLType result = (XMLType) helpInvokeMethod("xmlcomment", new Class<?>[] { DataTypeManager.DefaultDataClasses.STRING }, new Object[] { "comment" }, c);
    String xml = ObjectConverterUtil.convertToString(result.getCharacterStream());
    assertEquals("<!--comment-->", xml);
}
Also used : XMLType(org.teiid.core.types.XMLType) CommandContext(org.teiid.query.util.CommandContext) Test(org.junit.Test)

Example 19 with XMLType

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

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

Aggregations

XMLType (org.teiid.core.types.XMLType)25 SQLException (java.sql.SQLException)9 IOException (java.io.IOException)8 Test (org.junit.Test)7 SQLXMLImpl (org.teiid.core.types.SQLXMLImpl)7 Clob (java.sql.Clob)6 BlobType (org.teiid.core.types.BlobType)6 StringReader (java.io.StringReader)5 StAXSource (javax.xml.transform.stax.StAXSource)5 ClobType (org.teiid.core.types.ClobType)4 TranslatorException (org.teiid.translator.TranslatorException)4 XMLStreamException (javax.xml.stream.XMLStreamException)3 TransformerException (javax.xml.transform.TransformerException)3 StreamResult (javax.xml.transform.stream.StreamResult)3 WebServiceException (javax.xml.ws.WebServiceException)3 Item (net.sf.saxon.om.Item)3 InputStreamFactory (org.teiid.core.types.InputStreamFactory)3 TransformationException (org.teiid.core.types.TransformationException)3 Type (org.teiid.core.types.XMLType.Type)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2