Skip to content

Commit bece121

Browse files
committed
refactor mysql concat_partition
1 parent ad36950 commit bece121

File tree

6 files changed

+158
-143
lines changed

6 files changed

+158
-143
lines changed

src/concat/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::fmt;
22

3+
pub(crate) mod mysql;
34
pub(crate) mod non_standard;
45
pub(crate) mod sql_standard;
56
pub(crate) mod sqlite;

src/concat/mysql.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#[cfg(feature = "mysql")]
2+
use crate::{concat::concat_raw_before_after, fmt};
3+
4+
#[cfg(feature = "mysql")]
5+
pub(crate) trait ConcatPartition<Clause: PartialEq> {
6+
fn concat_partition(
7+
&self,
8+
items_raw_before: &Vec<(Clause, String)>,
9+
items_raw_after: &Vec<(Clause, String)>,
10+
query: String,
11+
fmts: &fmt::Formatter,
12+
clause: Clause,
13+
items: &Vec<String>,
14+
) -> String {
15+
let fmt::Formatter { comma, lb, space, .. } = fmts;
16+
17+
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);
24+
25+
if column_names.is_empty() == false {
26+
format!("PARTITION{space}({column_names}){space}{lb}")
27+
} else {
28+
"".to_string()
29+
}
30+
} else {
31+
"".to_string()
32+
};
33+
34+
concat_raw_before_after(items_raw_before, items_raw_after, query, fmts, clause, sql)
35+
}
36+
}

src/concat/non_standard.rs

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

44
#[cfg(any(feature = "postgresql", feature = "sqlite"))]
@@ -51,7 +51,7 @@ pub(crate) trait ConcatReturning<Clause: PartialEq> {
5151
}
5252
}
5353

