@@ -489,84 +489,75 @@ pub struct Insert {
489
489
490
490
impl Display for Insert {
491
491
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
492
- // Start building the insert statement.
493
- if self . replace_into {
494
- write ! ( f, "REPLACE INTO " ) ?;
492
+ let table_name = if let Some ( alias) = & self . table_alias {
493
+ format ! ( "{0} AS {alias}" , self . table_name)
495
494
} else {
496
- if self . ignore {
497
- write ! ( f, "INSERT IGNORE " ) ?;
498
- } else if let Some ( priority) = & self . priority {
499
- write ! ( f, "INSERT {} " , priority) ?;
500
- } else {
501
- write ! ( f, "INSERT " ) ?;
502
- }
495
+ self . table_name . to_string ( )
496
+ } ;
503
497
504
- if self . into {
505
- write ! ( f, "INTO " ) ?;
506
- }
507
- if self . table {
508
- write ! ( f, "TABLE " ) ?;
498
+ if let Some ( action) = self . or {
499
+ write ! ( f, "INSERT OR {action} INTO {table_name} " ) ?;
500
+ } else {
501
+ write ! (
502
+ f,
503
+ "{start}" ,
504
+ start = if self . replace_into {
505
+ "REPLACE"
506
+ } else {
507
+ "INSERT"
508
+ } ,
509
+ ) ?;
510
+ if let Some ( priority) = self . priority {
511
+ write ! ( f, " {priority}" , ) ?;
509
512
}
510
- }
511
513
512
- // Write table name and alias
513
- write ! ( f, "{}" , self . table_name) ?;
514
- if let Some ( alias) = & self . table_alias {
515
- write ! ( f, " AS {}" , alias) ?;
514
+ write ! (
515
+ f,
516
+ "{ignore}{over}{int}{tbl} {table_name} " ,
517
+ table_name = table_name,
518
+ ignore = if self . ignore { " IGNORE" } else { "" } ,
519
+ over = if self . overwrite { " OVERWRITE" } else { "" } ,
520
+ int = if self . into { " INTO" } else { "" } ,
521
+ tbl = if self . table { " TABLE" } else { "" } ,
522
+ ) ?;
516
523
}
517
-
518
- // Write columns if there are any
519
524
if !self . columns . is_empty ( ) {
520
- let cols = self
521
- . columns
522
- . iter ( )
523
- . map ( |col| col. to_string ( ) )
524
- . collect :: < Vec < _ > > ( )
525
- . join ( ", " ) ;
526
- write ! ( f, " ({})" , cols) ?;
527
- }
528
-
529
- // Write partitioned insert (Hive)
530
- if let Some ( partitions) = & self . partitioned {
531
- let parts = partitions
532
- . iter ( )
533
- . map ( |p| p. to_string ( ) )
534
- . collect :: < Vec < _ > > ( )
535
- . join ( ", " ) ;
536
- write ! ( f, " PARTITION ({})" , parts) ?;
537
- }
538
-
539
- // Write after columns (Hive)
525
+ write ! ( f, "({}) " , display_comma_separated( & self . columns) ) ?;
526
+ }
527
+ if let Some ( ref parts) = self . partitioned {
528
+ if !parts. is_empty ( ) {
529
+ write ! ( f, "PARTITION ({}) " , display_comma_separated( parts) ) ?;
530
+ }
531
+ }
540
532
if !self . after_columns . is_empty ( ) {
541
- let after_cols = self
542
- . after_columns
543
- . iter ( )
544
- . map ( |col| col. to_string ( ) )
545
- . collect :: < Vec < _ > > ( )
546
- . join ( ", " ) ;
547
- write ! ( f, " ({})" , after_cols) ?;
533
+ write ! ( f, "({}) " , display_comma_separated( & self . after_columns) ) ?;
548
534
}
549
535
550
- // Write the source query if it exists
551
536
if let Some ( source) = & self . source {
552
- write ! ( f, " {}" , source ) ?;
537
+ write ! ( f, "{source}" ) ?;
553
538
}
554
539
555
- // Write ON conflict handling for Sqlite, MySQL, etc.
556
- if let Some ( on_conflict) = & self . on {
557
- write ! ( f, " {}" , on_conflict) ?;
540
+ if self . source . is_none ( ) && self . columns . is_empty ( ) {
541
+ write ! ( f, "DEFAULT VALUES" ) ?;
558
542
}
559
543
560
- // Write RETURNING clause if present
561
- if let Some ( returning) = & self . returning {
562
- let returns = returning
563
- . iter ( )
564
- . map ( |r| r. to_string ( ) )
565
- . collect :: < Vec < _ > > ( )
566
- . join ( ", " ) ;
567
- write ! ( f, " RETURNING {}" , returns) ?;
544
+ if let Some ( insert_alias) = & self . insert_alias {
545
+ write ! ( f, " AS {0}" , insert_alias. row_alias) ?;
546
+
547
+ if let Some ( col_aliases) = & insert_alias. col_aliases {
548
+ if !col_aliases. is_empty ( ) {
549
+ write ! ( f, " ({})" , display_comma_separated( col_aliases) ) ?;
550
+ }
551
+ }
552
+ }
553
+
554
+ if let Some ( on) = & self . on {
555
+ write ! ( f, "{on}" ) ?;
568
556
}
569
557
558
+ if let Some ( returning) = & self . returning {
559
+ write ! ( f, " RETURNING {}" , display_comma_separated( returning) ) ?;
560
+ }
570
561
Ok ( ( ) )
571
562
}
572
563
}
@@ -595,62 +586,32 @@ pub struct Delete {
595
586
impl Display for Delete {
596
587
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
597
588
write ! ( f, "DELETE " ) ?;
598
-
599
- // Handle multi-table DELETE if present
600
589
if !self . tables . is_empty ( ) {
601
- let tables = self
602
- . tables
603
- . iter ( )
604
- . map ( |t| t. to_string ( ) )
605
- . collect :: < Vec < _ > > ( )
606
- . join ( ", " ) ;
607
- write ! ( f, "{} " , tables) ?;
590
+ write ! ( f, "{} " , display_comma_separated( & self . tables) ) ?;
591
+ }
592
+ match & self . from {
593
+ FromTable :: WithFromKeyword ( from) => {
594
+ write ! ( f, "FROM {}" , display_comma_separated( from) ) ?;
595
+ }
596
+ FromTable :: WithoutKeyword ( from) => {
597
+ write ! ( f, "{}" , display_comma_separated( from) ) ?;
598
+ }
608
599
}
609
-
610
- // The FromTable includes the `FROM` keyword.
611
- write ! ( f, "{} " , self . from) ?;
612
-
613
- // USING clause (if present)
614
600
if let Some ( using) = & self . using {
615
- let uses = using
616
- . iter ( )
617
- . map ( |tab| tab. to_string ( ) )
618
- . collect :: < Vec < _ > > ( )
619
- . join ( ", " ) ;
620
- write ! ( f, "USING {} " , uses) ?;
601
+ write ! ( f, " USING {}" , display_comma_separated( using) ) ?;
621
602
}
622
-
623
- // WHERE clause (if present)
624
- if let Some ( sel) = & self . selection {
625
- write ! ( f, "WHERE {} " , sel) ?;
603
+ if let Some ( selection) = & self . selection {
604
+ write ! ( f, " WHERE {selection}" ) ?;
626
605
}
627
-
628
- // RETURNING clause (if present)
629
- if let Some ( ret) = & self . returning {
630
- let rets = ret
631
- . iter ( )
632
- . map ( |col| col. to_string ( ) )
633
- . collect :: < Vec < _ > > ( )
634
- . join ( ", " ) ;
635
- write ! ( f, "RETURNING {} " , rets) ?;
606
+ if let Some ( returning) = & self . returning {
607
+ write ! ( f, " RETURNING {}" , display_comma_separated( returning) ) ?;
636
608
}
637
-
638
- // ORDER BY clause (if present)
639
609
if !self . order_by . is_empty ( ) {
640
- let order_by = self
641
- . order_by
642
- . iter ( )
643
- . map ( |ob| ob. to_string ( ) )
644
- . collect :: < Vec < _ > > ( )
645
- . join ( ", " ) ;
646
- write ! ( f, "ORDER BY {} " , order_by) ?;
610
+ write ! ( f, " ORDER BY {}" , display_comma_separated( & self . order_by) ) ?;
647
611
}
648
-
649
- // LIMIT clause (if present)
650
612
if let Some ( limit) = & self . limit {
651
- write ! ( f, "LIMIT {}" , limit ) ?;
613
+ write ! ( f, " LIMIT {limit}" ) ?;
652
614
}
653
-
654
615
Ok ( ( ) )
655
616
}
656
617
}
0 commit comments