Skip to content

Commit 38f1e57

Browse files
seve-martinezalamb
andauthored
feat: adding Display implementation to DELETE and INSERT (apache#1427)
Co-authored-by: Andrew Lamb <[email protected]>
1 parent a8432b5 commit 38f1e57

File tree

2 files changed

+128
-127
lines changed

2 files changed

+128
-127
lines changed

src/ast/dml.rs

+114-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616
// under the License.
1717

1818
#[cfg(not(feature = "std"))]
19-
use alloc::{boxed::Box, string::String, vec::Vec};
19+
use alloc::{
20+
boxed::Box,
21+
format,
22+
string::{String, ToString},
23+
vec::Vec,
24+
};
2025

2126
use core::fmt::{self, Display};
2227
#[cfg(feature = "serde")]
@@ -492,6 +497,81 @@ pub struct Insert {
492497
pub insert_alias: Option<InsertAliases>,
493498
}
494499

500+
impl Display for Insert {
501+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
502+
let table_name = if let Some(alias) = &self.table_alias {
503+
format!("{0} AS {alias}", self.table_name)
504+
} else {
505+
self.table_name.to_string()
506+
};
507+
508+
if let Some(action) = self.or {
509+
write!(f, "INSERT OR {action} INTO {table_name} ")?;
510+
} else {
511+
write!(
512+
f,
513+
"{start}",
514+
start = if self.replace_into {
515+
"REPLACE"
516+
} else {
517+
"INSERT"
518+
},
519+
)?;
520+
if let Some(priority) = self.priority {
521+
write!(f, " {priority}",)?;
522+
}
523+
524+
write!(
525+
f,
526+
"{ignore}{over}{int}{tbl} {table_name} ",
527+
table_name = table_name,
528+
ignore = if self.ignore { " IGNORE" } else { "" },
529+
over = if self.overwrite { " OVERWRITE" } else { "" },
530+
int = if self.into { " INTO" } else { "" },
531+
tbl = if self.table { " TABLE" } else { "" },
532+
)?;
533+
}
534+
if !self.columns.is_empty() {
535+
write!(f, "({}) ", display_comma_separated(&self.columns))?;
536+
}
537+
if let Some(ref parts) = self.partitioned {
538+
if !parts.is_empty() {
539+
write!(f, "PARTITION ({}) ", display_comma_separated(parts))?;
540+
}
541+
}
542+
if !self.after_columns.is_empty() {
543+
write!(f, "({}) ", display_comma_separated(&self.after_columns))?;
544+
}
545+
546+
if let Some(source) = &self.source {
547+
write!(f, "{source}")?;
548+
}
549+
550+
if self.source.is_none() && self.columns.is_empty() {
551+
write!(f, "DEFAULT VALUES")?;
552+
}
553+
554+
if let Some(insert_alias) = &self.insert_alias {
555+
write!(f, " AS {0}", insert_alias.row_alias)?;
556+
557+
if let Some(col_aliases) = &insert_alias.col_aliases {
558+
if !col_aliases.is_empty() {
559+
write!(f, " ({})", display_comma_separated(col_aliases))?;
560+
}
561+
}
562+
}
563+
564+
if let Some(on) = &self.on {
565+
write!(f, "{on}")?;
566+
}
567+
568+
if let Some(returning) = &self.returning {
569+
write!(f, " RETURNING {}", display_comma_separated(returning))?;
570+
}
571+
Ok(())
572+
}
573+
}
574+
495575
/// DELETE statement.
496576
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
497577
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
@@ -512,3 +592,36 @@ pub struct Delete {
512592
/// LIMIT (MySQL)
513593
pub limit: Option<Expr>,
514594
}
595+
596+
impl Display for Delete {
597+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
598+
write!(f, "DELETE ")?;
599+
if !self.tables.is_empty() {
600+
write!(f, "{} ", display_comma_separated(&self.tables))?;
601+
}
602+
match &self.from {
603+
FromTable::WithFromKeyword(from) => {
604+
write!(f, "FROM {}", display_comma_separated(from))?;
605+
}
606+
FromTable::WithoutKeyword(from) => {
607+
write!(f, "{}", display_comma_separated(from))?;
608+
}
609+
}
610+
if let Some(using) = &self.using {
611+
write!(f, " USING {}", display_comma_separated(using))?;
612+
}
613+
if let Some(selection) = &self.selection {
614+
write!(f, " WHERE {selection}")?;
615+
}
616+
if let Some(returning) = &self.returning {
617+
write!(f, " RETURNING {}", display_comma_separated(returning))?;
618+
}
619+
if !self.order_by.is_empty() {
620+
write!(f, " ORDER BY {}", display_comma_separated(&self.order_by))?;
621+
}
622+
if let Some(limit) = &self.limit {
623+
write!(f, " LIMIT {limit}")?;
624+
}
625+
Ok(())
626+
}
627+
}

src/ast/mod.rs

+14-126
Original file line numberDiff line numberDiff line change
@@ -2176,6 +2176,18 @@ pub enum FromTable {
21762176
/// <https://cloud.google.com/bigquery/docs/reference/standard-sql/dml-syntax#delete_statement>
21772177
WithoutKeyword(Vec<TableWithJoins>),
21782178
}
2179+
impl Display for FromTable {
2180+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2181+
match self {
2182+
FromTable::WithFromKeyword(tables) => {
2183+
write!(f, "FROM {}", display_comma_separated(tables))
2184+
}
2185+
FromTable::WithoutKeyword(tables) => {
2186+
write!(f, "{}", display_comma_separated(tables))
2187+
}
2188+
}
2189+
}
2190+
}
21792191

