Skip to content

Commit 8795fa6

Browse files
committed
Fixes for like operator.
Added tests for like operators.
1 parent f7565bd commit 8795fa6

3 files changed

Lines changed: 103 additions & 17 deletions

File tree

canyon_crud/src/query_elements/operators.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub enum Comp {
1616
/// Operator "<" less than value
1717
Lt,
1818
/// Operator "=<" less or equals than value
19-
LtEq
19+
LtEq,
2020
}
2121

2222
impl Operator for Comp {
@@ -27,7 +27,7 @@ impl Operator for Comp {
2727
Self::Gt => format!(" > ${placeholder_counter}"),
2828
Self::GtEq => format!(" >= ${placeholder_counter}"),
2929
Self::Lt => format!(" < ${placeholder_counter}"),
30-
Self::LtEq => format!(" <= ${placeholder_counter}")
30+
Self::LtEq => format!(" <= ${placeholder_counter}"),
3131
}
3232
}
3333
}
@@ -38,16 +38,17 @@ pub enum Like {
3838
/// Operator "LIKE" as '%pattern'
3939
Left,
4040
/// Operator "LIKE" as 'pattern%'
41-
Right
41+
Right,
4242
}
4343

44-
4544
impl Operator for Like {
4645
fn as_str(&self, placeholder_counter: usize) -> String {
4746
match *self {
48-
Like::Full => format!(" LIKE CONCAT('%', ${placeholder_counter} ,'%') "),
49-
Like::Left => format!(" LIKE CONCAT('%', ${placeholder_counter}) "),
50-
Like::Right => format!(" LIKE CONCAT(${placeholder_counter} ,'%') "),
47+
Like::Full => {
48+
format!(" LIKE CONCAT('%', CAST(${placeholder_counter} AS VARCHAR) ,'%')")
49+
}
50+
Like::Left => format!(" LIKE CONCAT('%', CAST(${placeholder_counter} AS VARCHAR))"),
51+
Like::Right => format!(" LIKE CONCAT(CAST(${placeholder_counter} AS VARCHAR) ,'%')"),
5152
}
5253
}
5354
}

canyon_crud/src/query_elements/query_builder.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,8 @@ where
180180
pub fn r#where<Z: FieldValueIdentifier<'a, T>>(&mut self, r#where: Z, op: impl Operator) {
181181
let (column_name, value) = r#where.value();
182182

183-
let where_ = String::from(" WHERE ")
184-
+ column_name
185-
+ &op.as_str(self.query.params.len() + 1);
183+
let where_ =
184+
String::from(" WHERE ") + column_name + &op.as_str(self.query.params.len() + 1);
186185

187186
self.query.sql.push_str(&where_);
188187
self.query.params.push(value);
@@ -191,9 +190,7 @@ where
191190
pub fn and<Z: FieldValueIdentifier<'a, T>>(&mut self, r#and: Z, op: impl Operator) {
192191
let (column_name, value) = r#and.value();
193192

194-
let and_ = String::from(" AND ")
195-
+ column_name
196-
+ &op.as_str(self.query.params.len() + 1);
193+
let and_ = String::from(" AND ") + column_name + &op.as_str(self.query.params.len() + 1);
197194

198195
self.query.sql.push_str(&and_);
199196
self.query.params.push(value);
@@ -202,9 +199,7 @@ where
202199
pub fn or<Z: FieldValueIdentifier<'a, T>>(&mut self, r#and: Z, op: impl Operator) {
203200
let (column_name, value) = r#and.value();
204201

205-
let and_ = String::from(" OR ")
206-
+ column_name
207-
+ &op.as_str(self.query.params.len() + 1);
202+
let and_ = String::from(" OR ") + column_name + &op.as_str(self.query.params.len() + 1);
208203

209204
self.query.sql.push_str(&and_);
210205
self.query.params.push(value);

tests/crud/querybuilder_operations.rs

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
///
77
use canyon_sql::{
88
crud::CrudOperations,
9-
query::{operators::Comp, ops::QueryBuilder},
9+
query::{operators::Comp, operators::Like, ops::QueryBuilder},
1010
};
1111

1212
#[cfg(feature = "mssql")]
@@ -59,6 +59,96 @@ fn test_crud_find_with_querybuilder() {
5959
assert_eq!(league_idx_0.region, "KOREA");
6060
}
6161

