diff --git a/src/ast/mod.rs b/src/ast/mod.rs index 73dd1f5b..bd31949e 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -1487,6 +1487,9 @@ pub enum Statement { object_type: CommentObject, object_name: ObjectName, comment: Option, + /// An optional `IF EXISTS` clause. (Non-standard.) + /// See + if_exists: bool, }, /// `COMMIT [ TRANSACTION | WORK ] [ AND [ NO ] CHAIN ]` Commit { chain: bool }, @@ -2637,8 +2640,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 { diff --git a/src/dialect/postgresql.rs b/src/dialect/postgresql.rs index fe1953d2..da07fefe 100644 --- a/src/dialect/postgresql.rs +++ b/src/dialect/postgresql.rs @@ -49,6 +49,8 @@ impl Dialect for PostgreSqlDialect { } pub fn parse_comment(parser: &mut Parser) -> Result { + let if_exists = parser.parse_keywords(&[Keyword::IF, Keyword::EXISTS]); + parser.expect_keyword(Keyword::ON)?; let token = parser.next_token(); @@ -74,5 +76,6 @@ pub fn parse_comment(parser: &mut Parser) -> Result { object_type, object_name, comment, + if_exists, }) } diff --git a/tests/sqlparser_postgres.rs b/tests/sqlparser_postgres.rs index 6a3f88e3..e541e515 100644 --- a/tests/sqlparser_postgres.rs +++ b/tests/sqlparser_postgres.rs @@ -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!(), } @@ -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!(), }