Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support IF EXISTS in COMMENT statements #831

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1487,6 +1487,8 @@ pub enum Statement {
object_type: CommentObject,
object_name: ObjectName,
comment: Option<String>,
/// An optional `IF EXISTS` clause. (Non-standard.)
if_exists: bool,
pawel-big-lebowski marked this conversation as resolved.
Show resolved Hide resolved
},
/// `COMMIT [ TRANSACTION | WORK ] [ AND [ NO ] CHAIN ]`
Commit { chain: bool },
Expand Down Expand Up @@ -2637,8 +2639,13 @@ impl fmt::Display for Statement {
object_type,
object_name,
comment,
if_exists,
} => {
write!(f, "COMMENT ON {object_type} {object_name} IS ")?;
write!(f, "COMMENT ")?;
if *if_exists {
write!(f, "IF EXISTS ")?
};
write!(f, "ON {object_type} {object_name} IS ")?;
if let Some(c) = comment {
write!(f, "'{c}'")
} else {
Expand Down
3 changes: 3 additions & 0 deletions src/dialect/postgresql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ impl Dialect for PostgreSqlDialect {
}

pub fn parse_comment(parser: &mut Parser) -> Result<Statement, ParserError> {
let if_exists = parser.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);

parser.expect_keyword(Keyword::ON)?;
let token = parser.next_token();

Expand All @@ -74,5 +76,6 @@ pub fn parse_comment(parser: &mut Parser) -> Result<Statement, ParserError> {
object_type,
object_name,
comment,
if_exists,
})
}
8 changes: 7 additions & 1 deletion tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1790,10 +1790,12 @@ fn parse_comments() {
object_type,
object_name,
comment: Some(comment),
if_exists,
} => {
assert_eq!("comment", comment);
assert_eq!("tab.name", object_name.to_string());
assert_eq!(CommentObject::Column, object_type);
assert!(!if_exists);
}
_ => unreachable!(),
}
Expand All @@ -1803,22 +1805,26 @@ fn parse_comments() {
object_type,
object_name,
comment: Some(comment),
if_exists,
} => {
assert_eq!("comment", comment);
assert_eq!("public.tab", object_name.to_string());
assert_eq!(CommentObject::Table, object_type);
assert!(!if_exists);
}
_ => unreachable!(),
}

match pg().verified_stmt("COMMENT ON TABLE public.tab IS NULL") {
match pg().verified_stmt("COMMENT IF EXISTS ON TABLE public.tab IS NULL") {
Statement::Comment {
object_type,
object_name,
comment: None,
if_exists,
} => {
assert_eq!("public.tab", object_name.to_string());
assert_eq!(CommentObject::Table, object_type);
assert!(if_exists);
}
_ => unreachable!(),
}
Expand Down