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