Skip to content

Commit f344ada

Browse files
authored
Merge pull request #19 from belchior/default-values-clause
Makes the default values clause available to standard syntax
2 parents 4511a84 + c3f4e3b commit f344ada

9 files changed

+98
-115
lines changed

src/behavior.rs

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,6 @@ pub(crate) trait ConcatSqlStandard<Clause: PartialEq> {
4444
format!("{query}{raw_sql}{space}{lb}")
4545
}
4646

47-
fn concat_values(
48-
&self,
49-
items_raw_before: &Vec<(Clause, String)>,
50-
items_raw_after: &Vec<(Clause, String)>,
51-
query: String,
52-
fmts: &fmt::Formatter,
53-
clause: Clause,
54-
items: &Vec<String>,
55-
) -> String {
56-
let fmt::Formatter { comma, lb, space, .. } = fmts;
57-
let sql = if items.is_empty() == false {
58-
let sep = format!("{comma}{lb}");
59-
let values = items.join(&sep);
60-
format!("VALUES{space}{lb}{values}{space}{lb}")
61-
} else {
62-
"".to_string()
63-
};
64-
65-
concat_raw_before_after(items_raw_before, items_raw_after, query, fmts, clause, sql)
66-
}
67-
6847
fn concat_where(
6948
&self,
7049
items_raw_before: &Vec<(Clause, String)>,
@@ -227,30 +206,6 @@ pub(crate) trait ConcatSqlite {
227206

228207
concat_raw_before_after(items_raw_before, items_raw_after, query, fmts, clause, sql)
229208
}
230-
231-
fn concat_values(
232-
&self,
233-
items_raw_before: &Vec<(InsertClause, String)>,
234-
items_raw_after: &Vec<(InsertClause, String)>,
235-
query: String,
236-
fmts: &fmt::Formatter,
237-
values: &Vec<String>,
238-
default_values: &bool,
239-
) -> String {
240-
let fmt::Formatter { comma, lb, space, .. } = fmts;
241-
242-
let (clause, sql) = if *default_values {
243-
(InsertClause::DefaultValues, format!("DEFAULT VALUES{space}{lb}"))
244-
} else if values.is_empty() == false {
245-
let sep = format!("{comma}{lb}");
246-
let values = values.join(&sep);
247-
(InsertClause::Values, format!("VALUES{space}{lb}{values}{space}{lb}"))
248-
} else {
249-
(InsertClause::Values, "".to_string())
250-
};
251-
252-
concat_raw_before_after(items_raw_before, items_raw_after, query, fmts, clause, sql)
253-
}
254209
}
255210

