Skip to content

Commit 0ff17d7

Browse files
committed
Use explicit variants
1 parent 25d6a40 commit 0ff17d7

File tree

3 files changed

+33
-45
lines changed

3 files changed

+33
-45
lines changed

src/ast/ddl.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,18 @@ pub enum AlterTableOperation {
169169
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
170170
pub enum Owner {
171171
Ident(Ident),
172-
Expr(Expr),
172+
CurrentRole,
173+
CurrentUser,
174+
SessionUser,
173175
}
174176

175177
impl fmt::Display for Owner {
176178
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
177179
match self {
178180
Owner::Ident(ident) => write!(f, "{}", ident),
179-
Owner::Expr(expr) => write!(f, "{}", expr),
181+
Owner::CurrentRole => write!(f, "CURRENT_ROLE"),
182+
Owner::CurrentUser => write!(f, "CURRENT_USER"),
183+
Owner::SessionUser => write!(f, "SESSION_USER"),
180184
}
181185
}
182186
}

src/parser/mod.rs

+12-17
Original file line numberDiff line numberDiff line change
@@ -6277,24 +6277,19 @@ impl<'a> Parser<'a> {
62776277
} else if dialect_of!(self is PostgreSqlDialect | GenericDialect)
62786278
&& self.parse_keywords(&[Keyword::OWNER, Keyword::TO])
62796279
{
6280-
let next_token = self.next_token();
6281-
let new_owner = match next_token.token {
6282-
Token::DoubleQuotedString(ref s) => Owner::Ident(Ident::new(s.to_string())),
6283-
Token::Word(ref w) => match w.keyword {
6284-
Keyword::CURRENT_USER | Keyword::CURRENT_ROLE | Keyword::SESSION_USER => {
6285-
Owner::Expr(Expr::Function(Function {
6286-
name: ObjectName(vec![w.to_ident()]),
6287-
args: FunctionArguments::None,
6288-
null_treatment: None,
6289-
filter: None,
6290-
over: None,
6291-
within_group: vec![],
6292-
}))
6293-
},
6294-
Keyword::NoKeyword => Owner::Ident(w.to_ident()),
6295-
_ => self.expected("CURRENT_USER, CURRENT_ROLE, SESSION_USER or identifier after OWNER TO clause", next_token)?,
6280+
let new_owner = match self.parse_one_of_keywords( &[Keyword::CURRENT_USER, Keyword::CURRENT_ROLE, Keyword::SESSION_USER]) {
6281+
Some(Keyword::CURRENT_USER) => Owner::CurrentUser,
6282+
Some(Keyword::CURRENT_ROLE) => Owner::CurrentRole,
6283+
Some(Keyword::SESSION_USER) => Owner::SessionUser,
6284+
Some(_) => unreachable!(),
6285+
None => {
6286+
match self.parse_identifier(false) {
6287+
Ok(ident) => Owner::Ident(ident),
6288+
Err(e) => {
6289+
return Err(ParserError::ParserError(format!("Expected CURRENT_USER, CURRENT_ROLE, SESSION_USER or identifier after OWNER TO. {e}")))
6290+
}
6291+
}
62966292
},
6297-
_ => self.expected("Token::Word", next_token)?
62986293
};
62996294

63006295
AlterTableOperation::OwnerTo { new_owner }

tests/sqlparser_postgres.rs

+15-26
Original file line numberDiff line numberDiff line change
@@ -725,42 +725,25 @@ fn parse_alter_table_owner_to() {
725725
sql: "ALTER TABLE tab OWNER TO new_owner",
726726
expected_owner: Owner::Ident(Ident::new("new_owner".to_string())),
727727
},
728+
TestCase {
729+
sql: "ALTER TABLE tab OWNER TO postgres",
730+
expected_owner: Owner::Ident(Ident::new("postgres".to_string())),
731+
},
728732
TestCase {
729733
sql: "ALTER TABLE tab OWNER TO \"new_owner\"",
730734
expected_owner: Owner::Ident(Ident::with_quote('\"', "new_owner".to_string())),
731735
},
732736
TestCase {
733737
sql: "ALTER TABLE tab OWNER TO CURRENT_USER",
734-
expected_owner: Owner::Expr(Expr::Function(Function {
735-
name: ObjectName(vec![Ident::new("CURRENT_USER")]),
736-
args: FunctionArguments::None,
737-
null_treatment: None,
738-
filter: None,
739-
over: None,
740-
within_group: vec![],
741-
})),
738+
expected_owner: Owner::CurrentUser,
742739
},
743740
TestCase {
744741
sql: "ALTER TABLE tab OWNER TO CURRENT_ROLE",
745-
expected_owner: Owner::Expr(Expr::Function(Function {
746-
name: ObjectName(vec![Ident::new("CURRENT_ROLE")]),
747-
args: FunctionArguments::None,
748-
null_treatment: None,
749-
filter: None,
750-
over: None,
751-
within_group: vec![],
752-
})),
742+
expected_owner: Owner::CurrentRole,
753743
},
754744
TestCase {
755745
sql: "ALTER TABLE tab OWNER TO SESSION_USER",
756-
expected_owner: Owner::Expr(Expr::Function(Function {
757-
name: ObjectName(vec![Ident::new("SESSION_USER")]),
758-
args: FunctionArguments::None,
759-
null_treatment: None,
760-
filter: None,
761-
over: None,
762-
within_group: vec![],
763-
})),
746+
expected_owner: Owner::SessionUser,
764747
},
765748
];
766749

@@ -785,9 +768,15 @@ fn parse_alter_table_owner_to() {
785768
}
786769
}
787770

788-
let res = pg().parse_sql_statements("ALTER TABLE tab OWNER TO CREATE");
771+
let res = pg().parse_sql_statements("ALTER TABLE tab OWNER TO CREATE FOO");
772+
assert_eq!(
773+
ParserError::ParserError("Expected end of statement, found: FOO".to_string()),
774+
res.unwrap_err()
775+
);
776+
777+
let res = pg().parse_sql_statements("ALTER TABLE tab OWNER TO 4");
789778
assert_eq!(
790-
ParserError::ParserError("Expected CURRENT_USER, CURRENT_ROLE, SESSION_USER or identifier after OWNER TO clause, found: CREATE".to_string()),
779+
ParserError::ParserError("Expected CURRENT_USER, CURRENT_ROLE, SESSION_USER or identifier after OWNER TO. sql parser error: Expected identifier, found: 4".to_string()),
791780
res.unwrap_err()
792781
);
793782
}

0 commit comments

Comments
 (0)