/** * Add a <code>Statement</code> to this block. * @param statement The <code>Statement</code> to be added to the block */ public void addStatement(Statement statement) { addStatement(statement, false); }
/** * Add a <code>Statement</code> to this block. * @param statement The <code>Statement</code> to be added to the block */ public void addStatement(Statement statement) { addStatement(statement, false); }
/** * Add a <code>Statement</code> to this block. * @param statement The <code>Statement</code> to be added to the block */ public void addStatement(Statement statement) { addStatement(statement, false); }
public static final Block sample1() { Block block = new Block(); block.addStatement(TestAssignmentStatement.sample1()); block.addStatement(TestCommandStatement.sample1()); block.addStatement(TestRaiseErrorStatement.sample1()); block.addStatement(TestAssignmentStatement.sample1()); return block; }
public static final Block sample2() { Block block = new Block(); block.addStatement(TestAssignmentStatement.sample2()); block.addStatement(TestCommandStatement.sample2()); block.addStatement(TestRaiseErrorStatement.sample2()); block.addStatement(TestAssignmentStatement.sample2()); return block; }
private Command asLoopProcedure(GroupSymbol group, QueryCommand query, Statement s, GroupSymbol varGroup, int updateType) throws QueryResolverException, TeiidComponentException, TeiidProcessingException { Block b = new Block(); b.addStatement(s); CreateProcedureCommand cupc = new CreateProcedureCommand(); cupc.setUpdateType(updateType); Block parent = new Block(); parent.setAtomic(true); ElementSymbol rowsUpdated = new ElementSymbol(ProcedureReservedWords.VARIABLES+Symbol.SEPARATOR+"ROWS_UPDATED"); //$NON-NLS-1$ DeclareStatement ds = new DeclareStatement(rowsUpdated, DataTypeManager.DefaultDataTypes.INTEGER, new Constant(0)); parent.addStatement(ds); LoopStatement ls = new LoopStatement(b, query, varGroup.getName()); parent.addStatement(ls); AssignmentStatement as = new AssignmentStatement(); rowsUpdated.setType(DataTypeManager.DefaultDataClasses.INTEGER); as.setVariable(rowsUpdated); as.setExpression(new Function("+", new Expression[] {rowsUpdated, new Constant(1)})); //$NON-NLS-1$ b.addStatement(as); Query returnQuery = new Query(); returnQuery.setSelect(new Select(Arrays.asList(rowsUpdated.clone()))); parent.addStatement(new CommandStatement(returnQuery)); cupc.setBlock(parent); cupc.setVirtualGroup(group); QueryResolver.resolveCommand(cupc, metadata); return rewrite(cupc, metadata, context); }
@Test public void testBlock1() { Delete d1 = new Delete(); d1.setGroup(new GroupSymbol("g")); //$NON-NLS-1$ CommandStatement cmdStmt = new CommandStatement(d1); AssignmentStatement assigStmt = new AssignmentStatement(new ElementSymbol("a"), new Constant(new Integer(1))); //$NON-NLS-1$ RaiseStatement errStmt = new RaiseStatement(new Constant("My Error")); //$NON-NLS-1$ Block b = new Block(); b.addStatement(cmdStmt); b.addStatement(assigStmt); b.addStatement(errStmt); helpTest(b, "BEGIN\nDELETE FROM g;\na = 1;\nRAISE 'My Error';\nEND"); //$NON-NLS-1$ }
@Test public void testWhileStatement() throws Exception { ElementSymbol x = new ElementSymbol("x", false); //$NON-NLS-1$ Function f = new Function("+", new Expression[] { x, new Constant(new Integer(1)) }); //$NON-NLS-1$ Statement assignmentStmt = new AssignmentStatement(x, f); Block block = new Block(); block.addStatement(assignmentStmt); Criteria crit = new CompareCriteria(x, CompareCriteria.LT, new Constant(new Integer(100))); WhileStatement whileStmt = new WhileStatement(crit, block); helpStmtTest("WHILE (x < 100) BEGIN x=x+1; END", //$NON-NLS-1$ "WHILE(x < 100)"+"\n"+ "BEGIN"+"\n"+"x = (x + 1);" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ +"\n"+"END", whileStmt); //$NON-NLS-1$ //$NON-NLS-2$ }
@Test public void testCreateUpdateProcedure3() { Delete d1 = new Delete(); d1.setGroup(new GroupSymbol("g")); //$NON-NLS-1$ CommandStatement cmdStmt = new CommandStatement(d1); AssignmentStatement assigStmt = new AssignmentStatement(new ElementSymbol("a"), new Constant(new Integer(1))); //$NON-NLS-1$ RaiseStatement errStmt = new RaiseStatement(new Constant("My Error")); //$NON-NLS-1$ Block b = new Block(); b.addStatement(cmdStmt); b.addStatement(assigStmt); b.addStatement(errStmt); CreateProcedureCommand cup = new CreateProcedureCommand(b); helpTest(cup, "BEGIN\nDELETE FROM g;\na = 1;\nRAISE 'My Error';\nEND"); //$NON-NLS-1$ }
@Test public void testaddStatement2() { Block b1 = sample2().clone(); b1.addStatement(TestCommandStatement.sample2()); Statement stmt = b1.getStatements().get(4); assertTrue("Incorrect statement in the Block", stmt.equals(TestCommandStatement.sample2())); //$NON-NLS-1$ }
@Test public void testCreateUpdateProcedure1() { Delete d1 = new Delete(); d1.setGroup(new GroupSymbol("g")); //$NON-NLS-1$ CommandStatement cmdStmt = new CommandStatement(d1); AssignmentStatement assigStmt = new AssignmentStatement(new ElementSymbol("a"), new Constant(new Integer(1))); //$NON-NLS-1$ RaiseStatement errStmt = new RaiseStatement(new Constant("My Error")); //$NON-NLS-1$ Block b = new Block(); b.addStatement(cmdStmt); b.addStatement(assigStmt); b.addStatement(errStmt); CreateProcedureCommand cup = new CreateProcedureCommand(b); helpTest(cup, "BEGIN\nDELETE FROM g;\na = 1;\nRAISE 'My Error';\nEND"); //$NON-NLS-1$ }
@Test public void testaddStatement1() { Block b1 = sample1().clone(); b1.addStatement(TestCommandStatement.sample2()); assertTrue("Incorrect number of statements in the Block", (b1.getStatements().size() == 5)); //$NON-NLS-1$ }
@Test public void testCreateUpdateProcedure2() { Delete d1 = new Delete(); d1.setGroup(new GroupSymbol("g")); //$NON-NLS-1$ CommandStatement cmdStmt = new CommandStatement(d1); AssignmentStatement assigStmt = new AssignmentStatement(new ElementSymbol("a"), new Constant(new Integer(1))); //$NON-NLS-1$ RaiseStatement errStmt = new RaiseStatement(new Constant("My Error")); //$NON-NLS-1$ Block b = new Block(); b.addStatement(cmdStmt); b.addStatement(assigStmt); b.addStatement(errStmt); CreateProcedureCommand cup = new CreateProcedureCommand(b); helpTest(cup, "BEGIN\nDELETE FROM g;\na = 1;\nRAISE 'My Error';\nEND"); //$NON-NLS-1$ }
TeiidComponentException, TeiidProcessingException { Block b = new Block(); b.addStatement(s); CreateProcedureCommand cupc = new CreateProcedureCommand(); cupc.setUpdateType(updateType); ElementSymbol rowsUpdated = new ElementSymbol(ProcedureReservedWords.VARIABLES+Symbol.SEPARATOR+"ROWS_UPDATED"); //$NON-NLS-1$ DeclareStatement ds = new DeclareStatement(rowsUpdated, DataTypeManager.DefaultDataTypes.INTEGER, new Constant(0)); parent.addStatement(ds); parent.addStatement(new CommandStatement(insert)); Query q = new Query(); q.setSelect(new Select()); q.setFrom(new From(Arrays.asList(new UnaryFromClause(new GroupSymbol("#changes"))))); //$NON-NLS-1$ LoopStatement ls = new LoopStatement(b, q, varGroup.getName()); parent.addStatement(ls); AssignmentStatement as = new AssignmentStatement(); rowsUpdated.setType(DataTypeManager.DefaultDataClasses.INTEGER); as.setVariable(rowsUpdated); as.setExpression(new Function("+", new Expression[] {rowsUpdated, new Constant(1)})); //$NON-NLS-1$ b.addStatement(as); Query returnQuery = new Query(); returnQuery.setSelect(new Select(Arrays.asList(rowsUpdated.clone()))); parent.addStatement(new CommandStatement(returnQuery)); cupc.setBlock(parent); cupc.setVirtualGroup(group);
@Test public void testWhileStatement1() throws Exception { ElementSymbol x = new ElementSymbol("x", false); //$NON-NLS-1$ Function f = new Function("+", new Expression[] { x, new Constant(new Integer(1)) }); //$NON-NLS-1$ Statement assignmentStmt = new AssignmentStatement(x, f); Block block = new Block(); block.setAtomic(true); block.setLabel("1y"); block.addStatement(assignmentStmt); BranchingStatement bs = new BranchingStatement(BranchingMode.CONTINUE); bs.setLabel("1y"); block.addStatement(bs); Criteria crit = new CompareCriteria(x, CompareCriteria.LT, new Constant(new Integer(100))); WhileStatement whileStmt = new WhileStatement(crit, block); helpStmtTest("WHILE (x < 100) \"1y\": BEGIN ATOMIC x=x+1; CONTINUE \"1y\"; END", //$NON-NLS-1$ "WHILE(x < 100)"+"\n"+ "\"1y\" : BEGIN ATOMIC"+"\n"+"x = (x + 1);\nCONTINUE \"1y\";" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ +"\n"+"END", whileStmt); //$NON-NLS-1$ //$NON-NLS-2$ }
@Test public void testIfStatement() throws Exception { ElementSymbol a = new ElementSymbol("a"); //$NON-NLS-1$ String shortType = new String("short"); //$NON-NLS-1$ Statement ifStmt = new DeclareStatement(a, shortType); ElementSymbol b = new ElementSymbol("b"); //$NON-NLS-1$ Statement elseStmt = new DeclareStatement(b, shortType); Block ifBlock = new Block(); ifBlock.addStatement(ifStmt); Block elseBlock = new Block(); elseBlock.addStatement(elseStmt); ElementSymbol c = new ElementSymbol("c"); //$NON-NLS-1$ Criteria crit = new CompareCriteria(c, CompareCriteria.EQ, new Constant(new Integer(5))); IfStatement stmt = new IfStatement(crit, ifBlock, elseBlock); helpStmtTest("IF(c = 5) BEGIN DECLARE short a; END ELSE BEGIN DECLARE short b; END", //$NON-NLS-1$ "IF(c = 5)"+"\n"+ "BEGIN"+"\n"+"DECLARE short a;"+"\n"+"END"+"\n"+ //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ "ELSE"+"\n"+"BEGIN"+"\n"+"DECLARE short b;"+"\n"+"END", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ stmt); }
@Test public void testIfElseWithoutBeginEnd() { String sql = "CREATE VIRTUAL PROCEDURE 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$ Query query = new Query(); query.setSelect(new Select(Arrays.asList(new Constant(1)))); //$NON-NLS-1$ CommandStatement commandStmt = new CommandStatement(query); CompareCriteria criteria = new CompareCriteria(new ElementSymbol("x"), CompareCriteria.GT, new Constant(1)); //$NON-NLS-1$ Block block = new Block(); block.addStatement(commandStmt); IfStatement ifStmt = new IfStatement(criteria, block); IfStatement ifStmt1 = (IfStatement)ifStmt.clone(); Block block2 = new Block(); block2.addStatement(commandStmt); ifStmt1.setElseBlock(block2); Block block3 = new Block(); block3.addStatement(ifStmt); block3.addStatement(ifStmt1); CreateProcedureCommand command = new CreateProcedureCommand(block3); helpTest(sql, expected, command); }
@Test public void testBlockExceptionHandling() throws ParseException { CommandStatement cmdStmt = new CommandStatement(new Query(new Select(Arrays.asList(new MultipleElementSymbol())), new From(Arrays.asList(new UnaryFromClause(new GroupSymbol("x")))), null, null, null)); AssignmentStatement assigStmt = new AssignmentStatement(new ElementSymbol("a"), new Constant(new Integer(1))); //$NON-NLS-1$ RaiseStatement errStmt = new RaiseStatement(new Constant("My Error")); //$NON-NLS-1$ Block b = new Block(); b.setExceptionGroup("e"); b.addStatement(cmdStmt); b.addStatement(assigStmt); b.addStatement(errStmt, true); helpStmtTest("BEGIN\nselect * from x;\na = 1;\nexception e\nERROR 'My Error';\nEND", "BEGIN\nSELECT * FROM x;\na = 1;\nEXCEPTION e\nRAISE SQLEXCEPTION 'My Error';\nEND", b); //$NON-NLS-1$ }
@Test public void testLoopStatement() throws Exception { GroupSymbol g = new GroupSymbol("m.g"); //$NON-NLS-1$ From from = new From(); from.addGroup(g); Select select = new Select(); ElementSymbol c1 = new ElementSymbol("c1", false); //$NON-NLS-1$ select.addSymbol(c1); select.addSymbol(new ElementSymbol("c2", false)); //$NON-NLS-1$ Query query = new Query(); query.setSelect(select); query.setFrom(from); ElementSymbol x = new ElementSymbol("x"); //$NON-NLS-1$ String intType = new String("integer"); //$NON-NLS-1$ Statement dStmt = new DeclareStatement(x, intType); c1 = new ElementSymbol("mycursor.c1", true); //$NON-NLS-1$ Statement assignmentStmt = new AssignmentStatement(x, c1); Block block = new Block(); block.addStatement(dStmt); block.addStatement(assignmentStmt); String cursor = "mycursor"; //$NON-NLS-1$ LoopStatement loopStmt = new LoopStatement(block, query, cursor); helpStmtTest("LOOP ON (SELECT c1, c2 FROM m.g) AS mycursor BEGIN DECLARE integer x; x=mycursor.c1; END", //$NON-NLS-1$ "LOOP ON (SELECT c1, c2 FROM m.g) AS mycursor"+"\n"+ "BEGIN"+"\n"+"DECLARE integer x;"+"\n"+"x = mycursor.c1;" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ +"\n"+"END", loopStmt); //$NON-NLS-1$ //$NON-NLS-2$ }
Statement assignmentStmt = new AssignmentStatement(x, c1); Block block = new Block(); block.addStatement(dStmt); block.addStatement(assignmentStmt);