Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] missing feature description show create table my_table #1790

Closed
foresta-yang opened this issue May 15, 2023 · 6 comments · Fixed by #1778
Closed

[FEATURE] missing feature description show create table my_table #1790

foresta-yang opened this issue May 15, 2023 · 6 comments · Fixed by #1778

Comments

@foresta-yang
Copy link

Grammar or Syntax Description

show create table my_table

SQL Example

show create table my_table

Additional context

The used JSQLParser Version (4.6).
net.sf.jsqlparser.parser.ParseException: Encountered unexpected token: "TABLE" "TABLE"
at line 1, column 13.

Was expecting one of:

<EOF>
<ST_SEMICOLON>
@manticore-projects
Copy link
Contributor

It seems to be MySQL specific and is not supported.
Why would you like to parse this kind of statement? What was the added value?

@foresta-yang
Copy link
Author

It seems to be MySQL specific and is not supported. Why would you like to parse this kind of statement? What was the added value?

Thanks for your reply.
We used jsqlParser as a syntax filtering check, but such statements cannot pass execution and instead throw an exception. We don't want to parse this statement but pass the syntax check instead of generating an exception and interrupt the process;

@manticore-projects
Copy link
Contributor

Ok, I get it and see your point.
Since it is impossible and too expensive to support all possible statements we really focus on statements, which add value in the meaning of "statement contains information".

For your particular use-case I would like to suggest a slightly different approach (assuming you will want to verify a long list of various statements from a text file):

  1. remove any comments based on a REGEX
  2. split the statements based on a REGEX into a collection single statement
  3. parse and verify statement by statement -- if JSQLParser throws an error, treat special (e. g. apply specific REGEX or discard)

The following 2 Regular expressions may be a good start:

public static final Pattern COMMENT_PATTERN = Pattern.compile("(?:'[^']*+')|(?:\\\"[^\\\"]*+\\\")"
      + "|(^/\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*/\\s?\\n?|/\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*/|--.*?\\r?[\\n])",
      Pattern.DOTALL | Pattern.MULTILINE | Pattern.UNIX_LINES);

public static final Pattern SQL_DELIMITER_SPLIT =
        Pattern.compile("((?:(?:'[^']*+')|(?:\"[^\"]*+\")|[^;])*+);");

Good luck!

@foresta-yang
Copy link
Author

Ok, I get it and see your point. Since it is impossible and too expensive to support all possible statements we really focus on statements, which add value in the meaning of "statement contains information".

For your particular use-case I would like to suggest a slightly different approach (assuming you will want to verify a long list of various statements from a text file):

  1. remove any comments based on a REGEX
  2. split the statements based on a REGEX into a collection single statement
  3. parse and verify statement by statement -- if JSQLParser throws an error, treat special (e. g. apply specific REGEX or discard)

The following 2 Regular expressions may be a good start:

public static final Pattern COMMENT_PATTERN = Pattern.compile("(?:'[^']*+')|(?:\\\"[^\\\"]*+\\\")"
      + "|(^/\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*/\\s?\\n?|/\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*/|--.*?\\r?[\\n])",
      Pattern.DOTALL | Pattern.MULTILINE | Pattern.UNIX_LINES);

public static final Pattern SQL_DELIMITER_SPLIT =
        Pattern.compile("((?:(?:'[^']*+')|(?:\"[^\"]*+\")|[^;])*+);");

Good luck!

Okay, what we are doing is a permission interception, which intercepts permissions through certain keywords in the statement. Although we can use exception handling methods, we are concerned about security issues that may arise.I should be able to find the best solution, thank you

@manticore-projects
Copy link
Contributor

Solved via UnsupportedStatement:

@Test
    void testShowCreateTable() throws JSQLParserException {
        String sqlStr =
                "show create table my_table";
        Statement statement = assertSqlCanBeParsedAndDeparsed(sqlStr, true);
        Assertions.assertTrue( statement instanceof UnsupportedStatement);
    }

@foresta-yang
Copy link
Author

Solved via UnsupportedStatement:

@Test
    void testShowCreateTable() throws JSQLParserException {
        String sqlStr =
                "show create table my_table";
        Statement statement = assertSqlCanBeParsedAndDeparsed(sqlStr, true);
        Assertions.assertTrue( statement instanceof UnsupportedStatement);
    }

I got it! Thank you very much. Although I have found other solutions, your support has still helped me a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants