@@ -487,6 +487,90 @@ pub struct Insert {
487
487
pub insert_alias : Option < InsertAliases > ,
488
488
}
489
489
490
+ impl Display for Insert {
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 " ) ?;
495
+ } 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
+ }
503
+
504
+ if self . into {
505
+ write ! ( f, "INTO " ) ?;
506
+ }
507
+ if self . table {
508
+ write ! ( f, "TABLE " ) ?;
509
+ }
510
+ }
511
+
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) ?;
516
+ }
517
+
518
+ // Write columns if there are any
519
+ 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)
540
+ 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) ?;
548
+ }
549
+
550
+ // Write the source query if it exists
551
+ if let Some ( source) = & self . source {
552
+ write ! ( f, " {}" , source) ?;
553
+ }
554
+
555
+ // Write ON conflict handling for Sqlite, MySQL, etc.
556
+ if let Some ( on_conflict) = & self . on {
557
+ write ! ( f, " {}" , on_conflict) ?;
558
+ }
559
+
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) ?;
568
+ }
569
+
570
+ Ok ( ( ) )
571
+ }
572
+ }
573
+
490
574
/// DELETE statement.
491
575
#[ derive( Debug , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
492
576
#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
@@ -507,3 +591,65 @@ pub struct Delete {
507
591
/// LIMIT (MySQL)
508
592
pub limit : Option < Expr > ,
509
593
}
594
+
595
+ impl Display for Delete {
596
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
597
+ write ! ( f, "DELETE " ) ?;
598
+
599
+ // Handle multi-table DELETE if present
600
+ if !self . tables . is_empty ( ) {
601
+ let tables = self . tables
602
+ . iter ( )
603
+ . map ( |t| t. to_string ( ) )
604
+ . collect :: < Vec < _ > > ( )
605
+ . join ( ", " ) ;
606
+ write ! ( f, "{} " , tables) ?;
607
+ }
608
+
609
+ // The FromTable includes the `FROM` keyword.
610
+ write ! ( f, "{} " , self . from) ?;
611
+
612
+ // USING clause (if present)
613
+ if let Some ( using) = & self . using {
614
+ let uses = using
615
+ . iter ( )
616
+ . map ( |tab| tab. to_string ( ) )
617
+ . collect :: < Vec < _ > > ( )
618
+ . join ( ", " ) ;
619
+ write ! ( f, "USING {} " , uses) ?;
620
+ }
621
+
622
+ // WHERE clause (if present)
623
+ if let Some ( sel) = & self . selection {
624
+ write ! ( f, "WHERE {} " , sel) ?;
625
+ }
626
+
627
+ // RETURNING clause (if present)
628
+ if let Some ( ret) = & self . returning {
629
+ let rets = ret
630
+ . iter ( )
631
+ . map ( |col| col. to_string ( ) )
632
+ . collect :: < Vec < _ > > ( )
633
+ . join ( ", " ) ;
634
+ write ! ( f, "RETURNING {} " , rets) ?;
635
+ }
636
+
637
+ // ORDER BY clause (if present)
638
+ if !self . order_by . is_empty ( ) {
639
+ let order_by = self
640
+ . order_by
641
+ . iter ( )
642
+ . map ( |ob| ob. to_string ( ) )
643
+ . collect :: < Vec < _ > > ( )
644
+ . join ( ", " ) ;
645
+ write ! ( f, "ORDER BY {} " , order_by) ?;
646
+ }
647
+
648
+ // LIMIT clause (if present)
649
+ if let Some ( limit) = & self . limit {
650
+ write ! ( f, "LIMIT {}" , limit) ?;
651
+ }
652
+
653
+ Ok ( ( ) )
654
+ }
655
+ }
0 commit comments