I am using jdbi3 with StringTemplate 4 templating engine, I have this test query:
@SqlQuery("select * from test "
+ "where field1 = 5"
+ "<if(cond1)> or field2 \\<= :value1<endif>"
+ "<if(cond2)> or field2 >= :value2<endif>"
+ "<if(cond3)> or field2 in (<values>)<endif>")
@RegisterBeanMapper(Test.class)
@UseStringTemplateEngine
public List<Test> selectTest(
@Define("cond1") boolean cond1, @Bind("value1") int value2,
@Define("cond2") boolean cond2, @Bind("value2") int value3,
@Define("cond3") boolean cond3,
@BindList(value="values", onEmpty=BindList.EmptyHandling.NULL_STRING ) List<Integer> values);
Using StringTemplate engine when I have to escape with \ the characters < or > in the query?
Testing I found that I have to escape <= in the query like I did.
In the IN clause using @BindList I have to use the <values> but in this case I was expecting to escape it like \\<values> otherwise it will be used as attribute by StringTemplate but if I do this the query doesn't work.
About >= escaping it or not seems the same in the query.
Introduction
Let's consider:
3.27.1Jdbi version as the current Jdbi version.4.3.1StringTemplate version as the current StringTemplate version, since the current Jdbi version uses it. Please, see: jdbi/pom.xml at v3.27.1 · jdbi/jdbi.Answer
Jdbi
Documentation: Which characters to escape
Please, note the warning on which characters to escape:
Unit-test: Do not escape
@BindListvariable namePlease, see the
BindListTest.ifValueGivenWithNullValueOptionThenResultIsTruthy()test method: jdbi/BindListTest.java at v3.27.1 · jdbi/jdbi.Please, note that the test covers a very similar annotated method:
Please, note that the
@BindListvariable name is not escaped:StringTemplate
Documentation: Which characters to escape
Please, note which characters to escape: