@@ -11629,3 +11629,91 @@ fn parse_factorial_operator() {
1162911629 ) ;
1163011630 }
1163111631}
11632+
11633+ #[ test]
11634+ fn parse_comments ( ) {
11635+ match all_dialects_where ( |d| d. supports_comment_on ( ) )
11636+ . verified_stmt ( "COMMENT ON COLUMN tab.name IS 'comment'" )
11637+ {
11638+ Statement :: Comment {
11639+ object_type,
11640+ object_name,
11641+ comment : Some ( comment) ,
11642+ if_exists,
11643+ } => {
11644+ assert_eq ! ( "comment" , comment) ;
11645+ assert_eq ! ( "tab.name" , object_name. to_string( ) ) ;
11646+ assert_eq ! ( CommentObject :: Column , object_type) ;
11647+ assert ! ( !if_exists) ;
11648+ }
11649+ _ => unreachable ! ( ) ,
11650+ }
11651+
11652+ let object_types = [
11653+ ( "COLUMN" , CommentObject :: Column ) ,
11654+ ( "EXTENSION" , CommentObject :: Extension ) ,
11655+ ( "TABLE" , CommentObject :: Table ) ,
11656+ ( "SCHEMA" , CommentObject :: Schema ) ,
11657+ ( "DATABASE" , CommentObject :: Database ) ,
11658+ ( "USER" , CommentObject :: User ) ,
11659+ ( "ROLE" , CommentObject :: Role ) ,
11660+ ] ;
11661+ for ( keyword, expected_object_type) in object_types. iter ( ) {
11662+ match all_dialects_where ( |d| d. supports_comment_on ( ) )
11663+ . verified_stmt ( format ! ( "COMMENT IF EXISTS ON {keyword} db.t0 IS 'comment'" ) . as_str ( ) )
11664+ {
11665+ Statement :: Comment {
11666+ object_type,
11667+ object_name,
11668+ comment : Some ( comment) ,
11669+ if_exists,
11670+ } => {
11671+ assert_eq ! ( "comment" , comment) ;
11672+ assert_eq ! ( "db.t0" , object_name. to_string( ) ) ;
11673+ assert_eq ! ( * expected_object_type, object_type) ;
11674+ assert ! ( if_exists) ;
11675+ }
11676+ _ => unreachable ! ( ) ,
11677+ }
11678+ }
11679+
11680+ match all_dialects_where ( |d| d. supports_comment_on ( ) )
11681+ . verified_stmt ( "COMMENT IF EXISTS ON TABLE public.tab IS NULL" )
11682+ {
11683+ Statement :: Comment {
11684+ object_type,
11685+ object_name,
11686+ comment : None ,
11687+ if_exists,
11688+ } => {
11689+ assert_eq ! ( "public.tab" , object_name. to_string( ) ) ;
11690+ assert_eq ! ( CommentObject :: Table , object_type) ;
11691+ assert ! ( if_exists) ;
11692+ }
11693+ _ => unreachable ! ( ) ,
11694+ }
11695+
11696+ // missing IS statement
11697+ assert_eq ! (
11698+ all_dialects_where( |d| d. supports_comment_on( ) )
11699+ . parse_sql_statements( "COMMENT ON TABLE t0" )
11700+ . unwrap_err( ) ,
11701+ ParserError :: ParserError ( "Expected: IS, found: EOF" . to_string( ) )
11702+ ) ;
11703+
11704+ // missing comment literal
11705+ assert_eq ! (
11706+ all_dialects_where( |d| d. supports_comment_on( ) )
11707+ . parse_sql_statements( "COMMENT ON TABLE t0 IS" )
11708+ . unwrap_err( ) ,
11709+ ParserError :: ParserError ( "Expected: literal string, found: EOF" . to_string( ) )
11710+ ) ;
11711+
11712+ // unknown object type
11713+ assert_eq ! (
11714+ all_dialects_where( |d| d. supports_comment_on( ) )
11715+ . parse_sql_statements( "COMMENT ON UNKNOWN t0 IS 'comment'" )
11716+ . unwrap_err( ) ,
11717+ ParserError :: ParserError ( "Expected: comment object_type, found: UNKNOWN" . to_string( ) )
11718+ ) ;
11719+ }
0 commit comments