Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions fuzz/fuzz_seeds/postgres_alter_role_set.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER ROLE app IN DATABASE d SET search_path = api, public;
ALTER ROLE app IN DATABASE d RESET TIME ZONE
68 changes: 30 additions & 38 deletions src/ast/dcl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ use sqlparser_derive::{Visit, VisitMut};

use super::{display_comma_separated, Expr, Ident, Password, Spanned};
use crate::ast::{
display_separated, CascadeOption, CurrentGrantsKind, GrantObjects, Grantee, GranteeName,
ObjectName, Privileges,
display_separated, CascadeOption, ConfigurationSet, CurrentGrantsKind, GrantObjects, Grantee,
GranteeName, ObjectName, Privileges,
};
use crate::tokenizer::Span;

Expand Down Expand Up @@ -131,24 +131,12 @@ impl fmt::Display for RoleOption {
}
}

/// SET config value option:
/// * SET `configuration_parameter` { TO | = } { `value` | DEFAULT }
/// * SET `configuration_parameter` FROM CURRENT
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum SetConfigValue {
/// Use the default value.
Default,
/// Use the current value (`FROM CURRENT`).
FromCurrent,
/// Set to the provided expression value.
Value(Expr),
}

/// RESET config option:
/// * RESET `configuration_parameter`
/// * RESET ALL
/// * RESET TIME ZONE
/// * RESET TRANSACTION ISOLATION LEVEL
/// * RESET SESSION AUTHORIZATION
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
Expand All @@ -157,6 +145,26 @@ pub enum ResetConfig {
ALL,
/// Reset the named configuration parameter.
ConfigName(ObjectName),
/// `TIME ZONE`
TimeZone,
/// `TRANSACTION ISOLATION LEVEL`
TransactionIsolationLevel,
/// `SESSION AUTHORIZATION`
SessionAuthorization,
}

impl fmt::Display for ResetConfig {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ResetConfig::ALL => f.write_str("RESET ALL"),
ResetConfig::ConfigName(name) => write!(f, "RESET {name}"),
ResetConfig::TimeZone => f.write_str("RESET TIME ZONE"),
ResetConfig::TransactionIsolationLevel => {
f.write_str("RESET TRANSACTION ISOLATION LEVEL")
}
ResetConfig::SessionAuthorization => f.write_str("RESET SESSION AUTHORIZATION"),
}
}
}

/// An `ALTER ROLE` (`Statement::AlterRole`) operation
Expand Down Expand Up @@ -190,20 +198,14 @@ pub enum AlterRoleOperation {
},
/// PostgreSQL
/// <https://www.postgresql.org/docs/current/sql-alterrole.html>
///
/// `SET configuration_parameter { TO | = } { value | DEFAULT }`
Set {
/// Configuration name to set.
config_name: ObjectName,
/// Value to assign to the configuration.
config_value: SetConfigValue,
/// The configuration parameter default to store.
config: ConfigurationSet,
/// Optional database scope for the setting.
in_database: Option<ObjectName>,
},
/// PostgreSQL
/// <https://www.postgresql.org/docs/current/sql-alterrole.html>
///
/// `RESET configuration_parameter` | `RESET ALL`
Reset {
/// Configuration to reset.
config_name: ResetConfig,
Expand All @@ -228,19 +230,13 @@ impl fmt::Display for AlterRoleOperation {
write!(f, "WITH {}", display_separated(options, " "))
}
AlterRoleOperation::Set {
config_name,
config_value,
config,
in_database,
} => {
if let Some(database_name) = in_database {
write!(f, "IN DATABASE {database_name} ")?;
}

match config_value {
SetConfigValue::Default => write!(f, "SET {config_name} TO DEFAULT"),
SetConfigValue::FromCurrent => write!(f, "SET {config_name} FROM CURRENT"),
SetConfigValue::Value(expr) => write!(f, "SET {config_name} TO {expr}"),
}
write!(f, "{config}")
}
AlterRoleOperation::Reset {
config_name,
Expand All @@ -249,11 +245,7 @@ impl fmt::Display for AlterRoleOperation {
if let Some(database_name) = in_database {
write!(f, "IN DATABASE {database_name} ")?;
}

match config_name {
ResetConfig::ALL => write!(f, "RESET ALL"),
ResetConfig::ConfigName(name) => write!(f, "RESET {name}"),
}
write!(f, "{config_name}")
}
}
}
Expand Down
9 changes: 3 additions & 6 deletions src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5420,7 +5420,7 @@ impl Spanned for AlterDatabase {
}

