gretard / sonar-sql-plugin

SQL language (T-SQL, MySQL, Snowflake, Vertica and PostgreSQL dialects) plugin for SonarQube
GNU General Public License v3.0
79 stars 21 forks source link

Problem parsing pssql dialect #24

Closed verbacom2 closed 2 years ago

verbacom2 commented 3 years ago

Hi gertard,

I´m trying to create custom rules with the sql plugin but I find me with the next issue:

java -jar rulesHelper.jar print text "CREATE USER user1;" pssql

Printing tree:

RootContext: @(1:0,1:6) with text: CREATE :CREATEUSERuser1;
        StmtContext: @(1:0,1:6) with text: CREATE :CREATEUSERuser1
                Create_stmtContext: @(1:0,1:6) with text: CREATE :CREATEUSERuser1
                        ErrorNodeImpl: @(1:0,1:6) with text: CREATE :CREATE
                        ErrorNodeImpl: @(1:7,1:11) with text: USER :USER
                        ErrorNodeImpl: @(1:12,1:17) with text: user1 :user1
        TerminalNodeImpl: @(1:17,1:18) with text: ; :;
        StmtContext: @(1:18,1:23) with text: <EOF> :

I think I should get a tree output contains a line with Create_user_stmtContext statement and not the message "ErrorNodeImpl".

The same issue is getting with "create tablespace" statement.

java -jar rulesHelper.jar print text "CREATE SCHEMA myschema;" pssql

Printing tree:

RootContext: @(1:0,1:6) with text: CREATE :CREATESCHEMAmyschema;<EOF>
        StmtContext: @(1:0,1:6) with text: CREATE :CREATESCHEMAmyschema;
                Create_stmtContext: @(1:0,1:6) with text: CREATE :CREATESCHEMAmyschema;
                        Create_schema_stmtContext: @(1:0,1:6) with text: CREATE :CREATESCHEMAmyschema;
                                TerminalNodeImpl: @(1:0,1:6) with text: CREATE :CREATE
                                TerminalNodeImpl: @(1:7,1:13) with text: SCHEMA :SCHEMA
                                IdentifierContext: @(1:14,1:22) with text: myschema :myschema
                                        TerminalNodeImpl: @(1:14,1:22) with text: myschema :myschema
                                Todo_fill_inContext: @(1:22,1:23) with text: ; :;
                                        TerminalNodeImpl: @(1:22,1:23) with text: ; :;
        TerminalNodeImpl: @(1:23,1:28) with text: <EOF> :<EOF>

Could you help with this issue?

Thanks and regards.

gretard commented 2 years ago

Hello,

I am sorry to inform, that this is actually a bug in ANTLR4 grammar. Currently used grammar is from https://github.com/tshprecher/antlr_psql. I see that there is more recent grammar at https://github.com/antlr/grammars-v4/tree/master/sql/postgresql, however I have not tried to integrate it yet.

In the meanwhile - you can try using Create_stmtContext class and check if some text is present (i.e. USER).

gretard commented 2 years ago

Please check latest release.