Skip to content

Commit c3a14cf

Browse files
authored
Merge pull request #42 from belchior/mysql-insert
Adds MySQL syntax to Insert builder
2 parents 352f587 + 5f443a4 commit c3a14cf

16 files changed

+1563
-593
lines changed

src/concat/mysql.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#[cfg(feature = "mysql")]
2-
use crate::{concat::concat_raw_before_after, fmt};
2+
use crate::{concat::concat_raw_before_after, fmt, utils};
33

44
#[cfg(feature = "mysql")]
55
pub(crate) trait ConcatPartition<Clause: PartialEq> {
@@ -15,12 +15,7 @@ pub(crate) trait ConcatPartition<Clause: PartialEq> {
1515
let fmt::Formatter { comma, lb, space, .. } = fmts;
1616

1717
let sql = if items.is_empty() == false {
18-
let column_names = items
19-
.iter()
20-
.filter(|column| column.is_empty() == false)
21-
.map(|column| column.as_str())
22-
.collect::<Vec<_>>()
23-
.join(comma);
18+
let column_names = utils::join(items, comma);
2419

2520
if column_names.is_empty() == false {
2621
format!("PARTITION{space}({column_names}){space}{lb}")

src/concat/non_standard.rs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#[cfg(any(feature = "postgresql", feature = "sqlite", feature = "mysql"))]
2-
use crate::{behavior::WithQuery, concat::concat_raw_before_after, fmt};
2+
use crate::{behavior::WithQuery, concat::concat_raw_before_after, fmt, utils};
33

44
#[cfg(any(feature = "postgresql", feature = "sqlite", feature = "mysql"))]
55
pub(crate) trait ConcatLimit<Clause: PartialEq> {
@@ -36,12 +36,7 @@ pub(crate) trait ConcatReturning<Clause: PartialEq> {
3636
) -> String {
3737
let fmt::Formatter { lb, space, comma, .. } = fmts;
3838
let sql = if items.is_empty() == false {
39-
let output_names = items
40-
.iter()
41-
.filter(|item| item.is_empty() == false)
42-
.map(|item| item.as_str())
43-
.collect::<Vec<_>>()
44-
.join(comma);
39+
let output_names = utils::join(items, comma);
4540
format!("RETURNING{space}{output_names}{space}{lb}")
4641
} else {
4742
"".to_string()
@@ -98,3 +93,31 @@ pub(crate) trait ConcatWith<Clause: PartialEq> {
9893
concat_raw_before_after(items_raw_before, items_raw_after, query, fmts, clause, sql)
9994
}
10095
}
96+
97+
#[cfg(feature = "mysql")]
98+
pub(crate) trait ConcatColumn<Clause: PartialEq> {
99+
fn concat_column(
100+
&self,
101+
items_raw_before: &Vec<(Clause, String)>,
102+
items_raw_after: &Vec<(Clause, String)>,
103+
query: String,
104+
fmts: &fmt::Formatter,
105+
clause: Clause,
106+
items: &Vec<String>,
107+
) -> String {
108+
let fmt::Formatter { lb, comma, space, .. } = fmts;
109+
110+
let sql = if items.is_empty() == false {
111+
let column_names = utils::join(&items, comma);
112+
if column_names.is_empty() == false {
113+
format!("({column_names}){space}{lb}")
114+
} else {
115+
"".to_string()
116+
}
117+
} else {
118+
"".to_string()
119+
};
120+
121+
concat_raw_before_after(items_raw_before, items_raw_after, query, fmts, clause, sql)
122+
}
123+
}

src/concat/sql_standard.rs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{concat::concat_raw_before_after, fmt, structure::LogicalOperator};
1+
use crate::{concat::concat_raw_before_after, fmt, structure::LogicalOperator, utils};
22

33
pub(crate) trait ConcatFrom<Clause: PartialEq> {
44
fn concat_from(
@@ -12,12 +12,7 @@ pub(crate) trait ConcatFrom<Clause: PartialEq> {
1212
) -> String {
1313
let fmt::Formatter { comma, lb, space, .. } = fmts;
1414
let sql = if items.is_empty() == false {
15-
let tables = items
16-
.iter()
17-
.filter(|item| item.is_empty() == false)
18-
.map(|item| item.as_str())
19-
.collect::<Vec<_>>()
20-
.join(comma);
15+
let tables = utils::join(items, comma);
2116
format!("FROM{space}{tables}{space}{lb}")
2217
} else {
2318
"".to_string()
@@ -61,12 +56,7 @@ pub(crate) trait ConcatOrderBy<Clause: PartialEq> {
6156
) -> String {
6257
let fmt::Formatter { comma, lb, space, .. } = fmts;
6358
let sql = if items.is_empty() == false {
64-
let columns = items
65-
.iter()
66-
.filter(|item| item.is_empty() == false)
67-
.map(|item| item.as_str())
68-
.collect::<Vec<_>>()
69-
.join(comma);
59+
let columns = utils::join(items, comma);
7060

7161
format!("ORDER BY{space}{columns}{space}{lb}")
7262
} else {
@@ -77,6 +67,28 @@ pub(crate) trait ConcatOrderBy<Clause: PartialEq> {
7767
}
7868
}
7969

70+
pub(crate) trait ConcatSet<Clause: PartialEq> {
71+
fn concat_set(
72+
&self,
73+
items_raw_before: &Vec<(Clause, String)>,
74+
items_raw_after: &Vec<(Clause, String)>,
75+
query: String,
76+
fmts: &fmt::Formatter,
77+
clause: Clause,
78+
items: &Vec<String>,
79+
) -> String {
80+
let fmt::Formatter { comma, lb, space, .. } = fmts;
81+
let sql = if items.is_empty() == false {
82+
let values = utils::join(items, comma);
83+
format!("SET{space}{values}{space}{lb}")
84+
} else {
85+
"".to_string()
86+
};
87+
88+
concat_raw_before_after(items_raw_before, items_raw_after, query, fmts, clause, sql)
89+
}
90+
}
91+
8092
pub(crate) trait ConcatWhere<Clause: PartialEq> {
8193
fn concat_where(
8294
&self,

src/concat/sqlite.rs

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,9 @@
22
use crate::{
33
concat::concat_raw_before_after,
44
fmt,
5-
structure::{InsertClause, InsertVars, UpdateClause, UpdateVars},
5+
structure::{UpdateClause, UpdateVars},
66
};
77

8-
#[cfg(feature = "sqlite")]
9-
pub(crate) trait ConcatInsert {
10-
fn concat_insert(
11-
&self,
12-
items_raw_before: &Vec<(InsertClause, String)>,
13-
items_raw_after: &Vec<(InsertClause, String)>,
14-
query: String,
15-
fmts: &fmt::Formatter,
16-
insert: &(InsertVars, String),
17-
) -> String {
18-
let fmt::Formatter { lb, space, .. } = fmts;
19-
20-
let (clause, sql) = match insert {
21-
(InsertVars::InsertInto, exp) if exp.is_empty() => (InsertClause::InsertInto, "".to_string()),
22-
(InsertVars::InsertInto, exp) => (InsertClause::InsertInto, format!("INSERT INTO{space}{exp}{space}{lb}")),
23-
24-
(InsertVars::InsertOr, exp) if exp.is_empty() => (InsertClause::InsertOr, "".to_string()),
25-
(InsertVars::InsertOr, exp) => (InsertClause::InsertOr, format!("INSERT OR{space}{exp}{space}{lb}")),
26-
27-
(InsertVars::ReplaceInto, exp) if exp.is_empty() => (InsertClause::ReplaceInto, "".to_string()),
28-
(InsertVars::ReplaceInto, exp) => (
29-
InsertClause::ReplaceInto,
30-
format!("REPLACE INTO{space}{exp}{space}{lb}"),
31-
),
32-
};
33-
34-
concat_raw_before_after(items_raw_before, items_raw_after, query, fmts, clause, sql)
35-
}
36-
}
37-
388
#[cfg(feature = "sqlite")]
399
pub(crate) trait ConcatUpdate {
4010
fn concat_update(

0 commit comments

Comments
 (0)