Skip to content

Commit

Permalink
fix: TablesNamesFinder UpdateSets
Browse files Browse the repository at this point in the history
- fixes #2028

Signed-off-by: Andreas Reichel <andreas@manticore-projects.com>
  • Loading branch information
manticore-projects committed Jul 11, 2024
1 parent e0ad7c8 commit 11cebcf
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@
import net.sf.jsqlparser.statement.show.ShowTablesStatement;
import net.sf.jsqlparser.statement.truncate.Truncate;
import net.sf.jsqlparser.statement.update.Update;
import net.sf.jsqlparser.statement.update.UpdateSet;
import net.sf.jsqlparser.statement.upsert.Upsert;

import java.util.ArrayList;
Expand Down Expand Up @@ -389,7 +390,7 @@ public <S> Void visit(Table tableName, S context) {

@Override
public void visit(Table tableName) {
FromItemVisitor.super.visit(tableName);
this.visit(tableName, null);
}

@Override
Expand Down Expand Up @@ -984,21 +985,24 @@ public void visit(Delete delete) {

@Override
public <S> Void visit(Update update, S context) {
visit(update.getTable(), context);
if (update.getWithItemsList() != null) {
for (WithItem withItem : update.getWithItemsList()) {
withItem.accept((SelectVisitor<?>) this, context);
}
}

visit(update.getTable(), context);

if (update.getStartJoins() != null) {
for (Join join : update.getStartJoins()) {
join.getRightItem().accept(this, context);
}
}
if (update.getExpressions() != null) {
for (Expression expression : update.getExpressions()) {
expression.accept(this, context);

if (update.getUpdateSets() != null) {
for (UpdateSet updateSet : update.getUpdateSets()) {
updateSet.getColumns().accept(this, context);
updateSet.getValues().accept(this, context);
}
}

Expand Down
52 changes: 52 additions & 0 deletions src/test/java/net/sf/jsqlparser/util/TablesNamesFinderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import net.sf.jsqlparser.test.TestUtils;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -509,5 +511,55 @@ void testSubqueryAliasesIssue2035() throws JSQLParserException {
tables = TablesNamesFinder.findTables(sqlStr);
assertThat(tables).containsExactlyInAnyOrder("B", "C");
}

@Test
void testTableRenamingIssue2028() throws JSQLParserException {
List<String> IGNORE_SCHEMAS =
Arrays.asList("mysql", "information_schema", "performance_schema");
final String prefix = "test_";

//@formatter:off
String sql =
"UPDATE table_1 a\n" +
"SET a.a1 = ( SELECT b1\n" +
" FROM table_2 b\n" +
" WHERE b.xx = 'xx' )\n" +
" , a.a2 = ( SELECT b2\n" +
" FROM table_2 b\n" +
" WHERE b.yy = 'yy' )\n" +
";";
String expected =
"UPDATE test_table_1 a\n" +
"SET a.a1 = ( SELECT b1\n" +
" FROM test_table_2 b\n" +
" WHERE b.xx = 'xx' )\n" +
" , a.a2 = ( SELECT b2\n" +
" FROM test_table_2 b\n" +
" WHERE b.yy = 'yy' )\n" +
";";
//@formatter:on

TablesNamesFinder finder = new TablesNamesFinder<Void>() {
@Override
public <S> Void visit(Table tableName, S context) {
String schemaName = tableName.getSchemaName();
if (schemaName != null && IGNORE_SCHEMAS.contains(schemaName.toLowerCase())) {
return super.visit(tableName, context);
}
String originTableName = tableName.getName();
tableName.setName(prefix + originTableName);
if (originTableName.startsWith("`")) {
tableName.setName("`" + prefix + originTableName.replace("`", "") + "`");
}
return super.visit(tableName, context);
}
};
finder.init(false);

Statement statement = CCJSqlParserUtil.parse(sql);
statement.accept(finder);

TestUtils.assertStatementCanBeDeparsedAs(statement, expected, true);
}
}

0 comments on commit 11cebcf

Please sign in to comment.