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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
use crate::Param;
use super::{
BoolExpr, Field, FieldRef, ValueExpr, plainto_tsquery, to_tsvector, ts_match,
websearch_to_tsquery,
};
macro_rules! field_text_delegates {
($($(#[$meta:meta])* $name:ident($arg:ident: $arg_ty:ty);)+) => {
impl Field<String> {
$(
$(#[$meta])*
pub fn $name(self, $arg: $arg_ty) -> BoolExpr {
self.reference().$name($arg)
}
)+
}
};
}
field_text_delegates! {
/// Builds a `LIKE` predicate using PostgreSQL pattern syntax.
like(pattern: impl AsRef<str>);
/// Builds a `LIKE` predicate from a SQL pattern expression.
like_expr(pattern: impl Into<ValueExpr>);
/// Builds a negated `LIKE` predicate.
not_like(pattern: impl AsRef<str>);
/// Builds a negated `LIKE` predicate from a SQL pattern expression.
not_like_expr(pattern: impl Into<ValueExpr>);
/// Builds an `ILIKE` predicate using PostgreSQL pattern syntax.
ilike(pattern: impl AsRef<str>);
/// Builds an `ILIKE` predicate from a SQL pattern expression.
ilike_expr(pattern: impl Into<ValueExpr>);
/// Builds a negated `ILIKE` predicate.
not_ilike(pattern: impl AsRef<str>);
/// Builds a negated `ILIKE` predicate from a SQL pattern expression.
not_ilike_expr(pattern: impl Into<ValueExpr>);
/// Builds a `SIMILAR TO` predicate.
similar_to(pattern: impl AsRef<str>);
/// Builds a `SIMILAR TO` predicate from a SQL pattern expression.
similar_to_expr(pattern: impl Into<ValueExpr>);
/// Builds a negated `SIMILAR TO` predicate.
not_similar_to(pattern: impl AsRef<str>);
/// Builds a negated `SIMILAR TO` predicate from a SQL pattern expression.
not_similar_to_expr(pattern: impl Into<ValueExpr>);
/// Builds an escaped case-insensitive contains predicate for a literal value.
contains(value: impl AsRef<str>);
/// Builds an escaped case-insensitive contains predicate from a SQL expression.
///
/// LIKE metacharacters (`%`, `_`, `\`) are escaped server-side with
/// `replace(...)`, so the expression value is matched literally.
contains_expr(value: impl Into<ValueExpr>);
/// Builds a negated escaped case-insensitive contains predicate.
not_contains(value: impl AsRef<str>);
/// Builds a negated escaped case-insensitive contains predicate from a SQL expression.
///
/// LIKE metacharacters (`%`, `_`, `\`) are escaped server-side with
/// `replace(...)`, so the expression value is matched literally.
not_contains_expr(value: impl Into<ValueExpr>);
/// Builds an escaped case-insensitive prefix predicate for a literal value.
starts_with(value: impl AsRef<str>);
/// Builds an escaped case-insensitive prefix predicate from a SQL expression.
///
/// LIKE metacharacters (`%`, `_`, `\`) are escaped server-side with
/// `replace(...)`, so the expression value is matched literally.
starts_with_expr(value: impl Into<ValueExpr>);
/// Builds a negated escaped case-insensitive prefix predicate.
not_starts_with(value: impl AsRef<str>);
/// Builds a negated escaped case-insensitive prefix predicate from a SQL expression.
///
/// LIKE metacharacters (`%`, `_`, `\`) are escaped server-side with
/// `replace(...)`, so the expression value is matched literally.
not_starts_with_expr(value: impl Into<ValueExpr>);
/// Builds an escaped case-insensitive suffix predicate for a literal value.
ends_with(value: impl AsRef<str>);
/// Builds an escaped case-insensitive suffix predicate from a SQL expression.
///
/// LIKE metacharacters (`%`, `_`, `\`) are escaped server-side with
/// `replace(...)`, so the expression value is matched literally.
ends_with_expr(value: impl Into<ValueExpr>);
/// Builds a negated escaped case-insensitive suffix predicate.
not_ends_with(value: impl AsRef<str>);
/// Builds a negated escaped case-insensitive suffix predicate from a SQL expression.
///
/// LIKE metacharacters (`%`, `_`, `\`) are escaped server-side with
/// `replace(...)`, so the expression value is matched literally.
not_ends_with_expr(value: impl Into<ValueExpr>);
/// Builds a case-sensitive PostgreSQL regex predicate.
regex(pattern: impl Into<String>);
/// Builds a case-sensitive PostgreSQL regex predicate from a SQL pattern expression.
regex_expr(pattern: impl Into<ValueExpr>);
/// Builds a negated case-sensitive PostgreSQL regex predicate.
not_regex(pattern: impl Into<String>);
/// Builds a negated case-sensitive PostgreSQL regex predicate from a SQL pattern expression.
not_regex_expr(pattern: impl Into<ValueExpr>);
/// Builds a case-insensitive PostgreSQL regex predicate.
iregex(pattern: impl Into<String>);
/// Builds a case-insensitive PostgreSQL regex predicate from a SQL pattern expression.
iregex_expr(pattern: impl Into<ValueExpr>);
/// Builds a negated case-insensitive PostgreSQL regex predicate.
not_iregex(pattern: impl Into<String>);
/// Builds a negated case-insensitive PostgreSQL regex predicate from a SQL pattern expression.
not_iregex_expr(pattern: impl Into<ValueExpr>);
/// Builds a plain full-text search predicate.
text_search(query: impl AsRef<str>);
/// Builds a full-text search predicate from a tsquery expression.
text_search_expr(query: impl Into<ValueExpr>);
/// Builds a web-style full-text search predicate.
websearch(query: impl AsRef<str>);
/// Builds a web-style full-text search predicate from a text expression.
websearch_expr(query: impl Into<ValueExpr>);
}
impl FieldRef<String> {
/// Builds a `LIKE` predicate using PostgreSQL pattern syntax.
pub fn like(self, pattern: impl AsRef<str>) -> BoolExpr {
self.like_predicate(pattern, false, false)
}
/// Builds a `LIKE` predicate from a SQL pattern expression.
pub fn like_expr(self, pattern: impl Into<ValueExpr>) -> BoolExpr {
self.like_expr_predicate(pattern, false, false)
}
/// Builds a negated `LIKE` predicate.
pub fn not_like(self, pattern: impl AsRef<str>) -> BoolExpr {
self.like_predicate(pattern, false, true)
}
/// Builds a negated `LIKE` predicate from a SQL pattern expression.
pub fn not_like_expr(self, pattern: impl Into<ValueExpr>) -> BoolExpr {
self.like_expr_predicate(pattern, false, true)
}
/// Builds an `ILIKE` predicate using PostgreSQL pattern syntax.
pub fn ilike(self, pattern: impl AsRef<str>) -> BoolExpr {
self.like_predicate(pattern, true, false)
}
/// Builds an `ILIKE` predicate from a SQL pattern expression.
pub fn ilike_expr(self, pattern: impl Into<ValueExpr>) -> BoolExpr {
self.like_expr_predicate(pattern, true, false)
}
/// Builds a negated `ILIKE` predicate.
pub fn not_ilike(self, pattern: impl AsRef<str>) -> BoolExpr {
self.like_predicate(pattern, true, true)
}
/// Builds a negated `ILIKE` predicate from a SQL pattern expression.
pub fn not_ilike_expr(self, pattern: impl Into<ValueExpr>) -> BoolExpr {
self.like_expr_predicate(pattern, true, true)
}
/// Builds a `SIMILAR TO` predicate.
pub fn similar_to(self, pattern: impl AsRef<str>) -> BoolExpr {
self.similar_predicate(pattern, false)
}
/// Builds a `SIMILAR TO` predicate from a SQL pattern expression.
pub fn similar_to_expr(self, pattern: impl Into<ValueExpr>) -> BoolExpr {
self.similar_expr_predicate(pattern, false)
}
/// Builds a negated `SIMILAR TO` predicate.
pub fn not_similar_to(self, pattern: impl AsRef<str>) -> BoolExpr {
self.similar_predicate(pattern, true)
}
/// Builds a negated `SIMILAR TO` predicate from a SQL pattern expression.
pub fn not_similar_to_expr(self, pattern: impl Into<ValueExpr>) -> BoolExpr {
self.similar_expr_predicate(pattern, true)
}
/// Builds an escaped case-insensitive contains predicate for a literal value.
pub fn contains(self, value: impl AsRef<str>) -> BoolExpr {
self.affix_predicate(value, "%", "%", false)
}
/// Builds an escaped case-insensitive contains predicate from a SQL expression.
///
/// LIKE metacharacters (`%`, `_`, `\`) are escaped server-side with
/// `replace(...)`, so the expression value is matched literally.
pub fn contains_expr(self, value: impl Into<ValueExpr>) -> BoolExpr {
self.affix_expr_predicate(value, "%", "%", false)
}
/// Builds a negated escaped case-insensitive contains predicate.
pub fn not_contains(self, value: impl AsRef<str>) -> BoolExpr {
self.affix_predicate(value, "%", "%", true)
}
/// Builds a negated escaped case-insensitive contains predicate from a SQL expression.
///
/// LIKE metacharacters (`%`, `_`, `\`) are escaped server-side with
/// `replace(...)`, so the expression value is matched literally.
pub fn not_contains_expr(self, value: impl Into<ValueExpr>) -> BoolExpr {
self.affix_expr_predicate(value, "%", "%", true)
}
/// Builds an escaped case-insensitive prefix predicate for a literal value.
pub fn starts_with(self, value: impl AsRef<str>) -> BoolExpr {
self.affix_predicate(value, "", "%", false)
}
/// Builds an escaped case-insensitive prefix predicate from a SQL expression.
///
/// LIKE metacharacters (`%`, `_`, `\`) are escaped server-side with
/// `replace(...)`, so the expression value is matched literally.
pub fn starts_with_expr(self, value: impl Into<ValueExpr>) -> BoolExpr {
self.affix_expr_predicate(value, "", "%", false)
}
/// Builds a negated escaped case-insensitive prefix predicate.
pub fn not_starts_with(self, value: impl AsRef<str>) -> BoolExpr {
self.affix_predicate(value, "", "%", true)
}
/// Builds a negated escaped case-insensitive prefix predicate from a SQL expression.
///
/// LIKE metacharacters (`%`, `_`, `\`) are escaped server-side with
/// `replace(...)`, so the expression value is matched literally.
pub fn not_starts_with_expr(self, value: impl Into<ValueExpr>) -> BoolExpr {
self.affix_expr_predicate(value, "", "%", true)
}
/// Builds an escaped case-insensitive suffix predicate for a literal value.
pub fn ends_with(self, value: impl AsRef<str>) -> BoolExpr {
self.affix_predicate(value, "%", "", false)
}
/// Builds an escaped case-insensitive suffix predicate from a SQL expression.
///
/// LIKE metacharacters (`%`, `_`, `\`) are escaped server-side with
/// `replace(...)`, so the expression value is matched literally.
pub fn ends_with_expr(self, value: impl Into<ValueExpr>) -> BoolExpr {
self.affix_expr_predicate(value, "%", "", false)
}
/// Builds a negated escaped case-insensitive suffix predicate.
pub fn not_ends_with(self, value: impl AsRef<str>) -> BoolExpr {
self.affix_predicate(value, "%", "", true)
}
/// Builds a negated escaped case-insensitive suffix predicate from a SQL expression.
///
/// LIKE metacharacters (`%`, `_`, `\`) are escaped server-side with
/// `replace(...)`, so the expression value is matched literally.
pub fn not_ends_with_expr(self, value: impl Into<ValueExpr>) -> BoolExpr {
self.affix_expr_predicate(value, "%", "", true)
}
/// Builds a case-sensitive PostgreSQL regex predicate.
pub fn regex(self, pattern: impl Into<String>) -> BoolExpr {
self.regex_predicate(pattern, false, false)
}
/// Builds a case-sensitive PostgreSQL regex predicate from a SQL pattern expression.
pub fn regex_expr(self, pattern: impl Into<ValueExpr>) -> BoolExpr {
self.regex_expr_predicate(pattern, false, false)
}
/// Builds a negated case-sensitive PostgreSQL regex predicate.
pub fn not_regex(self, pattern: impl Into<String>) -> BoolExpr {
self.regex_predicate(pattern, false, true)
}
/// Builds a negated case-sensitive PostgreSQL regex predicate from a SQL pattern expression.
pub fn not_regex_expr(self, pattern: impl Into<ValueExpr>) -> BoolExpr {
self.regex_expr_predicate(pattern, false, true)
}
/// Builds a case-insensitive PostgreSQL regex predicate.
pub fn iregex(self, pattern: impl Into<String>) -> BoolExpr {
self.regex_predicate(pattern, true, false)
}
/// Builds a case-insensitive PostgreSQL regex predicate from a SQL pattern expression.
pub fn iregex_expr(self, pattern: impl Into<ValueExpr>) -> BoolExpr {
self.regex_expr_predicate(pattern, true, false)
}
/// Builds a negated case-insensitive PostgreSQL regex predicate.
pub fn not_iregex(self, pattern: impl Into<String>) -> BoolExpr {
self.regex_predicate(pattern, true, true)
}
/// Builds a negated case-insensitive PostgreSQL regex predicate from a SQL pattern expression.
pub fn not_iregex_expr(self, pattern: impl Into<ValueExpr>) -> BoolExpr {
self.regex_expr_predicate(pattern, true, true)
}
/// Builds a plain full-text search predicate.
pub fn text_search(self, query: impl AsRef<str>) -> BoolExpr {
ts_match(
to_tsvector(self.expr()),
plainto_tsquery(ValueExpr::Param(Param::typed(query.as_ref().to_owned()))),
)
}
/// Builds a full-text search predicate from a tsquery expression.
pub fn text_search_expr(self, query: impl Into<ValueExpr>) -> BoolExpr {
ts_match(to_tsvector(self.expr()), query)
}
/// Builds a web-style full-text search predicate.
pub fn websearch(self, query: impl AsRef<str>) -> BoolExpr {
ts_match(
to_tsvector(self.expr()),
websearch_to_tsquery(ValueExpr::Param(Param::typed(query.as_ref().to_owned()))),
)
}
/// Builds a web-style full-text search predicate from a text expression.
pub fn websearch_expr(self, query: impl Into<ValueExpr>) -> BoolExpr {
ts_match(to_tsvector(self.expr()), websearch_to_tsquery(query))
}
fn like_predicate(
self,
pattern: impl AsRef<str>,
case_insensitive: bool,
negated: bool,
) -> BoolExpr {
BoolExpr::like(
self.expr(),
ValueExpr::Param(Param::typed(pattern.as_ref().to_owned())),
case_insensitive,
negated,
false,
)
}
fn like_expr_predicate(
self,
pattern: impl Into<ValueExpr>,
case_insensitive: bool,
negated: bool,
) -> BoolExpr {
BoolExpr::like(
self.expr(),
pattern.into(),
case_insensitive,
negated,
false,
)
}
fn similar_predicate(self, pattern: impl AsRef<str>, negated: bool) -> BoolExpr {
BoolExpr::similar_to(
self.expr(),
ValueExpr::Param(Param::typed(pattern.as_ref().to_owned())),
negated,
)
}
fn similar_expr_predicate(self, pattern: impl Into<ValueExpr>, negated: bool) -> BoolExpr {
BoolExpr::similar_to(self.expr(), pattern.into(), negated)
}
fn affix_predicate(
self,
value: impl AsRef<str>,
prefix: &'static str,
suffix: &'static str,
negated: bool,
) -> BoolExpr {
BoolExpr::like(
self.expr(),
ValueExpr::Param(Param::typed(escaped_like_pattern(
value.as_ref(),
prefix,
suffix,
))),
true,
negated,
true,
)
}
fn affix_expr_predicate(
self,
value: impl Into<ValueExpr>,
prefix: &'static str,
suffix: &'static str,
negated: bool,
) -> BoolExpr {
BoolExpr::like(
self.expr(),
affix_like_pattern_expr(value, prefix, suffix),
true,
negated,
true,
)
}
fn regex_predicate(
self,
pattern: impl Into<String>,
case_insensitive: bool,
negated: bool,
) -> BoolExpr {
BoolExpr::regex(
self.expr(),
ValueExpr::Param(Param::typed(pattern.into())),
case_insensitive,
negated,
)
}
fn regex_expr_predicate(
self,
pattern: impl Into<ValueExpr>,
case_insensitive: bool,
negated: bool,
) -> BoolExpr {
BoolExpr::regex(self.expr(), pattern.into(), case_insensitive, negated)
}
}
pub(crate) fn escaped_like_pattern(value: &str, prefix: &str, suffix: &str) -> String {
let mut escaped = String::with_capacity(prefix.len() + value.len() + suffix.len());
escaped.push_str(prefix);
if !value
.bytes()
.any(|byte| matches!(byte, b'\\' | b'%' | b'_'))
{
escaped.push_str(value);
escaped.push_str(suffix);
return escaped;
}
for ch in value.chars() {
if matches!(ch, '\\' | '%' | '_') {
escaped.push('\\');
}
escaped.push(ch);
}
escaped.push_str(suffix);
escaped
}
fn affix_like_pattern_expr(
value: impl Into<ValueExpr>,
prefix: &'static str,
suffix: &'static str,
) -> ValueExpr {
let escaped = escape_like_expr(value);
match (prefix.is_empty(), suffix.is_empty()) {
(true, true) => escaped,
(false, true) => concat_text_param(prefix, escaped),
(true, false) => concat_text_param(escaped, suffix),
(false, false) => concat_text_param(concat_text_param(prefix, escaped), suffix),
}
}
fn escape_like_expr(value: impl Into<ValueExpr>) -> ValueExpr {
let escaped_backslash = replace_text(value, "\\", "\\\\");
let escaped_percent = replace_text(escaped_backslash, "%", "\\%");
replace_text(escaped_percent, "_", "\\_")
}
fn replace_text(
value: impl Into<ValueExpr>,
from: impl Into<ValueExpr>,
to: impl Into<ValueExpr>,
) -> ValueExpr {
super::function("replace", [value.into(), from.into(), to.into()])
}
fn concat_text_param(left: impl Into<ValueExpr>, right: impl Into<ValueExpr>) -> ValueExpr {
super::concat_op(left, right)
}