Skip to content

Commit

Permalink
Review:
Browse files Browse the repository at this point in the history
Move toLower only compare methods
Add a more complex test with mix cases in SQL using where and order by
  • Loading branch information
strehle committed Sep 11, 2024
1 parent c8ba9da commit 6316d78
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ public List<E> subList(int fromIndex, int toIndex) {
}

private String getCountSql(String sql) {
String result = UaaStringUtils.getValidatedString(sql).toLowerCase();
if (result.startsWith("select") && result.contains(" from ")) {
result = String.format("select count(*) %s", result.substring(result.indexOf(" from ")));
String result = UaaStringUtils.getValidatedString(sql);
if (result.toLowerCase().startsWith("select") && result.toLowerCase().contains(" from ")) {
result = String.format("select count(*) %s", result.substring(result.toLowerCase().indexOf(" from ")));
}
int orderByPos = result.lastIndexOf("order by");
int orderByPos = result.toLowerCase().lastIndexOf("order by");
if (orderByPos >= 0) {
result = result.substring(0, orderByPos);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public void initJdbcPagingListTests(@Autowired DataSource dataSource) {
jdbcTemplate.execute("insert into foo (id, name) values (2, 'baz')");
jdbcTemplate.execute("insert into foo (id, name) values (3, 'zab')");
jdbcTemplate.execute("insert into foo (id, name) values (4, 'rab')");
jdbcTemplate.execute("insert into foo (id, name) values (5, 'FoO')");

}

Expand Down Expand Up @@ -114,6 +115,17 @@ void selectColumnsFull() {
assertEquals("zab", map.get("name"));
}

@Test
void selectMoreColumnsWithOrderBy() {
list = new JdbcPagingList<>(jdbcTemplate, limitSqlAdapter, "SELECT Foo.id, FOO.NAME FrOm foo wHere foo.name = 'FoO' OR foo.name = 'foo' OrDeR By foo.name", new ColumnMapRowMapper(), 3);
Map<String, Object> map = list.get(0);
assertNotNull(map.get("name"));
assertEquals("FoO", map.get("name"));
map = list.get(1);
assertNotNull(map.get("name"));
assertEquals("foo", map.get("name"));
}

@Test
void testWrongStatement() {
assertThrows(BadSqlGrammarException.class,
Expand Down

0 comments on commit 6316d78

Please sign in to comment.