@@ -546,65 +546,10 @@ impl Parser {
546
546
547
547
/// Parse a SQL CREATE statement
548
548
pub fn parse_create ( & mut self ) -> Result < SQLStatement , ParserError > {
549
- if self . parse_keywords ( vec ! [ "TABLE" ] ) {
550
- let table_name = self . parse_object_name ( ) ?;
551
- // parse optional column list (schema)
552
- let mut columns = vec ! [ ] ;
553
- if self . consume_token ( & Token :: LParen ) {
554
- loop {
555
- match self . next_token ( ) {
556
- Some ( Token :: SQLWord ( column_name) ) => {
557
- let data_type = self . parse_data_type ( ) ?;
558
- let is_primary = self . parse_keywords ( vec ! [ "PRIMARY" , "KEY" ] ) ;
559
- let is_unique = self . parse_keyword ( "UNIQUE" ) ;
560
- let default = if self . parse_keyword ( "DEFAULT" ) {
561
- let expr = self . parse_default_expr ( 0 ) ?;
562
- Some ( expr)
563
- } else {
564
- None
565
- } ;
566
- let allow_null = if self . parse_keywords ( vec ! [ "NOT" , "NULL" ] ) {
567
- false
568
- } else if self . parse_keyword ( "NULL" ) {
569
- true
570
- } else {
571
- true
572
- } ;
573
- debug ! ( "default: {:?}" , default ) ;
574
-
575
- columns. push ( SQLColumnDef {
576
- name : column_name. as_sql_ident ( ) ,
577
- data_type : data_type,
578
- allow_null,
579
- is_primary,
580
- is_unique,
581
- default,
582
- } ) ;
583
- match self . next_token ( ) {
584
- Some ( Token :: Comma ) => { }
585
- Some ( Token :: RParen ) => {
586
- break ;
587
- }
588
- other => {
589
- return parser_err ! (
590
- format!( "Expected ',' or ')' after column definition but found {:?}" , other)
591
- ) ;
592
- }
593
- }
594
- }
595
- unexpected => {
596
- return parser_err ! ( format!(
597
- "Expected column name, got {:?}" ,
598
- unexpected
599
- ) ) ;
600
- }
601
- }
602
- }
603
- }
604
- Ok ( SQLStatement :: SQLCreateTable {
605
- name : table_name,
606
- columns,
607
- } )
549
+ if self . parse_keyword ( "TABLE" ) {
550
+ self . parse_create_table ( )
551
+ } else if self . parse_keyword ( "VIEW" ) {
552
+ self . parse_create_view ( )
608
553
} else {
609
554
parser_err ! ( format!(
610
555
"Unexpected token after CREATE: {:?}" ,
@@ -613,6 +558,79 @@ impl Parser {
613
558
}
614
559
}
615
560
561
+ pub fn parse_create_view ( & mut self ) -> Result < SQLStatement , ParserError > {
562
+ // Many dialects support `OR REPLACE` | `OR ALTER` right after `CREATE`, but we don't (yet).
563
+ // ANSI SQL and Postgres support RECURSIVE here, but we don't support it either.
564
+ let name = self . parse_object_name ( ) ?;
565
+ // Parenthesized "output" columns list could be handled here.
566
+ // Some dialects allow WITH here, followed by some keywords (e.g. MS SQL)
567
+ // or `(k1=v1, k2=v2, ...)` (Postgres)
568
+ self . expect_keyword ( "AS" ) ?;
569
+ self . expect_keyword ( "SELECT" ) ?;
570
+ let query = self . parse_select ( ) ?;
571
+ // Optional `WITH [ CASCADED | LOCAL ] CHECK OPTION` is widely supported here.
572
+ Ok ( SQLStatement :: SQLCreateView { name, query } )
573
+ }
574
+
575
+ pub fn parse_create_table ( & mut self ) -> Result < SQLStatement , ParserError > {
576
+ let table_name = self . parse_object_name ( ) ?;
577
+ // parse optional column list (schema)
578
+ let mut columns = vec ! [ ] ;
579
+ if self . consume_token ( & Token :: LParen ) {
580
+ loop {
581
+ match self . next_token ( ) {
582
+ Some ( Token :: SQLWord ( column_name) ) => {
583
+ let data_type = self . parse_data_type ( ) ?;
584
+ let is_primary = self . parse_keywords ( vec ! [ "PRIMARY" , "KEY" ] ) ;
585
+ let is_unique = self . parse_keyword ( "UNIQUE" ) ;
586
+ let default = if self . parse_keyword ( "DEFAULT" ) {
587
+ let expr = self . parse_default_expr ( 0 ) ?;
588
+ Some ( expr)
589
+ } else {
590
+ None
591
+ } ;
592
+ let allow_null = if self . parse_keywords ( vec ! [ "NOT" , "NULL" ] ) {
593
+ false
594
+ } else if self . parse_keyword ( "NULL" ) {
595
+ true
596
+ } else {
597
+ true
598
+ } ;
599
+ debug ! ( "default: {:?}" , default ) ;
600
+
601
+ columns. push ( SQLColumnDef {
602
+ name : column_name. as_sql_ident ( ) ,
603
+ data_type : data_type,
604
+ allow_null,
605
+ is_primary,
606
+ is_unique,
607
+ default,
608
+ } ) ;
609
+ match self . next_token ( ) {
610
+ Some ( Token :: Comma ) => { }
611
+ Some ( Token :: RParen ) => {
612
+ break ;
613
+ }
614
+ other => {
615
+ return parser_err ! ( format!(
616
+ "Expected ',' or ')' after column definition but found {:?}" ,
617
+ other
618
+ ) ) ;
619
+ }
620
+ }
621
+ }
622
+ unexpected => {
623
+ return parser_err ! ( format!( "Expected column name, got {:?}" , unexpected) ) ;
624
+ }
625
+ }
626
+ }
627
+ }
628
+ Ok ( SQLStatement :: SQLCreateTable {
629
+ name : table_name,
630
+ columns,
631
+ } )
632
+ }
633
+
616
634
pub fn parse_table_key ( & mut self , constraint_name : SQLIdent ) -> Result < TableKey , ParserError > {
617
635
let is_primary_key = self . parse_keywords ( vec ! [ "PRIMARY" , "KEY" ] ) ;
618
636
let is_unique_key = self . parse_keywords ( vec ! [ "UNIQUE" , "KEY" ] ) ;
0 commit comments