mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-09-26 23:49:10 +00:00
Support 'SET ROLE' statement (#455)
This commit is contained in:
parent
7480e89f7b
commit
8f207db059
3 changed files with 79 additions and 0 deletions
|
@ -841,6 +841,16 @@ pub enum Statement {
|
||||||
/// deleted along with the dropped table
|
/// deleted along with the dropped table
|
||||||
purge: bool,
|
purge: bool,
|
||||||
},
|
},
|
||||||
|
/// SET [ SESSION | LOCAL ] ROLE role_name
|
||||||
|
///
|
||||||
|
/// Note: this is a PostgreSQL-specific statement,
|
||||||
|
/// but may also compatible with other SQL.
|
||||||
|
SetRole {
|
||||||
|
local: bool,
|
||||||
|
// SESSION is the default if neither SESSION nor LOCAL appears.
|
||||||
|
session: bool,
|
||||||
|
role_name: Option<Ident>,
|
||||||
|
},
|
||||||
/// SET <variable>
|
/// SET <variable>
|
||||||
///
|
///
|
||||||
/// Note: this is not a standard SQL statement, but it is supported by at
|
/// Note: this is not a standard SQL statement, but it is supported by at
|
||||||
|
@ -1453,6 +1463,24 @@ impl fmt::Display for Statement {
|
||||||
if *cascade { " CASCADE" } else { "" },
|
if *cascade { " CASCADE" } else { "" },
|
||||||
if *purge { " PURGE" } else { "" }
|
if *purge { " PURGE" } else { "" }
|
||||||
),
|
),
|
||||||
|
Statement::SetRole {
|
||||||
|
local,
|
||||||
|
session,
|
||||||
|
role_name,
|
||||||
|
} => {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"SET {local}{session}ROLE",
|
||||||
|
local = if *local { "LOCAL " } else { "" },
|
||||||
|
session = if *session { "SESSION " } else { "" },
|
||||||
|
)?;
|
||||||
|
if let Some(role_name) = role_name {
|
||||||
|
write!(f, " {}", role_name)?;
|
||||||
|
} else {
|
||||||
|
f.write_str(" NONE")?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
Statement::SetVariable {
|
Statement::SetVariable {
|
||||||
local,
|
local,
|
||||||
variable,
|
variable,
|
||||||
|
|
|
@ -3116,7 +3116,19 @@ impl<'a> Parser<'a> {
|
||||||
self.parse_one_of_keywords(&[Keyword::SESSION, Keyword::LOCAL, Keyword::HIVEVAR]);
|
self.parse_one_of_keywords(&[Keyword::SESSION, Keyword::LOCAL, Keyword::HIVEVAR]);
|
||||||
if let Some(Keyword::HIVEVAR) = modifier {
|
if let Some(Keyword::HIVEVAR) = modifier {
|
||||||
self.expect_token(&Token::Colon)?;
|
self.expect_token(&Token::Colon)?;
|
||||||
|
} else if self.parse_keyword(Keyword::ROLE) {
|
||||||
|
let role_name = if self.parse_keyword(Keyword::NONE) {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(self.parse_identifier()?)
|
||||||
|
};
|
||||||
|
return Ok(Statement::SetRole {
|
||||||
|
local: modifier == Some(Keyword::LOCAL),
|
||||||
|
session: modifier == Some(Keyword::SESSION),
|
||||||
|
role_name,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
let variable = self.parse_identifier()?;
|
let variable = self.parse_identifier()?;
|
||||||
if self.consume_token(&Token::Eq) || self.parse_keyword(Keyword::TO) {
|
if self.consume_token(&Token::Eq) || self.parse_keyword(Keyword::TO) {
|
||||||
let mut values = vec![];
|
let mut values = vec![];
|
||||||
|
|
|
@ -855,6 +855,45 @@ fn parse_set() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_set_role() {
|
||||||
|
let stmt = pg_and_generic().verified_stmt("SET SESSION ROLE NONE");
|
||||||
|
assert_eq!(
|
||||||
|
stmt,
|
||||||
|
Statement::SetRole {
|
||||||
|
local: false,
|
||||||
|
session: true,
|
||||||
|
role_name: None,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
let stmt = pg_and_generic().verified_stmt("SET LOCAL ROLE \"rolename\"");
|
||||||
|
assert_eq!(
|
||||||
|
stmt,
|
||||||
|
Statement::SetRole {
|
||||||
|
local: true,
|
||||||
|
session: false,
|
||||||
|
role_name: Some(Ident {
|
||||||
|
value: "rolename".to_string(),
|
||||||
|
quote_style: Some('\"'),
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
let stmt = pg_and_generic().verified_stmt("SET ROLE 'rolename'");
|
||||||
|
assert_eq!(
|
||||||
|
stmt,
|
||||||
|
Statement::SetRole {
|
||||||
|
local: false,
|
||||||
|
session: false,
|
||||||
|
role_name: Some(Ident {
|
||||||
|
value: "rolename".to_string(),
|
||||||
|
quote_style: Some('\''),
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_show() {
|
fn parse_show() {
|
||||||
let stmt = pg_and_generic().verified_stmt("SHOW a a");
|
let stmt = pg_and_generic().verified_stmt("SHOW a a");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue