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: tables not find in parentheses join sql. #1956

Merged
merged 5 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
10 changes: 5 additions & 5 deletions src/main/java/net/sf/jsqlparser/parser/AbstractJSqlParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
*/
package net.sf.jsqlparser.parser;

import java.util.ArrayList;
import java.util.List;

import net.sf.jsqlparser.parser.feature.Feature;
import net.sf.jsqlparser.parser.feature.FeatureConfiguration;

import java.util.ArrayList;
import java.util.List;

public abstract class AbstractJSqlParser<P> {

protected int jdbcParameterIndex = 0;
Expand All @@ -26,7 +26,7 @@ public P withSquareBracketQuotation(boolean allowSquareBracketQuotation) {
}

public P withAllowComplexParsing(boolean allowComplexParsing) {
return withFeature(Feature.allowComplexParsing, allowComplexParsing);
return withFeature(Feature.allowComplexParsing, allowComplexParsing);
}

public P withUnsupportedStatements(boolean allowUnsupportedStatements) {
Expand All @@ -40,7 +40,7 @@ public P withTimeOut(long timeOutMillSeconds) {
public P withBackslashEscapeCharacter(boolean allowBackslashEscapeCharacter) {
return withFeature(Feature.allowBackslashEscapeCharacter, allowBackslashEscapeCharacter);
}

public P withFeature(Feature f, boolean enabled) {
getConfiguration().setValue(f, enabled);
return me();
Expand Down
41 changes: 23 additions & 18 deletions src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@
import net.sf.jsqlparser.statement.update.Update;
import net.sf.jsqlparser.statement.upsert.Upsert;


/**
* Find all used tables within an select statement.
*
Expand Down Expand Up @@ -294,15 +295,7 @@ public void visit(PlainSelect plainSelect) {
plainSelect.getFromItem().accept(this);
}

if (plainSelect.getJoins() != null) {
for (Join join : plainSelect.getJoins()) {
join.getFromItem().accept(this);
join.getRightItem().accept(this);
for (Expression expression : join.getOnExpressions()) {
expression.accept(this);
}
}
}
visitJoins(plainSelect.getJoins());
if (plainSelect.getWhere() != null) {
plainSelect.getWhere().accept(this);
}
Expand Down Expand Up @@ -807,15 +800,7 @@ public void visit(Delete delete) {
}
}

if (delete.getJoins() != null) {
for (Join join : delete.getJoins()) {
join.getFromItem().accept(this);
join.getRightItem().accept(this);
for (Expression expression : join.getOnExpressions()) {
expression.accept(this);
}
}
}
visitJoins(delete.getJoins());

if (delete.getWhere() != null) {
delete.getWhere().accept(this);
Expand Down Expand Up @@ -1033,6 +1018,26 @@ public void visit(UseStatement use) {
@Override
public void visit(ParenthesedFromItem parenthesis) {
parenthesis.getFromItem().accept(this);
// support join keyword in fromItem
visitJoins(parenthesis.getJoins());
}

/**
* visit join block
*
* @param parenthesis join sql block
*/
private void visitJoins(List<Join> parenthesis) {
if (parenthesis == null) {
return;
}
for (Join join : parenthesis) {
join.getFromItem().accept(this);
join.getRightItem().accept(this);
for (Expression expression : join.getOnExpressions()) {
expression.accept(this);
}
}
}

@Override
Expand Down
30 changes: 19 additions & 11 deletions src/test/java/net/sf/jsqlparser/util/TablesNamesFinderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,6 @@
*/
package net.sf.jsqlparser.util;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.List;
import java.util.Set;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.OracleHint;
import net.sf.jsqlparser.parser.CCJSqlParserManager;
Expand All @@ -36,6 +25,16 @@
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.List;
import java.util.Set;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.*;
Hanchers marked this conversation as resolved.
Show resolved Hide resolved
Hanchers marked this conversation as resolved.
Show resolved Hide resolved

public class TablesNamesFinderTest {

private static final CCJSqlParserManager PARSER_MANAGER = new CCJSqlParserManager();
Expand Down Expand Up @@ -522,4 +521,13 @@ void testRefreshMaterializedView() throws JSQLParserException {
Set<String> tableNames6 = TablesNamesFinder.findTables(sqlStr6);
assertThat(tableNames6).isEmpty();
}

@Test
void testFromParenthesesJoin() throws JSQLParserException {
String sqlStr = "select * from (t1 left join t2 on t1.id = t2.id) t_select";
Set<String> tables = TablesNamesFinder.findTables(sqlStr);
assertThat(tables).containsExactly("t1", "t2");

}
}