1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
use super::PgBinOper;
use crate::{Expr, ExprTrait, IntoLikeExpr};
/// Postgres-specific operator methods for building expressions.
pub trait PgExpr: ExprTrait {
/// Express an postgres concatenate (`||`) expression.
///
/// # Examples
///
/// ```
/// use sea_query::{extension::postgres::PgExpr, tests_cfg::*, *};
///
/// let query = Query::select().expr(Expr::val("a").concatenate("b")).take();
///
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT 'a' || 'b'"#
/// );
///
/// #[cfg(feature = "postgres-array")]
/// {
/// let query = Query::select()
/// .expr(Expr::val(vec!["a".to_owned()]).concatenate(vec!["b".to_owned()]))
/// .take();
///
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT ARRAY ['a'] || ARRAY ['b']"#
/// );
/// }
/// ```
fn concatenate<T>(self, right: T) -> Expr
where
T: Into<Expr>,
{
self.binary(PgBinOper::Concatenate, right)
}
/// Alias of [`PgExpr::concatenate`]
fn concat<T>(self, right: T) -> Expr
where
T: Into<Expr>,
{
self.concatenate(right)
}
/// Express an postgres fulltext search matches (`@@`) expression.
///
/// # Examples
///
/// ```
/// use sea_query::{*, tests_cfg::*, extension::postgres::PgExpr};
///
/// let query = Query::select()
/// .columns([Font::Name, Font::Variant, Font::Language])
/// .from(Font::Table)
/// .and_where(Expr::val("a & b").matches("a b"))
/// .and_where(Expr::col(Font::Name).matches("a b"))
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT "name", "variant", "language" FROM "font" WHERE 'a & b' @@ 'a b' AND "name" @@ 'a b'"#
/// );
/// ```
fn matches<T>(self, expr: T) -> Expr
where
T: Into<Expr>,
{
self.binary(PgBinOper::Matches, expr)
}
/// Express an postgres fulltext search contains (`@>`) expression.
///
/// # Examples
///
/// ```
/// use sea_query::{*, tests_cfg::*, extension::postgres::PgExpr};
///
/// let query = Query::select()
/// .columns([Font::Name, Font::Variant, Font::Language])
/// .from(Font::Table)
/// .and_where(Expr::val("a & b").contains("a b"))
/// .and_where(Expr::col(Font::Name).contains("a b"))
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT "name", "variant", "language" FROM "font" WHERE 'a & b' @> 'a b' AND "name" @> 'a b'"#
/// );
/// ```
fn contains<T>(self, expr: T) -> Expr
where
T: Into<Expr>,
{
self.binary(PgBinOper::Contains, expr)
}
/// Express an postgres fulltext search contained (`<@`) expression.
///
/// # Examples
///
/// ```
/// use sea_query::{*, tests_cfg::*, extension::postgres::PgExpr};
///
/// let query = Query::select()
/// .columns([Font::Name, Font::Variant, Font::Language])
/// .from(Font::Table)
/// .and_where(Expr::val("a & b").contained("a b"))
/// .and_where(Expr::col(Font::Name).contained("a b"))
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT "name", "variant", "language" FROM "font" WHERE 'a & b' <@ 'a b' AND "name" <@ 'a b'"#
/// );
/// ```
fn contained<T>(self, expr: T) -> Expr
where
T: Into<Expr>,
{
self.binary(PgBinOper::Contained, expr)
}
/// Express a `ILIKE` expression.
///
/// # Examples
///
/// ```
/// use sea_query::{*, tests_cfg::*, extension::postgres::PgExpr};
///
/// let query = Query::select()
/// .columns([Char::Character, Char::SizeW, Char::SizeH])
/// .from(Char::Table)
/// .and_where(Expr::col((Char::Table, Char::Character)).ilike("Ours'%"))
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."character" ILIKE E'Ours\'%'"#
/// );
/// ```
fn ilike<L>(self, like: L) -> Expr
where
L: IntoLikeExpr,
{
self.binary(PgBinOper::ILike, like.into_like_expr())
}
/// Express a `NOT ILIKE` expression
fn not_ilike<L>(self, like: L) -> Expr
where
L: IntoLikeExpr,
{
self.binary(PgBinOper::NotILike, like.into_like_expr())
}
/// Express a postgres retrieves JSON field as JSON value (`->`).
///
/// # Examples
///
/// ```
/// use sea_query::{extension::postgres::PgExpr, tests_cfg::*, *};
///
/// let query = Query::select()
/// .column(Font::Variant)
/// .from(Font::Table)
/// .and_where(Expr::col(Font::Variant).get_json_field("a"))
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT "variant" FROM "font" WHERE "variant" -> 'a'"#
/// );
/// ```
fn get_json_field<T>(self, right: T) -> Expr
where
T: Into<Expr>,
{
self.binary(PgBinOper::GetJsonField, right)
}
/// Express a postgres retrieves JSON field and casts it to an appropriate SQL type (`->>`).
///
/// # Examples
///
/// ```
/// use sea_query::{extension::postgres::PgExpr, tests_cfg::*, *};
///
/// let query = Query::select()
/// .column(Font::Variant)
/// .from(Font::Table)
/// .and_where(Expr::col(Font::Variant).cast_json_field("a"))
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT "variant" FROM "font" WHERE "variant" ->> 'a'"#
/// );
/// ```
fn cast_json_field<T>(self, right: T) -> Expr
where
T: Into<Expr>,
{
self.binary(PgBinOper::CastJsonField, right)
}
/// Equivalent to `is_in` but bind parameters as array.
/// ```
/// use sea_query::{extension::postgres::PgExpr, tests_cfg::*, *};
///
/// let query = Query::select()
/// .columns([Char::Id])
/// .from(Char::Table)
/// .and_where(
/// (Char::Table, Char::SizeW)
/// .into_column_ref()
/// .eq_any([1, 2, 3]),
/// )
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT "id" FROM "character" WHERE "character"."size_w" = ANY(ARRAY [1,2,3])"#
/// );
/// ```
#[cfg(feature = "postgres-array")]
fn eq_any<V, I>(self, v: I) -> Expr
where
V: Into<crate::Value> + crate::ValueType,
I: IntoIterator<Item = V>,
{
self.eq(super::PgFunc::any(crate::Value::Array(
V::array_type(),
Some(Box::new(v.into_iter().map(|v| v.into()).collect())),
)))
}
/// Equivalent to `is_not_in` but bind parameters as array.
/// ```
/// use sea_query::{extension::postgres::PgExpr, tests_cfg::*, *};
///
/// let query = Query::select()
/// .columns([Char::Id])
/// .from(Char::Table)
/// .and_where(
/// (Char::Table, Char::SizeW)
/// .into_column_ref()
/// .ne_all([1, 2, 3]),
/// )
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT "id" FROM "character" WHERE "character"."size_w" <> ALL(ARRAY [1,2,3])"#
/// );
/// ```
#[cfg(feature = "postgres-array")]
fn ne_all<V, I>(self, v: I) -> Expr
where
V: Into<crate::Value> + crate::ValueType,
I: IntoIterator<Item = V>,
{
self.ne(super::PgFunc::all(crate::Value::Array(
V::array_type(),
Some(Box::new(v.into_iter().map(|v| v.into()).collect())),
)))
}
}
/// You should be able to use Postgres-specific operators with all types of expressions.
impl<T> PgExpr for T where T: ExprTrait {}