Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Unanswered
KafkaProServerless asked this question in Q&A
Discussion options

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.

You must be logged in to vote

Replies: 1 comment

Comment options

Greetings,

please check out the TableNamesFinder Class. It provides your with what you need.
Check out its Unit Test cases for an illustration how to use it.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
Converted from issue

This discussion was converted from issue #2036 on July 15, 2024 07:14.

AltStyle によって変換されたページ (->オリジナル) /