diff --git a/src/main/java/net/sf/jsqlparser/parser/AbstractJSqlParser.java b/src/main/java/net/sf/jsqlparser/parser/AbstractJSqlParser.java index 771402b1a..13ebcd781 100644 --- a/src/main/java/net/sf/jsqlparser/parser/AbstractJSqlParser.java +++ b/src/main/java/net/sf/jsqlparser/parser/AbstractJSqlParser.java @@ -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

{ protected int jdbcParameterIndex = 0; @@ -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) { @@ -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(); diff --git a/src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java b/src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java index 7a94ceff7..4dc760925 100644 --- a/src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java +++ b/src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java @@ -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. * @@ -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); } @@ -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); @@ -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 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 diff --git a/src/test/java/net/sf/jsqlparser/util/TablesNamesFinderTest.java b/src/test/java/net/sf/jsqlparser/util/TablesNamesFinderTest.java index 2f9566088..ab63d1f9f 100644 --- a/src/test/java/net/sf/jsqlparser/util/TablesNamesFinderTest.java +++ b/src/test/java/net/sf/jsqlparser/util/TablesNamesFinderTest.java @@ -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; @@ -36,6 +25,18 @@ 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.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + public class TablesNamesFinderTest { private static final CCJSqlParserManager PARSER_MANAGER = new CCJSqlParserManager(); @@ -522,4 +523,13 @@ void testRefreshMaterializedView() throws JSQLParserException { Set 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 tables = TablesNamesFinder.findTables(sqlStr); + assertThat(tables).containsExactly("t1", "t2"); + + } } +