use of org.teiid.query.sql.util.VariableContext in project teiid by teiid.
the class QueryProcessorFactoryImpl method createQueryProcessor.
@Override
public QueryProcessor createQueryProcessor(String query, String recursionGroup, CommandContext commandContext, Object... params) throws TeiidProcessingException, TeiidComponentException {
CommandContext copy = commandContext.clone();
copy.resetDeterminismLevel(true);
copy.setDataObjects(null);
QueryMetadataInterface metadata = commandContext.getMetadata();
if (metadata == null) {
metadata = defaultMetadata;
}
PreparedPlan pp = getPreparedPlan(query, recursionGroup, copy, metadata);
copy.pushVariableContext(new VariableContext());
PreparedStatementRequest.resolveParameterValues(pp.getReferences(), Arrays.asList(params), copy, metadata);
return new QueryProcessor(pp.getPlan().clone(), copy, bufferMgr, dataMgr);
}
use of org.teiid.query.sql.util.VariableContext in project teiid by teiid.
the class TestProcessor method setParameterValues.
public static void setParameterValues(List<?> values, Command command, CommandContext context) {
VariableContext vc = new VariableContext();
Iterator<?> valIter = values.iterator();
for (Reference ref : ReferenceCollectorVisitor.getReferences(command)) {
if (!ref.isPositional()) {
continue;
}
// $NON-NLS-1$
vc.setGlobalValue(ref.getContextSymbol(), valIter.next());
}
context.setVariableContext(vc);
}
use of org.teiid.query.sql.util.VariableContext in project teiid by teiid.
the class PreparedStatementRequest method handlePreparedBatchUpdate.
/**
* There are two cases
* if
* The source supports preparedBatchUpdate -> just let the command and values pass to the source
* else
* create a batchedupdatecommand that represents the batch operation
* @param command
* @throws QueryMetadataException
* @throws TeiidComponentException
* @throws QueryResolverException
* @throws QueryPlannerException
* @throws QueryValidatorException
*/
private void handlePreparedBatchUpdate() throws QueryMetadataException, TeiidComponentException, QueryResolverException, QueryPlannerException, QueryValidatorException {
List<List<?>> paramValues = (List<List<?>>) requestMsg.getParameterValues();
if (paramValues.isEmpty()) {
throw new QueryValidatorException(QueryPlugin.Event.TEIID30555, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30555));
}
boolean supportPreparedBatchUpdate = false;
Command command = null;
if (this.processPlan instanceof RelationalPlan) {
RelationalPlan rPlan = (RelationalPlan) this.processPlan;
if (rPlan.getRootNode() instanceof AccessNode) {
AccessNode aNode = (AccessNode) rPlan.getRootNode();
String modelName = aNode.getModelName();
command = aNode.getCommand();
SourceCapabilities caps = capabilitiesFinder.findCapabilities(modelName);
supportPreparedBatchUpdate = caps.supportsCapability(SourceCapabilities.Capability.BULK_UPDATE);
if (supportPreparedBatchUpdate && // only allow the plan if the multi-valued references result in expressions that can be pushed
!CriteriaCapabilityValidatorVisitor.canPushLanguageObject(command, metadata.getModelID(modelName), metadata, capabilitiesFinder, analysisRecord, false, false, true)) {
supportPreparedBatchUpdate = false;
}
}
}
List<Command> commands = new LinkedList<Command>();
List<VariableContext> contexts = new LinkedList<VariableContext>();
List<List<Object>> multiValues = new ArrayList<List<Object>>(this.prepPlan.getReferences().size());
for (List<?> values : paramValues) {
PreparedStatementRequest.resolveParameterValues(this.prepPlan.getReferences(), values, this.context, this.metadata);
contexts.add(this.context.getVariableContext());
if (supportPreparedBatchUpdate) {
if (multiValues.isEmpty()) {
for (int i = 0; i < values.size(); i++) {
multiValues.add(new ArrayList<Object>(paramValues.size()));
}
}
for (int i = 0; i < values.size(); i++) {
List<Object> multiValue = multiValues.get(i);
Object value = this.context.getVariableContext().getGlobalValue(this.prepPlan.getReferences().get(i).getContextSymbol());
multiValue.add(value);
}
} else {
// just accumulate copies of the command/plan - clones are not necessary
if (command == null) {
command = this.prepPlan.getCommand();
}
command.setProcessorPlan(this.processPlan);
commands.add(command);
}
}
if (paramValues.size() > 1) {
this.context.setVariableContext(new VariableContext());
}
if (paramValues.size() == 1) {
// just use the existing plan, and global reference evaluation
return;
}
if (supportPreparedBatchUpdate) {
for (int i = 0; i < this.prepPlan.getReferences().size(); i++) {
Constant c = new Constant(null, this.prepPlan.getReferences().get(i).getType());
c.setMultiValued(multiValues.get(i));
this.context.getVariableContext().setGlobalValue(this.prepPlan.getReferences().get(i).getContextSymbol(), c);
}
return;
}
BatchedUpdateCommand buc = new BatchedUpdateCommand(commands);
buc.setVariableContexts(contexts);
BatchedUpdatePlanner planner = new BatchedUpdatePlanner();
this.processPlan = planner.optimize(buc, idGenerator, metadata, capabilitiesFinder, analysisRecord, context);
}
use of org.teiid.query.sql.util.VariableContext in project teiid by teiid.
the class PreparedStatementRequest method resolveParameterValues.
/**
* @param params
* @param values
* @throws QueryResolverException
* @throws QueryValidatorException
*/
public static void resolveParameterValues(List<Reference> params, List values, CommandContext context, QueryMetadataInterface metadata) throws QueryResolverException, TeiidComponentException, QueryValidatorException {
VariableContext result = new VariableContext();
// the size of the values must be the same as that of the parameters
if (params.size() != values.size()) {
// $NON-NLS-1$
String msg = QueryPlugin.Util.getString("QueryUtil.wrong_number_of_values", new Object[] { new Integer(values.size()), new Integer(params.size()) });
throw new QueryResolverException(QueryPlugin.Event.TEIID30556, msg);
}
boolean embedded = context != null && context.getSession() != null && context.getSession().isEmbedded();
// to that of the reference
for (int i = 0; i < params.size(); i++) {
Reference param = params.get(i);
Object value = values.get(i);
if (value != null) {
if (embedded && value instanceof BaseLob) {
createStreamCopy(context, i, param, value);
}
try {
String targetTypeName = DataTypeManager.getDataTypeName(param.getType());
Expression expr = ResolverUtil.convertExpression(new Constant(DataTypeManager.convertToRuntimeType(value, param.getType() != DataTypeManager.DefaultDataClasses.OBJECT)), targetTypeName, metadata);
value = Evaluator.evaluate(expr);
} catch (ExpressionEvaluationException e) {
// $NON-NLS-1$
String msg = QueryPlugin.Util.getString("QueryUtil.Error_executing_conversion_function_to_convert_value", i + 1, value, value.getClass(), DataTypeManager.getDataTypeName(param.getType()));
throw new QueryResolverException(QueryPlugin.Event.TEIID30557, e, msg);
} catch (QueryResolverException e) {
// $NON-NLS-1$
String msg = QueryPlugin.Util.getString("QueryUtil.Error_executing_conversion_function_to_convert_value", i + 1, value, value.getClass(), DataTypeManager.getDataTypeName(param.getType()));
throw new QueryResolverException(QueryPlugin.Event.TEIID30558, e, msg);
}
}
if (param.getConstraint() != null) {
param.getConstraint().validate(value);
}
// bind variable
result.setGlobalValue(param.getContextSymbol(), value);
}
context.setVariableContext(result);
}
use of org.teiid.query.sql.util.VariableContext in project teiid by teiid.
the class Evaluator method getRowValue.
private RowValue getRowValue(final List<?> tuple, final LanguageObject lo) {
if (lo instanceof GroupSymbol) {
GroupSymbol leftRowValue = (GroupSymbol) lo;
TempMetadataID id = (TempMetadataID) leftRowValue.getMetadataID();
VariableContext vc = this.context.getVariableContext();
List<TempMetadataID> cols = id.getElements();
return new RowValue() {
@Override
public int length() {
return cols.size();
}
@Override
public Object get(int index) {
return vc.getValue(new ElementSymbol(cols.get(index).getName(), leftRowValue));
}
};
}
return new RowValue() {
@Override
public int length() {
return 1;
}
@Override
public Object get(int index) throws ExpressionEvaluationException, BlockedException, TeiidComponentException {
return internalEvaluate((Expression) lo, tuple);
}
};
}
Aggregations