Skip to content
Merged
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
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
32 changes: 21 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,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();
Expand Down Expand Up @@ -522,4 +523,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");

}
}