Skip to content

Commit f6fa202

Browse files
author
system
committed
Add Transaction SAVEPOINTS, revert changes made to insert
1 parent 8edfdec commit f6fa202

5 files changed

Lines changed: 191 additions & 196 deletions

File tree

rwf/src/model/insert.rs

Lines changed: 15 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,15 @@
11
//! Implements the `SELECT` query.
2-
use super::{
3-
Column, Escape, FilterQuery, FromRow, Model, Placeholders, Select, ToColumn, ToSql, ToValue,
4-
WhereClause,
5-
};
6-
use crate::model::filter::JoinOp;
7-
use crate::model::select::Op;
8-
use crate::model::temporary::{With, WithQuery};
2+
use super::{Column, Escape, FromRow, Model, Placeholders, ToColumn, ToSql, ToValue};
93
use std::marker::PhantomData;
104

11-
#[derive(Debug, Clone, crate::prelude::Deserialize, crate::prelude::Serialize)]
12-
enum InsertValues<T: FromRow> {
13-
Values(Placeholders, i32),
14-
Select(Select<T>),
15-
}
16-
17-
impl<T: FromRow> InsertValues<T> {
18-
pub fn is_select(&self) -> bool {
19-
match self {
20-
Self::Select(_) => true,
21-
_ => false,
22-
}
23-
}
24-
}
25-
26-
impl<T: FromRow> AsRef<Placeholders> for InsertValues<T> {
27-
fn as_ref(&self) -> &Placeholders {
28-
match &self {
29-
Self::Values(placeholders, _) => placeholders,
30-
Self::Select(select) => select.get_placeholders(),
31-
}
32-
}
33-
}
34-
35-
impl<T: FromRow> AsMut<Placeholders> for InsertValues<T> {
36-
fn as_mut(&mut self) -> &mut Placeholders {
37-
match self {
38-
Self::Values(placeholders, _) => placeholders,
39-
Self::Select(select) => select.get_placeholders_mut(),
40-
}
41-
}
42-
}
43-
445
#[derive(Debug, Clone, crate::prelude::Deserialize, crate::prelude::Serialize)]
456
pub struct Insert<T: FromRow + ?Sized> {
467
table_name: String,
478
columns: Vec<Column>,
48-
values: InsertValues<T>,
9+
pub placeholders: Placeholders,
4910
marker: PhantomData<T>,
5011
no_conflict: bool,
5112
unique_by: Vec<Column>,
52-
with: With,
5313
}
5414

