PlaceholderConfigurer extends ParametersParserBaseListener { public List<String> getQueries() throws SQLDataException { CharStream input = new ANTLRInputStream(sqlWithPlaceholders); ANTLRErrorListener errorListener = new InternalErrorListener(); ParametersLexer parametersLexer = new ParametersLexer(input); parametersLexer.removeErrorListeners(); parametersLexer.addErrorListener(errorListener); CommonTokenStream commonTokenStream = new CommonTokenStream(parametersLexer); ParametersParser parametersParser = new ParametersParser(commonTokenStream); parametersParser.removeErrorListeners(); parametersParser.addErrorListener(errorListener); ParseTree parseTree = parametersParser.queries(); ParseTreeWalker walker = new ParseTreeWalker(); walker.walk(this, parseTree); if (exception.isPresent()) { throw exception.get(); } return preparedQueries; } PlaceholderConfigurer(String sqlWithPlaceholders, List<Parameter> parameters); List<String> getQueries(); @Override void exitQuery(ParametersParser.QueryContext ctx); @Override void exitText(ParametersParser.TextContext ctx); @Override void exitPositional_placeholder(ParametersParser.Positional_placeholderContext ctx); @Override void exitNamed_placeholder(ParametersParser.Named_placeholderContext ctx);  }
@Test public void testQueryWithPositionalPlaceholder() throws Exception { String query = "SELECT * FROM some_table WHERE id = ? AND name = ?"; List<Parameter> parameters = Arrays.asList( createParameter(1, "42"), createParameter(2, "test-'name") ); PlaceholderConfigurer placeholderConfigurer = new PlaceholderConfigurer(query, parameters); List<String> queries = placeholderConfigurer.getQueries(); assertThat(queries, hasSize(1)); assertThat(queries.get(0), equalTo("SELECT * FROM some_table WHERE id = 42 AND name = 'test-\\'name'")); }
@Test public void testQueryWithNamedPlaceholder() throws Exception { String query = "SELECT * FROM some_table WHERE id = :some.id: AND name = :name:;"; List<Parameter> parameters = Arrays.asList( createParameter("some.id", "42"), createParameter("name", "test-\\name") ); PlaceholderConfigurer placeholderConfigurer = new PlaceholderConfigurer(query, parameters); List<String> queries = placeholderConfigurer.getQueries(); assertThat(queries, hasSize(1)); assertThat(queries.get(0), equalTo("SELECT * FROM some_table WHERE id = 42 AND name = 'test-\\\\name'")); }
@Test public void testMultipleQueries() throws Exception { String query = "SELECT * FROM some_table WHERE name = :name:;\nSELECT id, name FROM another_table WHERE enabled = ?"; List<Parameter> parameters = Arrays.asList( createParameter("name", "test-;name"), createParameter(1, "1") ); PlaceholderConfigurer placeholderConfigurer = new PlaceholderConfigurer(query, parameters); List<String> queries = placeholderConfigurer.getQueries(); assertThat(queries, hasSize(2)); assertThat(queries.get(0), equalTo("SELECT * FROM some_table WHERE name = 'test-\\;name'")); assertThat(queries.get(1), equalTo("SELECT id, name FROM another_table WHERE enabled = 1")); }
@Test public void testPlaceholderInLiteral() throws Exception { String query = "SELECT * FROM some_table WHERE id = '?' name = ':name:'"; PlaceholderConfigurer placeholderConfigurer = new PlaceholderConfigurer(query, Collections.emptyList()); List<String> queries = placeholderConfigurer.getQueries(); assertThat(queries, hasSize(1)); assertThat(queries.get(0), equalTo(query)); }
@Test(expected = SQLDataException.class) public void testMissingPositionalParameter() throws Exception { String query = "SELECT * FROM some_table WHERE name = ?"; new PlaceholderConfigurer(query, Collections.emptyList()).getQueries(); }
@Test(expected = SQLDataException.class) public void testMissingNamedParameter() throws Exception { String query = "SELECT * FROM some_table WHERE name = :name:"; new PlaceholderConfigurer(query, Collections.emptyList()).getQueries(); }
@Test(expected = SQLDataException.class) public void testInvalidQuery() throws Exception { String query = "? SELECT * FROM some_table WHERE 1 = 1"; new PlaceholderConfigurer(query, Collections.emptyList()).getQueries(); }