@Override @Test public void testIfElseWithoutBeginAndWithoutCreateVirtualProcedurePrefix() { String sql = "BEGIN IF (x > 1) select 1; IF (x > 1) select 1; ELSE select 1; END"; //$NON-NLS-1$ String expected = "BEGIN\nIF(x > 1)\nBEGIN\nSELECT 1;\nEND\nIF(x > 1)\nBEGIN\nSELECT 1;\nEND\nELSE\nBEGIN\nSELECT 1;\nEND\nEND"; //$NON-NLS-1$ QueryImpl query = getFactory().newQuery(); BaseExpression expr = getFactory().wrapExpression(getFactory().newConstant(1)); query.setSelect(getFactory().newSelect(Arrays.asList(expr))); // $NON-NLS-1$ CommandStatementImpl commandStmt = getFactory().newCommandStatement(query); CompareCriteriaImpl criteria = getFactory() .newCompareCriteria( getFactory().newElementSymbol("x"), CriteriaOperator.Operator.GT, getFactory().newConstant(1)); // $NON-NLS-1$ BlockImpl block = getFactory().newBlock(); block.addStatement(commandStmt); IfStatementImpl ifStmt = getFactory().newIfStatement(criteria, block); IfStatementImpl ifStmt1 = ifStmt.clone(); BlockImpl block2 = getFactory().newBlock(); block2.addStatement(commandStmt); ifStmt1.setElseBlock(block2); BlockImpl block3 = getFactory().newBlock(); block3.addStatement(ifStmt); block3.addStatement(ifStmt1); CreateProcedureCommandImpl command = getFactory().newCreateProcedureCommand(); command.setBlock(block3); helpTest(sql, expected, command); }
/** Drops the CREATE VIRTUAL PROCEDURE prefix */ @Override @Test public void testVirtualProcedure() { ElementSymbolImpl x = getFactory().newElementSymbol("x"); String intType = new String("integer"); StatementImpl dStmt = getFactory().newDeclareStatement(x, intType); GroupSymbolImpl g = getFactory().newGroupSymbol("m.g"); FromImpl from = getFactory().newFrom(); from.addGroup(g); SelectImpl select = getFactory().newSelect(); ElementSymbolImpl c1 = getFactory().newElementSymbol("c1"); select.addSymbol(c1); select.addSymbol(getFactory().newElementSymbol("c2")); QueryImpl query = getFactory().newQuery(select, from); x = getFactory().newElementSymbol("x"); c1 = getFactory().newElementSymbol("mycursor.c1"); StatementImpl assignmentStmt = getFactory().newAssignmentStatement(x, c1); BlockImpl block = getFactory().newBlock(); block.addStatement(assignmentStmt); BlockImpl ifBlock = getFactory().newBlock(); StatementImpl continueStmt = getFactory().newBranchingStatement(BranchingMode.CONTINUE); ifBlock.addStatement(continueStmt); CriteriaImpl crit = getFactory().newCompareCriteria(x, Operator.GT, getFactory().newConstant(new Integer(5))); IfStatementImpl ifStmt = getFactory().newIfStatement(crit, ifBlock); block.addStatement(ifStmt); String cursor = "mycursor"; LoopStatementImpl loopStmt = getFactory().newLoopStatement(block, query, cursor); block = getFactory().newBlock(); block.addStatement(dStmt); block.addStatement(loopStmt); CommandStatementImpl cmdStmt = getFactory().newCommandStatement(query); block.addStatement(cmdStmt); CreateProcedureCommandImpl virtualProcedureCommand = getFactory().newCreateProcedureCommand(); virtualProcedureCommand.setBlock(block); helpTest( "BEGIN DECLARE integer x; LOOP ON (SELECT c1, c2 FROM m.g) AS mycursor BEGIN x=mycursor.c1; IF(x > 5) BEGIN CONTINUE; END END SELECT c1, c2 FROM m.g; END", "BEGIN\nDECLARE integer x;\n" + "LOOP ON (SELECT c1, c2 FROM m.g) AS mycursor\nBEGIN\n" + "x = mycursor.c1;\nIF(x > 5)\nBEGIN\nCONTINUE;\nEND\nEND\n" + "SELECT c1, c2 FROM m.g;\nEND", virtualProcedureCommand); }