Skip to content

[FEATURE] CCJSqlParserUtil.parse(sqlStr) to something generic, in order to get table name #2036

@KafkaProServerless

Description

@KafkaProServerless

Hello team,

This is my first post in this repository.
If not anything else, I just wanted to say thank you for this project.
It is clear, easy to use, and interesting.

Just wanted to reach out with an enhancement request.
What I am trying to do, is to get table names from different types of statements (not just select)

The example from the project, which is a select statement, is very clear.

String sqlStr = "select 1 from dual where a=b";

PlainSelect select = (PlainSelect) CCJSqlParserUtil.parse(sqlStr);

SelectItem selectItem =
        select.getSelectItems().get(0);
Assertions.assertEquals(
        new LongValue(1)
        , selectItem.getExpression());

Table table = (Table) select.getFromItem();
Assertions.assertEquals("dual", table.getName());

However, this is only for select. As I need to scale this to other verbs, such as drop, insert, alter, analyze, etc... (just not want to write them all here, but imagine a lot of other verbs)

I ended up with an ugly and horrible code block which looks like this.

            if (sqlStr.toLowerCase(Locale.ROOT).startsWith("select")) {
                try {
                    PlainSelect select = (PlainSelect) CCJSqlParserUtil.parse(sqlStr);
                    Table table = (Table) select.getFromItem();
                    System.out.println("the table name for select statement " + table.getName() + " " + sqlStr);
                } catch (Exception e) {
                    System.err.println("issue with select statement " + sqlStr);
                }
            }
            if (sqlStr.toLowerCase(Locale.ROOT).startsWith("drop")) {
                try {
                    Drop drop = (Drop) CCJSqlParserUtil.parse(sqlStr);
                    Table table = drop.getName();
                    System.out.println("the table name for drop statement " + table.getName() + " " + sqlStr);
                } catch (Exception e) {
                    System.err.println("issue with drop statement " + sqlStr);
                }
            }
            if (sqlStr.toLowerCase(Locale.ROOT).startsWith("insert")) {
                try {
                    Insert insert = (Insert) CCJSqlParserUtil.parse(sqlStr);
                    Table table = insert.getTable();
                    System.out.println("the table name for insert statement " + table.getName() + " " + sqlStr);
                } catch (Exception e) {
                    System.err.println("issue with select statement " + sqlStr);
                }
            }
            if (sqlStr.toLowerCase(Locale.ROOT).startsWith("alter")) {
                try {
                    Alter alter = (Alter) CCJSqlParserUtil.parse(sqlStr);
                    Table table = alter.getTable();
                    System.out.println("the table name for alter statement " + table.getName() + " " + sqlStr);
                } catch (Exception e) {
                    System.err.println("issue with alter statement " + sqlStr);
                }
            }
            if (sqlStr.toLowerCase(Locale.ROOT).startsWith("analyze")) {
                try {
                    Analyze analyze = (Analyze) CCJSqlParserUtil.parse(sqlStr);
                    Table table = analyze.getTable();
                    System.out.println("the table name for analyze statement " + table.getName() + " " + sqlStr);
                } catch (Exception e) {
                    System.err.println("issue with analyze statement " + sqlStr);
                }
            }
... the list is long here...

This code is confusing, especially I feel I am repeating

(TheVerbNeeded) statement = (TheVerbNeeded) CCJSqlParserUtil.parse(sqlStr);
Table table = drop.getName();

Would it be possible to have a piece of code, something like this, which would scale verbs other than select?

Statement statement = CCJSqlParserUtil.parse(sqlStr); // no need to cast here
Table table = select.getTable(); //no need to cast, no need to choose between .getFromItem(), .getTable(), .getName(), but an unified API
Assertions.assertEquals("tablename", table.getName());

Thank you for your time.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions