easy_sqlx_core/sql/builder/
delete_builder.rs1use crate::sql::{
2 dialects::{
3 condition::{Condition, Where, WhereAppend},
4 schema::{self, schema::Schema},
5 },
6 schema::table::TableSchema,
7};
8
9use super::builder::ExecuteBuilder;
10use sqlx::Database;
11
12#[derive(Debug)]
13pub struct DeleteBuilder<'a> {
14 table: TableSchema,
15 default_schema: &'a str,
16 wh: Option<Where>,
17}
18
19impl<'a> DeleteBuilder<'a> {
20 pub fn new(table: TableSchema) -> Self {
21 Self {
22 table,
23 default_schema: "",
24 wh: None,
25 }
26 }
27
28 pub fn with_default_schema(mut self, schema: &'a str) -> Self {
29 self.default_schema = schema;
30 self
31 }
32}
33impl<'a> WhereAppend<Condition> for DeleteBuilder<'a> {
34 fn and(mut self, cond: Condition) -> Self {
35 if let Some(w) = self.wh {
36 self.wh = Some(w.and(cond));
37 } else {
38 self.wh = Some(Where::new(cond));
39 }
40 self
41 }
42
43 fn or(mut self, cond: Condition) -> Self {
44 if let Some(w) = self.wh {
45 self.wh = Some(w.or(cond));
46 } else {
47 self.wh = Some(Where::new(cond));
48 }
49 self
50 }
51}
52
53impl<'a> WhereAppend<Where> for DeleteBuilder<'a> {
54 fn and(mut self, wh: Where) -> Self {
55 if let Some(w) = self.wh {
56 self.wh = Some(w.and(wh));
57 } else {
58 self.wh = Some(wh);
59 }
60 self
61 }
62
63 fn or(mut self, wh: Where) -> Self {
64 if let Some(w) = self.wh {
65 self.wh = Some(w.or(wh));
66 } else {
67 self.wh = Some(wh);
68 }
69 self
70 }
71}
72
73#[cfg(feature = "postgres")]
74use sqlx::Postgres;
75
76impl<'a> ExecuteBuilder for DeleteBuilder<'a> {
77 #[cfg(feature = "postgres")]
78 type DB = Postgres;
79
80 async fn execute<C>(
81 &self,
82 conn: &mut C,
83 ) -> Result<<Self::DB as sqlx::Database>::QueryResult, sqlx::Error>
84 where
85 for<'e> &'e mut C: sqlx::Executor<'e, Database = Self::DB>,
86 {
87 let schema = schema::new(self.default_schema.to_string());
88
89 let sql = schema.sql_delete(&self.table, self.wh.clone(), false);
90
91 let mut query: sqlx::query::Query<'_, Self::DB, <Self::DB as Database>::Arguments<'_>> =
92 sqlx::query::<Self::DB>(&sql);
93
94 if let Some(w) = &self.wh {
95 query = w.bind_to_query(query);
96 }
97
98 let result = query.execute(conn).await.map_err(|err| {
99 tracing::error!(
100 "easy-sqlx: {} [{}]\n{:?}",
101 sql,
102 if let Some(w) = &self.wh {
103 w.list_params()
104 } else {
105 "".to_string()
106 },
107 err
108 );
109 err
110 })?;
111 #[cfg(feature = "logsql")]
112 tracing::info!(
113 "easy-sqlx: {sql} [{}]\nrows_affected: {}",
114 if let Some(w) = &self.wh {
115 w.list_params()
116 } else {
117 "".to_string()
118 },
119 result.rows_affected()
120 );
121 Ok(result)
122 }
123
124 #[cfg(feature = "postgres")]
125 async fn execute_return<'e, 'c: 'e, C, O>(&self, executor: C) -> sqlx::Result<Vec<O>>
126 where
127 C: 'e + sqlx::Executor<'c, Database = Self::DB>,
128 O: 'e,
129 for<'r> O: sqlx::FromRow<'r, <Self::DB as Database>::Row>,
130 O: std::marker::Send,
131 O: Unpin,
132 {
133 let schema = schema::new(self.default_schema.to_string());
134
135 let sql = schema.sql_delete(&self.table, self.wh.clone(), true);
136
137 let mut query = sqlx::query_as::<Self::DB, O>(&sql);
138
139 if let Some(w) = &self.wh {
140 query = w.bind_to_query_as(query);
141 }
142
143 let result = query.fetch_all(executor).await.map_err(|err| {
144 tracing::error!(
145 "easy-sqlx: {} [{}]\n{:?}",
146 sql,
147 if let Some(w) = &self.wh {
148 w.list_params()
149 } else {
150 "".to_string()
151 },
152 err
153 );
154 err
155 });
156 #[cfg(feature = "logsql")]
157 tracing::info!(
158 "easy-sqlx: {sql} [{}]",
159 if let Some(w) = &self.wh {
160 w.list_params()
161 } else {
162 "".to_string()
163 }
164 );
165 result
166 }
167}