Search in sources :

Example 1 with LoopStatement

use of org.teiid.query.sql.proc.LoopStatement in project teiid by teiid.

the class ValidationVisitor method visit.

@Override
public void visit(BranchingStatement obj) {
    boolean matchedLabel = false;
    boolean inLoop = false;
    for (LanguageObject lo : stack) {
        if (lo instanceof LoopStatement || lo instanceof WhileStatement) {
            inLoop = true;
            if (obj.getLabel() == null) {
                break;
            }
            matchedLabel |= obj.getLabel().equalsIgnoreCase(((Labeled) lo).getLabel());
        } else if (obj.getLabel() != null && lo instanceof Block && obj.getLabel().equalsIgnoreCase(((Block) lo).getLabel())) {
            matchedLabel = true;
            if (obj.getMode() != BranchingMode.LEAVE) {
                // $NON-NLS-1$
                handleValidationError(QueryPlugin.Util.getString("ValidationVisitor.invalid_label", obj.getLabel()), obj);
            }
        }
    }
    if (obj.getMode() != BranchingMode.LEAVE && !inLoop) {
        // $NON-NLS-1$
        handleValidationError(QueryPlugin.Util.getString("ValidationVisitor.no_loop"), obj);
    }
    if (obj.getLabel() != null && !matchedLabel) {
        // $NON-NLS-1$
        handleValidationError(QueryPlugin.Util.getString("ValidationVisitor.unknown_block_label", obj.getLabel()), obj);
    }
}
Also used : Labeled(org.teiid.query.sql.proc.Statement.Labeled) Block(org.teiid.query.sql.proc.Block) LoopStatement(org.teiid.query.sql.proc.LoopStatement) WhileStatement(org.teiid.query.sql.proc.WhileStatement) LanguageObject(org.teiid.query.sql.LanguageObject)

Example 2 with LoopStatement

use of org.teiid.query.sql.proc.LoopStatement in project teiid by teiid.

the class TestProcedureResolving method testProcedureScoping.

@Test
public void testProcedureScoping() throws Exception {
    StringBuffer proc = // $NON-NLS-1$
    new StringBuffer("FOR EACH ROW").append(// $NON-NLS-1$
    "\nBEGIN").append(// $NON-NLS-1$
    "\n  declare integer e1 = 1;").append(// $NON-NLS-1$
    "\n  e1 = e1;").append(// $NON-NLS-1$
    "\n  LOOP ON (SELECT pm1.g1.e1 FROM pm1.g1) AS loopCursor").append(// $NON-NLS-1$
    "\n  BEGIN").append(// $NON-NLS-1$
    "\n    variables.e1 = convert(e1, integer);").append(// $NON-NLS-1$
    "\n  END").append(// $NON-NLS-1$
    "\nEND");
    // $NON-NLS-1$
    String userUpdateStr = "UPDATE vm1.g1 SET e1='x'";
    TriggerAction command = helpResolveUpdateProcedure(proc.toString(), userUpdateStr, Table.TriggerEvent.UPDATE);
    Block block = command.getBlock();
    AssignmentStatement assStmt = (AssignmentStatement) block.getStatements().get(1);
    assertEquals(ProcedureReservedWords.VARIABLES, assStmt.getVariable().getGroupSymbol().getName());
    assertEquals(ProcedureReservedWords.VARIABLES, ((ElementSymbol) assStmt.getExpression()).getGroupSymbol().getName());
    Block inner = ((LoopStatement) block.getStatements().get(2)).getBlock();
    assStmt = (AssignmentStatement) inner.getStatements().get(0);
    ElementSymbol value = ElementCollectorVisitor.getElements(assStmt.getExpression(), false).iterator().next();
    // $NON-NLS-1$
    assertEquals("loopCursor", value.getGroupSymbol().getName());
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) TriggerAction(org.teiid.query.sql.proc.TriggerAction) AssignmentStatement(org.teiid.query.sql.proc.AssignmentStatement) Block(org.teiid.query.sql.proc.Block) LoopStatement(org.teiid.query.sql.proc.LoopStatement) Test(org.junit.Test)

Aggregations

Block (org.teiid.query.sql.proc.Block)2 LoopStatement (org.teiid.query.sql.proc.LoopStatement)2 Test (org.junit.Test)1 LanguageObject (org.teiid.query.sql.LanguageObject)1 AssignmentStatement (org.teiid.query.sql.proc.AssignmentStatement)1 Labeled (org.teiid.query.sql.proc.Statement.Labeled)1 TriggerAction (org.teiid.query.sql.proc.TriggerAction)1 WhileStatement (org.teiid.query.sql.proc.WhileStatement)1 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)1