21802192
/// Policy type for a `CREATE POLICY` statement.
21812193
/// ```sql
@@ -3533,93 +3545,7 @@ impl fmt::Display for Statement {
35333545
}
35343546
Ok(())
35353547
}
3536-
Statement::Insert(insert) => {
3537-
let Insert {
3538-
or,
3539-
ignore,
3540-
into,
3541-
table_name,
3542-
table_alias,
3543-
overwrite,
3544-
partitioned,
3545-
columns,
3546-
after_columns,
3547-
source,
3548-
table,
3549-
on,
3550-
returning,
3551-
replace_into,
3552-
priority,
3553-
insert_alias,
3554-
} = insert;
3555-
let table_name = if let Some(alias) = table_alias {
3556-
format!("{table_name} AS {alias}")
3557-
} else {
3558-
table_name.to_string()
3559-
};
3560-
3561-
if let Some(action) = or {
3562-
write!(f, "INSERT OR {action} INTO {table_name} ")?;
3563-
} else {
3564-
write!(
3565-
f,
3566-
"{start}",
3567-
start = if *replace_into { "REPLACE" } else { "INSERT" },
3568-
)?;
3569-
if let Some(priority) = priority {
3570-
write!(f, " {priority}",)?;
3571-
}
3572-
3573-
write!(
3574-
f,
3575-
"{ignore}{over}{int}{tbl} {table_name} ",
3576-
table_name = table_name,
3577-
ignore = if *ignore { " IGNORE" } else { "" },
3578-
over = if *overwrite { " OVERWRITE" } else { "" },
3579-
int = if *into { " INTO" } else { "" },
3580-
tbl = if *table { " TABLE" } else { "" },
3581-
)?;
3582-
}
3583-
if !columns.is_empty() {
3584-
write!(f, "({}) ", display_comma_separated(columns))?;
3585-
}
3586-
if let Some(ref parts) = partitioned {
3587-
if !parts.is_empty() {
3588-
write!(f, "PARTITION ({}) ", display_comma_separated(parts))?;
3589-
}
3590-
}
3591-
if !after_columns.is_empty() {
3592-
write!(f, "({}) ", display_comma_separated(after_columns))?;
3593-
}
3594-
3595-
if let Some(source) = source {
3596-
write!(f, "{source}")?;
3597-
}
3598-
3599-
if source.is_none() && columns.is_empty() {
3600-
write!(f, "DEFAULT VALUES")?;
3601-
}
3602-
3603-
if let Some(insert_alias) = insert_alias {
3604-
write!(f, " AS {0}", insert_alias.row_alias)?;
3605-
3606-
if let Some(col_aliases) = &insert_alias.col_aliases {
3607-
if !col_aliases.is_empty() {
3608-
write!(f, " ({})", display_comma_separated(col_aliases))?;
3609-
}
3610-
}
3611-
}
3612-
3613-
if let Some(on) = on {
3614-
write!(f, "{on}")?;
3615-
}
3616-
3617-
if let Some(returning) = returning {
3618-
write!(f, " RETURNING {}", display_comma_separated(returning))?;
3619-
}
3620-
3621-
Ok(())
3622-
}
3548+
Statement::Insert(insert) => write!(f, "{insert}"),
36233549
Statement::Install {
36243550
extension_name: name,
36253551
} => write!(f, "INSTALL {name}"),
@@ -3696,45 +3622,7 @@ impl fmt::Display for Statement {
36963622
}
36973623
Ok(())
36983624
}
3699-
Statement::Delete(delete) => {
3700-
let Delete {
3701-
tables,
3702-
from,
3703-
using,
3704-
selection,
3705-
returning,
3706-
order_by,
3707-
limit,
3708-
} = delete;
3709-
write!(f, "DELETE ")?;
3710-
if !tables.is_empty() {
3711-
write!(f, "{} ", display_comma_separated(tables))?;
3712-
}
3713-
match from {
3714-
FromTable::WithFromKeyword(from) => {
3715-
write!(f, "FROM {}", display_comma_separated(from))?;
3716-
}
3717-
FromTable::WithoutKeyword(from) => {
3718-
write!(f, "{}", display_comma_separated(from))?;
3719-
}
3720-
}
3721-
if let Some(using) = using {
3722-
write!(f, " USING {}", display_comma_separated(using))?;
3723-
}
3724-
if let Some(selection) = selection {
3725-
write!(f, " WHERE {selection}")?;
3726-
}
3727-
if let Some(returning) = returning {
3728-
write!(f, " RETURNING {}", display_comma_separated(returning))?;
3729-
}
3730-
if !order_by.is_empty() {
3731-
write!(f, " ORDER BY {}", display_comma_separated(order_by))?;
3732-
}
3733-
if let Some(limit) = limit {
3734-
write!(f, " LIMIT {limit}")?;
3735-
}
3736-
Ok(())
3737-
}
3625+
Statement::Delete(delete) => write!(f, "{delete}"),
37383626
Statement::Close { cursor } => {
37393627
write!(f, "CLOSE {cursor}")?;
37403628

0 commit comments

Comments
 (0)