5515
impl<T: Model> Insert<T> {
@@ -63,12 +23,11 @@ impl<T: Model> Insert<T> {
6323

6424
Self {
6525
table_name: T::table_name().to_string(),
66-
values: InsertValues::Values(placeholders, 0),
6726
columns,
27+
placeholders,
6828
marker: PhantomData,
6929
no_conflict: false,
7030
unique_by: vec![],
71-
with: With::default(),
7231
}
7332
}
7433

@@ -82,11 +41,10 @@ impl<T: Model> Insert<T> {
8241
Insert {
8342
table_name: T::table_name().to_string(),
8443
columns: columns.iter().map(|c| c.to_column().unqualify()).collect(),
85-
values: InsertValues::Values(placeholders, 0),
44+
placeholders,
8645
marker: PhantomData,
8746
no_conflict: false,
8847
unique_by: vec![],
89-
with: With::default(),
9048
}
9149
}
9250

@@ -99,110 +57,6 @@ impl<T: Model> Insert<T> {
9957
self.unique_by = columns.iter().map(|c| c.to_column()).collect();
10058
self
10159
}
102-
103-
pub fn is_select(&self) -> bool {
104-
self.values.is_select()
105-
}
106-
}
107-
108-
impl<T: FromRow> FilterQuery for Insert<T> {
109-
fn get_table_name(&self) -> &str {
110-
self.table_name.as_str()
111-
}
112-
113-
fn get_where_clause(&self) -> &WhereClause {
114-
match &self.values {
115-
InsertValues::Select(select) => &select.get_where_clause(),
116-
InsertValues::Values(..) => unimplemented!(
117-
"FilterQuery is only implemented for INSERT where the source is a SELECT Statement"
118-
),
119-
}
120-
}
121-
122-
fn get_placeholders(&self) -> &Placeholders {
123-
self.values.as_ref()
124-
}
125-
126-
fn get_where_clause_mut(&mut self) -> &mut WhereClause {
127-
match &mut self.values {
128-
InsertValues::Select(select) => select.get_where_clause_mut(),
129-
InsertValues::Values(..) => unimplemented!(
130-
"FilterQuery is only implemented for INSERT where the source is a SELECT Statement"
131-
),
132-
}
133-
}
134-
135-
fn get_placeholders_mut(&mut self) -> &mut Placeholders {
136-
self.values.as_mut()
137-
}
138-
139-
fn filter(
140-
mut self,
141-
column: impl ToColumn,
142-
value: impl ToValue,
143-
join_op: JoinOp,
144-
op: Op,
145-
) -> Self {
146-
match self.values {
147-
InsertValues::Values(placeholders, offset) => {
148-
self.values = InsertValues::Values(placeholders, offset)
149-
}
150-
InsertValues::Select(select) => {
151-
self.values =
152-
InsertValues::Select(select.filter_internal(column, value, join_op, op))
153-
}
154-
}
155-
self
156-
}
157-
}
158-
159-
impl<T: FromRow> WithQuery for Insert<T> {
160-
fn with_statements(&self) -> &With {
161-
&self.with
162-
}
163-
164-
fn with_statements_mut(&mut self) -> &mut With {
165-
&mut self.with
166-
}
167-
168-
fn get_statement_offset(&self) -> i32 {
169-
match &self.values {
170-
InsertValues::Values(..) => self.columns.len() as i32,
171-
InsertValues::Select(select) => select.get_statement_offset(),
172-
}
173-
}
174-
175-
fn add_offset(&mut self, offset: i32) {
176-
match &mut self.values {
177-
InsertValues::Values(_, value_offset) => *value_offset += offset,
178-
InsertValues::Select(select) => select.add_offset(offset),
179-
}
180-
}
181-
182-
fn placeholders(&self) -> Placeholders {
183-
let mut placeholders = self.with.placeholders();
184-
placeholders.push(match &self.values {
185-
InsertValues::Values(placeholders, _) => placeholders.clone(),
186-
InsertValues::Select(select) => select.placeholders(),
187-
});
188-
Placeholders::from_iter(placeholders)
189-
}
190-
}
191-
192-
impl<T: Model> From<Select<T>> for Insert<T> {
193-
fn from(mut value: Select<T>) -> Self {
194-
let with = std::mem::take(value.with_statements_mut());
195-
let values = InsertValues::Select(value);
196-
Self {
197-
table_name: T::table_name().to_string(),
198-
columns: T::column_names().iter().map(Column::name).collect(),
199-
values,
200-
marker: Default::default(),
201-
no_conflict: false,
202-
unique_by: vec![],
203-
with,
204-
}
205-
}
20660
}
20761

