Search in sources :

Example 1 with FunctionExecutionException

use of org.teiid.api.exception.query.FunctionExecutionException 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 2 with FunctionExecutionException

use of org.teiid.api.exception.query.FunctionExecutionException in project teiid by teiid.

the class Evaluator method evaluateJSONObject.

private Object evaluateJSONObject(List<?> tuple, JSONObject function, JSONBuilder builder) throws ExpressionEvaluationException, BlockedException, TeiidComponentException, FunctionExecutionException {
    List<DerivedColumn> args = function.getArgs();
    Evaluator.NameValuePair<Object>[] nameValuePairs = getNameValuePairs(tuple, args, false, false);
    boolean returnValue = false;
    try {
        if (builder == null) {
            returnValue = true;
            // preevaluate subqueries to prevent blocked exceptions
            for (SubqueryContainer<?> container : ValueIteratorProviderCollectorVisitor.getValueIteratorProviders(function)) {
                evaluateSubquery(container, tuple);
            }
            builder = new JSONBuilder(context.getBufferManager());
        }
        builder.start(false);
        for (NameValuePair<Object> nameValuePair : nameValuePairs) {
            addValue(tuple, builder, nameValuePair.name, nameValuePair.value);
        }
        builder.end(false);
        if (returnValue) {
            ClobType result = builder.close(context);
            builder = null;
            return result;
        }
        return null;
    } catch (TeiidProcessingException e) {
        throw new FunctionExecutionException(e);
    } finally {
        if (returnValue && builder != null) {
            builder.remove();
        }
    }
}
Also used : JSONBuilder(org.teiid.query.function.JSONFunctionMethods.JSONBuilder) FunctionExecutionException(org.teiid.api.exception.query.FunctionExecutionException) LanguageObject(org.teiid.query.sql.LanguageObject) TeiidProcessingException(org.teiid.core.TeiidProcessingException)

Example 3 with FunctionExecutionException

use of org.teiid.api.exception.query.FunctionExecutionException in project teiid by teiid.

the class Evaluator method evaluateXMLSerialize.

private Object evaluateXMLSerialize(List<?> tuple, XMLSerialize xs) throws ExpressionEvaluationException, BlockedException, TeiidComponentException, FunctionExecutionException {
    XMLType value = (XMLType) internalEvaluate(xs.getExpression(), tuple);
    if (value == null) {
        return null;
    }
    try {
        if (!xs.isDocument()) {
            return XMLSystemFunctions.serialize(xs, value);
        }
        if (value.getType() == Type.UNKNOWN) {
            Type type = StringToSQLXMLTransform.isXml(value.getCharacterStream());
            value.setType(type);
        }
        if (value.getType() == Type.DOCUMENT || value.getType() == Type.ELEMENT) {
            return XMLSystemFunctions.serialize(xs, value);
        }
    } catch (SQLException e) {
        throw new FunctionExecutionException(QueryPlugin.Event.TEIID30334, e);
    } catch (TransformationException e) {
        throw new FunctionExecutionException(QueryPlugin.Event.TEIID30335, e);
    }
    throw new FunctionExecutionException(QueryPlugin.Event.TEIID30336, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30336));
}
Also used : Type(org.teiid.core.types.XMLType.Type) FunctionExecutionException(org.teiid.api.exception.query.FunctionExecutionException) TeiidSQLException(org.teiid.jdbc.TeiidSQLException) SQLException(java.sql.SQLException)

Example 4 with FunctionExecutionException

use of org.teiid.api.exception.query.FunctionExecutionException in project teiid by teiid.

the class Evaluator method evaluateXMLElement.

private Object evaluateXMLElement(List<?> tuple, XMLElement function) throws ExpressionEvaluationException, BlockedException, TeiidComponentException, FunctionExecutionException {
    List<Expression> content = function.getContent();
    List<Object> values = new ArrayList<Object>(content.size());
    for (Expression exp : content) {
        values.add(internalEvaluate(exp, tuple));
    }
    try {
        Evaluator.NameValuePair<Object>[] attributes = null;
        if (function.getAttributes() != null) {
            attributes = getNameValuePairs(tuple, function.getAttributes().getArgs(), true, true);
        }
        return XMLSystemFunctions.xmlElement(context, function.getName(), namespaces(function.getNamespaces()), attributes, values);
    } catch (TeiidProcessingException e) {
        throw new FunctionExecutionException(e);
    }
}
Also used : FunctionExecutionException(org.teiid.api.exception.query.FunctionExecutionException) ExceptionExpression(org.teiid.query.sql.proc.ExceptionExpression) ArrayList(java.util.ArrayList) LanguageObject(org.teiid.query.sql.LanguageObject) TeiidProcessingException(org.teiid.core.TeiidProcessingException)

