Search in sources :

Example 6 with CommandStatement

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());
}
Also used : CommandStatement(org.teiid.query.sql.proc.CommandStatement) CreateProcedureCommand(org.teiid.query.sql.proc.CreateProcedureCommand) Insert(org.teiid.query.sql.lang.Insert) Test(org.junit.Test)

Example 7 with CommandStatement

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);
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) CommandStatement(org.teiid.query.sql.proc.CommandStatement) Statement(org.teiid.query.sql.proc.Statement) ArrayList(java.util.ArrayList) SymbolMap(org.teiid.query.sql.util.SymbolMap) ExpressionMappingVisitor(org.teiid.query.sql.visitor.ExpressionMappingVisitor) TeiidProcessingException(org.teiid.core.TeiidProcessingException) CommandStatement(org.teiid.query.sql.proc.CommandStatement) Expression(org.teiid.query.sql.symbol.Expression) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) QueryPlannerException(org.teiid.api.exception.query.QueryPlannerException)

Example 8 with CommandStatement

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;");
}
Also used : CommandStatement(org.teiid.query.sql.proc.CommandStatement) Test(org.junit.Test)

Example 9 with CommandStatement

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");
}
Also used : CommandStatement(org.teiid.query.sql.proc.CommandStatement) CreateProcedureCommand(org.teiid.query.sql.proc.CreateProcedureCommand) AssignmentStatement(org.teiid.query.sql.proc.AssignmentStatement) Block(org.teiid.query.sql.proc.Block) RaiseStatement(org.teiid.query.sql.proc.RaiseStatement) Test(org.junit.Test)

Example 10 with CommandStatement

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;");
}
Also used : CommandStatement(org.teiid.query.sql.proc.CommandStatement) Test(org.junit.Test)

Aggregations

CommandStatement (org.teiid.query.sql.proc.CommandStatement)12 Test (org.junit.Test)8 AssignmentStatement (org.teiid.query.sql.proc.AssignmentStatement)4 Block (org.teiid.query.sql.proc.Block)4 CreateProcedureCommand (org.teiid.query.sql.proc.CreateProcedureCommand)4 RaiseStatement (org.teiid.query.sql.proc.RaiseStatement)4 ArrayList (java.util.ArrayList)1 QueryPlannerException (org.teiid.api.exception.query.QueryPlannerException)1 TeiidProcessingException (org.teiid.core.TeiidProcessingException)1 Insert (org.teiid.query.sql.lang.Insert)1 Statement (org.teiid.query.sql.proc.Statement)1 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)1 Expression (org.teiid.query.sql.symbol.Expression)1 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)1 SymbolMap (org.teiid.query.sql.util.SymbolMap)1 ExpressionMappingVisitor (org.teiid.query.sql.visitor.ExpressionMappingVisitor)1