20862
impl<T: FromRow> ToSql for Insert<T> {
@@ -214,6 +68,14 @@ impl<T: FromRow> ToSql for Insert<T> {
21468
.collect::<Vec<_>>()
21569
.join(", ");
21670

71+
let placeholders = self
72+
.columns
73+
.iter()
74+
.enumerate()
75+
.map(|(i, _)| format!("${}", i + 1))
76+
.collect::<Vec<_>>()
77+
.join(", ");
78+
21779
let no_conflict = if self.no_conflict {
21880
"ON CONFLICT DO NOTHING ".to_string()
21981
} else if !self.unique_by.is_empty() {
@@ -238,26 +100,12 @@ impl<T: FromRow> ToSql for Insert<T> {
238100
"".to_string()
239101
};
240102

241-
let values = match &self.values {
242-
InsertValues::Select(select) => select.to_sql(),
243-
InsertValues::Values(_, offset) => {
244-
let placeholders = self
245-
.columns
246-
.iter()
247-
.enumerate()
248-
.map(|(i, _)| format!("${}", i as i32 + 1 + offset + self.get_with_offset()))
249-
.collect::<Vec<_>>()
250-
.join(", ");
251-
format!("VALUES ({})", placeholders)
252-
}
253-
};
254103
format!(
255-
r#"{}INSERT INTO "{}" ({}) {} {}RETURNING *"#,
256-
self.with.to_sql(),
104+
r#"INSERT INTO "{}" ({}) VALUES ({}) {}RETURNING *"#,
257105
self.table_name.escape(),
258106
columns,
259-
values,
260-
no_conflict,
107+
placeholders,
108+
no_conflict
261109
)
262110
}
263111
}

rwf/src/model/mod.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ impl<T: Model> FilterQuery for Query<T> {
264264
Query::Delete(delete) => delete.get_table_name(),
265265
Query::Picked(picked) => picked.get_table_name(),
266266
Query::InsertIfNotExists { select, .. } => select.get_table_name(),
267-
Query::Insert(insert) => insert.get_table_name(),
267+
//Query::Insert(insert) => insert.get_table_name(),
268268
_ => T::table_name(),
269269
}
270270
}
@@ -275,7 +275,7 @@ impl<T: Model> FilterQuery for Query<T> {
275275
Query::Delete(delete) => delete.get_where_clause(),
276276
Query::Picked(picked) => picked.get_where_clause(),
277277
Query::InsertIfNotExists { select, .. } => select.get_where_clause(),
278-
Query::Insert(insert) => insert.get_where_clause(),
278+
//Query::Insert(insert) => insert.get_where_clause(),
279279
_query => {
280280
unimplemented!("FilterQuery is only implemented for SELECT, UPDATE and DELETE")
281281
}
@@ -288,7 +288,7 @@ impl<T: Model> FilterQuery for Query<T> {
288288
Query::Delete(delete) => delete.get_placeholders(),
289289
Query::Picked(picked) => picked.get_placeholders(),
290290
Query::InsertIfNotExists { select, .. } => select.get_placeholders(),
291-
Query::Insert(insert) => insert.get_placeholders(),
291+
//Query::Insert(insert) => insert.get_placeholders(),
292292
_query => {
293293
unimplemented!("FilterQuery is only implemented for SELECT, UP DATE and DELETE")
294294
}
@@ -301,7 +301,7 @@ impl<T: Model> FilterQuery for Query<T> {
301301
Query::Delete(delete) => delete.get_where_clause_mut(),
302302
Query::Picked(picked) => picked.get_where_clause_mut(),
303303
Query::InsertIfNotExists { select, .. } => select.get_where_clause_mut(),
304-
Query::Insert(insert) => insert.get_where_clause_mut(),
304+
//Query::Insert(insert) => insert.get_where_clause_mut(),
305305
_query => {
306306
unimplemented!("FilterQuery is only implemented for SELECT, UP DATE and DELETE")
307307
}
@@ -314,7 +314,7 @@ impl<T: Model> FilterQuery for Query<T> {
314314
Query::Delete(delete) => delete.get_placeholders_mut(),
315315
Query::Picked(picked) => picked.get_placeholders_mut(),
316316
Query::InsertIfNotExists { select, .. } => select.get_placeholders_mut(),
317-
Query::Insert(insert) => insert.get_placeholders_mut(),
317+
//Query::Insert(insert) => insert.get_placeholders_mut(),
318318
_query => {
319319
unimplemented!("FilterQuery is only implemented for SELECT, UP DATE and DELETE")
320320
}
@@ -335,7 +335,7 @@ impl<T: Model> FilterQuery for Query<T> {
335335
insert,
336336
created,
337337
},
338-
Query::Insert(insert) => Query::Insert(insert.filter(column, value, join_op, op)),
338+
//Query::Insert(insert) => Query::Insert(insert.filter(column, value, join_op, op)),
339339
query => query,
340340
}
341341
}
@@ -358,8 +358,8 @@ impl<T: Model> WithQuery for Query<T> {
358358
Query::Picked(picked) => picked.with_statements(),
359359
Query::Update(update) => update.with_statements(),
360360
Query::Delete(delete) => delete.with_statements(),
361-
Query::Insert(insert) => insert.with_statements(),
362361
Query::InsertIfNotExists { select, .. } => select.with_statements(),
362+
//Query::Insert(insert) => insert.with_statements(),
363363
query => unimplemented!("WithQuery is not implemented for {}", query.action()),
364364
}
365365
}
@@ -370,8 +370,8 @@ impl<T: Model> WithQuery for Query<T> {
370370
Query::Picked(picked) => picked.with_statements_mut(),
371371
Query::Update(update) => update.with_statements_mut(),
372372
Query::Delete(delete) => delete.with_statements_mut(),
373-
Query::Insert(insert) => insert.with_statements_mut(),
374373
Query::InsertIfNotExists { select, .. } => select.with_statements_mut(),
374+
//Query::Insert(insert) => insert.with_statements_mut(),
375375
query => unimplemented!("WithQuery is not implemented for {}", query.action()),
376376
}
377377
}
@@ -382,8 +382,8 @@ impl<T: Model> WithQuery for Query<T> {
382382
Query::Picked(picked) => picked.get_statement_offset(),
383383
Query::Update(update) => update.get_statement_offset(),
384384
Query::Delete(delete) => delete.get_statement_offset(),
385-
Query::Insert(insert) => insert.get_statement_offset(),
386385
Query::InsertIfNotExists { select, .. } => select.get_statement_offset(),
386+
//Query::Insert(insert) => insert.get_statement_offset(),
387387
_query => 0,
388388
}
389389
}
@@ -394,8 +394,8 @@ impl<T: Model> WithQuery for Query<T> {
394394
Query::Picked(picked) => picked.add_offset(offset),
395395
Query::Update(update) => update.add_offset(offset),
396396
Query::Delete(delete) => delete.add_offset(offset),
397-
Query::Insert(insert) => insert.add_offset(offset),
398397
Query::InsertIfNotExists { select, .. } => select.add_offset(offset),
398+
//Query::Insert(insert) => insert.add_offset(offset),
399399
_query => {}
400400
}
401401
}
@@ -406,8 +406,8 @@ impl<T: Model> WithQuery for Query<T> {
406406
Query::Picked(picked) => picked.placeholders(),
407407
Query::Update(update) => update.placeholders(),
408408
Query::Delete(delete) => delete.placeholders(),
409-
Query::Insert(insert) => insert.placeholders(),
410409
Query::InsertIfNotExists { select, .. } => select.placeholders(),
410+
Query::Insert(insert) => insert.placeholders.clone(),
411411
Query::Raw {
412412
query: _,
413413
placeholders,
@@ -1057,8 +1057,9 @@ impl<T: Model> Query<T> {
10571057

10581058
Query::Insert(insert) => {
10591059
let query = self.to_sql();
1060-
let placeholders = insert.placeholders();
1061-
let values = placeholders.values();
1060+
//let placeholders = insert.placeholders();
1061+
//let values = placeholders.values();
1062+
let values = insert.placeholders.values();
10621063
client.query_cached(&query, &values).await
10631064
}
10641065

@@ -1078,8 +1079,9 @@ impl<T: Model> Query<T> {
10781079

10791080
if result.is_empty() {
10801081
let query = insert.to_sql();
1081-
let placeholders = insert.placeholders();
1082-
let values = placeholders.values();
1082+
//let placeholders = insert.placeholders();
1083+
//let values = placeholders.values();
1084+
let values = insert.placeholders.values();
10831085
client.query_cached(&query, &values).await
10841086
} else {
10851087
Ok(result)
@@ -1144,7 +1146,7 @@ impl<T: Model> Query<T> {
11441146
let placeholders = match self {
11451147
Query::Select(select) => select.placeholders(),
11461148
Query::Update(update) => update.placeholders(),
1147-
Query::Insert(insert) => insert.placeholders(),
1149+
Query::Insert(insert) => insert.placeholders.clone(),
11481150
Query::Picked(picked) => picked.placeholders(),
11491151
_ => todo!("explain"),
11501152
};

0 commit comments

Comments
 (0)