good_ormning/sqlite/query/
delete.rs1use std::collections::HashMap;
2use crate::{
3 utils::Tokens,
4 sqlite::{
5 QueryResCount,
6 schema::table::Table,
7 },
8};
9use super::{
10 expr::{
11 check_bool,
12 Expr,
13 ExprType,
14 Binding,
15 },
16 select_body::Returning,
17 utils::{
18 build_returning,
19 build_with,
20 QueryBody,
21 With,
22 },
23};
24
25pub struct Delete {
26 pub with: Option<With>,
27 pub table: Table,
28 pub where_: Option<Expr>,
29 pub returning: Vec<Returning>,
30}
31
32impl QueryBody for Delete {
33 fn build(
34 &self,
35 ctx: &mut super::utils::SqliteQueryCtx,
36 path: &rpds::Vector<String>,
37 res_count: QueryResCount,
38 ) -> (super::expr::ExprType, crate::utils::Tokens) {
39 let mut out = Tokens::new();
40
41 if let Some(w) = &self.with {
43 out.s(&build_with(ctx, path, w).to_string());
44 }
45 let mut scope = HashMap::new();
46 for field in match ctx.tables.get(&self.table) {
47 Some(t) => t,
48 None => {
49 ctx.errs.err(path, format!("Unknown table {} for delete", self.table));
50 return (ExprType(vec![]), Tokens::new());
51 },
52 } {
53 scope.insert(Binding::field(field), field.type_.type_.clone());
54 }
55
56 out.s("delete from").id(&self.table.id);
58 if let Some(where_) = &self.where_ {
59 out.s("where");
60 let path = path.push_back("Where".into());
61 let (where_t, where_tokens) = where_.build(ctx, &path, &scope);
62 check_bool(ctx, &path, &where_t);
63 out.s(&where_tokens.to_string());
64 }
65 let out_type = build_returning(ctx, path, &scope, &mut out, &self.returning, res_count);
66 (out_type, out)
67 }
68}