Skip to content

Commit f7565bd

Browse files
committed
Added 'like' operator.
Removed unneeded whitespaces on queries.
1 parent cca1c8f commit f7565bd

3 files changed

Lines changed: 37 additions & 24 deletions

File tree

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
pub trait Operator {
2-
fn as_str(&self) -> &'static str;
2+
fn as_str(&self, placeholder_counter: usize) -> String;
33
}
44

55
/// Enumerated type for represent the comparison operations
@@ -16,17 +16,38 @@ pub enum Comp {
1616
/// Operator "<" less than value
1717
Lt,
1818
/// Operator "=<" less or equals than value
19-
LtEq,
19+
LtEq
2020
}
21+
2122
impl Operator for Comp {
22-
fn as_str(&self) -> &'static str {
23+
fn as_str(&self, placeholder_counter: usize) -> String {
24+
match *self {
25+
Self::Eq => format!(" = ${placeholder_counter}"),
26+
Self::Neq => format!(" <> ${placeholder_counter}"),
27+
Self::Gt => format!(" > ${placeholder_counter}"),
28+
Self::GtEq => format!(" >= ${placeholder_counter}"),
29+
Self::Lt => format!(" < ${placeholder_counter}"),
30+
Self::LtEq => format!(" <= ${placeholder_counter}")
31+
}
32+
}
33+
}
34+
35+
pub enum Like {
36+
/// Operator "LIKE" as '%pattern%'
37+
Full,
38+
/// Operator "LIKE" as '%pattern'
39+
Left,
40+
/// Operator "LIKE" as 'pattern%'
41+
Right
42+
}
43+
44+
45+
impl Operator for Like {
46+
fn as_str(&self, placeholder_counter: usize) -> String {
2347
match *self {
24-
Self::Eq => " = ",
25-
Self::Neq => " <> ",
26-
Self::Gt => " > ",
27-
Self::GtEq => " >= ",
28-
Self::Lt => " < ",
29-
Self::LtEq => " <= ",
48+
Like::Full => format!(" LIKE CONCAT('%', ${placeholder_counter} ,'%') "),
49+
Like::Left => format!(" LIKE CONCAT('%', ${placeholder_counter}) "),
50+
Like::Right => format!(" LIKE CONCAT(${placeholder_counter} ,'%') "),
3051
}
3152
}
3253
}

canyon_crud/src/query_elements/query_builder.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,7 @@ where
182182

183183
let where_ = String::from(" WHERE ")
184184
+ column_name
185-
+ op.as_str()
186-
+ "$"
187-
+ &(self.query.params.len() + 1).to_string();
185+
+ &op.as_str(self.query.params.len() + 1);
188186

189187
self.query.sql.push_str(&where_);
190188
self.query.params.push(value);
@@ -195,10 +193,7 @@ where
195193

196194
let and_ = String::from(" AND ")
197195
+ column_name
198-
+ op.as_str()
199-
+ "$"
200-
+ &(self.query.params.len() + 1).to_string()
201-
+ " ";
196+
+ &op.as_str(self.query.params.len() + 1);
202197

203198
self.query.sql.push_str(&and_);
204199
self.query.params.push(value);
@@ -209,10 +204,7 @@ where
209204

210205
let and_ = String::from(" OR ")
211206
+ column_name
212-
+ op.as_str()
213-
+ "$"
214-
+ &(self.query.params.len() + 1).to_string()
215-
+ " ";
207+
+ &op.as_str(self.query.params.len() + 1);
216208

217209
self.query.sql.push_str(&and_);
218210
self.query.params.push(value);
@@ -246,7 +238,7 @@ where
246238
self.query.params.push(qp)
247239
});
248240

249-
self.query.sql.push_str(") ");
241+
self.query.sql.push_str(")");
250242
}
251243

252244
fn or_values_in<Z, Q>(&mut self, r#or: Z, values: &'a [Q])
@@ -277,7 +269,7 @@ where
277269
self.query.params.push(qp)
278270
});
279271

280-
self.query.sql.push_str(") ");
272+
self.query.sql.push_str(")");
281273
}
282274

283275
#[inline]

tests/crud/querybuilder_operations.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fn test_generated_sql_by_the_select_querybuilder() {
3434
// generated SQL by the SelectQueryBuilder<T> is the spected
3535
assert_eq!(
3636
select_with_joins.read_sql(),
37-
"SELECT * FROM league INNER JOIN tournament ON league.id = tournament.league_id LEFT JOIN team ON tournament.id = player.tournament_id WHERE id > $1 AND name = $2 AND name IN ($2, $3) "
37+
"SELECT * FROM league INNER JOIN tournament ON league.id = tournament.league_id LEFT JOIN team ON tournament.id = player.tournament_id WHERE id > $1 AND name = $2 AND name IN ($2, $3)"
3838
)
3939
}
4040

@@ -239,7 +239,7 @@ fn test_or_clause_with_in_constraint() {
239239

240240
assert_eq!(
241241
l.read_sql(),
242-
"SELECT * FROM league WHERE name = $1 OR id IN ($1, $2, $3) "
242+
"SELECT * FROM league WHERE name = $1 OR id IN ($1, $2, $3)"
243243
)
244244
}
245245

0 commit comments

Comments
 (0)