256211
impl std::fmt::Display for LogicalOperator {

src/insert/insert.rs

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,31 @@ impl Insert {
6565
self
6666
}
6767

68+
/// The `default values` clause
69+
///
70+
/// # Example
71+
///
72+
/// ```
73+
/// # use sql_query_builder as sql;
74+
/// let insert_query = sql::Insert::new()
75+
/// .insert_into("users")
76+
/// .default_values()
77+
/// .to_string();
78+
///
79+
/// # let expected = "INSERT INTO users DEFAULT VALUES";
80+
/// # assert_eq!(insert_query, expected);
81+
/// ```
82+
///
83+
/// Output
84+
///
85+
/// ```sql
86+
/// INSERT INTO users DEFAULT VALUES
87+
/// ```
88+
pub fn default_values(mut self) -> Self {
89+
self._default_values = true;
90+
self
91+
}
92+
6893
/// The `insert into` clause. This method overrides the previous value
6994
///
7095
/// # Example
@@ -381,34 +406,6 @@ impl Insert {
381406

382407
#[cfg(any(doc, feature = "sqlite"))]
383408
impl Insert {
384-
/// The `default values` clause, this method can be used enabling the feature flag `sqlite`
385-
///
386-
/// # Example
387-
///
388-
/// ```
389-
/// # #[cfg(feature = "sqlite")]
390-
/// # {
391-
/// # use sql_query_builder as sql;
392-
/// let insert_query = sql::Insert::new()
393-
/// .insert_into("users")
394-
/// .default_values()
395-
/// .to_string();
396-
///
397-
/// # let expected = "INSERT INTO users DEFAULT VALUES";
398-
/// # assert_eq!(insert_query, expected);
399-
/// # }
400-
/// ```
401-
///
402-
/// Output
403-
///
404-
/// ```sql
405-
/// INSERT INTO users DEFAULT VALUES
406-
/// ```
407-
pub fn default_values(mut self) -> Self {
408-
self._default_values = true;
409-
self
410-
}
411-
412409
/// The `insert into` clause, this method overrides the previous value and can be used enabling the feature flag `sqlite`
413410
#[cfg(not(doc))]
414411
pub fn insert_into(mut self, expression: &str) -> Self {

src/insert/insert_internal.rs

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#[cfg(any(feature = "postgresql", feature = "sqlite"))]
22
use crate::behavior::ConcatCommon;
3-
#[cfg(any(feature = "sqlite"))]
3+
#[cfg(feature = "sqlite")]
44
use crate::behavior::ConcatSqlite;
55
use crate::{
66
behavior::{concat_raw_before_after, Concat, ConcatSqlStandard},
@@ -87,6 +87,22 @@ impl Insert {
8787
sql,
8888
)
8989
}
90+
91+
fn concat_values(&self, query: String, fmts: &fmt::Formatter) -> String {
92+
let fmt::Formatter { comma, lb, space, .. } = fmts;
93+
94+
let (clause, sql) = if self._default_values {
95+
(InsertClause::DefaultValues, format!("DEFAULT VALUES{space}{lb}"))
96+
} else if self._values.is_empty() == false {
97+
let sep = format!("{comma}{lb}");
98+
let values = self._values.join(&sep);
99+
(InsertClause::Values, format!("VALUES{space}{lb}{values}{space}{lb}"))
100+
} else {
101+
(InsertClause::Values, "".to_string())
102+
};
103+
104+
concat_raw_before_after(&self._raw_before, &self._raw_after, query, fmts, clause, sql)
105+
}
90106
}
91107

92108
impl Concat for Insert {
@@ -118,32 +134,10 @@ impl Concat for Insert {
118134

119135
query = self.concat_overriding(query, &fmts);
120136

121-
#[cfg(not(feature = "sqlite"))]
122-
{
123-
query = ConcatSqlStandard::concat_values(
124-
self,
125-
&self._raw_before,
126-
&self._raw_after,
127-
query,
128-
&fmts,
129-
InsertClause::Values,
130-
&self._values,
131-
);
132-
}
133-
#[cfg(feature = "sqlite")]
134-
{
135-
query = ConcatSqlite::concat_values(
136-
self,
137-
&self._raw_before,
138-
&self._raw_after,
139-
query,
140-
&fmts,
141-
&self._values,
142-
&self._default_values,
143-
);
144-
}
137+
query = self.concat_values(query, &fmts);
145138

146139
query = self.concat_select(query, &fmts);
140+
147141
query = self.concat_on_conflict(query, &fmts);
148142

149143
#[cfg(any(feature = "postgresql", feature = "sqlite"))]

src/structure.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ pub enum DeleteClause {
5252
/// Builder to contruct a [Insert] command
5353
#[derive(Default, Clone)]
5454
pub struct Insert {
55+
pub(crate) _default_values: bool,
5556
pub(crate) _on_conflict: String,
5657
pub(crate) _overriding: String,
5758
pub(crate) _raw_after: Vec<(InsertClause, String)>,
@@ -69,9 +70,6 @@ pub struct Insert {
6970
pub(crate) _insert_into: String,
7071
#[cfg(feature = "sqlite")]
7172
pub(crate) _insert: (InsertVars, String),
72-
73-
#[cfg(feature = "sqlite")]
74-
pub(crate) _default_values: bool,
7573
}
7674

7775
#[cfg(feature = "sqlite")]
@@ -97,6 +95,7 @@ pub(crate) enum InsertVars {
9795
/// ```
9896
#[derive(PartialEq, Clone)]
9997
pub enum InsertClause {
98+
DefaultValues,
10099
InsertInto,
101100
OnConflict,
102101
Overriding,
@@ -107,12 +106,11 @@ pub enum InsertClause {
107106
Returning,
108107
#[cfg(any(feature = "postgresql", feature = "sqlite"))]
109108
With,
110-
#[cfg(any(feature = "sqlite"))]
109+
110+
#[cfg(feature = "sqlite")]
111111
InsertOr,
112-
#[cfg(any(feature = "sqlite"))]
112+
#[cfg(feature = "sqlite")]
113113
ReplaceInto,
114-
#[cfg(any(feature = "sqlite"))]
115-
DefaultValues,
116114
}
117115

118116
#[derive(Clone, PartialEq)]

src/values/values_internal.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,40 @@
11
use crate::{
2-
behavior::{Concat, ConcatSqlStandard},
2+
behavior::{concat_raw_before_after, Concat, ConcatSqlStandard},
33
fmt,
44
structure::{Values, ValuesClause},
55
};
66

77
impl ConcatSqlStandard<ValuesClause> for Values {}
88

9-
impl Concat for Values {
10-
fn concat(&self, fmts: &fmt::Formatter) -> String {
11-
let mut query = "".to_string();
9+
impl Values {
10+
fn concat_values(&self, query: String, fmts: &fmt::Formatter) -> String {
11+
let fmt::Formatter { comma, lb, space, .. } = fmts;
12+
let sql = if self._values.is_empty() == false {
13+
let sep = format!("{comma}{lb}");
14+
let values = self._values.join(&sep);
15+
format!("VALUES{space}{lb}{values}{space}{lb}")
16+
} else {
17+
"".to_string()
18+
};
1219

13-
query = self.concat_raw(query, &fmts, &self._raw);
14-
query = self.concat_values(
20+
concat_raw_before_after(
1521
&self._raw_before,
1622
&self._raw_after,
1723
query,
18-
&fmts,
24+
fmts,
1925
ValuesClause::Values,
20-
&self._values,
21-
);
26+
sql,
27+
)
28+
}
29+
}
30+
31+
impl Concat for Values {
32+
fn concat(&self, fmts: &fmt::Formatter) -> String {
33+
let mut query = "".to_string();
34+
35+
query = self.concat_raw(query, &fmts, &self._raw);
36+
37+
query = self.concat_values(query, &fmts);
2238

2339
query.trim_end().to_string()
2440
}

tests/clause_default_values_spec.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#[cfg(feature = "sqlite")]
21
mod insert_command {
32
use pretty_assertions::assert_eq;
43
use sql_query_builder as sql;

tests/clause_insert_or_spec.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ mod insert_command {
2424
assert_eq!(query, expected_query);
2525
}
2626

27+
#[test]
28+
fn method_insert_or_should_not_add_clause_when_argument_is_empty() {
29+
let query = sql::Insert::new().insert_or("").as_string();
30+
let expected_query = "";
31+
32+
assert_eq!(query, expected_query);
33+
}
34+
2735
#[test]
2836
fn method_insert_or_should_trim_space_of_the_argument() {
2937
let query = sql::Insert::new().insert_or(" IGNORE INTO users (name) ").as_string();

tests/clause_replace_into_spec.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ mod insert_command {
2222
assert_eq!(query, expected_query);
2323
}
2424

25+
#[test]
26+
fn method_replace_into_should_not_add_clause_when_argument_is_empty() {
27+
let query = sql::Insert::new().replace_into("").as_string();
28+
let expected_query = "";
29+
30+
assert_eq!(query, expected_query);
31+
}
32+
2533
#[test]
2634
fn method_replace_into_should_trim_space_of_the_argument() {
2735
let query = sql::Insert::new().replace_into(" users (name) ").as_string();

tests/clause_update_or_spec.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ mod update_command {
4141
assert_eq!(query, expected_query);
4242
}
4343

44+
#[test]
45+
fn method_update_or_should_not_add_clause_when_argument_is_empty() {
46+
let query = sql::Update::new().update_or("").as_string();
47+
let expected_query = "";
48+
49+
assert_eq!(query, expected_query);
50+
}
51+
4452
#[test]
4553
fn method_update_or_should_trim_space_of_the_argument() {
4654
let query = sql::Update::new().update_or(" REPLACE orders ").as_string();

0 commit comments

Comments
 (0)