Skip to content

Commit 41b29fb

Browse files
committed
Implement no object as none
1 parent 528709c commit 41b29fb

File tree

4 files changed

+61
-25
lines changed

4 files changed

+61
-25
lines changed

src/ast/mod.rs

+26-7
Original file line numberDiff line numberDiff line change
@@ -3224,7 +3224,7 @@ pub enum Statement {
32243224
/// ```
32253225
Grant {
32263226
privileges: Privileges,
3227-
objects: GrantObjects,
3227+
objects: Option<GrantObjects>,
32283228
grantees: Vec<Grantee>,
32293229
with_grant_option: bool,
32303230
granted_by: Option<Ident>,
@@ -3234,7 +3234,7 @@ pub enum Statement {
32343234
/// ```
32353235
Revoke {
32363236
privileges: Privileges,
3237-
objects: GrantObjects,
3237+
objects: Option<GrantObjects>,
32383238
grantees: Vec<Grantee>,
32393239
granted_by: Option<Ident>,
32403240
cascade: Option<CascadeOption>,
@@ -4779,7 +4779,7 @@ impl fmt::Display for Statement {
47794779
granted_by,
47804780
} => {
47814781
write!(f, "GRANT {privileges} ")?;
4782-
if !matches!(objects, GrantObjects::None) {
4782+
if let Some(objects) = objects {
47834783
write!(f, "ON {objects} ")?;
47844784
}
47854785
write!(f, "TO {}", display_comma_separated(grantees))?;
@@ -4799,7 +4799,9 @@ impl fmt::Display for Statement {
47994799
cascade,
48004800
} => {
48014801
write!(f, "REVOKE {privileges} ")?;
4802-
write!(f, "ON {objects} ")?;
4802+
if let Some(objects) = objects {
4803+
write!(f, "ON {objects} ")?;
4804+
}
48034805
write!(f, "FROM {}", display_comma_separated(grantees))?;
48044806
if let Some(grantor) = granted_by {
48054807
write!(f, " GRANTED BY {grantor}")?;
@@ -5888,14 +5890,20 @@ pub enum GrantObjects {
58885890
AllSequencesInSchema { schemas: Vec<ObjectName> },
58895891
/// Grant privileges on `ALL TABLES IN SCHEMA <schema_name> [, ...]`
58905892
AllTablesInSchema { schemas: Vec<ObjectName> },
5893+
/// Grant privileges on specific databases
5894+
Databases(Vec<ObjectName>),
58915895
/// Grant privileges on specific schemas
58925896
Schemas(Vec<ObjectName>),
58935897
/// Grant privileges on specific sequences
58945898
Sequences(Vec<ObjectName>),
58955899
/// Grant privileges on specific tables
58965900
Tables(Vec<ObjectName>),
5897-
/// e.g. GRANT ROLE x TO USER y, the privilege is the role itself and the object is none
5898-
None,
5901+
/// Grant privileges on specific views
5902+
Views(Vec<ObjectName>),
5903+
/// Grant privileges on specific warehouses
5904+
Warehouses(Vec<ObjectName>),
5905+
/// Grant privileges on specific integrations
5906+
Integrations(Vec<ObjectName>),
58995907
}
59005908

59015909
impl fmt::Display for GrantObjects {
@@ -5904,12 +5912,24 @@ impl fmt::Display for GrantObjects {
59045912
GrantObjects::Sequences(sequences) => {
59055913
write!(f, "SEQUENCE {}", display_comma_separated(sequences))
59065914
}
5915+
GrantObjects::Databases(databases) => {
5916+
write!(f, "DATABASE {}", display_comma_separated(databases))
5917+
}
59075918
GrantObjects::Schemas(schemas) => {
59085919
write!(f, "SCHEMA {}", display_comma_separated(schemas))
59095920
}
59105921
GrantObjects::Tables(tables) => {
59115922
write!(f, "{}", display_comma_separated(tables))
59125923
}
5924+
GrantObjects::Views(views) => {
5925+
write!(f, "VIEW {}", display_comma_separated(views))
5926+
}
5927+
GrantObjects::Warehouses(warehouses) => {
5928+
write!(f, "WAREHOUSE {}", display_comma_separated(warehouses))
5929+
}
5930+
GrantObjects::Integrations(integrations) => {
5931+
write!(f, "INTEGRATION {}", display_comma_separated(integrations))
5932+
}
59135933
GrantObjects::AllSequencesInSchema { schemas } => {
59145934
write!(
59155935
f,
@@ -5924,7 +5944,6 @@ impl fmt::Display for GrantObjects {
59245944
display_comma_separated(schemas)
59255945
)
59265946
}
5927-
GrantObjects::None => Ok(()),
59285947
}
59295948
}
59305949
}

src/parser/mod.rs

+17-9
Original file line numberDiff line numberDiff line change
@@ -12105,7 +12105,7 @@ impl<'a> Parser<'a> {
1210512105

1210612106
pub fn parse_grant_revoke_privileges_objects(
1210712107
&mut self,
12108-
) -> Result<(Privileges, GrantObjects), ParserError> {
12108+
) -> Result<(Privileges, Option<GrantObjects>), ParserError> {
1210912109
let privileges = if self.parse_keyword(Keyword::ALL) {
1211012110
Privileges::All {
1211112111
with_privileges_keyword: self.parse_keyword(Keyword::PRIVILEGES),
@@ -12117,35 +12117,43 @@ impl<'a> Parser<'a> {
1211712117

1211812118
let objects = if self.parse_keyword(Keyword::ON) {
1211912119
if self.parse_keywords(&[Keyword::ALL, Keyword::TABLES, Keyword::IN, Keyword::SCHEMA]) {
12120-
GrantObjects::AllTablesInSchema {
12120+
Some(GrantObjects::AllTablesInSchema {
1212112121
schemas: self.parse_comma_separated(|p| p.parse_object_name(false))?,
12122-
}
12122+
})
1212312123
} else if self.parse_keywords(&[
1212412124
Keyword::ALL,
1212512125
Keyword::SEQUENCES,
1212612126
Keyword::IN,
1212712127
Keyword::SCHEMA,
1212812128
]) {
12129-
GrantObjects::AllSequencesInSchema {
12129+
Some(GrantObjects::AllSequencesInSchema {
1213012130
schemas: self.parse_comma_separated(|p| p.parse_object_name(false))?,
12131-
}
12131+
})
1213212132
} else {
1213312133
let object_type = self.parse_one_of_keywords(&[
1213412134
Keyword::SEQUENCE,
12135+
Keyword::DATABASE,
1213512136
Keyword::SCHEMA,
1213612137
Keyword::TABLE,
12138+
Keyword::VIEW,
12139+
Keyword::WAREHOUSE,
12140+
Keyword::INTEGRATION,
1213712141
]);
1213812142
let objects =
1213912143
self.parse_comma_separated(|p| p.parse_object_name_with_wildcards(false, true));
1214012144
match object_type {
12141-
Some(Keyword::SCHEMA) => GrantObjects::Schemas(objects?),
12142-
Some(Keyword::SEQUENCE) => GrantObjects::Sequences(objects?),
12143-
Some(Keyword::TABLE) | None => GrantObjects::Tables(objects?),
12145+
Some(Keyword::DATABASE) => Some(GrantObjects::Databases(objects?)),
12146+
Some(Keyword::SCHEMA) => Some(GrantObjects::Schemas(objects?)),
12147+
Some(Keyword::SEQUENCE) => Some(GrantObjects::Sequences(objects?)),
12148+
Some(Keyword::WAREHOUSE) => Some(GrantObjects::Warehouses(objects?)),
12149+
Some(Keyword::INTEGRATION) => Some(GrantObjects::Integrations(objects?)),
12150+
Some(Keyword::VIEW) => Some(GrantObjects::Views(objects?)),
12151+
Some(Keyword::TABLE) | None => Some(GrantObjects::Tables(objects?)),
1214412152
_ => unreachable!(),
1214512153
}
1214612154
}
1214712155
} else {
12148-
GrantObjects::None
12156+
None
1214912157
};
1215012158

1215112159
Ok((privileges, objects))

tests/sqlparser_common.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -8587,7 +8587,7 @@ fn parse_grant() {
85878587
granted_by,
85888588
..
85898589
} => match (privileges, objects) {
8590-
(Privileges::Actions(actions), GrantObjects::Tables(objects)) => {
8590+
(Privileges::Actions(actions), Some(GrantObjects::Tables(objects))) => {
85918591
assert_eq!(
85928592
vec![
85938593
Action::Select { columns: None },
@@ -8637,7 +8637,7 @@ fn parse_grant() {
86378637
with_grant_option,
86388638
..
86398639
} => match (privileges, objects) {
8640-
(Privileges::Actions(actions), GrantObjects::AllTablesInSchema { schemas }) => {
8640+
(Privileges::Actions(actions), Some(GrantObjects::AllTablesInSchema { schemas })) => {
86418641
assert_eq!(vec![Action::Insert { columns: None }], actions);
86428642
assert_eq_vec(&["public"], &schemas);
86438643
assert_eq_vec(&["browser"], &grantees);
@@ -8657,7 +8657,7 @@ fn parse_grant() {
86578657
granted_by,
86588658
..
86598659
} => match (privileges, objects, granted_by) {
8660-
(Privileges::Actions(actions), GrantObjects::Sequences(objects), None) => {
8660+
(Privileges::Actions(actions), Some(GrantObjects::Sequences(objects)), None) => {
86618661
assert_eq!(
86628662
vec![Action::Usage, Action::Select { columns: None }],
86638663
actions
@@ -8694,7 +8694,7 @@ fn parse_grant() {
86948694
Privileges::All {
86958695
with_privileges_keyword,
86968696
},
8697-
GrantObjects::Schemas(schemas),
8697+
Some(GrantObjects::Schemas(schemas)),
86988698
) => {
86998699
assert!(!with_privileges_keyword);
87008700
assert_eq_vec(&["aa", "b"], &schemas);
@@ -8711,7 +8711,10 @@ fn parse_grant() {
87118711
objects,
87128712
..
87138713
} => match (privileges, objects) {
8714-
(Privileges::Actions(actions), GrantObjects::AllSequencesInSchema { schemas }) => {
8714+
(
8715+
Privileges::Actions(actions),
8716+
Some(GrantObjects::AllSequencesInSchema { schemas }),
8717+
) => {
87158718
assert_eq!(vec![Action::Usage], actions);
87168719
assert_eq_vec(&["bus"], &schemas);
87178720
}
@@ -8737,7 +8740,7 @@ fn test_revoke() {
87378740
match verified_stmt(sql) {
87388741
Statement::Revoke {
87398742
privileges,
8740-
objects: GrantObjects::Tables(tables),
8743+
objects: Some(GrantObjects::Tables(tables)),
87418744
grantees,
87428745
granted_by,
87438746
cascade,
@@ -8763,7 +8766,7 @@ fn test_revoke_with_cascade() {
87638766
match all_dialects_except(|d| d.is::<MySqlDialect>()).verified_stmt(sql) {
87648767
Statement::Revoke {
87658768
privileges,
8766-
objects: GrantObjects::Tables(tables),
8769+
objects: Some(GrantObjects::Tables(tables)),
87678770
grantees,
87688771
granted_by,
87698772
cascade,

tests/sqlparser_mysql.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -3046,7 +3046,10 @@ fn parse_grant() {
30463046
);
30473047
assert_eq!(
30483048
objects,
3049-
GrantObjects::Tables(vec![ObjectName::from(vec!["*".into(), "*".into()])])
3049+
Some(GrantObjects::Tables(vec![ObjectName::from(vec![
3050+
"*".into(),
3051+
"*".into()
3052+
])]))
30503053
);
30513054
assert!(!with_grant_option);
30523055
assert!(granted_by.is_none());
@@ -3087,7 +3090,10 @@ fn parse_revoke() {
30873090
);
30883091
assert_eq!(
30893092
objects,
3090-
GrantObjects::Tables(vec![ObjectName::from(vec!["db1".into(), "*".into()])])
3093+
Some(GrantObjects::Tables(vec![ObjectName::from(vec![
3094+
"db1".into(),
3095+
"*".into()
3096+
])]))
30913097
);
30923098
if let [Grantee {
30933099
grantee_type: GranteesType::None,

0 commit comments

Comments
 (0)