Example 5 with FunctionExecutionException

use of org.teiid.api.exception.query.FunctionExecutionException in project teiid by teiid.

the class TeiidScriptEngine method compile.

@Override
public CompiledScript compile(String script) throws ScriptException {
    final String[] parts = splitter.split(script);
    final int[] indexes = new int[parts.length];
    for (int i = 1; i < parts.length; i++) {
        String string = parts[i];
        for (int j = 0; j < string.length(); j++) {
            if (!Character.isJavaIdentifierPart(string.charAt(j))) {
                throw new ScriptException(QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30431, string, string.charAt(j)));
            }
        }
        try {
            indexes[i] = Integer.parseInt(string);
        } catch (NumberFormatException e) {
            indexes[i] = -1;
        }
    }
    return new CompiledScript() {

        @Override
        public ScriptEngine getEngine() {
            return TeiidScriptEngine.this;
        }

        @Override
        public Object eval(ScriptContext sc) throws ScriptException {
            if (sc == null) {
                throw new NullPointerException();
            }
            Object obj = null;
            if (parts.length > 0) {
                obj = sc.getAttribute(parts[0]);
            }
            for (int i = 1; i < parts.length; i++) {
                if (obj == null) {
                    return null;
                }
                String part = parts[i];
                Map<String, Method> methodMap = getMethodMap(obj.getClass());
                Method m = methodMap.get(part);
                if (m == null) {
                    int index = indexes[i];
                    if (index > 0) {
                        // assume it's a list/array
                        if (obj instanceof List) {
                            try {
                                obj = ((List<?>) obj).get(index - 1);
                            } catch (IndexOutOfBoundsException e) {
                                obj = null;
                            }
                            continue;
                        }
                        try {
                            obj = FunctionMethods.array_get(obj, index);
                            continue;
                        } catch (FunctionExecutionException e) {
                            throw new ScriptException(e);
                        } catch (SQLException e) {
                            throw new ScriptException(e);
                        }
                    }
                    throw new ScriptException(QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31111, part, obj.getClass()));
                }
                try {
                    obj = m.invoke(obj);
                } catch (IllegalAccessException e) {
                    throw new ScriptException(e);
                } catch (InvocationTargetException e) {
                    if (e.getCause() instanceof Exception) {
                        throw new ScriptException((Exception) e.getCause());
                    }
                    throw new ScriptException(e);
                }
            }
            return obj;
        }
    };
}
Also used : CompiledScript(javax.script.CompiledScript) SQLException(java.sql.SQLException) ScriptContext(javax.script.ScriptContext) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) SQLException(java.sql.SQLException) ScriptException(javax.script.ScriptException) IOException(java.io.IOException) IntrospectionException(java.beans.IntrospectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) FunctionExecutionException(org.teiid.api.exception.query.FunctionExecutionException) ScriptException(javax.script.ScriptException) FunctionExecutionException(org.teiid.api.exception.query.FunctionExecutionException) List(java.util.List)

Aggregations

FunctionExecutionException (org.teiid.api.exception.query.FunctionExecutionException)31 IOException (java.io.IOException)9 SQLException (java.sql.SQLException)9 ParseException (com.vividsolutions.jts.io.ParseException)5 TeiidProcessingException (org.teiid.core.TeiidProcessingException)5 LanguageObject (org.teiid.query.sql.LanguageObject)4 ClobType (org.teiid.core.types.ClobType)3 TransformationException (org.teiid.core.types.TransformationException)3 WKBReader (com.vividsolutions.jts.io.WKBReader)2 WKTReader (com.vividsolutions.jts.io.WKTReader)2 StringReader (java.io.StringReader)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 XPathException (net.sf.saxon.trans.XPathException)2 BlockedException (org.teiid.common.buffer.BlockedException)2