62+
/// Builds a new SQL statement for retrieves entities of the `T` type, filtered
63+
/// with the parameters that modifies the base SQL to SELECT * FROM <entity>
64+
#[cfg(feature = "postgres")]
65+
#[canyon_sql::macros::canyon_tokio_test]
66+
fn test_crud_find_with_querybuilder_and_fulllike() {
67+
// Find all the leagues with "LC" in their name
68+
let mut filtered_leagues_result = League::select_query();
69+
filtered_leagues_result.r#where(LeagueFieldValue::name(&"LC"), Like::Full);
70+
71+
assert_eq!(
72+
filtered_leagues_result.read_sql(),
73+
"SELECT * FROM league WHERE name LIKE CONCAT('%', CAST($1 AS VARCHAR) ,'%')"
74+
)
75+
}
76+
77+
/// Builds a new SQL statement for retrieves entities of the `T` type, filtered
78+
/// with the parameters that modifies the base SQL to SELECT * FROM <entity>
79+
#[cfg(feature = "mssql")]
80+
#[canyon_sql::macros::canyon_tokio_test]
81+
fn test_crud_find_with_querybuilder_and_fulllike_datasource() {
82+
// Find all the leagues with "LC" in their name
83+
let mut filtered_leagues_result = League::select_query_datasource(SQL_SERVER_DS);
84+
filtered_leagues_result.r#where(LeagueFieldValue::name(&"LC"), Like::Full);
85+
86+
assert_eq!(
87+
filtered_leagues_result.read_sql(),
88+
"SELECT * FROM league WHERE name LIKE CONCAT('%', CAST($1 AS VARCHAR) ,'%')"
89+
)
90+
}
91+
92+
/// Builds a new SQL statement for retrieves entities of the `T` type, filtered
93+
/// with the parameters that modifies the base SQL to SELECT * FROM <entity>
94+
#[cfg(feature = "postgres")]
95+
#[canyon_sql::macros::canyon_tokio_test]
96+
fn test_crud_find_with_querybuilder_and_leftlike() {
97+
// Find all the leagues whose name ends with "CK"
98+
let mut filtered_leagues_result = League::select_query();
99+
filtered_leagues_result.r#where(LeagueFieldValue::name(&"CK"), Like::Left);
100+
101+
assert_eq!(
102+
filtered_leagues_result.read_sql(),
103+
"SELECT * FROM league WHERE name LIKE CONCAT('%', CAST($1 AS VARCHAR))"
104+
)
105+
}
106+
107+
/// Builds a new SQL statement for retrieves entities of the `T` type, filtered
108+
/// with the parameters that modifies the base SQL to SELECT * FROM <entity>
109+
#[cfg(feature = "mssql")]
110+
#[canyon_sql::macros::canyon_tokio_test]
111+
fn test_crud_find_with_querybuilder_and_leftlike_datasource() {
112+
// Find all the leagues whose name ends with "CK"
113+
let mut filtered_leagues_result = League::select_query();
114+
filtered_leagues_result.r#where(LeagueFieldValue::name(&"CK"), Like::Left);
115+
116+
assert_eq!(
117+
filtered_leagues_result.read_sql(),
118+
"SELECT * FROM league WHERE name LIKE CONCAT('%', CAST($1 AS VARCHAR))"
119+
)
120+
}
121+
122+
/// Builds a new SQL statement for retrieves entities of the `T` type, filtered
123+
/// with the parameters that modifies the base SQL to SELECT * FROM <entity>
124+
#[cfg(feature = "postgres")]
125+
#[canyon_sql::macros::canyon_tokio_test]
126+
fn test_crud_find_with_querybuilder_and_rightlike() {
127+
// Find all the leagues whose name starts with "LC"
128+
let mut filtered_leagues_result = League::select_query();
129+
filtered_leagues_result.r#where(LeagueFieldValue::name(&"LC"), Like::Right);
130+
131+
assert_eq!(
132+
filtered_leagues_result.read_sql(),
133+
"SELECT * FROM league WHERE name LIKE CONCAT(CAST($1 AS VARCHAR) ,'%')"
134+
)
135+
}
136+
137+
/// Builds a new SQL statement for retrieves entities of the `T` type, filtered
138+
/// with the parameters that modifies the base SQL to SELECT * FROM <entity>
139+
#[cfg(feature = "mssql")]
140+
#[canyon_sql::macros::canyon_tokio_test]
141+
fn test_crud_find_with_querybuilder_and_rightlike_datasource() {
142+
// Find all the leagues whose name starts with "LC"
143+
let mut filtered_leagues_result = League::select_query_datasource(SQL_SERVER_DS);
144+
filtered_leagues_result.r#where(LeagueFieldValue::name(&"LC"), Like::Right);
145+
146+
assert_eq!(
147+
filtered_leagues_result.read_sql(),
148+
"SELECT * FROM league WHERE name LIKE CONCAT(CAST($1 AS VARCHAR) ,'%')"
149+
)
150+
}
151+
62152
/// Same than the above but with the specified datasource
63153
#[cfg(feature = "mssql")]
64154
#[canyon_sql::macros::canyon_tokio_test]

0 commit comments

Comments
 (0)