/// A `SET` clause that stores a configuration parameter default, as in
/// `ALTER DATABASE ... SET`.
/// `ALTER DATABASE ... SET` and `ALTER ROLE ... SET`.
///
/// [PostgreSQL](https://www.postgresql.org/docs/current/sql-set.html)
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
Expand Down Expand Up @@ -6498,7 +6498,7 @@ pub enum AlterFunctionAction {
/// `SET configuration_parameter { TO | = } { value | DEFAULT }`
/// or `SET configuration_parameter FROM CURRENT`
Set(FunctionDefinitionSetParam),
/// `RESET configuration_parameter` or `RESET ALL`
/// `RESET ...`
Reset(ResetConfig),
}

Expand Down Expand Up @@ -6561,10 +6561,7 @@ impl fmt::Display for AlterFunctionAction {
write!(f, "SUPPORT {support_function}")
}
AlterFunctionAction::Set(set_param) => write!(f, "{set_param}"),
AlterFunctionAction::Reset(reset_config) => match reset_config {
ResetConfig::ALL => write!(f, "RESET ALL"),
ResetConfig::ConfigName(name) => write!(f, "RESET {name}"),
},
AlterFunctionAction::Reset(reset_config) => write!(f, "{reset_config}"),
}
}
}
Expand Down
29 changes: 5 additions & 24 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub use self::dcl::{
AlterDefaultPrivileges, AlterDefaultPrivilegesOperation, AlterRoleOperation, CreateRole,
DefaultPrivilegesObjectType, DefaultPrivilegesOption, Grant, GrantRole, ResetConfig, Revoke,
RevokeRole, RoleGrantOption, RoleGrantOptionValue, RoleKeyword, RoleOption, SecondaryRoles,
SetConfigValue, Use,
Use,
};
pub use self::ddl::{
AggregateArgs, AggregateModifyKind, AggregateSignature, Alignment, AlterCollation,
Expand Down Expand Up @@ -12476,31 +12476,16 @@ impl fmt::Display for VacuumStatement {
}
}

/// Variants of the RESET statement
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum Reset {
/// Resets all session parameters to their default values.
ALL,

/// Resets session authorization to the session user.
SessionAuthorization,

/// Resets a specific session parameter to its default value.
ConfigurationParameter(ObjectName),
}

/// Resets a session parameter to its default value.
/// ```sql
/// RESET { ALL | SESSION AUTHORIZATION | <configuration_parameter> }
/// RESET { ALL | TIME ZONE | TRANSACTION ISOLATION LEVEL | SESSION AUTHORIZATION | <configuration_parameter> }
/// ```
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub struct ResetStatement {
/// The reset action to perform (either `ALL` or a specific configuration parameter).
pub reset: Reset,
/// What to reset.
pub reset: ResetConfig,
}

/// `REFRESH MATERIALIZED VIEW [ CONCURRENTLY ] name [ WITH [ NO ] DATA ]`
Expand Down Expand Up @@ -12598,11 +12583,7 @@ impl fmt::Display for OptimizerHint {

impl fmt::Display for ResetStatement {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match &self.reset {
Reset::ALL => write!(f, "RESET ALL"),
Reset::SessionAuthorization => write!(f, "RESET SESSION AUTHORIZATION"),
Reset::ConfigurationParameter(param) => write!(f, "RESET {}", param),
}
write!(f, "{}", self.reset)
}
}