54-
#[cfg(any(feature = "postgresql", feature = "sqlite"))]
54+
#[cfg(any(feature = "postgresql", feature = "sqlite", feature = "mysql"))]
5555
pub(crate) trait ConcatWith<Clause: PartialEq> {
5656
fn concat_with(
5757
&self,

src/select/select_internal.rs

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,14 @@ impl Concat for Select {
5151

5252
#[cfg(feature = "mysql")]
5353
{
54-
query = self.concat_partition(query, &fmts);
54+
query = self.concat_partition(
55+
&self._raw_before,
56+
&self._raw_after,
57+
query,
58+
&fmts,
59+
SelectClause::Partition,
60+
&self._partition,
61+
);
5562
}
5663

5764
query = self.concat_where(
@@ -281,35 +288,7 @@ impl Select {
281288
}
282289

283290
#[cfg(feature = "mysql")]
284-
impl Select {
285-
fn concat_partition(&self, query: String, fmts: &fmt::Formatter) -> String {
286-
let fmt::Formatter { comma, lb, space, .. } = fmts;
291+
use crate::concat::mysql::ConcatPartition;
287292

288-
let sql = if self._partition.is_empty() == false {
289-
let column_names = self
290-
._partition
291-
.iter()
292-
.filter(|column| column.is_empty() == false)
293-
.map(|column| column.as_str())
294-
.collect::<Vec<_>>()
295-
.join(comma);
296-
297-
if column_names.is_empty() == false {
298-
format!("PARTITION{space}({column_names}){space}{lb}")
299-
} else {
300-
"".to_string()
301-
}
302-
} else {
303-
"".to_string()
304-
};
305-
306-
concat_raw_before_after(
307-
&self._raw_before,
308-
&self._raw_after,
309-
query,
310-
fmts,
311-
SelectClause::Partition,
312-
sql,
313-
)
314-
}
315-
}
293+
#[cfg(feature = "mysql")]
294+
impl ConcatPartition<SelectClause> for Select {}

tests/clause_partition_spec.rs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#[cfg(feature = "mysql")]
2+
mod select_command {
3+
use pretty_assertions::assert_eq;
4+
use sql_query_builder as sql;
5+
6+
#[test]
7+
fn method_partition_should_define_the_partition_clause() {
8+
let query = sql::Select::new().partition("p0").as_string();
9+
let expected_query = "PARTITION (p0)";
10+
11+
assert_eq!(expected_query, query);
12+
}
13+
14+
#[test]
15+
fn method_partition_should_not_defined_the_clause_without_partition_names() {
16+
let query = sql::Select::new().partition("").as_string();
17+
let expected_query = "";
18+
19+
assert_eq!(expected_query, query);
20+
}
21+
22+
#[test]
23+
fn method_partition_should_accumulate_names_on_consecutive_calls() {
24+
let query = sql::Select::new().partition("p0").partition("p1").as_string();
25+
26+
let expected_query = "PARTITION (p0, p1)";
27+
28+
assert_eq!(expected_query, query);
29+
}
30+
31+
#[test]
32+
fn method_partition_should_not_accumulate_values_when_expression_is_empty() {
33+
let query = sql::Select::new()
34+
.partition("")
35+
.partition("p0")
36+
.partition("")
37+
.as_string();
38+
39+
let expected_query = "PARTITION (p0)";
40+
41+
assert_eq!(expected_query, query);
42+
}
43+
44+
#[test]
45+
fn method_partition_should_not_accumulate_names_with_the_same_content() {
46+
let query = sql::Select::new().partition("p0").partition("p0").as_string();
47+
let expected_query = "PARTITION (p0)";
48+
49+
assert_eq!(expected_query, query);
50+
}
51+
52+
#[test]
53+
fn method_partition_should_trim_space_of_the_argument() {
54+
let query = sql::Select::new().partition(" p0 ").as_string();
55+
let expected_query = "PARTITION (p0)";
56+
57+
assert_eq!(expected_query, query);
58+
}
59+
60+
#[test]
61+
fn method_partition_should_be_defined_after_from_clause() {
62+
let query = sql::Select::new().from("employees").partition("p0").as_string();
63+
let expected_query = "FROM employees PARTITION (p0)";
64+
65+
assert_eq!(expected_query, query);
66+
}
67+
68+
#[test]
69+
fn method_partition_should_be_defined_after_join_clauses() {
70+
let query = sql::Select::new()
71+
.from("employees")
72+
.inner_join("addresses ON employees.login = addresses.login")
73+
.partition("p0")
74+
.as_string();
75+
76+
let expected_query = "\
77+
FROM employees \
78+
INNER JOIN addresses ON employees.login = addresses.login \
79+
PARTITION (p0)\
80+
";
81+
82+
assert_eq!(expected_query, query);
83+
}
84+
85+
#[test]
86+
fn method_raw_after_should_add_raw_sql_after_partition_parameter() {
87+
let query = sql::Select::new()
88+
.partition("name")
89+
.raw_after(sql::SelectClause::Partition, "/* uncommon parameter */")
90+
.as_string();
91+
92+
let expected_query = "PARTITION (name) /* uncommon parameter */";
93+
94+
assert_eq!(expected_query, query);
95+
}
96+
97+
#[test]
98+
fn method_raw_before_should_add_raw_sql_before_partition_parameter() {
99+
let query = sql::Select::new()
100+
.raw_before(sql::SelectClause::Partition, "/* uncommon parameter */")
101+
.partition("name")
102+
.as_string();
103+
104+
let expected_query = "/* uncommon parameter */ PARTITION (name)";
105+
106+
assert_eq!(expected_query, query);
107+
}
108+
}

tests/command_select_spec.rs

Lines changed: 0 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -280,112 +280,3 @@ mod builder_methods {
280280
assert_eq!(query, expected_query);
281281
}
282282
}
283-
284-
#[cfg(feature = "mysql")]
285-
mod partition_method {
286-
use pretty_assertions::assert_eq;
287-
use sql_query_builder as sql;
288-
289-
#[test]
290-
fn method_partition_should_define_the_partition_clause() {
291-
let query = sql::Select::new().partition("p0").as_string();
292-
let expected_query = "PARTITION (p0)";
293-
294-
assert_eq!(expected_query, query);
295-
}
296-
297-
#[test]
298-
fn method_partition_should_not_defined_the_clause_without_partition_names() {
299-
let query = sql::Select::new().partition("").as_string();
300-
let expected_query = "";
301-
302-
assert_eq!(expected_query, query);
303-
}
304-
305-
#[test]
306-
fn method_partition_should_accumulate_names_on_consecutive_calls() {
307-
let query = sql::Select::new().partition("p0").partition("p1").as_string();
308-
309-
let expected_query = "PARTITION (p0, p1)";
310-
311-
assert_eq!(expected_query, query);
312-
}
313-
314-
#[test]
315-
fn method_partition_should_not_accumulate_values_when_expression_is_empty() {
316-
let query = sql::Select::new()
317-
.partition("")
318-
.partition("p0")
319-
.partition("")
320-
.as_string();
321-
322-
let expected_query = "PARTITION (p0)";
323-
324-
assert_eq!(expected_query, query);
325-
}
326-
327-
#[test]
328-
fn method_partition_should_not_accumulate_names_with_the_same_content() {
329-
let query = sql::Select::new().partition("p0").partition("p0").as_string();
330-
let expected_query = "PARTITION (p0)";
331-
332-
assert_eq!(expected_query, query);
333-
}
334-
335-
#[test]
336-
fn method_partition_should_trim_space_of_the_argument() {
337-
let query = sql::Select::new().partition(" p0 ").as_string();
338-
let expected_query = "PARTITION (p0)";
339-
340-
assert_eq!(expected_query, query);
341-
}
342-
343-
#[test]
344-
fn method_partition_should_be_defined_after_from_clause() {
345-
let query = sql::Select::new().from("employees").partition("p0").as_string();
346-
let expected_query = "FROM employees PARTITION (p0)";
347-
348-
assert_eq!(expected_query, query);
349-
}
350-
351-
#[test]
352-
fn method_partition_should_be_defined_after_join_clauses() {
353-
let query = sql::Select::new()
354-
.from("employees")
355-
.inner_join("addresses ON employees.login = addresses.login")
356-
.partition("p0")
357-
.as_string();
358-
359-
let expected_query = "\
360-
FROM employees \
361-
INNER JOIN addresses ON employees.login = addresses.login \
362-
PARTITION (p0)\
363-
";
364-
365-
assert_eq!(expected_query, query);
366-
}
367-
368-
#[test]
369-
fn method_raw_after_should_add_raw_sql_after_partition_parameter() {
370-
let query = sql::Select::new()
371-
.partition("name")
372-
.raw_after(sql::SelectClause::Partition, "/* uncommon parameter */")
373-
.as_string();
374-
375-
let expected_query = "PARTITION (name) /* uncommon parameter */";
376-
377-
assert_eq!(expected_query, query);
378-
}
379-
380-
#[test]
381-
fn method_raw_before_should_add_raw_sql_before_partition_parameter() {
382-
let query = sql::Select::new()
383-
.raw_before(sql::SelectClause::Partition, "/* uncommon parameter */")
384-
.partition("name")
385-
.as_string();
386-
387-
let expected_query = "/* uncommon parameter */ PARTITION (name)";
388-
389-
assert_eq!(expected_query, query);
390-
}
391-
}

0 commit comments

Comments
 (0)