impl ToSqlString for DELETE stmt

This commit is contained in:
pedrocarlo
2025-05-28 13:13:57 -03:00
parent 6ba6fae2c6
commit 3b1da29b50
3 changed files with 84 additions and 17 deletions

View File

@@ -0,0 +1,50 @@
use crate::{ast, to_sql_string::ToSqlString};
impl ToSqlString for ast::Delete {
fn to_sql_string<C: crate::to_sql_string::ToSqlContext>(&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::<Vec<_>>()
.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::<Vec<_>>()
.join(", ")
)),
self.limit.as_ref().map_or("".to_string(), |limit| format!(
" {}",
limit.to_sql_string(context)
))
)
}
}

View File

@@ -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!(),
}

View File

@@ -7,18 +7,7 @@ impl ToSqlString for ast::Select {
fn to_sql_string<C: ToSqlContext>(&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::<Vec<_>>()
.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<C: ToSqlContext>(&self, context: &C) -> String {
format!(
"WITH{} {}",
if self.recursive { " RECURSIVE " } else { "" },
self.ctes
.iter()
.map(|cte| cte.to_sql_string(context))
.collect::<Vec<_>>()
.join(", ")
)
}
}
impl ToSqlString for ast::Limit {
fn to_sql_string<C: ToSqlContext>(&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<C: ToSqlContext>(&self, context: &C) -> String {
let mut ret = Vec::with_capacity(self.columns.as_ref().map_or(2, |cols| cols.len()));