Skip to content

Commit 11cebcf

Browse files
fix: TablesNamesFinder UpdateSets
- fixes #2028 Signed-off-by: Andreas Reichel <andreas@manticore-projects.com>
1 parent e0ad7c8 commit 11cebcf

File tree

2 files changed

+61
-5
lines changed

2 files changed

+61
-5
lines changed

src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@
172172
import net.sf.jsqlparser.statement.show.ShowTablesStatement;
173173
import net.sf.jsqlparser.statement.truncate.Truncate;
174174
import net.sf.jsqlparser.statement.update.Update;
175+
import net.sf.jsqlparser.statement.update.UpdateSet;
175176
import net.sf.jsqlparser.statement.upsert.Upsert;
176177

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

390391
@Override
391392
public void visit(Table tableName) {
392-
FromItemVisitor.super.visit(tableName);
393+
this.visit(tableName, null);
393394
}
394395

395396
@Override
@@ -984,21 +985,24 @@ public void visit(Delete delete) {
984985

985986
@Override
986987
public <S> Void visit(Update update, S context) {
987-
visit(update.getTable(), context);
988988
if (update.getWithItemsList() != null) {
989989
for (WithItem withItem : update.getWithItemsList()) {
990990
withItem.accept((SelectVisitor<?>) this, context);
991991
}
992992
}
993993

994+
visit(update.getTable(), context);
995+
994996
if (update.getStartJoins() != null) {
995997
for (Join join : update.getStartJoins()) {
996998
join.getRightItem().accept(this, context);
997999
}
9981000
}
999-
if (update.getExpressions() != null) {
1000-
for (Expression expression : update.getExpressions()) {
1001-
expression.accept(this, context);
1001+
1002+
if (update.getUpdateSets() != null) {
1003+
for (UpdateSet updateSet : update.getUpdateSets()) {
1004+
updateSet.getColumns().accept(this, context);
1005+
updateSet.getValues().accept(this, context);
10021006
}
10031007
}
10041008

src/test/java/net/sf/jsqlparser/util/TablesNamesFinderTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import net.sf.jsqlparser.test.TestUtils;
2222
import org.junit.jupiter.api.Test;
2323

24+
import java.util.Arrays;
25+
import java.util.List;
2426
import java.util.Set;
2527

2628
import static org.assertj.core.api.Assertions.assertThat;
@@ -509,5 +511,55 @@ void testSubqueryAliasesIssue2035() throws JSQLParserException {
509511
tables = TablesNamesFinder.findTables(sqlStr);
510512
assertThat(tables).containsExactlyInAnyOrder("B", "C");
511513
}
514+
515+
@Test
516+
void testTableRenamingIssue2028() throws JSQLParserException {
517+
List<String> IGNORE_SCHEMAS =
518+
Arrays.asList("mysql", "information_schema", "performance_schema");
519+
final String prefix = "test_";
520+
521+
//@formatter:off
522+
String sql =
523+
"UPDATE table_1 a\n" +
524+
"SET a.a1 = ( SELECT b1\n" +
525+
" FROM table_2 b\n" +
526+
" WHERE b.xx = 'xx' )\n" +
527+
" , a.a2 = ( SELECT b2\n" +
528+
" FROM table_2 b\n" +
529+
" WHERE b.yy = 'yy' )\n" +
530+
";";
531+
String expected =
532+
"UPDATE test_table_1 a\n" +
533+
"SET a.a1 = ( SELECT b1\n" +
534+
" FROM test_table_2 b\n" +
535+
" WHERE b.xx = 'xx' )\n" +
536+
" , a.a2 = ( SELECT b2\n" +
537+
" FROM test_table_2 b\n" +
538+
" WHERE b.yy = 'yy' )\n" +
539+
";";
540+
//@formatter:on
541+
542+
TablesNamesFinder finder = new TablesNamesFinder<Void>() {
543+
@Override
544+
public <S> Void visit(Table tableName, S context) {
545+
String schemaName = tableName.getSchemaName();
546+
if (schemaName != null && IGNORE_SCHEMAS.contains(schemaName.toLowerCase())) {
547+
return super.visit(tableName, context);
548+
}
549+
String originTableName = tableName.getName();
550+
tableName.setName(prefix + originTableName);
551+
if (originTableName.startsWith("`")) {
552+
tableName.setName("`" + prefix + originTableName.replace("`", "") + "`");
553+
}
554+
return super.visit(tableName, context);
555+
}
556+
};
557+
finder.init(false);
558+
559+
Statement statement = CCJSqlParserUtil.parse(sql);
560+
statement.accept(finder);
561+
562+
TestUtils.assertStatementCanBeDeparsedAs(statement, expected, true);
563+
}
512564
}
513565

0 commit comments

Comments
 (0)