Skip to content

Commit 2bdbd17

Browse files
committed
adding display to DELETE and INSERT
1 parent cb0c511 commit 2bdbd17

File tree

2 files changed

+176
-0
lines changed

2 files changed

+176
-0
lines changed

src/ast/dml.rs

+147
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,90 @@ pub struct Insert {
487487
pub insert_alias: Option<InsertAliases>,
488488
}
489489

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+
490574
/// DELETE statement.
491575
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
492576
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
@@ -507,3 +591,66 @@ pub struct Delete {
507591
/// LIMIT (MySQL)
508592
pub limit: Option<Expr>,
509593
}
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
602+
.tables
603+
.iter()
604+
.map(|t| t.to_string())
605+
.collect::<Vec<_>>()
606+
.join(", ");
607+
write!(f, "{} ", tables)?;
608+
}
609+
610+
// The FromTable includes the `FROM` keyword.
611+
write!(f, "{} ", self.from)?;
612+
613+
// USING clause (if present)
614+
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)?;
621+
}
622+
623+
// WHERE clause (if present)
624+
if let Some(sel) = &self.selection {
625+
write!(f, "WHERE {} ", sel)?;
626+
}
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)?;
636+
}
637+
638+
// ORDER BY clause (if present)
639+
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)?;
647+
}
648+
649+
// LIMIT clause (if present)
650+
if let Some(limit) = &self.limit {
651+
write!(f, "LIMIT {}", limit)?;
652+
}
653+
654+
Ok(())
655+
}
656+
}

src/ast/mod.rs

+29
Original file line numberDiff line numberDiff line change
@@ -2121,6 +2121,35 @@ pub enum FromTable {
21212121
WithoutKeyword(Vec<TableWithJoins>),
21222122
}
21232123

2124+
impl Display for FromTable {
2125+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2126+
match self {
2127+
FromTable::WithFromKeyword(tables) => {
2128+
write!(
2129+
f,
2130+
"FROM {}",
2131+
tables
2132+
.iter()
2133+
.map(|t| t.to_string())
2134+
.collect::<Vec<_>>()
2135+
.join(", ")
2136+
)
2137+
}
2138+
FromTable::WithoutKeyword(tables) => {
2139+
write!(
2140+
f,
2141+
"{}",
2142+
tables
2143+
.iter()
2144+
.map(|t| t.to_string())
2145+
.collect::<Vec<_>>()
2146+
.join(", ")
2147+
)
2148+
}
2149+
}
2150+
}
2151+
}
2152+
21242153
/// A top-level statement (SELECT, INSERT, CREATE, etc.)
21252154
#[allow(clippy::large_enum_variant)]
21262155
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]

0 commit comments

Comments
 (0)