Skip to content

Finding Table Names in Insert, Create Table and Drop Table Statements #134

@holzgraefe

Description

@holzgraefe

Hi,

I recently started using JSqlParser and I think it's really a great project.
My first goal was to parse some SQL files to extract all table names using TablesNamesFinder. This worked fine except for Insert and Create Table statements, for which the table names from select statements didn't get included. I came up with the following workaround for TablesNamesFinder.java:

/**
* Main entry for this Tool class. A list of found tables is returned.
*
* @param insert
* @return
*/
public List getTableList(Insert insert) {
init();
tables.add(insert.getTable().getFullyQualifiedName());
if (insert.getItemsList() != null) {
insert.getItemsList().accept(this);
}
if(insert.getSelect() != null) {
if (insert.getSelect().getWithItemsList() != null) {
for (WithItem withItem : insert.getSelect().getWithItemsList()) {
withItem.accept(this);
}
}
insert.getSelect().getSelectBody().accept(this);
}

    return tables;
}

/**
 * Main entry for this Tool class. A list of found tables is returned.
 *
 * @param create
 * @return
 */
public List<String> getTableList(CreateTable create) {
    init();
    tables.add(create.getTable().getFullyQualifiedName());
    if(create.getSelect() != null) {
        if (create.getSelect().getWithItemsList() != null) {
            for (WithItem withItem : create.getSelect().getWithItemsList()) {
                withItem.accept(this);
            }
        }
        create.getSelect().getSelectBody().accept(this);
    }

    return tables;
}

It's not very pretty, but maybe somebody has a better idea.

Secondly, I noticed that quoted table names are not allowed in drop table statements, so something like
drop table "schema"."table";
throws an error.
I looked into JSqlParserCC.jj and tried to fool around with the definition of the drop statement there, but I didn't manage to fix this yet, mainly because I have no clue about the grammar definition. Is this easy to fix?

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions