Skip to content

Commit 68953a1

Browse files
committed
Add ilike method for postgres
It's mostly a copy/paste job from like and not_like. Fixes diesel-rs#784
1 parent 0f0c3b2 commit 68953a1

8 files changed

Lines changed: 83 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ for Rust libraries in [RFC #1105](https://github.com/rust-lang/rfcs/blob/master/
88

99
### Added
1010

11+
* Added support for `ILIKE` in PostgreSQL.
12+
1113
* Added the `migration list` command to Diesel CLI for listing all available migrations and marking those that have been applied.
1214

1315
* Added support for adding two nullable columns.

diesel/src/expression/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub mod expression_methods;
3333
pub mod functions;
3434
#[doc(hidden)]
3535
pub mod grouped;
36+
#[macro_use]
3637
pub mod helper_types;
3738
#[doc(hidden)]
3839
pub mod nullable;

diesel/src/pg/expression/expression_methods.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use expression::{Expression, AsExpression};
22
use super::predicates::*;
3-
use types::Array;
3+
use types::{Array, Text};
44

55
pub trait PgExpressionMethods: Expression + Sized {
66
/// Creates a PostgreSQL `IS NOT DISTINCT FROM` expression. This behaves
@@ -313,3 +313,17 @@ pub trait SortExpressionMethods : Sized {
313313
impl<T> SortExpressionMethods for Asc<T> {}
314314

315315
impl<T> SortExpressionMethods for Desc<T> {}
316+
317+
pub trait PgTextExpressionMethods: Expression<SqlType=Text> + Sized {
318+
/// Returns a SQL `ILIKE` expression
319+
fn ilike<T: AsExpression<Text>>(self, other: T) -> ILike<Self, T::Expression> {
320+
ILike::new(self.as_expression(), other.as_expression())
321+
}
322+
323+
/// Returns a SQL `NOT ILIKE` expression
324+
fn not_ilike<T: AsExpression<Text>>(self, other: T) -> NotILike<Self, T::Expression> {
325+
NotILike::new(self.as_expression(), other.as_expression())
326+
}
327+
}
328+
329+
impl<T: Expression<SqlType=Text>> PgTextExpressionMethods for T {}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use ::expression::AsExpression;
2+
use types;
3+
4+
gen_helper_type!(ILike, VarChar);
5+
gen_helper_type!(NotILike, VarChar);
6+

diesel/src/pg/expression/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ pub mod expression_methods;
44
pub mod extensions;
55
#[doc(hidden)]
66
pub mod predicates;
7+
#[doc(hidden)]
8+
pub mod helper_types;
79

810
mod date_and_time;
911

diesel/src/pg/expression/predicates.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ infix_predicate!(IsNotDistinctFrom, " IS NOT DISTINCT FROM ", backend: Pg);
55
infix_predicate!(OverlapsWith, " && ", backend: Pg);
66
infix_predicate!(Contains, " @> ", backend: Pg);
77
infix_predicate!(IsContainedBy, " <@ ", backend: Pg);
8+
infix_predicate!(ILike, " ILIKE ", backend: Pg);
9+
infix_predicate!(NotILike, " NOT ILIKE ", backend: Pg);
810
postfix_expression!(NullsFirst, " NULLS FIRST", ());
911
postfix_expression!(NullsLast, " NULLS LAST", ());
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#[macro_use]
2+
extern crate diesel;
3+
#[macro_use]
4+
extern crate diesel_codegen;
5+
6+
use diesel::*;
7+
use diesel::sqlite::SqliteConnection;
8+
use diesel::mysql::MysqlConnection;
9+
10+
table! {
11+
users {
12+
id -> Integer,
13+
name -> VarChar,
14+
}
15+
}
16+
17+
#[derive(Insertable)]
18+
#[table_name="users"]
19+
struct User {
20+
id: i32,
21+
name: String,
22+
}
23+
24+
fn main() {
25+
let connection = SqliteConnection::establish("").unwrap();
26+
users::table.filter(users::name.ilike("%hey%")).execute(&connection);
27+
//~^ ERROR E0277
28+
29+
let connection = MysqlConnection::establish("").unwrap();
30+
users::table.filter(users::name.ilike("%hey%")).execute(&connection);
31+
//~^ ERROR E0277
32+
}

diesel_tests/tests/filter_operators.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,29 @@ fn filter_by_like() {
103103
users.filter(name.not_like("%Griffin")).order(id.asc()).load(&connection).unwrap());
104104
}
105105

106+
#[test]
107+
#[cfg(feature = "postgres")]
108+
fn filter_by_ilike() {
109+
use schema::users::dsl::*;
110+
111+
let connection = connection();
112+
let data = vec![
113+
NewUser::new("Sean Griffin", None),
114+
NewUser::new("Tess Griffin", None),
115+
NewUser::new("Jim", None),
116+
];
117+
insert(&data).into(users).execute(&connection).unwrap();
118+
let data = users.load::<User>(&connection).unwrap();
119+
let sean = data[0].clone();
120+
let tess = data[1].clone();
121+
let jim = data[2].clone();
122+
123+
assert_eq!(vec![sean, tess],
124+
users.filter(name.ilike("%grifFin")).order(id.asc()).load(&connection).unwrap());
125+
assert_eq!(vec![jim],
126+
users.filter(name.not_ilike("%grifFin")).order(id.asc()).load(&connection).unwrap());
127+
}
128+
106129
#[test]
107130
#[cfg(feature = "postgres")]
108131
fn filter_by_any() {

0 commit comments

Comments
 (0)