KonnexionsGmbH / sqlparse

LALR grammar based SQL Parser
Other
43 stars 15 forks source link

GRANT statement: Fix contradictions between sqlparse and Oracle syntax #82

Closed walter-weinmann closed 6 years ago

walter-weinmann commented 7 years ago

This solves the issues #22 , #39 and removes the keywords JAVA and RESOURCE. The new grammar definition is parse tree compatible with the existing grammar.


Oracle 12c grammar (simplified):

Oracle Database SQL Language Reference

grant -> GRANT grant_object_privileges | grant_system_privileges

grant_object_privileges -> ( object_privilege | 'ALL' 'PRIVILEGES'? ) ( '(' column_commalist ')' )? on_obj_clause
                           'TO' grantee_clause ( 'WITH' 'HIERARCHY' 'OPTION' )? ( 'WITH' 'GRANT' 'OPTION' )?

object_privilege -> 'ALTER'
                  | 'DELETE'
                  | 'INDEX'
                  | 'INSERT'
                  | 'REFERENCES'
                  | 'SELECT'
                  | 'UPDATE'
                  | ( 'All? 'PRIVILEGES'? )

on_obj_clause -> 'ON' ( ( ( schema )? object ) | user_commalist | 'DIRECTORY' directory_name )

grantee_clause -> ( ( user ( 'IDENTIFIED' 'BY' 'PASSWORD' ) ) | role | 'PUBLIC' )+

grant_system_privileges -> ( ( system_privilege | role | ( 'ALL' 'PRIVILEGES' ) )+ 'TO' grantee_clause ( 'WITH' 'ADMIN' 'OPTION' )?

system_privilege -> 'ADMIN'
                  | 'ALTER' 'ANY' 'INDEX'
                  | 'ALTER' 'ANY' 'MATERIALIZED' 'VIEW'
                  | 'ALTER' 'ANY' 'TABLE'
                  | 'ALTER' 'ANY' 'VIEW'
                  | 'CREATE' 'ANY' 'INDEX'
                  | 'CREATE' 'ANY' 'MATERIALIZED' 'VIEW'
                  | 'CREATE' 'ANY' 'TABLE'
                  | 'CREATE' 'ANY' 'VIEW'
                  | 'CREATE' 'MATERIALIZED' 'VIEW'
                  | 'CREATE' 'TABLE'
                  | 'CREATE' 'VIEW'
                  | 'DELETE' 'ANY' 'TABLE'
                  | 'DROP' 'ANY' 'INDEX'
                  | 'DROP' 'ANY' 'MATERIALIZED' 'VIEW'
                  | 'DROP' 'ANY' 'TABLE'
                  | 'DROP' 'ANY' 'VIEW'
                  | 'INSERT' 'ANY' 'TABLE'
                  | 'SELECT' 'ANY' 'TABLE'
                  | 'UPDATE' 'ANY' 'TABLE'

grant::= image

grant_system_privileges::= image

grantee_clause::= image

grantee_identified_by::= image

grant_object_privileges::= image

on_object_clause ::= image


Current sqlparse grammar:

grant_def -> GRANT                                     TO grantee_commalist                     : {grant, [],   {on, <<"">>}, {to, '$3'}, ''}.
grant_def -> GRANT                                     TO grantee_commalist with_grant_option   : {grant, [],   {on, <<"">>}, {to, '$3'}, '$4'}.
grant_def -> GRANT                       on_obj_clause TO grantee_commalist                     : {grant, [],   '$2',         {to, '$4'}, ''}.
grant_def -> GRANT                       on_obj_clause TO grantee_commalist with_grant_option   : {grant, [],   '$2',         {to, '$4'}, '$5'}.
grant_def -> GRANT system_privilege_list               TO grantee_commalist                     : {grant, '$2', {on, <<"">>}, {to, '$4'}, ''}.
grant_def -> GRANT system_privilege_list               TO grantee_commalist with_grant_option   : {grant, '$2', {on, <<"">>}, {to, '$4'}, '$5'}.
grant_def -> GRANT system_privilege_list on_obj_clause TO grantee_commalist                     : {grant, '$2', '$3',         {to, '$5'}, ''}.
grant_def -> GRANT system_privilege_list on_obj_clause TO grantee_commalist with_grant_option   : {grant, '$2', '$3',         {to, '$5'}, '$6'}.

on_obj_clause -> ON DIRECTORY NAME                                                              : {'on directory',     unwrap_bin('$3')}.
on_obj_clause -> ON JAVA SOURCE   table                                                         : {'on java source',   '$4'}.
on_obj_clause -> ON JAVA RESOURCE table                                                         : {'on java resource', '$4'}.
on_obj_clause -> ON table                                                                       : {on, '$2'}.

system_privilege_list -> ALL                                                                    : ['all'].
system_privilege_list -> ALL PRIVILEGES                                                         : ['all privileges'].
system_privilege_list -> system_privilege                                                       : ['$1'].
system_privilege_list -> system_privilege ',' system_privilege_list                             : ['$1'|'$3'].

system_privilege -> SELECT                                                                      : 'select'.
system_privilege -> UPDATE                                                                      : 'update'.
system_privilege -> DELETE                                                                      : 'delete'.
system_privilege -> INSERT                                                                      : 'insert'.
system_privilege -> DROP                                                                        : 'drop'.
system_privilege -> NAME                                                                        : strl2atom(['$1']).
system_privilege -> NAME NAME                                                                   : strl2atom(['$1', '$2']).
system_privilege -> NAME NAME NAME                                                              : strl2atom(['$1', '$2', '$3']).
system_privilege -> NAME NAME NAME NAME                                                         : strl2atom(['$1', '$2', '$3', '$4']).
system_privilege -> NAME NAME NAME NAME NAME                                                    : strl2atom(['$1', '$2', '$3', '$4', '$5']).

with_grant_option -> WITH GRANT     OPTION                                                      : 'with grant option'.
with_grant_option -> WITH HIERARCHY OPTION                                                      : 'with hierarchy option'.
with_grant_option -> WITH NAME      OPTION                                                      : strl2atom(["with", '$2', "option"]).

grantee_commalist ->                       grantee                                              :         ['$1'].
grantee_commalist -> grantee_commalist ',' grantee                                              : '$1' ++ ['$3'].

grantee -> NAME                                                                                 : unwrap_bin('$1').
grantee -> NAME IDENTIFIED BY NAME                                                              : {'identified by', unwrap_bin('$1'), unwrap_bin('$4')}.
grantee -> PUBLIC                                                                               : 'public'.

New sqlparse grammar:

grant_def -> GRANT object_privilege      on_obj_clause TO grantee_commalist                     : {grant, '$2', '$3',         {to, '$5'}, ''}.
grant_def -> GRANT object_privilege      on_obj_clause TO grantee_commalist object with_grant_option   
                                                                                                : {grant, '$2', '$3',         {to, '$5'}, '$6'}.
grant_def -> GRANT system_privilege_list               TO grantee_commalist                     : {grant, '$2', {on, <<"">>}, {to, '$4'}, ''}.
grant_def -> GRANT system_privilege_list               TO grantee_commalist system_with_grant_oprion
                                                                                                : {grant, '$2', {on, <<"">>}, {to, '$4'}, '$5'}.

grantee -> NAME                                                                                 : unwrap_bin('$1').
grantee -> NAME IDENTIFIED BY STRING                                                            : {'identified by', unwrap_bin('$1'), unwrap_bin('$4')}.
grantee -> PUBLIC                                                                               : 'public'.

grantee_commalist ->                       grantee                                              :         ['$1'].
grantee_commalist -> grantee_commalist ',' grantee                                              : '$1' ++ ['$3'].

object_privilege -> ALL                                                                         : 'all'.
object_privilege -> ALL PRIVILEGES                                                              : 'all privileges'.
object_privilege -> ALTER                                                                       : 'alter'.
object_privilege -> DELETE                                                                      : 'delete'.
object_privilege -> INDEX                                                                       : 'index'.
object_privilege -> INSERT                                                                      : 'insert'.
object_privilege -> REFERENCES                                                                  : 'references'.
object_privilege -> SELECT                                                                      : 'select'.
object_privilege -> UPDATE                                                                      : 'update'.

object_with_grant_option -> WITH GRANT     OPTION                                               : 'with grant option'.
object_with_grant_option -> WITH HIERARCHY OPTION                                               : 'with hierarchy option'.

on_obj_clause -> ON DIRECTORY NAME                                                              : {'on directory', unwrap_bin('$3')}.
on_obj_clause -> ON table                                                                       : {on, '$2'}.

system_privilege -> ADMIN                                                                       : 'admin'.
system_privilege -> ALL                                                                         : 'all'.
system_privilege -> ALL PRIVILEGES                                                              : 'all privileges'.
system_privilege -> ALTER ANY INDEX                                                             : 'alter any index'.
system_privilege -> ALTER ANY MATERIALIZED VIEW                                                 : 'alter any materialized view'.
system_privilege -> ALTER ANY TABLE                                                             : 'alter any table'.
system_privilege -> ALTER ANY VIEW                                                              : 'alter any view'.
system_privilege -> CREATE ANY INDEX                                                            : 'create any index'.
system_privilege -> CREATE ANY MATERIALIZED VIEW                                                : 'create any materialized view'.
system_privilege -> CREATE ANY TABLE                                                            : 'create any table'.
system_privilege -> CREATE ANY VIEW                                                             : 'create any view'.
system_privilege -> CREATE MATERIALIZED VIEW                                                    : 'create materialized view'.
system_privilege -> CREATE TABLE                                                                : 'create table'.
system_privilege -> CREATE VIEW                                                                 : 'create view'.
system_privilege -> DELETE ANY TABLE                                                            : 'delete any table'.
system_privilege -> DROP ANY INDEX                                                              : 'drop any index'.
system_privilege -> DROP ANY MATERIALIZED VIEW                                                  : 'drop any materialized view'.
system_privilege -> DROP ANY TABLE                                                              : 'drop any table'.
system_privilege -> DROP ANY VIEW                                                               : 'drop any view'.
system_privilege -> INSERT ANY TABLE                                                            : 'insert any table'.
system_privilege -> SELECT ANY TABLE                                                            : 'select any table'.
system_privilege -> UPDATE ANY TABLE                                                            : 'update any table'.
system_privilege -> NAME                                                                        : strl2atom(['$1']).

system_privilege_list -> system_privilege                                                       : ['$1'].
system_privilege_list -> system_privilege ',' system_privilege_list                             : ['$1'|'$3'].

system_with_grant_option -> WITH ADMIN    OPTION                                                : 'with admin option'.
system_with_grant_option -> WITH DELEGATE OPTION                                                : 'with delegate option'.
walter-weinmann commented 6 years ago

Solved with PR #101.