Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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 @@ -167,6 +167,7 @@ public static void visit(CreateTable table, Consumer<Expression> expressions,
accept(table.getExecute().getExprList(), expressions);
}
accept(table.getTrailingLikeTable(), tables);
accept(table.getCloneTable(), tables);
accept(table.getPartitionOf(), tables);
visit(table.getPartitioning(), expressions);
visit(table.getPartitionBound(), expressions);
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,47 @@ private <S> void visitTargetTable(Table table, S context) {
withItemsInScope = scope;
}

/**
* The tables listed between DELETE and FROM (T-SQL {@code DELETE t FROM t JOIN u ...}, MySQL
* {@code DELETE t1, t2 FROM t1 JOIN t2 ...}) are the ones rows are deleted from. An entry
* naming the alias of a FROM item stands for that item, which is visited with the FROM clause.
*/
private <S> void visitDeleteTargets(Delete delete, S context) {
if (delete.getTables() != null) {
for (Table target : delete.getTables()) {
if (!isAliasInDelete(delete, target.getFullyQualifiedName())) {
visitTargetTable(target, context);
}
}
}
}

private static boolean isAliasInDelete(Delete delete, String name) {
boolean found = hasAlias(delete.getTable(), name) || hasAlias(delete.getJoins(), name)
|| hasAlias(delete.getUsingJoins(), name);
if (!found && delete.getUsingFromItemList() != null) {
for (FromItem usingFromItem : delete.getUsingFromItemList()) {
found |= hasAlias(usingFromItem, name);
}
}
return found;
}

private static boolean hasAlias(List<Join> joins, String name) {
boolean found = false;
if (joins != null) {
for (Join join : joins) {
found |= hasAlias(join.getRightItem(), name);
}
}
return found;
}

private static boolean hasAlias(FromItem fromItem, String name) {
return fromItem != null && fromItem.getAlias() != null
&& name.equalsIgnoreCase(fromItem.getAlias().getName());
}

@Override
public <S> Void visit(WithItem<?> withItem, S context) {
if (withItem.getAlias() != null) {
Expand Down Expand Up @@ -1601,6 +1642,7 @@ public <S> Void visit(MySQLGroupConcat groupConcat, S context) {
public <S> Void visit(Delete delete, S context) {
visitWithItems(delete.getWithItemsList(), context);
visitTargetTable(delete.getTable(), context);
visitDeleteTargets(delete, context);

if (delete.getUsingFromItemList() != null) {
for (FromItem usingFromItem : delete.getUsingFromItemList()) {
Expand Down Expand Up @@ -2401,6 +2443,7 @@ public void visit(Commit commit) {
@Override
public <S> Void visit(Upsert upsert, S context) {
visit(upsert.getTable(), context);
visitUpdateSets(upsert.getUpdateSets(), context);
if (upsert.getExpressions() != null) {
upsert.getExpressions().accept(this, context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import net.sf.jsqlparser.statement.upsert.Upsert;
import net.sf.jsqlparser.util.validation.ValidationCapability;

import java.util.List;

/**
* @author gitmotte
*/
Expand All @@ -28,9 +30,10 @@ public void validate(Upsert upsert) {
}
validateOptionalFromItem(upsert.getTable());
validateOptionalExpressions(upsert.getColumns());
validateUpdateSets(upsert.getUpdateSets());
validateOptionalExpressions(upsert.getExpressions());
validateOptionalSelect(upsert.getSelect());
validateDuplicate(upsert);
validateUpdateSets(upsert.getDuplicateUpdateSets());
}

private void validateOptionalSelect(Select select) {
Expand All @@ -40,9 +43,9 @@ private void validateOptionalSelect(Select select) {
}
}

private void validateDuplicate(Upsert upsert) {
if (upsert.getDuplicateUpdateSets() != null) {
for (UpdateSet updateSet : upsert.getDuplicateUpdateSets()) {
private void validateUpdateSets(List<UpdateSet> updateSets) {
if (updateSets != null) {
for (UpdateSet updateSet : updateSets) {
validateOptionalExpressions(updateSet.getColumns());
validateOptionalExpressions(updateSet.getValues());
}
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/net/sf/jsqlparser/util/TablesNamesFinderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,38 @@ void testSubqueryInDeclareIsFound(String sqlStr) throws JSQLParserException {
assertThat(TablesNamesFinder.findTables(sqlStr)).containsExactly("secrets");
}

@ParameterizedTest
@ValueSource(strings = {
// T-SQL and MySQL list the tables to delete from in front of FROM
"DELETE secrets FROM t",
"DELETE secrets FROM t WHERE t.id = 1",
"DELETE secrets OUTPUT deleted.* FROM t",
"DELETE secrets, t FROM secrets JOIN t ON secrets.id = t.id",
// an entry naming the alias of a FROM item is that item, not a table
"DELETE s FROM secrets s JOIN t ON s.id = t.id",
"DELETE s FROM t JOIN secrets AS s ON s.id = t.id",
"DELETE S, t FROM t JOIN secrets s ON s.id = t.id"})
void testDeleteTargetListIsFound(String sqlStr) throws JSQLParserException {
assertThat(TablesNamesFinder.findTables(sqlStr)).containsExactlyInAnyOrder("secrets", "t");
}

@ParameterizedTest
@ValueSource(strings = {
"UPSERT INTO t SET a = (SELECT max(pw) FROM secrets)",
"UPSERT INTO t SET a = 1, (b, c) = (SELECT x, y FROM secrets), d = 4"})
void testSubqueryInUpsertSetIsFound(String sqlStr) throws JSQLParserException {
assertThat(TablesNamesFinder.findTables(sqlStr)).containsExactlyInAnyOrder("t", "secrets");
}

@ParameterizedTest
@ValueSource(strings = {
"CREATE TABLE copy CLONE secrets",
"CREATE SNAPSHOT TABLE copy CLONE secrets FOR SYSTEM_TIME AS OF TIMESTAMP '2020-01-01'"})
void testCloneSourceIsFound(String sqlStr) throws JSQLParserException {
assertThat(TablesNamesFinder.findTables(sqlStr)).containsExactlyInAnyOrder("copy",
"secrets");
}

@Test
void testManyDerivedNamesDoNotScaleQuadratically() {
// Every plain table reference used to be matched against the CTE aliases with
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,11 @@ public void testValidationExecuteNotAllowed() throws Exception {
}
}

@Test
public void testValidateSetAssignmentsOfUpsert() {
String sql = "UPSERT INTO t SET a = f(1), b = 2";
validateNotAllowed(sql, 1, 1,
new FeaturesAllowed(Feature.values()).remove(Feature.function), Feature.function);
}

}
Loading