and u.active=${_true}
</if>
<if test="searchText != null">
- and (u.login like #{searchTextSql} or u.name like #{searchTextSql})
+ and (u.login like #{searchTextSql} escape '/' or u.name like #{searchTextSql} escape '/')
</if>
</where>
order by u.name
assertThat(users.get(0).getLogin()).isEqualTo("sbrandhof");
}
+ @Test
+ public void selectUsersByQuery_escape_special_characters_in_like() throws Exception {
+ setupData("selectUsersByText");
+
+ UserQuery query = UserQuery.builder().searchText("%s%").build();
+ // we expect really a login or name containing the 3 characters "%s%"
+
+ List<UserDto> users = dao.selectUsers(query);
+ assertThat(users).isEmpty();
+ }
+
@Test
public void selectGroupByName() {
setupData("selectGroupByName");
this.logins = builder.logins;
this.includeDeactivated = builder.includeDeactivated;
this.searchText = builder.searchText;
- this.searchTextSql = (searchText !=null ? "%" + searchText + "%" : null);
+
+ this.searchTextSql = searchTextToSql(searchText);
+ }
+
+ private String searchTextToSql(@Nullable String s) {
+ String sql = null;
+ if (s != null) {
+ sql = StringUtils.replace(s, "%", "/%");
+ sql = StringUtils.replace(sql, "_", "/_");
+ sql = "%" + sql + "%";
+ }
+ return sql;
}
@CheckForNull
}
@Test
- public void test_searchText() throws Exception {
+ public void searchText() throws Exception {
UserQuery query = UserQuery.builder().searchText("sim").build();
assertThat(query.searchText()).isEqualTo("sim");
assertThat(query.searchTextSql).isEqualTo("%sim%");
}
+
+ @Test
+ public void searchText_escape_special_characters_in_like() throws Exception {
+ UserQuery query = UserQuery.builder().searchText("%sim_").build();
+ assertThat(query.searchText()).isEqualTo("%sim_");
+ assertThat(query.searchTextSql).isEqualTo("%/%sim/_%");
+ }
}