Skip to content

Commit 182f484

Browse files
authored
fix: tables not find in parentheses join sql. (#1956)
* fix: tables not find in parentheses join sql. * fix: test unit fix * fix: code format * fix: remove import * * fix: remove import * in unit test
1 parent 98aa90c commit 182f484

File tree

3 files changed

+49
-34
lines changed

3 files changed

+49
-34
lines changed

src/main/java/net/sf/jsqlparser/parser/AbstractJSqlParser.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
*/
1010
package net.sf.jsqlparser.parser;
1111

12-
import java.util.ArrayList;
13-
import java.util.List;
14-
1512
import net.sf.jsqlparser.parser.feature.Feature;
1613
import net.sf.jsqlparser.parser.feature.FeatureConfiguration;
1714

15+
import java.util.ArrayList;
16+
import java.util.List;
17+
1818
public abstract class AbstractJSqlParser<P> {
1919

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

2828
public P withAllowComplexParsing(boolean allowComplexParsing) {
29-
return withFeature(Feature.allowComplexParsing, allowComplexParsing);
29+
return withFeature(Feature.allowComplexParsing, allowComplexParsing);
3030
}
3131

3232
public P withUnsupportedStatements(boolean allowUnsupportedStatements) {
@@ -40,7 +40,7 @@ public P withTimeOut(long timeOutMillSeconds) {
4040
public P withBackslashEscapeCharacter(boolean allowBackslashEscapeCharacter) {
4141
return withFeature(Feature.allowBackslashEscapeCharacter, allowBackslashEscapeCharacter);
4242
}
43-
43+
4444
public P withFeature(Feature f, boolean enabled) {
4545
getConfiguration().setValue(f, enabled);
4646
return me();

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

+23-18
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@
176176
import net.sf.jsqlparser.statement.update.Update;
177177
import net.sf.jsqlparser.statement.upsert.Upsert;
178178

179+
179180
/**
180181
* Find all used tables within an select statement.
181182
*
@@ -294,15 +295,7 @@ public void visit(PlainSelect plainSelect) {
294295
plainSelect.getFromItem().accept(this);
295296
}
296297

297-
if (plainSelect.getJoins() != null) {
298-
for (Join join : plainSelect.getJoins()) {
299-
join.getFromItem().accept(this);
300-
join.getRightItem().accept(this);
301-
for (Expression expression : join.getOnExpressions()) {
302-
expression.accept(this);
303-
}
304-
}
305-
}
298+
visitJoins(plainSelect.getJoins());
306299
if (plainSelect.getWhere() != null) {
307300
plainSelect.getWhere().accept(this);
308301
}
@@ -807,15 +800,7 @@ public void visit(Delete delete) {
807800
}
808801
}
809802

810-
if (delete.getJoins() != null) {
811-
for (Join join : delete.getJoins()) {
812-
join.getFromItem().accept(this);
813-
join.getRightItem().accept(this);
814-
for (Expression expression : join.getOnExpressions()) {
815-
expression.accept(this);
816-
}
817-
}
818-
}
803+
visitJoins(delete.getJoins());
819804

820805
if (delete.getWhere() != null) {
821806
delete.getWhere().accept(this);
@@ -1033,6 +1018,26 @@ public void visit(UseStatement use) {
10331018
@Override
10341019
public void visit(ParenthesedFromItem parenthesis) {
10351020
parenthesis.getFromItem().accept(this);
1021+
// support join keyword in fromItem
1022+
visitJoins(parenthesis.getJoins());
1023+
}
1024+
1025+
/**
1026+
* visit join block
1027+
*
1028+
* @param parenthesis join sql block
1029+
*/
1030+
private void visitJoins(List<Join> parenthesis) {
1031+
if (parenthesis == null) {
1032+
return;
1033+
}
1034+
for (Join join : parenthesis) {
1035+
join.getFromItem().accept(this);
1036+
join.getRightItem().accept(this);
1037+
for (Expression expression : join.getOnExpressions()) {
1038+
expression.accept(this);
1039+
}
1040+
}
10361041
}
10371042

10381043
@Override

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

+21-11
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,6 @@
99
*/
1010
package net.sf.jsqlparser.util;
1111

12-
import static org.assertj.core.api.Assertions.assertThat;
13-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
14-
import static org.junit.jupiter.api.Assertions.assertEquals;
15-
import static org.junit.jupiter.api.Assertions.assertNull;
16-
import static org.junit.jupiter.api.Assertions.assertTrue;
17-
18-
import java.io.BufferedReader;
19-
import java.io.InputStreamReader;
20-
import java.io.StringReader;
21-
import java.util.List;
22-
import java.util.Set;
2312
import net.sf.jsqlparser.JSQLParserException;
2413
import net.sf.jsqlparser.expression.OracleHint;
2514
import net.sf.jsqlparser.parser.CCJSqlParserManager;
@@ -36,6 +25,18 @@
3625
import org.junit.jupiter.api.Disabled;
3726
import org.junit.jupiter.api.Test;
3827

28+
import java.io.BufferedReader;
29+
import java.io.InputStreamReader;
30+
import java.io.StringReader;
31+
import java.util.List;
32+
import java.util.Set;
33+
34+
import static org.assertj.core.api.Assertions.assertThat;
35+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
36+
import static org.junit.jupiter.api.Assertions.assertEquals;
37+
import static org.junit.jupiter.api.Assertions.assertNull;
38+
import static org.junit.jupiter.api.Assertions.assertTrue;
39+
3940
public class TablesNamesFinderTest {
4041

4142
private static final CCJSqlParserManager PARSER_MANAGER = new CCJSqlParserManager();
@@ -522,4 +523,13 @@ void testRefreshMaterializedView() throws JSQLParserException {
522523
Set<String> tableNames6 = TablesNamesFinder.findTables(sqlStr6);
523524
assertThat(tableNames6).isEmpty();
524525
}
526+
527+
@Test
528+
void testFromParenthesesJoin() throws JSQLParserException {
529+
String sqlStr = "select * from (t1 left join t2 on t1.id = t2.id) t_select";
530+
Set<String> tables = TablesNamesFinder.findTables(sqlStr);
531+
assertThat(tables).containsExactly("t1", "t2");
532+
533+
}
525534
}
535+

0 commit comments

Comments
 (0)