Skip to content

Commit

Permalink
fixes #600
Browse files Browse the repository at this point in the history
  • Loading branch information
wumpz committed Apr 13, 2018
1 parent c28a549 commit e80ea68
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@

/**
* Find all used tables within an select statement.
*
* Override extractTableName method to modify the extracted table names (e.g. without schema).
*/
public class TablesNamesFinder implements SelectVisitor, FromItemVisitor, ExpressionVisitor, ItemsListVisitor, SelectItemVisitor, StatementVisitor {

Expand Down Expand Up @@ -218,9 +220,18 @@ public void visit(PlainSelect plainSelect) {
}
}

/**
* Override to adapt the tableName generation (e.g. with / without schema).
* @param table
* @return
*/
protected String extractTableName(Table table) {
return table.getFullyQualifiedName();
}

@Override
public void visit(Table tableName) {
String tableWholeName = tableName.getFullyQualifiedName();
String tableWholeName = extractTableName(tableName);
if (!otherItemNames.contains(tableWholeName.toLowerCase())
&& !tables.contains(tableWholeName)) {
tables.add(tableWholeName);
Expand Down Expand Up @@ -618,7 +629,7 @@ public void visit(ValueListExpression valueList) {

@Override
public void visit(Delete delete) {
tables.add(delete.getTable().getName());
visit(delete.getTable());

if (delete.getJoins() != null) {
for (Join join : delete.getJoins()) {
Expand All @@ -634,7 +645,7 @@ public void visit(Delete delete) {
@Override
public void visit(Update update) {
for (Table table : update.getTables()) {
tables.add(table.getName());
visit(table);
}
if (update.getExpressions() != null) {
for (Expression expression : update.getExpressions()) {
Expand All @@ -659,7 +670,7 @@ public void visit(Update update) {

@Override
public void visit(Insert insert) {
tables.add(insert.getTable().getName());
visit(insert.getTable());
if (insert.getItemsList() != null) {
insert.getItemsList().accept(this);
}
Expand All @@ -670,7 +681,7 @@ public void visit(Insert insert) {

@Override
public void visit(Replace replace) {
tables.add(replace.getTable().getName());
visit(replace.getTable());
if (replace.getExpressions() != null) {
for (Expression expression : replace.getExpressions()) {
expression.accept(this);
Expand Down Expand Up @@ -698,7 +709,7 @@ public void visit(CreateIndex createIndex) {

@Override
public void visit(CreateTable create) {
tables.add(create.getTable().getFullyQualifiedName());
visit(create.getTable());
if (create.getSelect() != null) {
create.getSelect().accept(this);
}
Expand Down Expand Up @@ -743,7 +754,7 @@ public void visit(HexValue hexValue) {

@Override
public void visit(Merge merge) {
tables.add(merge.getTable().getName());
visit(merge.getTable());
if (merge.getUsingTable() != null) {
merge.getUsingTable().accept(this);
} else if (merge.getUsingSelect() != null) {
Expand Down Expand Up @@ -780,7 +791,7 @@ public void visit(Commit commit) {

@Override
public void visit(Upsert upsert) {
tables.add(upsert.getTable().getName());
visit(upsert.getTable());
if (upsert.getItemsList() != null) {
upsert.getItemsList().accept(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,4 +506,13 @@ public void testMySQLValueListExpression() throws JSQLParserException {
assertTrue(tableList.contains("TABLE1"));
}

@Test
public void testSkippedSchemaIssue600() throws JSQLParserException {
String sql = "delete from schema.table where id = 1";
TablesNamesFinder finder = new TablesNamesFinder();
List<String> tableList = finder.getTableList(CCJSqlParserUtil.parse(sql));
assertEquals(1, tableList.size());
assertTrue(tableList.contains("schema.table"));
}

}

0 comments on commit e80ea68

Please sign in to comment.