Skip to content

Commit

Permalink
fix: also replace column names in select
Browse files Browse the repository at this point in the history
  • Loading branch information
olavloite committed Oct 28, 2023
1 parent 722495c commit c352d3b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,13 @@ Tuple<Set<TableOrIndexName>, Statement> detectAndReplaceTables(
parseTableList(detectAndReplaceMap, replacedTables, detectedTablesBuilder, multipleTables)
|| detectedOrReplacedTable;
}
// Reset to start and search for the WHERE clause.
// Reset to start replace full-qualified column names of tables that have been replaced.
parser.setPos(0);
parser.parseExpressionUntilKeyword(ImmutableList.of("where"), true, false, false);
// Check whether we found a keyword.
if (parser.peekKeyword("where")) {
int positionBeforeWhereClause = parser.getPos();
String replaced =
replaceFullyQualifiedColumnNames(
detectAndReplaceMap,
replacedTables,
positionBeforeWhereClause,
parser.getSql().length());
if (replaced != null) {
parser.setSql(parser.getSql().substring(0, positionBeforeWhereClause) + replaced);
}
String replaced =
replaceFullyQualifiedColumnNames(
detectAndReplaceMap, replacedTables, 0, parser.getSql().length());
if (replaced != null) {
parser.setSql(replaced);
}

return detectedOrReplacedTable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,12 @@ public void testReplaceTables() {
"pg_type"));
assertEquals(
Statement.of(
"select * from pg_namespace "
"select pg_class.relname from pg_namespace "
+ "inner join pg_class on pg_namespace.oid = pg_class.relnamespace "
+ "where pg_class.relname = $1::VARCHAR AND pg_class.relkind = ANY (ARRAY[$2::VARCHAR, $3::VARCHAR, $4::VARCHAR, $5::VARCHAR, $6::VARCHAR]) AND true AND pg_namespace.nspname != $7::VARCHAR'"),
replace(
Statement.of(
"select * from pg_catalog.pg_namespace "
"select pg_catalog.pg_class.relname from pg_catalog.pg_namespace "
+ "inner join pg_catalog.pg_class on pg_catalog.pg_namespace.oid = pg_catalog.pg_class.relnamespace "
+ "where pg_catalog.pg_class.relname = $1::VARCHAR AND pg_catalog.pg_class.relkind = ANY (ARRAY[$2::VARCHAR, $3::VARCHAR, $4::VARCHAR, $5::VARCHAR, $6::VARCHAR]) AND true AND pg_catalog.pg_namespace.nspname != $7::VARCHAR'"),
ImmutableMap.of(
Expand Down

0 comments on commit c352d3b

Please sign in to comment.