static Block asBlock(Statement stmt) { if (stmt == null) { return null; } if (stmt instanceof Block) { return (Block)stmt; } return new Block(stmt); }
static Block asBlock(Statement stmt) { if (stmt == null) { return null; } if (stmt instanceof Block) { return (Block)stmt; } return new Block(stmt); }
static Block asBlock(Statement stmt) { if (stmt == null) { return null; } if (stmt instanceof Block) { return (Block)stmt; } return new Block(stmt); }
/** * Deep clone statement to produce a new identical block. * @return Deep clone */ public Block clone() { Block copy = new Block(); copy.setAtomic(atomic); copy.statements = LanguageObject.Util.deepClone(statements, Statement.class); if (exceptionStatements != null) { copy.exceptionStatements = LanguageObject.Util.deepClone(exceptionStatements, Statement.class); } copy.exceptionGroup = this.exceptionGroup; copy.setLabel(label); return copy; }
/** * Deep clone statement to produce a new identical block. * @return Deep clone */ public Block clone() { Block copy = new Block(); copy.setAtomic(atomic); copy.statements = LanguageObject.Util.deepClone(statements, Statement.class); if (exceptionStatements != null) { copy.exceptionStatements = LanguageObject.Util.deepClone(exceptionStatements, Statement.class); } copy.exceptionGroup = this.exceptionGroup; copy.setLabel(label); return copy; }
/** * Deep clone statement to produce a new identical block. * @return Deep clone */ public Block clone() { Block copy = new Block(); copy.setAtomic(atomic); copy.statements = LanguageObject.Util.deepClone(statements, Statement.class); if (exceptionStatements != null) { copy.exceptionStatements = LanguageObject.Util.deepClone(exceptionStatements, Statement.class); } copy.exceptionGroup = this.exceptionGroup; copy.setLabel(label); return copy; }
@Test public void testCacheProc() { String sql = "/*+ cache */ CREATE VIRTUAL PROCEDURE BEGIN END"; //$NON-NLS-1$ CreateProcedureCommand command = new CreateProcedureCommand(new Block()); command.setCacheHint(new CacheHint()); TestParser.helpTest(sql, "/*+ cache */ BEGIN\nEND", command); //$NON-NLS-1$ }
@Test public void testBlock() throws Exception { ElementSymbol a = new ElementSymbol("a"); //$NON-NLS-1$ String type = new String("short"); //$NON-NLS-1$ DeclareStatement declStmt = new DeclareStatement(a, type); Statement stmt = declStmt; Block block = new Block(stmt); helpBlockTest("BEGIN DECLARE short a; END", "BEGIN"+"\n"+"DECLARE short a;"+"\n"+"END", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ block); }
@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 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 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 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$ }
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; }
@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 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 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 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$ }
@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$ }