Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replace regex usage with simple search #2866

Merged
merged 7 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
Expand Down Expand Up @@ -73,7 +74,7 @@
this.sql = sql;
this.args = args;
this.mapper = mapper;
this.size = parameterJdbcTemplate.queryForObject(getCountSql(sql), args, Integer.class);
this.size = Optional.ofNullable(parameterJdbcTemplate.queryForObject(getCountSql(sql), args, Integer.class)).orElse(0);
strehle marked this conversation as resolved.
Dismissed
Show resolved Hide resolved
this.pageSize = pageSize;
this.limitSqlAdapter = limitSqlAdapter;
}
Expand All @@ -92,20 +93,23 @@

@Override
public Iterator<E> iterator() {
return new SafeIterator<E>(super.iterator());
return new SafeIterator<>(super.iterator());
}

@Override
public List<E> subList(int fromIndex, int toIndex) {
if (fromIndex < 0 || toIndex > size || fromIndex > toIndex) {
throw new IndexOutOfBoundsException("The indexes provided are outside the bounds of this list.");
}
return new SafeIteratorList<E>(super.subList(fromIndex, toIndex));
return new SafeIteratorList<>(super.subList(fromIndex, toIndex));
}

private String getCountSql(String sql) {
String result = sql.replaceAll("(?i)select (.*?) from (.*)", "select count(*) from $2");
int orderByPos = result.toLowerCase().lastIndexOf("order by");
String result = sql.toLowerCase();
if (result.startsWith("select") && result.contains(" from ")) {
result = String.format("select count(*) %s", result.substring(result.indexOf(" from ")));
}
int orderByPos = result.lastIndexOf("order by");
if (orderByPos >= 0) {
result = result.substring(0, orderByPos);
}
Expand Down Expand Up @@ -143,7 +147,7 @@

@Override
public Iterator<T> iterator() {
return new SafeIterator<T>(super.iterator());
return new SafeIterator<>(super.iterator());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.jdbc.core.ColumnMapRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

Expand Down Expand Up @@ -105,6 +106,23 @@ void jumpOverPages() {
assertNotNull(map.get("name"));
}

@Test
void selectColumnsFull() {
strehle marked this conversation as resolved.
Show resolved Hide resolved
list = new JdbcPagingList<>(jdbcTemplate, limitSqlAdapter, "SELECT Foo.id, FOO.NAME from foo", new ColumnMapRowMapper(), 3);
Map<String, Object> map = list.get(3);
assertNotNull(map.get("name"));
assertEquals("zab", map.get("name"));
}

@Test
void testWrongStatement() {
assertThrows(BadSqlGrammarException.class,
() -> new JdbcPagingList<>(jdbcTemplate, limitSqlAdapter, "Insert ('6', 'sab') from foo", new ColumnMapRowMapper(), 3));

assertThrows(BadSqlGrammarException.class,
() -> new JdbcPagingList<>(jdbcTemplate, limitSqlAdapter, "SELECT * ", new ColumnMapRowMapper(), 3));
}

@Test
void iterationOverSubList() {
list = new JdbcPagingList<Map<String, Object>>(jdbcTemplate, limitSqlAdapter, "SELECT * from foo",
Expand Down
Loading