use of org.teiid.query.sql.proc.CommandStatement in project teiid by teiid.
the class TestProcedureResolving method testDefect23257.
/**
* Constants will now auto resolve if they are consistently representable in the target type
*/
@Test
public void testDefect23257() throws Exception {
// $NON-NLS-1$
CreateProcedureCommand command = (CreateProcedureCommand) helpResolve("EXEC pm6.vsp59()", RealMetadataFactory.example1Cached());
CommandStatement cs = (CommandStatement) command.getBlock().getStatements().get(1);
Insert insert = (Insert) cs.getCommand();
assertEquals(DataTypeManager.DefaultDataClasses.SHORT, ((Expression) insert.getValues().get(1)).getType());
}
use of org.teiid.query.sql.proc.CommandStatement in project teiid by teiid.
the class TriggerActionPlanner method rewritePlan.
/**
* look for the simple case of a mapping to a single insert statement trigger action - and reconstruct the plan as a single insert
* TODO: need internal primitives for delete/update batching in a loop for delete/update cases
*/
private ProcessorPlan rewritePlan(TriggerAction ta, IDGenerator idGenerator, QueryMetadataInterface metadata, CapabilitiesFinder capFinder, AnalysisRecord analysisRecord, CommandContext context, QueryCommand query, Map<ElementSymbol, Expression> mapping, Insert insert) throws QueryMetadataException, QueryResolverException, TeiidComponentException, QueryPlannerException {
if (ta.getBlock().getStatements().size() != 1) {
return null;
}
Statement s = ta.getBlock().getStatements().get(0);
if (!(s instanceof CommandStatement)) {
return null;
}
CommandStatement cs = (CommandStatement) s;
if (!(cs.getCommand() instanceof Insert)) {
return null;
}
Insert mapped = (Insert) cs.getCommand();
if (mapped.getQueryExpression() != null) {
return null;
}
if (insert.getQueryExpression() != null) {
// use a unique inline view name to make the final remapping easier
GroupSymbol group = new GroupSymbol("X");
Collection<GroupSymbol> groups = GroupCollectorVisitor.getGroups(query, true);
for (int i = 0; groups.contains(group); i++) {
group.setName("X_" + i);
}
List<Expression> projectedSymbols = query.getProjectedSymbols();
Query queryExpression = QueryRewriter.createInlineViewQuery(group, query, metadata, projectedSymbols);
List<Expression> viewSymbols = new ArrayList<Expression>(queryExpression.getSelect().getSymbols());
// switch to the values
queryExpression.getSelect().clearSymbols();
List<Expression> values = mapped.getValues();
queryExpression.getSelect().addSymbols(values);
values.clear();
// update the mapping to the view symbols
for (int i = 0; i < projectedSymbols.size(); i++) {
ElementSymbol es = insert.getVariables().get(i);
mapping.put(new ElementSymbol(es.getShortName(), new GroupSymbol(SQLConstants.Reserved.NEW)), SymbolMap.getExpression(viewSymbols.get(i)));
}
// map to the query form - changes references back to element form
SymbolMap queryMapping = new SymbolMap();
queryMapping.asUpdatableMap().putAll(mapping);
ExpressionMappingVisitor visitor = new RuleMergeCriteria.ReferenceReplacementVisitor(queryMapping);
DeepPostOrderNavigator.doVisit(queryExpression.getSelect(), visitor);
// now we can return a plan based off a single insert statement
mapped.setQueryExpression(queryExpression);
return QueryOptimizer.optimizePlan(mapped, metadata, idGenerator, capFinder, analysisRecord, context);
}
List<Expression> values = mapped.getValues();
SymbolMap queryMapping = new SymbolMap();
queryMapping.asUpdatableMap().putAll(mapping);
ExpressionMappingVisitor visitor = new RuleMergeCriteria.ReferenceReplacementVisitor(queryMapping);
Select select = new Select();
select.addSymbols(values);
DeepPostOrderNavigator.doVisit(select, visitor);
values.clear();
for (Expression ex : select.getSymbols()) {
try {
values.add(QueryRewriter.rewriteExpression(SymbolMap.getExpression(ex), context, metadata));
} catch (TeiidProcessingException e) {
throw new QueryPlannerException(e);
}
}
return QueryOptimizer.optimizePlan(mapped, metadata, idGenerator, capFinder, analysisRecord, context);
}
use of org.teiid.query.sql.proc.CommandStatement in project teiid by teiid.
the class TestSQLStringVisitor method testCommandStatement1a.
@Test
public void testCommandStatement1a() {
Query q1 = new Query();
Select select = new Select();
// $NON-NLS-1$
select.addSymbol(new ElementSymbol("x"));
q1.setSelect(select);
From from = new From();
// $NON-NLS-1$
from.addGroup(new GroupSymbol("g"));
q1.setFrom(from);
CommandStatement cmdStmt = new CommandStatement(q1);
cmdStmt.setReturnable(false);
// $NON-NLS-1$
helpTest(cmdStmt, "SELECT x FROM g WITHOUT RETURN;");
}
use of org.teiid.query.sql.proc.CommandStatement in project teiid by teiid.
the class TestSQLStringVisitor method testCreateUpdateProcedure2.
@Test
public void testCreateUpdateProcedure2() {
Delete d1 = new Delete();
// $NON-NLS-1$
d1.setGroup(new GroupSymbol("g"));
CommandStatement cmdStmt = new CommandStatement(d1);
// $NON-NLS-1$
AssignmentStatement assigStmt = new AssignmentStatement(new ElementSymbol("a"), new Constant(new Integer(1)));
// $NON-NLS-1$
RaiseStatement errStmt = new RaiseStatement(new Constant("My Error"));
Block b = new Block();
b.addStatement(cmdStmt);
b.addStatement(assigStmt);
b.addStatement(errStmt);
CreateProcedureCommand cup = new CreateProcedureCommand(b);
// $NON-NLS-1$
helpTest(cup, "BEGIN\nDELETE FROM g;\na = 1;\nRAISE 'My Error';\nEND");
}
use of org.teiid.query.sql.proc.CommandStatement in project teiid by teiid.
the class TestSQLStringVisitor method testCommandStatement1.
@Test
public void testCommandStatement1() {
Query q1 = new Query();
Select select = new Select();
// $NON-NLS-1$
select.addSymbol(new ElementSymbol("x"));
q1.setSelect(select);
From from = new From();
// $NON-NLS-1$
from.addGroup(new GroupSymbol("g"));
q1.setFrom(from);
CommandStatement cmdStmt = new CommandStatement(q1);
// $NON-NLS-1$
helpTest(cmdStmt, "SELECT x FROM g;");
}
Aggregations