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;
}
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();
}
}
}
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));
}
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);
}
}
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;
}
};
}
Aggregations