Skip to content

Commit 8a4f530

Browse files
replace ContextModifier with Option and remove None variant
1 parent 94f5266 commit 8a4f530

File tree

7 files changed

+55
-45
lines changed

7 files changed

+55
-45
lines changed

src/ast/mod.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2629,7 +2629,7 @@ pub enum Set {
26292629
/// SQL Standard-style
26302630
/// SET a = 1;
26312631
SingleAssignment {
2632-
scope: ContextModifier,
2632+
scope: Option<ContextModifier>,
26332633
hivevar: bool,
26342634
variable: ObjectName,
26352635
values: Vec<Expr>,
@@ -2659,7 +2659,7 @@ pub enum Set {
26592659
/// [4]: https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_10004.htm
26602660
SetRole {
26612661
/// Non-ANSI optional identifier to inform if the role is defined inside the current session (`SESSION`) or transaction (`LOCAL`).
2662-
context_modifier: ContextModifier,
2662+
context_modifier: Option<ContextModifier>,
26632663
/// Role name. If NONE is specified, then the current role name is removed.
26642664
role_name: Option<Ident>,
26652665
},
@@ -2711,7 +2711,13 @@ impl Display for Set {
27112711
role_name,
27122712
} => {
27132713
let role_name = role_name.clone().unwrap_or_else(|| Ident::new("NONE"));
2714-
write!(f, "SET {context_modifier}ROLE {role_name}")
2714+
write!(
2715+
f,
2716+
"SET {modifier}ROLE {role_name}",
2717+
modifier = context_modifier
2718+
.map(|m| format!("{}", m))
2719+
.unwrap_or_default()
2720+
)
27152721
}
27162722
Self::SetSessionParam(kind) => write!(f, "SET {kind}"),
27172723
Self::SetTransaction {
@@ -2766,7 +2772,7 @@ impl Display for Set {
27662772
write!(
27672773
f,
27682774
"SET {}{}{} = {}",
2769-
scope,
2775+
scope.map(|s| format!("{}", s)).unwrap_or_default(),
27702776
if *hivevar { "HIVEVAR:" } else { "" },
27712777
variable,
27722778
display_comma_separated(values)
@@ -5727,14 +5733,20 @@ impl fmt::Display for SequenceOptions {
57275733
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
57285734
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
57295735
pub struct SetAssignment {
5730-
pub scope: ContextModifier,
5736+
pub scope: Option<ContextModifier>,
57315737
pub name: ObjectName,
57325738
pub value: Expr,
57335739
}
57345740

57355741
impl fmt::Display for SetAssignment {
57365742
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5737-
write!(f, "{}{} = {}", self.scope, self.name, self.value)
5743+
write!(
5744+
f,
5745+
"{}{} = {}",
5746+
self.scope.map(|s| format!("{}", s)).unwrap_or_default(),
5747+
self.name,
5748+
self.value
5749+
)
57385750
}
57395751
}
57405752

@@ -7961,8 +7973,6 @@ impl fmt::Display for FlushLocation {
79617973
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
79627974
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
79637975
pub enum ContextModifier {
7964-
/// No context defined. Each dialect defines the default in this scenario.
7965-
None,
79667976
/// `LOCAL` identifier, usually related to transactional states.
79677977
Local,
79687978
/// `SESSION` identifier
@@ -7974,9 +7984,6 @@ pub enum ContextModifier {
79747984
impl fmt::Display for ContextModifier {
79757985
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
79767986
match self {
7977-
Self::None => {
7978-
write!(f, "")
7979-
}
79807987
Self::Local => {
79817988
write!(f, "LOCAL ")
79827989
}

src/parser/mod.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1819,12 +1819,12 @@ impl<'a> Parser<'a> {
18191819
})
18201820
}
18211821

1822-
fn keyword_to_modifier(k: Option<Keyword>) -> ContextModifier {
1822+
fn keyword_to_modifier(k: Keyword) -> Option<ContextModifier> {
18231823
match k {
1824-
Some(Keyword::LOCAL) => ContextModifier::Local,
1825-
Some(Keyword::GLOBAL) => ContextModifier::Global,
1826-
Some(Keyword::SESSION) => ContextModifier::Session,
1827-
_ => ContextModifier::None,
1824+
Keyword::LOCAL => Some(ContextModifier::Local),
1825+
Keyword::GLOBAL => Some(ContextModifier::Global),
1826+
Keyword::SESSION => Some(ContextModifier::Session),
1827+
_ => None,
18281828
}
18291829
}
18301830

@@ -11145,7 +11145,10 @@ impl<'a> Parser<'a> {
1114511145
}
1114611146

1114711147
/// Parse a `SET ROLE` statement. Expects SET to be consumed already.
11148-
fn parse_set_role(&mut self, modifier: ContextModifier) -> Result<Statement, ParserError> {
11148+
fn parse_set_role(
11149+
&mut self,
11150+
modifier: Option<ContextModifier>,
11151+
) -> Result<Statement, ParserError> {
1114911152
self.expect_keyword_is(Keyword::ROLE)?;
1115011153

1115111154
let role_name = if self.parse_keyword(Keyword::NONE) {
@@ -11190,9 +11193,9 @@ impl<'a> Parser<'a> {
1119011193
}
1119111194
}
1119211195

11193-
fn parse_context_modifier(&mut self) -> ContextModifier {
11196+
fn parse_context_modifier(&mut self) -> Option<ContextModifier> {
1119411197
let modifier =
11195-
self.parse_one_of_keywords(&[Keyword::SESSION, Keyword::LOCAL, Keyword::GLOBAL]);
11198+
self.parse_one_of_keywords(&[Keyword::SESSION, Keyword::LOCAL, Keyword::GLOBAL])?;
1119611199

1119711200
Self::keyword_to_modifier(modifier)
1119811201
}
@@ -11228,7 +11231,7 @@ impl<'a> Parser<'a> {
1122811231
let scope = if !hivevar {
1122911232
self.parse_context_modifier()
1123011233
} else {
11231-
ContextModifier::None
11234+
None
1123211235
};
1123311236

1123411237
if hivevar {
@@ -11256,7 +11259,7 @@ impl<'a> Parser<'a> {
1125611259
// the assignment operator. It's originally PostgreSQL specific,
1125711260
// but we allow it for all the dialects
1125811261
return Ok(Set::SetTimeZone {
11259-
local: scope == ContextModifier::Local,
11262+
local: scope == Some(ContextModifier::Local),
1126011263
value: self.parse_expr()?,
1126111264
}
1126211265
.into());
@@ -11304,7 +11307,7 @@ impl<'a> Parser<'a> {
1130411307
}
1130511308

1130611309
if self.dialect.supports_comma_separated_set_assignments() {
11307-
if scope != ContextModifier::None {
11310+
if scope.is_some() {
1130811311
self.prev_token();
1130911312
}
1131011313

tests/sqlparser_common.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8632,7 +8632,7 @@ fn parse_set_variable() {
86328632
variable,
86338633
values,
86348634
}) => {
8635-
assert_eq!(scope, ContextModifier::None);
8635+
assert_eq!(scope, None);
86368636
assert!(!hivevar);
86378637
assert_eq!(variable, ObjectName::from(vec!["SOMETHING".into()]));
86388638
assert_eq!(
@@ -8652,7 +8652,7 @@ fn parse_set_variable() {
86528652
variable,
86538653
values,
86548654
}) => {
8655-
assert_eq!(scope, ContextModifier::Global);
8655+
assert_eq!(scope, Some(ContextModifier::Global));
86568656
assert!(!hivevar);
86578657
assert_eq!(variable, ObjectName::from(vec!["VARIABLE".into()]));
86588658
assert_eq!(
@@ -8744,7 +8744,7 @@ fn parse_set_role_as_variable() {
87448744
variable,
87458745
values,
87468746
}) => {
8747-
assert_eq!(scope, ContextModifier::None);
8747+
assert_eq!(scope, None);
87488748
assert!(!hivevar);
87498749
assert_eq!(variable, ObjectName::from(vec!["role".into()]));
87508750
assert_eq!(
@@ -8791,7 +8791,7 @@ fn parse_set_time_zone() {
87918791
variable,
87928792
values,
87938793
}) => {
8794-
assert_eq!(scope, ContextModifier::None);
8794+
assert_eq!(scope, None);
87958795
assert!(!hivevar);
87968796
assert_eq!(variable, ObjectName::from(vec!["TIMEZONE".into()]));
87978797
assert_eq!(
@@ -14856,12 +14856,12 @@ fn parse_multiple_set_statements() -> Result<(), ParserError> {
1485614856
assignments,
1485714857
vec![
1485814858
SetAssignment {
14859-
scope: ContextModifier::None,
14859+
scope: None,
1486014860
name: ObjectName::from(vec!["@a".into()]),
1486114861
value: Expr::value(number("1"))
1486214862
},
1486314863
SetAssignment {
14864-
scope: ContextModifier::None,
14864+
scope: None,
1486514865
name: ObjectName::from(vec!["b".into()]),
1486614866
value: Expr::value(number("2"))
1486714867
}
@@ -14879,22 +14879,22 @@ fn parse_multiple_set_statements() -> Result<(), ParserError> {
1487914879
assignments,
1488014880
vec![
1488114881
SetAssignment {
14882-
scope: ContextModifier::Global,
14882+
scope: Some(ContextModifier::Global),
1488314883
name: ObjectName::from(vec!["@a".into()]),
1488414884
value: Expr::value(number("1"))
1488514885
},
1488614886
SetAssignment {
14887-
scope: ContextModifier::Session,
14887+
scope: Some(ContextModifier::Session),
1488814888
name: ObjectName::from(vec!["b".into()]),
1488914889
value: Expr::value(number("2"))
1489014890
},
1489114891
SetAssignment {
14892-
scope: ContextModifier::Local,
14892+
scope: Some(ContextModifier::Local),
1489314893
name: ObjectName::from(vec!["c".into()]),
1489414894
value: Expr::value(number("3"))
1489514895
},
1489614896
SetAssignment {
14897-
scope: ContextModifier::None,
14897+
scope: None,
1489814898
name: ObjectName::from(vec!["d".into()]),
1489914899
value: Expr::value(number("4"))
1490014900
}

tests/sqlparser_hive.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ fn set_statement_with_minus() {
370370
assert_eq!(
371371
hive().verified_stmt("SET hive.tez.java.opts = -Xmx4g"),
372372
Statement::Set(Set::SingleAssignment {
373-
scope: ContextModifier::None,
373+
scope: None,
374374
hivevar: false,
375375
variable: ObjectName::from(vec![
376376
Ident::new("hive"),

tests/sqlparser_mssql.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1251,7 +1251,7 @@ fn parse_mssql_declare() {
12511251
}]
12521252
},
12531253
Statement::Set(Set::SingleAssignment {
1254-
scope: ContextModifier::None,
1254+
scope: None,
12551255
hivevar: false,
12561256
variable: ObjectName::from(vec![Ident::new("@bar")]),
12571257
values: vec![Expr::Value(

tests/sqlparser_mysql.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ fn parse_set_variables() {
618618
assert_eq!(
619619
mysql_and_generic().verified_stmt("SET LOCAL autocommit = 1"),
620620
Statement::Set(Set::SingleAssignment {
621-
scope: ContextModifier::Local,
621+
scope: Some(ContextModifier::Local),
622622
hivevar: false,
623623
variable: ObjectName::from(vec!["autocommit".into()]),
624624
values: vec![Expr::value(number("1"))],

tests/sqlparser_postgres.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,7 +1432,7 @@ fn parse_set() {
14321432
assert_eq!(
14331433
stmt,
14341434
Statement::Set(Set::SingleAssignment {
1435-
scope: ContextModifier::None,
1435+
scope: None,
14361436
hivevar: false,
14371437
variable: ObjectName::from(vec![Ident::new("a")]),
14381438
values: vec![Expr::Identifier(Ident {
@@ -1447,7 +1447,7 @@ fn parse_set() {
14471447
assert_eq!(
14481448
stmt,
14491449
Statement::Set(Set::SingleAssignment {
1450-
scope: ContextModifier::None,
1450+
scope: None,
14511451
hivevar: false,
14521452
variable: ObjectName::from(vec![Ident::new("a")]),
14531453
values: vec![Expr::Value(
@@ -1460,7 +1460,7 @@ fn parse_set() {
14601460
assert_eq!(
14611461
stmt,
14621462
Statement::Set(Set::SingleAssignment {
1463-
scope: ContextModifier::None,
1463+
scope: None,
14641464
hivevar: false,
14651465
variable: ObjectName::from(vec![Ident::new("a")]),
14661466
values: vec![Expr::value(number("0"))],
@@ -1471,7 +1471,7 @@ fn parse_set() {
14711471
assert_eq!(
14721472
stmt,
14731473
Statement::Set(Set::SingleAssignment {
1474-
scope: ContextModifier::None,
1474+
scope: None,
14751475
hivevar: false,
14761476
variable: ObjectName::from(vec![Ident::new("a")]),
14771477
values: vec![Expr::Identifier(Ident::new("DEFAULT"))],
@@ -1482,7 +1482,7 @@ fn parse_set() {
14821482
assert_eq!(
14831483
stmt,
14841484
Statement::Set(Set::SingleAssignment {
1485-
scope: ContextModifier::Local,
1485+
scope: Some(ContextModifier::Local),
14861486
hivevar: false,
14871487
variable: ObjectName::from(vec![Ident::new("a")]),
14881488
values: vec![Expr::Identifier("b".into())],
@@ -1493,7 +1493,7 @@ fn parse_set() {
14931493
assert_eq!(
14941494
stmt,
14951495
Statement::Set(Set::SingleAssignment {
1496-
scope: ContextModifier::None,
1496+
scope: None,
14971497
hivevar: false,
14981498
variable: ObjectName::from(vec![Ident::new("a"), Ident::new("b"), Ident::new("c")]),
14991499
values: vec![Expr::Identifier(Ident {
@@ -1511,7 +1511,7 @@ fn parse_set() {
15111511
assert_eq!(
15121512
stmt,
15131513
Statement::Set(Set::SingleAssignment {
1514-
scope: ContextModifier::None,
1514+
scope: None,
15151515
hivevar: false,
15161516
variable: ObjectName::from(vec![
15171517
Ident::new("hive"),
@@ -1555,7 +1555,7 @@ fn parse_set_role() {
15551555
assert_eq!(
15561556
stmt,
15571557
Statement::Set(Set::SetRole {
1558-
context_modifier: ContextModifier::Session,
1558+
context_modifier: Some(ContextModifier::Session),
15591559
role_name: None,
15601560
})
15611561
);
@@ -1566,7 +1566,7 @@ fn parse_set_role() {
15661566
assert_eq!(
15671567
stmt,
15681568
Statement::Set(Set::SetRole {
1569-
context_modifier: ContextModifier::Local,
1569+
context_modifier: Some(ContextModifier::Local),
15701570
role_name: Some(Ident {
15711571
value: "rolename".to_string(),
15721572
quote_style: Some('\"'),
@@ -1581,7 +1581,7 @@ fn parse_set_role() {
15811581
assert_eq!(
15821582
stmt,
15831583
Statement::Set(Set::SetRole {
1584-
context_modifier: ContextModifier::None,
1584+
context_modifier: None,
15851585
role_name: Some(Ident {
15861586
value: "rolename".to_string(),
15871587
quote_style: Some('\''),

0 commit comments

Comments
 (0)