public void testWhere_embeddedVariable_orderBy() throws Exception { ExpressionEvaluator evaluator = new ExpressionEvaluator(); evaluator.add("embedded", new Value(String.class, "order by bbb")); String testSql = "select * from aaa where /*%if false*/ename = 'aaa'/*%end */ /*#embedded*/"; SqlParser parser = new SqlParser(testSql); SqlNode sqlNode = parser.parse(); PreparedSql sql = new NodePreparedSqlBuilder( config, SqlKind.SELECT, "dummyPath", evaluator, SqlLogType.FORMATTED) .build(sqlNode, Function.identity()); assertEquals("select * from aaa order by bbb", sql.getRawSql()); }
public void testEmbeddedVariable_blockComment() throws Exception { ExpressionEvaluator evaluator = new ExpressionEvaluator(); evaluator.add("name", new Value(String.class, "hoge")); evaluator.add("salary", new Value(BigDecimal.class, new BigDecimal(10000))); evaluator.add("orderBy", new Value(String.class, "aaa/*bbb")); String testSql = "select * from aaa where ename = /*name*/'aaa' and sal = /*salary*/-2000 /*#orderBy*/"; SqlParser parser = new SqlParser(testSql); SqlNode sqlNode = parser.parse(); try { new NodePreparedSqlBuilder( config, SqlKind.SELECT, "dummyPath", evaluator, SqlLogType.FORMATTED) .build(sqlNode, Function.identity()); fail(); } catch (JdbcException expected) { System.out.println(expected.getMessage()); assertEquals(Message.DOMA2123, expected.getMessageResource()); } }
public void testBindVariableNode() throws Exception { SelectClauseNode select = new SelectClauseNode("select"); select.appendNode(OtherNode.of(" * ")); FromClauseNode from = new FromClauseNode("from"); from.appendNode(WhitespaceNode.of(" ")); from.appendNode(new WordNode("aaa")); from.appendNode(WhitespaceNode.of(" ")); WhereClauseNode where = new WhereClauseNode("where"); where.appendNode(WhitespaceNode.of(" ")); where.appendNode(new WordNode("bbb")); where.appendNode(OtherNode.of(" = ")); BindVariableNode variable1 = new BindVariableNode(location, "name", "/*#name*/"); variable1.setWordNode(new WordNode("'hoge'")); where.appendNode(variable1); where.appendNode(WhitespaceNode.of(" ")); where.appendNode(new LogicalOperatorNode("and")); where.appendNode(WhitespaceNode.of(" ")); where.appendNode(new WordNode("ccc")); where.appendNode(OtherNode.of(" = ")); BindVariableNode variable2 = new BindVariableNode(location, "salary", "/*#salary*/"); variable2.setWordNode(new WordNode("100")); where.appendNode(variable2); SelectStatementNode statement = new SelectStatementNode(); statement.setSelectClauseNode(select); statement.setFromClauseNode(from); statement.setWhereClauseNode(where); AnonymousNode root = new AnonymousNode(); root.appendNode(statement); ExpressionEvaluator evaluator = new ExpressionEvaluator(); evaluator.add("name", new Value(String.class, "hoge")); evaluator.add("salary", new Value(BigDecimal.class, new BigDecimal(100))); NodePreparedSqlBuilder builder = new NodePreparedSqlBuilder( config, SqlKind.SELECT, "dummyPath", evaluator, SqlLogType.FORMATTED); PreparedSql sql = builder.build(root, Function.identity()); assertEquals("select * from aaa where bbb = ? and ccc = ?", sql.getRawSql()); assertEquals(2, sql.getParameters().size()); assertEquals("hoge", sql.getParameters().get(0).getWrapper().get()); assertEquals(new BigDecimal(100), sql.getParameters().get(1).getWrapper().get()); }