Skip to content

Commit c80931a

Browse files
committed
reusing display impls
1 parent 2bdbd17 commit c80931a

File tree

2 files changed

+71
-230
lines changed

2 files changed

+71
-230
lines changed

src/ast/dml.rs

+69-108
Original file line numberDiff line numberDiff line change
@@ -489,84 +489,75 @@ pub struct Insert {
489489

490490
impl Display for Insert {
491491
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)
495494
} 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+
};
503497

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}",)?;
509512
}
510-
}
511513

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+
)?;
516523
}
517-
518-
// Write columns if there are any
519524
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+
}
540532
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))?;
548534
}
549535

550-
// Write the source query if it exists
551536
if let Some(source) = &self.source {
552-
write!(f, " {}", source)?;
537+
write!(f, "{source}")?;
553538
}
554539

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")?;
558542
}
559543

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}")?;
568556
}
569557

558+
if let Some(returning) = &self.returning {
559+
write!(f, " RETURNING {}", display_comma_separated(returning))?;
560+
}
570561
Ok(())
571562
}
572563
}
@@ -595,62 +586,32 @@ pub struct Delete {
595586
impl Display for Delete {
596587
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
597588
write!(f, "DELETE ")?;
598-
599-
// Handle multi-table DELETE if present
600589
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+
}
608599
}
609-
610-
// The FromTable includes the `FROM` keyword.
611-
write!(f, "{} ", self.from)?;
612-
613-
// USING clause (if present)
614600
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))?;
621602
}
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}")?;
626605
}
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))?;
636608
}
637-
638-
// ORDER BY clause (if present)
639609
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))?;
647611
}
648-
649-
// LIMIT clause (if present)
650612
if let Some(limit) = &self.limit {
651-
write!(f, "LIMIT {}", limit)?;
613+
write!(f, " LIMIT {limit}")?;
652614
}
653-
654615
Ok(())
655616
}
656617
}

src/ast/mod.rs

+2-122
Original file line numberDiff line numberDiff line change
@@ -3419,91 +3419,7 @@ impl fmt::Display for Statement {
34193419
Ok(())
34203420
}
34213421
Statement::Insert(insert) => {
3422-
let Insert {
3423-
or,
3424-
ignore,
3425-
into,
3426-
table_name,
3427-
table_alias,
3428-
overwrite,
3429-
partitioned,
3430-
columns,
3431-
after_columns,
3432-
source,
3433-
table,
3434-
on,
3435-
returning,
3436-
replace_into,
3437-
priority,
3438-
insert_alias,
3439-
} = insert;
3440-
let table_name = if let Some(alias) = table_alias {
3441-
format!("{table_name} AS {alias}")
3442-
} else {
3443-
table_name.to_string()
3444-
};
3445-
3446-
if let Some(action) = or {
3447-
write!(f, "INSERT OR {action} INTO {table_name} ")?;
3448-
} else {
3449-
write!(
3450-
f,
3451-
"{start}",
3452-
start = if *replace_into { "REPLACE" } else { "INSERT" },
3453-
)?;
3454-
if let Some(priority) = priority {
3455-
write!(f, " {priority}",)?;
3456-
}
3457-
3458-
write!(
3459-
f,
3460-
"{ignore}{over}{int}{tbl} {table_name} ",
3461-
table_name = table_name,
3462-
ignore = if *ignore { " IGNORE" } else { "" },
3463-
over = if *overwrite { " OVERWRITE" } else { "" },
3464-
int = if *into { " INTO" } else { "" },
3465-
tbl = if *table { " TABLE" } else { "" },
3466-
)?;
3467-
}
3468-
if !columns.is_empty() {
3469-
write!(f, "({}) ", display_comma_separated(columns))?;
3470-
}
3471-
if let Some(ref parts) = partitioned {
3472-
if !parts.is_empty() {
3473-
write!(f, "PARTITION ({}) ", display_comma_separated(parts))?;
3474-
}
3475-
}
3476-
if !after_columns.is_empty() {
3477-
write!(f, "({}) ", display_comma_separated(after_columns))?;
3478-
}
3479-
3480-
if let Some(source) = source {
3481-
write!(f, "{source}")?;
3482-
}
3483-
3484-
if source.is_none() && columns.is_empty() {
3485-
write!(f, "DEFAULT VALUES")?;
3486-
}
3487-
3488-
if let Some(insert_alias) = insert_alias {
3489-
write!(f, " AS {0}", insert_alias.row_alias)?;
3490-
3491-
if let Some(col_aliases) = &insert_alias.col_aliases {
3492-
if !col_aliases.is_empty() {
3493-
write!(f, " ({})", display_comma_separated(col_aliases))?;
3494-
}
3495-
}
3496-
}
3497-
3498-
if let Some(on) = on {
3499-
write!(f, "{on}")?;
3500-
}
3501-
3502-
if let Some(returning) = returning {
3503-
write!(f, " RETURNING {}", display_comma_separated(returning))?;
3504-
}
3505-
3506-
Ok(())
3422+
write!(f, "{insert}")
35073423
}
35083424
Statement::Install {
35093425
extension_name: name,
@@ -3582,43 +3498,7 @@ impl fmt::Display for Statement {
35823498
Ok(())
35833499
}
35843500
Statement::Delete(delete) => {
3585-
let Delete {
3586-
tables,
3587-
from,
3588-
using,
3589-
selection,
3590-
returning,
3591-
order_by,
3592-
limit,
3593-
} = delete;
3594-
write!(f, "DELETE ")?;
3595-
if !tables.is_empty() {
3596-
write!(f, "{} ", display_comma_separated(tables))?;
3597-
}
3598-
match from {
3599-
FromTable::WithFromKeyword(from) => {
3600-
write!(f, "FROM {}", display_comma_separated(from))?;
3601-
}
3602-
FromTable::WithoutKeyword(from) => {
3603-
write!(f, "{}", display_comma_separated(from))?;
3604-
}
3605-
}
3606-
if let Some(using) = using {
3607-
write!(f, " USING {}", display_comma_separated(using))?;
3608-
}
3609-
if let Some(selection) = selection {
3610-
write!(f, " WHERE {selection}")?;
3611-
}
3612-
if let Some(returning) = returning {
3613-
write!(f, " RETURNING {}", display_comma_separated(returning))?;
3614-
}
3615-
if !order_by.is_empty() {
3616-
write!(f, " ORDER BY {}", display_comma_separated(order_by))?;
3617-
}
3618-
if let Some(limit) = limit {
3619-
write!(f, " LIMIT {limit}")?;
3620-
}
3621-
Ok(())
3501+
write!(f, "{delete}")
36223502
}
36233503
Statement::Close { cursor } => {
36243504
write!(f, "CLOSE {cursor}")?;

0 commit comments

Comments
 (0)