Expand Down
47 changes: 7 additions & 40 deletions src/parser/alter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ use crate::{
AlterConnectorOwner, AlterPolicy, AlterPolicyOperation, AlterRoleOperation, AlterTrigger,
AlterTriggerOperation, AlterUser, AlterUserAddMfaMethodOtp, AlterUserAddRoleDelegation,
AlterUserModifyMfaMethod, AlterUserPassword, AlterUserRemoveRoleDelegation,
AlterUserSetPolicy, Expr, MfaMethodKind, Password, ResetConfig, RoleOption, SetConfigValue,
Statement, UserPolicyKind,
AlterUserSetPolicy, Expr, MfaMethodKind, Password, RoleOption, Statement, UserPolicyKind,
},
dialect::{MsSqlDialect, PostgreSqlDialect},
keywords::Keyword,
Expand Down Expand Up @@ -434,47 +433,15 @@ impl Parser<'_> {
}
// SET
} else if self.parse_keyword(Keyword::SET) {
let config_name = self.parse_object_name(false)?;
// FROM CURRENT
if self.parse_keywords(&[Keyword::FROM, Keyword::CURRENT]) {
AlterRoleOperation::Set {
config_name,
config_value: SetConfigValue::FromCurrent,
in_database,
}
// { TO | = } { value | DEFAULT }
} else if self.consume_token(&Token::Eq) || self.parse_keyword(Keyword::TO) {
if self.parse_keyword(Keyword::DEFAULT) {
AlterRoleOperation::Set {
config_name,
config_value: SetConfigValue::Default,
in_database,
}
} else if let Ok(expr) = self.parse_expr() {
AlterRoleOperation::Set {
config_name,
config_value: SetConfigValue::Value(expr),
in_database,
}
} else {
self.expected_ref("config value", self.peek_token_ref())?
}
} else {
self.expected_ref("'TO' or '=' or 'FROM CURRENT'", self.peek_token_ref())?
AlterRoleOperation::Set {
config: self.parse_configuration_set()?,
in_database,
}
// RESET
} else if self.parse_keyword(Keyword::RESET) {
if self.parse_keyword(Keyword::ALL) {
AlterRoleOperation::Reset {
config_name: ResetConfig::ALL,
in_database,
}
} else {
let config_name = self.parse_object_name(false)?;
AlterRoleOperation::Reset {
config_name: ResetConfig::ConfigName(config_name),
in_database,
}
AlterRoleOperation::Reset {
config_name: self.parse_reset_config()?,
in_database,
}
// option
} else {
Expand Down
37 changes: 18 additions & 19 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12300,12 +12300,7 @@ impl<'a> Parser<'a> {
self.parse_function_definition_set_param()?,
))
} else if self.parse_keyword(Keyword::RESET) {
let reset_config = if self.parse_keyword(Keyword::ALL) {
ResetConfig::ALL
} else {
ResetConfig::ConfigName(self.parse_object_name(false)?)
};
Some(AlterFunctionAction::Reset(reset_config))
Some(AlterFunctionAction::Reset(self.parse_reset_config()?))
} else {
None
};
Expand Down Expand Up @@ -12334,6 +12329,21 @@ impl<'a> Parser<'a> {
Ok(FunctionDefinitionSetParam { name, value })
}

/// Parse what follows `RESET`, as a statement or in `ALTER ROLE` and `ALTER FUNCTION`.
fn parse_reset_config(&mut self) -> Result<ResetConfig, ParserError> {
Ok(if self.parse_keyword(Keyword::ALL) {
ResetConfig::ALL
} else if self.parse_keywords(&[Keyword::TIME, Keyword::ZONE]) {
ResetConfig::TimeZone
} else if self.parse_keywords(&[Keyword::TRANSACTION, Keyword::ISOLATION, Keyword::LEVEL]) {
ResetConfig::TransactionIsolationLevel
} else if self.parse_keywords(&[Keyword::SESSION, Keyword::AUTHORIZATION]) {
ResetConfig::SessionAuthorization
} else {
ResetConfig::ConfigName(self.parse_object_name(false)?)
})
}

fn parse_alter_function_actions(
&mut self,
) -> Result<(Vec<AlterFunctionAction>, bool), ParserError> {
Expand Down Expand Up @@ -12632,7 +12642,7 @@ impl<'a> Parser<'a> {
}

/// Parse the clause after `SET` that stores a configuration parameter
/// default, as in `ALTER DATABASE ... SET`.
/// default, as in `ALTER DATABASE ... SET` and `ALTER ROLE ... SET`.
///
/// [PostgreSQL Documentation](https://www.postgresql.org/docs/current/sql-set.html)
fn parse_configuration_set(&mut self) -> Result<ConfigurationSet, ParserError> {
Expand Down Expand Up @@ -23161,19 +23171,8 @@ impl<'a> Parser<'a> {

/// Parses a RESET statement
fn parse_reset(&mut self) -> Result<ResetStatement, ParserError> {
if self.parse_keyword(Keyword::ALL) {
return Ok(ResetStatement { reset: Reset::ALL });
}

if self.parse_keywords(&[Keyword::SESSION, Keyword::AUTHORIZATION]) {
return Ok(ResetStatement {
reset: Reset::SessionAuthorization,
});
}

let obj = self.parse_object_name(false)?;
Ok(ResetStatement {
reset: Reset::ConfigurationParameter(obj),
reset: self.parse_reset_config()?,
})
}

Expand Down
Loading
Loading