Skip to content

Commit 9c1e7c5

Browse files
committed
refactor's to make the code base more scalable to receive more feature flags
1 parent 9fdbc39 commit 9c1e7c5

File tree

10 files changed

+255
-229
lines changed

10 files changed

+255
-229
lines changed

src/behavior.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub fn raw_queries<'a, Clause: PartialEq>(raw_list: &'a Vec<(Clause, String)>, c
1616
.collect::<Vec<_>>()
1717
}
1818

19-
/// Represents all statements that can be used in the with method
19+
/// Represents all statements that can be used inside the with method
2020
pub trait WithQuery: Concat {}
2121

2222
pub trait Concat {
@@ -40,7 +40,7 @@ pub fn concat_raw_before_after<Clause: PartialEq>(
4040
format!("{query}{raw_before}{space_before}{sql}{raw_after}{space_after}")
4141
}
4242

43-
pub trait ConcatMethods<'a, Clause: PartialEq> {
43+
pub trait ConcatSqlStandard<'a, Clause: PartialEq> {
4444
fn concat_from(
4545
&self,
4646
items_raw_before: &Vec<(Clause, String)>,
@@ -71,8 +71,7 @@ pub trait ConcatMethods<'a, Clause: PartialEq> {
7171
format!("{query}{raw_sql}{space}{lb}")
7272
}
7373

74-
#[cfg(feature = "postgresql")]
75-
fn concat_returning(
74+
fn concat_values(
7675
&self,
7776
items_raw_before: &Vec<(Clause, String)>,
7877
items_raw_after: &Vec<(Clause, String)>,
@@ -81,18 +80,19 @@ pub trait ConcatMethods<'a, Clause: PartialEq> {
8180
clause: Clause,
8281
items: &Vec<String>,
8382
) -> String {
84-
let fmt::Formatter { lb, space, comma, .. } = fmts;
83+
let fmt::Formatter { comma, lb, space, .. } = fmts;
8584
let sql = if items.is_empty() == false {
86-
let output_names = items.join(comma);
87-
format!("RETURNING{space}{output_names}{space}{lb}")
85+
let sep = format!("{comma}{lb}");
86+
let values = items.join(&sep);
87+
format!("VALUES{space}{lb}{values}{space}{lb}")
8888
} else {
8989
"".to_owned()
9090
};
9191

9292
concat_raw_before_after(items_raw_before, items_raw_after, query, fmts, clause, sql)
9393
}
9494

95-
fn concat_values(
95+
fn concat_where(
9696
&self,
9797
items_raw_before: &Vec<(Clause, String)>,
9898
items_raw_after: &Vec<(Clause, String)>,
@@ -101,19 +101,21 @@ pub trait ConcatMethods<'a, Clause: PartialEq> {
101101
clause: Clause,
102102
items: &Vec<String>,
103103
) -> String {
104-
let fmt::Formatter { comma, lb, space, .. } = fmts;
104+
let fmt::Formatter { lb, space, indent, .. } = fmts;
105105
let sql = if items.is_empty() == false {
106-
let sep = format!("{comma}{lb}");
107-
let values = items.join(&sep);
108-
format!("VALUES{space}{lb}{values}{space}{lb}")
106+
let conditions = items.join(&format!("{space}{lb}{indent}AND{space}"));
107+
format!("WHERE{space}{conditions}{space}{lb}")
109108
} else {
110109
"".to_owned()
111110
};
112111

113112
concat_raw_before_after(items_raw_before, items_raw_after, query, fmts, clause, sql)
114113
}
114+
}
115115

116-
fn concat_where(
116+
#[cfg(feature = "postgresql")]
117+
pub trait ConcatPostgres<'a, Clause: PartialEq> {
118+
fn concat_returning(
117119
&self,
118120
items_raw_before: &Vec<(Clause, String)>,
119121
items_raw_after: &Vec<(Clause, String)>,
@@ -122,18 +124,17 @@ pub trait ConcatMethods<'a, Clause: PartialEq> {
122124
clause: Clause,
123125
items: &Vec<String>,
124126
) -> String {
125-
let fmt::Formatter { lb, space, indent, .. } = fmts;
127+
let fmt::Formatter { lb, space, comma, .. } = fmts;
126128
let sql = if items.is_empty() == false {
127-
let conditions = items.join(&format!("{space}{lb}{indent}AND{space}"));
128-
format!("WHERE{space}{conditions}{space}{lb}")
129+
let output_names = items.join(comma);
130+
format!("RETURNING{space}{output_names}{space}{lb}")
129131
} else {
130132
"".to_owned()
131133
};
132134

133135
concat_raw_before_after(items_raw_before, items_raw_after, query, fmts, clause, sql)
134136
}
135137

136-
#[cfg(feature = "postgresql")]
137138
fn concat_with(
138139
&self,
139140
items_raw_before: &Vec<(Clause, String)>,

src/delete/delete.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,6 @@ impl<'a> Delete<'a> {
172172
self
173173
}
174174

175-
/// The returning clause, this method can be used enabling the feature flag `postgresql`
176-
#[cfg(any(doc, feature = "postgresql"))]
177-
pub fn returning(mut self, output_name: &str) -> Self {
178-
push_unique(&mut self._returning, output_name.trim().to_owned());
179-
self
180-
}
181-
182175
/// The where clause
183176
///
184177
/// # Examples
@@ -193,6 +186,15 @@ impl<'a> Delete<'a> {
193186
push_unique(&mut self._where, condition.trim().to_owned());
194187
self
195188
}
189+
}
190+
191+
#[cfg(any(doc, feature = "postgresql"))]
192+
impl<'a> Delete<'a> {
193+
/// The returning clause, this method can be used enabling the feature flag `postgresql`
194+
pub fn returning(mut self, output_name: &str) -> Self {
195+
push_unique(&mut self._returning, output_name.trim().to_owned());
196+
self
197+
}
196198

197199
/// The with clause, this method can be used enabling the feature flag `postgresql`
198200
///
@@ -219,7 +221,6 @@ impl<'a> Delete<'a> {
219221
/// DELETE FROM users
220222
/// WHERE id in (select * from deactivated_users)
221223
/// ```
222-
#[cfg(any(doc, feature = "postgresql"))]
223224
pub fn with(mut self, name: &'a str, query: impl WithQuery + 'static) -> Self {
224225
self._with.push((name.trim(), std::sync::Arc::new(query)));
225226
self

src/delete/delete_internal.rs

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,36 @@
1+
#[cfg(feature = "postgresql")]
2+
use crate::behavior::ConcatPostgres;
13
use crate::{
2-
behavior::{concat_raw_before_after, Concat, ConcatMethods},
4+
behavior::{concat_raw_before_after, Concat, ConcatSqlStandard},
35
fmt,
46
structure::{Delete, DeleteClause},
57
};
68

7-
impl<'a> ConcatMethods<'a, DeleteClause> for Delete<'_> {}
9+
impl<'a> ConcatSqlStandard<'a, DeleteClause> for Delete<'_> {}
10+
11+
#[cfg(feature = "postgresql")]
12+
impl<'a> ConcatPostgres<'a, DeleteClause> for Delete<'_> {}
13+
14+
impl Delete<'_> {
15+
fn concat_delete_from(&self, query: String, fmts: &fmt::Formatter) -> String {
16+
let fmt::Formatter { lb, space, .. } = fmts;
17+
let sql = if self._delete_from.is_empty() == false {
18+
let table_name = self._delete_from;
19+
format!("DELETE FROM{space}{table_name}{space}{lb}")
20+
} else {
21+
"".to_owned()
22+
};
23+
24+
concat_raw_before_after(
25+
&self._raw_before,
26+
&self._raw_after,
27+
query,
28+
fmts,
29+
DeleteClause::DeleteFrom,
30+
sql,
31+
)
32+
}
33+
}
834

935
impl Concat for Delete<'_> {
1036
fn concat(&self, fmts: &fmt::Formatter) -> String {
@@ -46,24 +72,3 @@ impl Concat for Delete<'_> {
4672
query.trim_end().to_owned()
4773
}
4874
}
49-
50-
impl Delete<'_> {
51-
fn concat_delete_from(&self, query: String, fmts: &fmt::Formatter) -> String {
52-
let fmt::Formatter { lb, space, .. } = fmts;
53-
let sql = if self._delete_from.is_empty() == false {
54-
let table_name = self._delete_from;
55-
format!("DELETE FROM{space}{table_name}{space}{lb}")
56-
} else {
57-
"".to_owned()
58-
};
59-
60-
concat_raw_before_after(
61-
&self._raw_before,
62-
&self._raw_after,
63-
query,
64-
fmts,
65-
DeleteClause::DeleteFrom,
66-
sql,
67-
)
68-
}
69-
}

src/insert/insert.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -198,18 +198,20 @@ impl<'a> Insert<'a> {
198198
self
199199
}
200200

201-
/// The returning clause, this method can be used enabling the feature flag `postgresql`
202-
#[cfg(any(doc, feature = "postgresql"))]
203-
pub fn returning(mut self, output_name: &str) -> Self {
204-
push_unique(&mut self._returning, output_name.trim().to_owned());
205-
self
206-
}
207-
208201
/// The values clause
209202
pub fn values(mut self, value: &str) -> Self {
210203
push_unique(&mut self._values, value.trim().to_owned());
211204
self
212205
}
206+
}
207+
208+
#[cfg(any(doc, feature = "postgresql"))]
209+
impl<'a> Insert<'a> {
210+
/// The returning clause, this method can be used enabling the feature flag `postgresql`
211+
pub fn returning(mut self, output_name: &str) -> Self {
212+
push_unique(&mut self._returning, output_name.trim().to_owned());
213+
self
214+
}
213215

214216
/// The with clause, this method can be used enabling the feature flag `postgresql`
215217
///
@@ -237,7 +239,6 @@ impl<'a> Insert<'a> {
237239
/// SELECT *
238240
/// FROM active_users
239241
/// ```
240-
#[cfg(any(doc, feature = "postgresql"))]
241242
pub fn with(mut self, name: &'a str, query: impl WithQuery + 'static) -> Self {
242243
self._with.push((name.trim(), std::sync::Arc::new(query)));
243244
self

src/insert/insert_internal.rs

Lines changed: 51 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,15 @@
1+
#[cfg(feature = "postgresql")]
2+
use crate::behavior::ConcatPostgres;
13
use crate::{
2-
behavior::{concat_raw_before_after, Concat, ConcatMethods},
4+
behavior::{concat_raw_before_after, Concat, ConcatSqlStandard},
35
fmt,
46
structure::{Insert, InsertClause},
57
};
68

7-
impl<'a> ConcatMethods<'a, InsertClause> for Insert<'_> {}
9+
impl<'a> ConcatSqlStandard<'a, InsertClause> for Insert<'_> {}
810

9-
impl Concat for Insert<'_> {
10-
fn concat(&self, fmts: &fmt::Formatter) -> String {
11-
let mut query = "".to_owned();
12-
13-
query = self.concat_raw(query, &fmts, &self._raw);
14-
#[cfg(feature = "postgresql")]
15-
{
16-
query = self.concat_with(
17-
&self._raw_before,
18-
&self._raw_after,
19-
query,
20-
&fmts,
21-
InsertClause::With,
22-
&self._with,
23-
);
24-
}
25-
query = self.concat_insert_into(query, &fmts);
26-
query = self.concat_overriding(query, &fmts);
27-
query = self.concat_values(
28-
&self._raw_before,
29-
&self._raw_after,
30-
query,
31-
&fmts,
32-
InsertClause::Values,
33-
&self._values,
34-
);
35-
query = self.concat_select(query, &fmts);
36-
query = self.concat_on_conflict(query, &fmts);
37-
38-
#[cfg(feature = "postgresql")]
39-
{
40-
query = self.concat_returning(
41-
&self._raw_before,
42-
&self._raw_after,
43-
query,
44-
&fmts,
45-
InsertClause::Returning,
46-
&self._returning,
47-
);
48-
}
49-
50-
query.trim_end().to_owned()
51-
}
52-
}
11+
#[cfg(feature = "postgresql")]
12+
impl<'a> ConcatPostgres<'a, InsertClause> for Insert<'_> {}
5313

5414
impl Insert<'_> {
5515
fn concat_insert_into(&self, query: String, fmts: &fmt::Formatter) -> String {
@@ -128,3 +88,48 @@ impl Insert<'_> {
12888
)
12989
}
13090
}
91+
92+
impl Concat for Insert<'_> {
93+
fn concat(&self, fmts: &fmt::Formatter) -> String {
94+
let mut query = "".to_owned();
95+
96+
query = self.concat_raw(query, &fmts, &self._raw);
97+
#[cfg(feature = "postgresql")]
98+
{
99+
query = self.concat_with(
100+
&self._raw_before,
101+
&self._raw_after,
102+
query,
103+
&fmts,
104+
InsertClause::With,
105+
&self._with,
106+
);
107+
}
108+
query = self.concat_insert_into(query, &fmts);
109+
query = self.concat_overriding(query, &fmts);
110+
query = self.concat_values(
111+
&self._raw_before,
112+
&self._raw_after,
113+
query,
114+
&fmts,
115+
InsertClause::Values,
116+
&self._values,
117+
);
118+
query = self.concat_select(query, &fmts);
119+
query = self.concat_on_conflict(query, &fmts);
120+
121+
#[cfg(feature = "postgresql")]
122+
{
123+
query = self.concat_returning(
124+
&self._raw_before,
125+
&self._raw_after,
126+
query,
127+
&fmts,
128+
InsertClause::Returning,
129+
&self._returning,
130+
);
131+
}
132+
133+
query.trim_end().to_owned()
134+
}
135+
}

0 commit comments

Comments
 (0)