From 3b1da29b5085c20202a58c3ec479ec2b4e1837cf Mon Sep 17 00:00:00 2001 From: pedrocarlo Date: Wed, 28 May 2025 13:13:57 -0300 Subject: [PATCH] impl ToSqlString for DELETE stmt --- .../src/to_sql_string/stmt/delete.rs | 50 +++++++++++++++++++ .../src/to_sql_string/stmt/mod.rs | 2 + .../src/to_sql_string/stmt/select.rs | 49 +++++++++++------- 3 files changed, 84 insertions(+), 17 deletions(-) create mode 100644 vendored/sqlite3-parser/src/to_sql_string/stmt/delete.rs diff --git a/vendored/sqlite3-parser/src/to_sql_string/stmt/delete.rs b/vendored/sqlite3-parser/src/to_sql_string/stmt/delete.rs new file mode 100644 index 000000000..af6158ac1 --- /dev/null +++ b/vendored/sqlite3-parser/src/to_sql_string/stmt/delete.rs @@ -0,0 +1,50 @@ +use crate::{ast, to_sql_string::ToSqlString}; + +impl ToSqlString for ast::Delete { + fn to_sql_string(&self, context: &C) -> String { + format!( + "{}DELETE FROM {}{}{}{}{}{};", + self.with.as_ref().map_or("".to_string(), |with| format!( + "{} ", + with.to_sql_string(context) + )), + self.tbl_name.to_sql_string(context), + self.indexed + .as_ref() + .map_or("".to_string(), |indexed| format!( + " {}", + indexed.to_sql_string(context) + )), + self.where_clause + .as_ref() + .map_or("".to_string(), |expr| format!( + " {}", + expr.to_sql_string(context) + )), + self.returning + .as_ref() + .map_or("".to_string(), |returning| format!( + " {}", + returning + .iter() + .map(|col| col.to_sql_string(context)) + .collect::>() + .join(", ") + )), + self.order_by + .as_ref() + .map_or("".to_string(), |order_by| format!( + " ORDER BY {}", + order_by + .iter() + .map(|col| col.to_sql_string(context)) + .collect::>() + .join(", ") + )), + self.limit.as_ref().map_or("".to_string(), |limit| format!( + " {}", + limit.to_sql_string(context) + )) + ) + } +} diff --git a/vendored/sqlite3-parser/src/to_sql_string/stmt/mod.rs b/vendored/sqlite3-parser/src/to_sql_string/stmt/mod.rs index 4e9ba4a71..57b0fd5df 100644 --- a/vendored/sqlite3-parser/src/to_sql_string/stmt/mod.rs +++ b/vendored/sqlite3-parser/src/to_sql_string/stmt/mod.rs @@ -6,6 +6,7 @@ mod alter_table; mod create_table; mod create_trigger; mod create_virtual_table; +mod delete; mod select; impl ToSqlString for ast::Stmt { @@ -119,6 +120,7 @@ impl ToSqlString for ast::Stmt { Self::CreateVirtualTable(create_virtual_table) => { create_virtual_table.to_sql_string(context) } + Self::Delete(delete) => delete.to_sql_string(context), Self::Select(select) => format!("{};", select.to_sql_string(context)), _ => todo!(), } diff --git a/vendored/sqlite3-parser/src/to_sql_string/stmt/select.rs b/vendored/sqlite3-parser/src/to_sql_string/stmt/select.rs index c31012e2d..f8a36da26 100644 --- a/vendored/sqlite3-parser/src/to_sql_string/stmt/select.rs +++ b/vendored/sqlite3-parser/src/to_sql_string/stmt/select.rs @@ -7,18 +7,7 @@ impl ToSqlString for ast::Select { fn to_sql_string(&self, context: &C) -> String { let mut ret = Vec::new(); if let Some(with) = &self.with { - let joined_expr = with - .ctes - .iter() - .map(|cte| cte.to_sql_string(context)) - .collect::>() - .join(", "); - - ret.push(format!( - "WITH{} {}", - if with.recursive { " RECURSIVE " } else { "" }, - joined_expr - )); + ret.push(with.to_sql_string(context)); } ret.push(self.body.to_sql_string(context)); @@ -33,11 +22,7 @@ impl ToSqlString for ast::Select { ret.push(format!("ORDER BY {}", joined_cols)); } if let Some(limit) = &self.limit { - ret.push(format!("LIMIT {}", limit.expr.to_sql_string(context))); - // TODO: missing , + expr in ast - if let Some(offset) = &limit.offset { - ret.push(format!("OFFSET {}", offset.to_sql_string(context))); - } + ret.push(limit.to_sql_string(context)); } ret.join(" ") } @@ -210,6 +195,36 @@ impl ToSqlString for ast::SelectTable { } } +impl ToSqlString for ast::With { + fn to_sql_string(&self, context: &C) -> String { + format!( + "WITH{} {}", + if self.recursive { " RECURSIVE " } else { "" }, + self.ctes + .iter() + .map(|cte| cte.to_sql_string(context)) + .collect::>() + .join(", ") + ) + } +} + +impl ToSqlString for ast::Limit { + fn to_sql_string(&self, context: &C) -> String { + format!( + "LIMIT {}{}", + self.expr.to_sql_string(context), + self.offset + .as_ref() + .map_or("".to_string(), |offset| format!( + " OFFSET {}", + offset.to_sql_string(context) + )) + ) + // TODO: missing , + expr in ast + } +} + impl ToSqlString for ast::CommonTableExpr { fn to_sql_string(&self, context: &C) -> String { let mut ret = Vec::with_capacity(self.columns.as_ref().map_or(2, |cols| cols.len()));