File tree Expand file tree Collapse file tree 3 files changed +79
-0
lines changed Expand file tree Collapse file tree 3 files changed +79
-0
lines changed Original file line number Diff line number Diff line change @@ -841,6 +841,16 @@ pub enum Statement {
841
841
/// deleted along with the dropped table
842
842
purge : bool ,
843
843
} ,
844
+ /// SET [ SESSION | LOCAL ] ROLE role_name
845
+ ///
846
+ /// Note: this is a PostgreSQL-specific statement,
847
+ /// but may also compatible with other SQL.
848
+ SetRole {
849
+ local : bool ,
850
+ // SESSION is the default if neither SESSION nor LOCAL appears.
851
+ session : bool ,
852
+ role_name : Option < Ident > ,
853
+ } ,
844
854
/// SET <variable>
845
855
///
846
856
/// Note: this is not a standard SQL statement, but it is supported by at
@@ -1453,6 +1463,24 @@ impl fmt::Display for Statement {
1453
1463
if * cascade { " CASCADE" } else { "" } ,
1454
1464
if * purge { " PURGE" } else { "" }
1455
1465
) ,
1466
+ Statement :: SetRole {
1467
+ local,
1468
+ session,
1469
+ role_name,
1470
+ } => {
1471
+ write ! (
1472
+ f,
1473
+ "SET {local}{session}ROLE" ,
1474
+ local = if * local { "LOCAL " } else { "" } ,
1475
+ session = if * session { "SESSION " } else { "" } ,
1476
+ ) ?;
1477
+ if let Some ( role_name) = role_name {
1478
+ write ! ( f, " {}" , role_name) ?;
1479
+ } else {
1480
+ f. write_str ( " NONE" ) ?;
1481
+ }
1482
+ Ok ( ( ) )
1483
+ }
1456
1484
Statement :: SetVariable {
1457
1485
local,
1458
1486
variable,
Original file line number Diff line number Diff line change @@ -3116,7 +3116,19 @@ impl<'a> Parser<'a> {
3116
3116
self . parse_one_of_keywords ( & [ Keyword :: SESSION , Keyword :: LOCAL , Keyword :: HIVEVAR ] ) ;
3117
3117
if let Some ( Keyword :: HIVEVAR ) = modifier {
3118
3118
self . expect_token ( & Token :: Colon ) ?;
3119
+ } else if self . parse_keyword ( Keyword :: ROLE ) {
3120
+ let role_name = if self . parse_keyword ( Keyword :: NONE ) {
3121
+ None
3122
+ } else {
3123
+ Some ( self . parse_identifier ( ) ?)
3124
+ } ;
3125
+ return Ok ( Statement :: SetRole {
3126
+ local : modifier == Some ( Keyword :: LOCAL ) ,
3127
+ session : modifier == Some ( Keyword :: SESSION ) ,
3128
+ role_name,
3129
+ } ) ;
3119
3130
}
3131
+
3120
3132
let variable = self . parse_identifier ( ) ?;
3121
3133
if self . consume_token ( & Token :: Eq ) || self . parse_keyword ( Keyword :: TO ) {
3122
3134
let mut values = vec ! [ ] ;
Original file line number Diff line number Diff line change @@ -855,6 +855,45 @@ fn parse_set() {
855
855
) ;
856
856
}
857
857
858
+ #[ test]
859
+ fn parse_set_role ( ) {
860
+ let stmt = pg_and_generic ( ) . verified_stmt ( "SET SESSION ROLE NONE" ) ;
861
+ assert_eq ! (
862
+ stmt,
863
+ Statement :: SetRole {
864
+ local: false ,
865
+ session: true ,
866
+ role_name: None ,
867
+ }
868
+ ) ;
869
+
870
+ let stmt = pg_and_generic ( ) . verified_stmt ( "SET LOCAL ROLE \" rolename\" " ) ;
871
+ assert_eq ! (
872
+ stmt,
873
+ Statement :: SetRole {
874
+ local: true ,
875
+ session: false ,
876
+ role_name: Some ( Ident {
877
+ value: "rolename" . to_string( ) ,
878
+ quote_style: Some ( '\"' ) ,
879
+ } ) ,
880
+ }
881
+ ) ;
882
+
883
+ let stmt = pg_and_generic ( ) . verified_stmt ( "SET ROLE 'rolename'" ) ;
884
+ assert_eq ! (
885
+ stmt,
886
+ Statement :: SetRole {
887
+ local: false ,
888
+ session: false ,
889
+ role_name: Some ( Ident {
890
+ value: "rolename" . to_string( ) ,
891
+ quote_style: Some ( '\'' ) ,
892
+ } ) ,
893
+ }
894
+ ) ;
895
+ }
896
+
858
897
#[ test]
859
898
fn parse_show ( ) {
860
899
let stmt = pg_and_generic ( ) . verified_stmt ( "SHOW a a" ) ;
You can’t perform that action at this time.
0 commit comments