drizzle_postgres/expr/ilike.rs
1//! `PostgreSQL` ILIKE operators.
2
3use crate::values::PostgresValue;
4use drizzle_core::expr::{Expr, NonNull, SQLExpr, Scalar};
5use drizzle_core::sql::{SQL, SQLChunk, Token};
6use drizzle_types::postgres::types::Boolean;
7
8/// Case-insensitive LIKE pattern matching (PostgreSQL-specific)
9///
10/// The result is a boolean expression, so it can be used directly in
11/// `WHERE`, `HAVING` and join conditions.
12///
13/// # Example
14///
15/// ```
16/// # use drizzle_postgres::expr::ilike;
17/// # use drizzle_core::{SQL, ToSQL};
18/// # use drizzle_postgres::values::PostgresValue;
19/// let name = SQL::<PostgresValue>::raw("name");
20/// let cond = ilike(name, "%john%");
21/// assert!(cond.to_sql().sql().contains("ILIKE"));
22/// ```
23pub fn ilike<'a, E, P>(
24 expr: E,
25 pattern: P,
26) -> SQLExpr<'a, PostgresValue<'a>, Boolean, NonNull, Scalar>
27where
28 E: Expr<'a, PostgresValue<'a>>,
29 P: Into<PostgresValue<'a>>,
30{
31 SQLExpr::new(
32 expr.to_sql()
33 .push(SQLChunk::Raw("ILIKE".into()))
34 .append(SQL::param(pattern.into())),
35 )
36}
37
38/// Case-insensitive NOT LIKE pattern matching (PostgreSQL-specific)
39///
40/// # Example
41///
42/// ```
43/// # use drizzle_postgres::expr::not_ilike;
44/// # use drizzle_core::{SQL, ToSQL};
45/// # use drizzle_postgres::values::PostgresValue;
46/// let name = SQL::<PostgresValue>::raw("name");
47/// let cond = not_ilike(name, "%admin%");
48/// assert!(cond.to_sql().sql().contains("NOT ILIKE"));
49/// ```
50pub fn not_ilike<'a, E, P>(
51 expr: E,
52 pattern: P,
53) -> SQLExpr<'a, PostgresValue<'a>, Boolean, NonNull, Scalar>
54where
55 E: Expr<'a, PostgresValue<'a>>,
56 P: Into<PostgresValue<'a>>,
57{
58 SQLExpr::new(
59 expr.to_sql()
60 .push(Token::NOT)
61 .push(SQLChunk::Raw("ILIKE".into()))
62 .append(SQL::param(pattern.into())),
63 )
64}