Debian Codegraph @

src/expression/operators.rs

raw
1 #[macro_export]
2 #[doc(hidden)]
3 macro_rules! __diesel_operator_body {
4 (
5 notation = $notation:ident,
6 struct_name = $name:ident,
7 operator = $operator:expr,
8 return_ty = (ReturnBasedOnArgs),
9 ty_params = ($($ty_param:ident,)+),
10 field_names = $field_names:tt,
11 backend_ty_params = $backend_ty_params:tt,
12 backend_ty = $backend_ty:ty,
13 ) => {
14 $crate::__diesel_operator_body! {
15 notation = $notation,
16 struct_name = $name,
17 operator = $operator,
18 return_ty = (ST),
19 ty_params = ($($ty_param,)+),
20 field_names = $field_names,
21 backend_ty_params = $backend_ty_params,
22 backend_ty = $backend_ty,
23 expression_ty_params = (ST,),
24 expression_bounds = ($($ty_param: $crate::expression::Expression<SqlType = ST>,)+),
25 }
26 };
27
28 (
29 notation = $notation:ident,
30 struct_name = $name:ident,
31 operator = $operator:expr,
32 return_ty = ($($return_ty:tt)+),
33 ty_params = ($($ty_param:ident,)+),
34 field_names = $field_names:tt,
35 backend_ty_params = $backend_ty_params:tt,
36 backend_ty = $backend_ty:ty,
37 ) => {
38 $crate::__diesel_operator_body! {
39 notation = $notation,
40 struct_name = $name,
41 operator = $operator,
42 return_ty = ($($return_ty)*),
43 ty_params = ($($ty_param,)+),
44 field_names = $field_names,
45 backend_ty_params = $backend_ty_params,
46 backend_ty = $backend_ty,
47 expression_ty_params = (),
48 expression_bounds = ($($ty_param: $crate::expression::Expression,)+),
49 }
50 };
51
52 (
53 notation = $notation:ident,
54 struct_name = $name:ident,
55 operator = $operator:expr,
56 return_ty = ($($return_ty:tt)+),
57 ty_params = ($($ty_param:ident,)+),
58 field_names = ($($field_name:ident,)+),
59 backend_ty_params = ($($backend_ty_param:ident,)*),
60 backend_ty = $backend_ty:ty,
61 expression_ty_params = ($($expression_ty_params:ident,)*),
62 expression_bounds = ($($expression_bounds:tt)*),
63 ) => {
64 #[derive(
65 Debug,
66 Clone,
67 Copy,
68 $crate::query_builder::QueryId,
69 $crate::sql_types::DieselNumericOps,
70 $crate::expression::ValidGrouping
71 )]
72 #[doc(hidden)]
73 #[allow(unreachable_pub)]
74 pub struct $name<$($ty_param,)+> {
75 $(pub(crate) $field_name: $ty_param,)+
76 }
77
78 impl<$($ty_param,)+> $name<$($ty_param,)+> {
79 #[allow(dead_code)]
80 pub(crate) fn new($($field_name: $ty_param,)+) -> Self {
81 $name { $($field_name,)+ }
82 }
83 }
84
85 $crate::impl_selectable_expression!($name<$($ty_param),+>);
86
87 impl<$($ty_param,)+ $($expression_ty_params,)*> $crate::expression::Expression for $name<$($ty_param,)+> where
88 $($expression_bounds)*
89 {
90 type SqlType = $($return_ty)*;
91 }
92
93 impl<$($ty_param,)+ $($backend_ty_param,)*> $crate::query_builder::QueryFragment<$backend_ty>
94 for $name<$($ty_param,)+> where
95 $($ty_param: $crate::query_builder::QueryFragment<$backend_ty>,)+
96 $($backend_ty_param: $crate::backend::Backend,)*
97 {
98 fn walk_ast<'b>(
99 &'b self,
100 mut out: $crate::query_builder::AstPass<'_, 'b, $backend_ty>
101 ) -> $crate::result::QueryResult<()>
102 {
103 $crate::__diesel_operator_to_sql!(
104 notation = $notation,
105 operator_expr = out.push_sql($operator),
106 field_exprs = ($(self.$field_name.walk_ast(out.reborrow())?),+),
107 );
108 Ok(())
109 }
110 }
111
112 impl<S, $($ty_param,)+> $crate::internal::operators_macro::FieldAliasMapper<S> for $name<$($ty_param,)+>
113 where
114 S: $crate::query_source::AliasSource,
115 $($ty_param: $crate::internal::operators_macro::FieldAliasMapper<S>,)+
116 {
117 type Out = $name<
118 $(<$ty_param as $crate::internal::operators_macro::FieldAliasMapper<S>>::Out,)+
119 >;
120 fn map(self, alias: &$crate::query_source::Alias<S>) -> Self::Out {
121 $name {
122 $($field_name: self.$field_name.map(alias),)+
123 }
124 }
125 }
126 }
127 }
128
129 #[macro_export]
130 #[doc(hidden)]
131 macro_rules! __diesel_operator_to_sql {
132 (
133 notation = infix,
134 operator_expr = $op:expr,
135 field_exprs = ($left:expr, $right:expr),
136 ) => {
137 $left;
138 $op;
139 $right;
140 };
141
142 (
143 notation = postfix,
144 operator_expr = $op:expr,
145 field_exprs = ($expr:expr),
146 ) => {
147 $expr;
148 $op;
149 };
150
151 (
152 notation = prefix,
153 operator_expr = $op:expr,
154 field_exprs = ($expr:expr),
155 ) => {
156 $op;
157 $expr;
158 };
159 }
160
161 /// Useful for libraries adding support for new SQL types. Apps should never
162 /// need to call this.
163 ///
164 /// This will create a new type with the given name. It will implement all
165 /// methods needed to be used as an expression in Diesel, placing the given
166 /// SQL between the two elements. The third argument specifies the SQL type
167 /// that the operator returns. If it is not given, the type will be assumed
168 /// to be `Bool`.
169 ///
170 /// If the operator is specific to a single backend, you can specify this by
171 /// adding `backend: Pg` or similar as the last argument.
172 ///
173 /// It should be noted that the generated impls will not constrain the SQL
174 /// types of the arguments. You should ensure that they are of the right
175 /// type in your function which constructs the operator.
176 ///
177 /// Typically you would not expose the type that this generates directly. You'd
178 /// expose a function (or trait) used to construct the expression, and a helper
179 /// type which represents the return type of that function. See the source of
180 /// `diesel::expression::expression_methods` and
181 /// `diesel::expression::helper_types` for real world examples of this.
182 ///
183 /// # Examples
184 ///
185 /// # Possible invocations
186 ///
187 /// ```ignore
188 /// // The SQL type will be boolean. The backend will not be constrained
189 /// infix_operator!(Matches, " @@ ");
190 ///
191 /// // Queries which try to execute `Contains` on a backend other than Pg
192 /// // will fail to compile
193 /// infix_operator!(Contains, " @> ", backend: Pg);
194 ///
195 /// // The type of `Concat` will be `TsVector` rather than Bool
196 /// infix_operator!(Concat, " || ", TsVector);
197 ///
198 /// // It is perfectly fine to have multiple operators with the same SQL.
199 /// // Diesel will ensure that the queries are always unambiguous in which
200 /// // operator applies
201 /// infix_operator!(Or, " || ", TsQuery);
202 ///
203 /// // Specifying both the return types and the backend
204 /// infix_operator!(And, " && ", TsQuery, backend: Pg);
205 ///
206 /// // Generic types are also supported as the return type
207 /// infix_operator!(MyOp, " OP ", Unsigned<TinyInt>);
208 /// ```
209 ///
210 /// ## Example usage
211 ///
212 /// ```rust
213 /// # include!("../doctest_setup.rs");
214 /// # use diesel::sql_types::SqlType;
215 /// # use diesel::expression::TypedExpressionType;
216 /// #
217 /// # fn main() {
218 /// # use schema::users::dsl::*;
219 /// # let connection = &mut establish_connection();
220 /// diesel::infix_operator!(MyEq, " = ");
221 ///
222 /// use diesel::expression::AsExpression;
223 ///
224 /// // Normally you would put this on a trait instead
225 /// fn my_eq<T, U, ST>(left: T, right: U) -> MyEq<T, U::Expression>
226 /// where
227 /// T: Expression<SqlType = ST>,
228 /// U: AsExpression<ST>,
229 /// ST: SqlType + TypedExpressionType,
230 /// {
231 /// MyEq::new(left, right.as_expression())
232 /// }
233 ///
234 /// let users_with_name = users.select(id).filter(my_eq(name, "Sean"));
235 ///
236 /// assert_eq!(Ok(1), users_with_name.first(connection));
237 /// # }
238 /// ```
239 #[macro_export]
240 macro_rules! infix_operator {
241 ($name:ident, $operator:expr) => {
242 $crate::infix_operator!($name, $operator, $crate::sql_types::Bool);
243 };
244
245 ($name:ident, $operator:expr, backend: $backend:ty) => {
246 $crate::infix_operator!($name, $operator, $crate::sql_types::Bool, backend: $backend);
247 };
248
249 ($name:ident, $operator:expr, $return_ty:ty) => {
250 $crate::__diesel_infix_operator!(
251 name = $name,
252 operator = $operator,
253 return_ty = NullableBasedOnArgs ($return_ty),
254 backend_ty_params = (DB,),
255 backend_ty = DB,
256 );
257 };
258
259 ($name:ident, $operator:expr, $return_ty:ty, backend: $backend:ty) => {
260 $crate::__diesel_infix_operator!(
261 name = $name,
262 operator = $operator,
263 return_ty = NullableBasedOnArgs ($return_ty),
264 backend_ty_params = (),
265 backend_ty = $backend,
266 );
267 };
268
269 }
270 #[macro_export]
271 #[doc(hidden)]
272 macro_rules! __diesel_infix_operator {
273 ($name:ident, $operator:expr, ConstantNullability $return_ty:ty) => {
274 $crate::__diesel_infix_operator!(
275 name = $name,
276 operator = $operator,
277 return_ty = ($return_ty),
278 backend_ty_params = (DB,),
279 backend_ty = DB,
280 );
281 };
282 ($name:ident, $operator:expr, __diesel_internal_SameResultAsInput, backend: $backend:ty) => {
283 $crate::__diesel_infix_operator!(
284 name = $name,
285 operator = $operator,
286 return_ty = (<T as $crate::expression::Expression>::SqlType),
287 backend_ty_params = (),
288 backend_ty = $backend,
289 );
290 };
291 ($name:ident, $operator:expr, ConstantNullability $return_ty:ty, backend: $backend:ty) => {
292 $crate::__diesel_infix_operator!(
293 name = $name,
294 operator = $operator,
295 return_ty = ($return_ty),
296 backend_ty_params = (),
297 backend_ty = $backend,
298 );
299 };
300
301 (
302 name = $name:ident,
303 operator = $operator:expr,
304 return_ty = NullableBasedOnArgs ($($return_ty:tt)+),
305 backend_ty_params = $backend_ty_params:tt,
306 backend_ty = $backend_ty:ty,
307 ) => {
308 $crate::__diesel_infix_operator!(
309 name = $name,
310 operator = $operator,
311 return_ty = (
312 $crate::sql_types::is_nullable::MaybeNullable<
313 $crate::sql_types::is_nullable::IsOneNullable<
314 <T as $crate::expression::Expression>::SqlType,
315 <U as $crate::expression::Expression>::SqlType
316 >,
317 $($return_ty)+
318 >
319 ),
320 expression_bounds = (
321 $crate::sql_types::is_nullable::IsSqlTypeNullable<
322 <T as $crate::expression::Expression>::SqlType
323 >: $crate::sql_types::OneIsNullable<
324 $crate::sql_types::is_nullable::IsSqlTypeNullable<
325 <U as $crate::expression::Expression>::SqlType
326 >
327 >,
328 $crate::sql_types::is_nullable::IsOneNullable<
329 <T as $crate::expression::Expression>::SqlType,
330 <U as $crate::expression::Expression>::SqlType
331 >: $crate::sql_types::MaybeNullableType<$($return_ty)+>,
332 ),
333 backend_ty_params = $backend_ty_params,
334 backend_ty = $backend_ty,
335 );
336 };
337
338 (
339 name = $name:ident,
340 operator = $operator:expr,
341 return_ty = ($($return_ty:tt)+),
342 backend_ty_params = $backend_ty_params:tt,
343 backend_ty = $backend_ty:ty,
344 ) => {
345 $crate::__diesel_infix_operator!(
346 name = $name,
347 operator = $operator,
348 return_ty = ($($return_ty)+),
349 expression_bounds = (),
350 backend_ty_params = $backend_ty_params,
351 backend_ty = $backend_ty,
352 );
353 };
354
355 (
356 name = $name:ident,
357 operator = $operator:expr,
358 return_ty = ($($return_ty:tt)+),
359 expression_bounds = ($($expression_bounds:tt)*),
360 backend_ty_params = $backend_ty_params:tt,
361 backend_ty = $backend_ty:ty,
362 ) => {
363 $crate::__diesel_operator_body!(
364 notation = infix,
365 struct_name = $name,
366 operator = $operator,
367 return_ty = ($($return_ty)+),
368 ty_params = (T, U,),
369 field_names = (left, right,),
370 backend_ty_params = $backend_ty_params,
371 backend_ty = $backend_ty,
372 expression_ty_params = (),
373 expression_bounds = (
374 T: $crate::expression::Expression,
375 U: $crate::expression::Expression,
376 <T as $crate::expression::Expression>::SqlType: $crate::sql_types::SqlType,
377 <U as $crate::expression::Expression>::SqlType: $crate::sql_types::SqlType,
378 $($expression_bounds)*
379 ),
380 );
381 };
382 }
383
384 #[macro_export]
385 #[deprecated(since = "2.0.0", note = "use `diesel::infix_operator!` instead")]
386 #[cfg(all(feature = "with-deprecated", not(feature = "without-deprecated")))]
387 #[doc(hidden)]
388 macro_rules! diesel_infix_operator {
389 ($($args:tt)*) => {
390 $crate::infix_operator!($($args)*);
391 }
392 }
393
394 /// Useful for libraries adding support for new SQL types. Apps should never
395 /// need to call this.
396 ///
397 /// Similar to [`infix_operator!`], but the generated type will only take
398 /// a single argument rather than two. The operator SQL will be placed after
399 /// the single argument. See [`infix_operator!`] for example usage.
400 #[macro_export]
401 macro_rules! postfix_operator {
402 ($name:ident, $operator:expr) => {
403 $crate::postfix_operator!($name, $operator, $crate::sql_types::Bool);
404 };
405
406 ($name:ident, $operator:expr, backend: $backend:ty) => {
407 $crate::postfix_operator!($name, $operator, $crate::sql_types::Bool, backend: $backend);
408 };
409
410 ($name:ident, $operator:expr, ConditionalNullability $($return_ty:tt)::*) => {
411 $crate::postfix_operator!(
412 name = $name,
413 operator = $operator,
414 return_ty = NullableBasedOnArgs ($($return_ty)::*),
415 );
416 };
417
418 ($name:ident, $operator:expr, ConditionalNullability $($return_ty:tt)::*, backend: $backend:ty) => {
419 $crate::postfix_operator!(
420 $name,
421 $operator,
422 return_ty = NullableBasedOnArgs ($($return_ty)::*),
423 backend: $backend
424 );
425 };
426
427 ($name:ident, $operator:expr, return_ty = NullableBasedOnArgs($return_ty:ty)) => {
428 $crate::__diesel_operator_body!(
429 notation = postfix,
430 struct_name = $name,
431 operator = $operator,
432 return_ty = (
433 $crate::sql_types::is_nullable::MaybeNullable<
434 <<Expr as $crate::expression::Expression>::SqlType as $crate::sql_types::SqlType>::IsNull,
435 $return_ty
436 >
437 ),
438 ty_params = (Expr,),
439 field_names = (expr,),
440 backend_ty_params = (DB,),
441 backend_ty = DB,
442 expression_ty_params = (),
443 expression_bounds = (
444 Expr: $crate::expression::Expression,
445 <Expr as $crate::expression::Expression>::SqlType: $crate::sql_types::SqlType,
446 $crate::sql_types::is_nullable::IsOneNullable<
447 <Expr as $crate::expression::Expression>::SqlType,
448 $return_ty
449 >: $crate::sql_types::MaybeNullableType<$return_ty>,
450 ),
451 );
452 };
453
454 ($name:ident, $operator:expr, return_ty = NullableBasedOnArgs($return_ty:ty), backend: $backend:ty) => {
455 $crate::__diesel_operator_body!(
456 notation = postfix,
457 struct_name = $name,
458 operator = $operator,
459 return_ty = (
460 $crate::sql_types::is_nullable::MaybeNullable<
461 $crate::sql_types::is_nullable::IsOneNullable<
462 <Expr as $crate::expression::Expression>::SqlType,
463 $return_ty
464 >,
465 $return_ty
466 >
467 ),
468 ty_params = (Expr,),
469 field_names = (expr,),
470 backend_ty_params = (),
471 backend_ty = $backend,
472 expression_ty_params = (),
473 expression_bounds = (
474 Expr: $crate::expression::Expression,
475 <Expr as $crate::expression::Expression>::SqlType: $crate::sql_types::SqlType,
476 $crate::sql_types::is_nullable::IsOneNullable<
477 <Expr as $crate::expression::Expression>::SqlType,
478 $return_ty
479 >: $crate::sql_types::MaybeNullableType<$return_ty>,
480 ),
481 );
482 };
483
484 ($name:ident, $operator:expr, $return_ty:ty) => {
485 $crate::__diesel_operator_body!(
486 notation = postfix,
487 struct_name = $name,
488 operator = $operator,
489 return_ty = ($return_ty),
490 ty_params = (Expr,),
491 field_names = (expr,),
492 backend_ty_params = (DB,),
493 backend_ty = DB,
494 );
495 };
496
497 ($name:ident, $operator:expr, $return_ty:ty, backend: $backend:ty) => {
498 $crate::__diesel_operator_body!(
499 notation = postfix,
500 struct_name = $name,
501 operator = $operator,
502 return_ty = ($return_ty),
503 ty_params = (Expr,),
504 field_names = (expr,),
505 backend_ty_params = (),
506 backend_ty = $backend,
507 );
508 };
509 }
510
511 #[macro_export]
512 #[deprecated(since = "2.0.0", note = "use `diesel::postfix_operator!` instead")]
513 #[cfg(all(feature = "with-deprecated", not(feature = "without-deprecated")))]
514 #[doc(hidden)]
515 macro_rules! diesel_postfix_operator {
516 ($($args:tt)*) => {
517 $crate::postfix_operator!($($args)*);
518 }
519 }
520
521 /// Useful for libraries adding support for new SQL types. Apps should never
522 /// need to call this.
523 ///
524 /// Similar to [`infix_operator!`], but the generated type will only take
525 /// a single argument rather than two. The operator SQL will be placed before
526 /// the single argument. See [`infix_operator!`] for example usage.
527 #[macro_export]
528 macro_rules! prefix_operator {
529 ($name:ident, $operator:expr) => {
530 $crate::prefix_operator!($name, $operator, $crate::sql_types::Bool);
531 };
532
533 ($name:ident, $operator:expr, backend: $backend:ty) => {
534 $crate::prefix_operator!($name, $operator, $crate::sql_types::Bool, backend: $backend);
535 };
536
537 ($name:ident, $operator:expr, $return_ty:ty) => {
538 $crate::__diesel_operator_body!(
539 notation = prefix,
540 struct_name = $name,
541 operator = $operator,
542 return_ty = (
543 $crate::sql_types::is_nullable::MaybeNullable<
544 $crate::sql_types::is_nullable::IsSqlTypeNullable<
545 <Expr as $crate::expression::Expression>::SqlType
546 >,
547 $return_ty,
548 >
549 ),
550 ty_params = (Expr,),
551 field_names = (expr,),
552 backend_ty_params = (DB,),
553 backend_ty = DB,
554 expression_ty_params = (),
555 expression_bounds = (
556 Expr: $crate::expression::Expression,
557 <Expr as $crate::expression::Expression>::SqlType: $crate::sql_types::SqlType,
558 $crate::sql_types::is_nullable::IsSqlTypeNullable<
559 <Expr as $crate::expression::Expression>::SqlType
560 >: $crate::sql_types::MaybeNullableType<$return_ty>,
561 ),
562 );
563 };
564
565 ($name:ident, $operator:expr, $return_ty:ty, backend: $backend:ty) => {
566 $crate::__diesel_operator_body!(
567 notation = prefix,
568 struct_name = $name,
569 operator = $operator,
570 return_ty = (
571 $crate::sql_types::is_nullable::MaybeNullable<
572 $crate::sql_types::is_nullable::IsSqlTypeNullable<
573 <Expr as $crate::expression::Expression>::SqlType
574 >,
575 $return_ty,
576 >
577 ),
578 ty_params = (Expr,),
579 field_names = (expr,),
580 backend_ty_params = (),
581 backend_ty = $backend,
582 expression_ty_params = (),
583 expression_bounds = (
584 Expr: $crate::expression::Expression,
585 <Expr as $crate::expression::Expression>::SqlType: $crate::sql_types::SqlType,
586 $crate::sql_types::is_nullable::IsSqlTypeNullable<
587 <Expr as $crate::expression::Expression>::SqlType
588 >: $crate::sql_types::MaybeNullableType<$return_ty>,
589 ),
590 );
591 };
592 }
593
594 #[macro_export]
595 #[deprecated(since = "2.0.0", note = "use `diesel::prefix_operator!` instead")]
596 #[cfg(all(feature = "with-deprecated", not(feature = "without-deprecated")))]
597 #[doc(hidden)]
598 macro_rules! diesel_prefix_operator {
599 ($($args:tt)*) => {
600 $crate::prefix_operator!($($args)*);
601 }
602 }
603
604 infix_operator!(And, " AND ");
605 infix_operator!(Or, " OR ");
606 infix_operator!(Escape, " ESCAPE ");
607 infix_operator!(Eq, " = ");
608 infix_operator!(Gt, " > ");
609 infix_operator!(GtEq, " >= ");
610 infix_operator!(Lt, " < ");
611 infix_operator!(LtEq, " <= ");
612 infix_operator!(NotEq, " != ");
613 infix_operator!(NotLike, " NOT LIKE ");
614 infix_operator!(Between, " BETWEEN ");
615 infix_operator!(NotBetween, " NOT BETWEEN ");
616
617 postfix_operator!(IsNull, " IS NULL");
618 postfix_operator!(IsNotNull, " IS NOT NULL");
619 postfix_operator!(
620 Asc,
621 " ASC",
622 crate::expression::expression_types::NotSelectable
623 );
624 postfix_operator!(
625 Desc,
626 " DESC",
627 crate::expression::expression_types::NotSelectable
628 );
629
630 prefix_operator!(Not, " NOT ");
631
632 use crate::backend::{sql_dialect, Backend, SqlDialect};
633 use crate::expression::{TypedExpressionType, ValidGrouping};
634 use crate::insertable::{ColumnInsertValue, Insertable};
635 use crate::query_builder::{QueryFragment, QueryId, ValuesClause};
636 use crate::query_source::Column;
637 use crate::sql_types::{DieselNumericOps, SqlType};
638
639 impl<T, U> Insertable<T::Table> for Eq<T, U>
640 where
641 T: Column,
642 {
643 type Values = ValuesClause<ColumnInsertValue<T, U>, T::Table>;
644
645 fn values(self) -> Self::Values {
646 ValuesClause::new(ColumnInsertValue::new(self.right))
647 }
648 }
649
650 impl<'a, T, Tab, U> Insertable<Tab> for &'a Eq<T, U>
651 where
652 T: Copy,
653 Eq<T, &'a U>: Insertable<Tab>,
654 {
655 type Values = <Eq<T, &'a U> as Insertable<Tab>>::Values;
656
657 fn values(self) -> Self::Values {
658 Eq::new(self.left, &self.right).values()
659 }
660 }
661
662 /// This type represents a string concat operator
663 #[diesel_derives::__diesel_public_if(
664 feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes",
665 public_fields(left, right)
666 )]
667 #[derive(Debug, Clone, Copy, QueryId, DieselNumericOps, ValidGrouping)]
668 pub struct Concat<L, R> {
669 /// The left side expression of the operator
670 pub(crate) left: L,
671 /// The right side expression of the operator
672 pub(crate) right: R,
673 }
674
675 impl<L, R> Concat<L, R> {
676 pub(crate) fn new(left: L, right: R) -> Self {
677 Self { left, right }
678 }
679 }
680
681 impl<L, R, ST> crate::expression::Expression for Concat<L, R>
682 where
683 L: crate::expression::Expression<SqlType = ST>,
684 R: crate::expression::Expression<SqlType = ST>,
685 ST: SqlType + TypedExpressionType,
686 {
687 type SqlType = ST;
688 }
689
690 impl_selectable_expression!(Concat<L, R>);
691
692 impl<L, R, DB> QueryFragment<DB> for Concat<L, R>
693 where
694 DB: Backend,
695 Self: QueryFragment<DB, DB::ConcatClause>,
696 {
697 fn walk_ast<'b>(
698 &'b self,
699 pass: crate::query_builder::AstPass<'_, 'b, DB>,
700 ) -> crate::result::QueryResult<()> {
701 <Self as QueryFragment<DB, DB::ConcatClause>>::walk_ast(self, pass)
702 }
703 }
704
705 impl<L, R, DB> QueryFragment<DB, sql_dialect::concat_clause::ConcatWithPipesClause> for Concat<L, R>
706 where
707 L: QueryFragment<DB>,
708 R: QueryFragment<DB>,
709 DB: Backend + SqlDialect<ConcatClause = sql_dialect::concat_clause::ConcatWithPipesClause>,
710 {
711 fn walk_ast<'b>(
712 &'b self,
713 mut out: crate::query_builder::AstPass<'_, 'b, DB>,
714 ) -> crate::result::QueryResult<()> {
715 // Since popular MySQL scalability layer Vitess does not support pipes in query parsing
716 // CONCAT has been implemented separately for MySQL (see MysqlConcatClause)
717 out.push_sql("(");
718 self.left.walk_ast(out.reborrow())?;
719 out.push_sql(" || ");
720 self.right.walk_ast(out.reborrow())?;
721 out.push_sql(")");
722 Ok(())
723 }
724 }
725
726 // need an explicit impl here to control which types are allowed
727 #[derive(
728 Debug,
729 Clone,
730 Copy,
731 crate::query_builder::QueryId,
732 crate::sql_types::DieselNumericOps,
733 crate::expression::ValidGrouping,
734 )]
735 #[doc(hidden)]
736 pub struct Like<T, U> {
737 pub(crate) left: T,
738 pub(crate) right: U,
739 }
740
741 impl<T, U> Like<T, U> {
742 pub(crate) fn new(left: T, right: U) -> Self {
743 Like { left, right }
744 }
745 }
746
747 impl<T, U, QS> crate::expression::SelectableExpression<QS> for Like<T, U>
748 where
749 Like<T, U>: crate::expression::AppearsOnTable<QS>,
750 T: crate::expression::SelectableExpression<QS>,
751 U: crate::expression::SelectableExpression<QS>,
752 {
753 }
754
755 impl<T, U, QS> crate::expression::AppearsOnTable<QS> for Like<T, U>
756 where
757 Like<T, U>: crate::expression::Expression,
758 T: crate::expression::AppearsOnTable<QS>,
759 U: crate::expression::AppearsOnTable<QS>,
760 {
761 }
762
763 impl<T, U> crate::expression::Expression for Like<T, U>
764 where
765 T: crate::expression::Expression,
766 U: crate::expression::Expression,
767 <T as crate::expression::Expression>::SqlType: crate::sql_types::SqlType,
768 <U as crate::expression::Expression>::SqlType: crate::sql_types::SqlType,
769 crate::sql_types::is_nullable::IsSqlTypeNullable<<T as crate::expression::Expression>::SqlType>:
770 crate::sql_types::OneIsNullable<
771 crate::sql_types::is_nullable::IsSqlTypeNullable<
772 <U as crate::expression::Expression>::SqlType,
773 >,
774 >,
775 crate::sql_types::is_nullable::IsOneNullable<
776 <T as crate::expression::Expression>::SqlType,
777 <U as crate::expression::Expression>::SqlType,
778 >: crate::sql_types::MaybeNullableType<crate::sql_types::Bool>,
779 {
780 type SqlType = crate::sql_types::is_nullable::MaybeNullable<
781 crate::sql_types::is_nullable::IsOneNullable<
782 <T as crate::expression::Expression>::SqlType,
783 <U as crate::expression::Expression>::SqlType,
784 >,
785 crate::sql_types::Bool,
786 >;
787 }
788
789 impl<T, U, DB> crate::query_builder::QueryFragment<DB> for Like<T, U>
790 where
791 T: crate::query_builder::QueryFragment<DB> + crate::Expression,
792 U: crate::query_builder::QueryFragment<DB>,
793 DB: crate::backend::Backend,
794 DB: LikeIsAllowedForType<T::SqlType>,
795 {
796 fn walk_ast<'b>(
797 &'b self,
798 mut out: crate::query_builder::AstPass<'_, 'b, DB>,
799 ) -> crate::result::QueryResult<()> {
800 (self.left.walk_ast(out.reborrow())?);
801 (out.push_sql(" LIKE "));
802 (self.right.walk_ast(out.reborrow())?);
803 Ok(())
804 }
805 }
806
807 impl<S, T, U> crate::internal::operators_macro::FieldAliasMapper<S> for Like<T, U>
808 where
809 S: crate::query_source::AliasSource,
810 T: crate::internal::operators_macro::FieldAliasMapper<S>,
811 U: crate::internal::operators_macro::FieldAliasMapper<S>,
812 {
813 type Out = Like<
814 <T as crate::internal::operators_macro::FieldAliasMapper<S>>::Out,
815 <U as crate::internal::operators_macro::FieldAliasMapper<S>>::Out,
816 >;
817 fn map(self, alias: &crate::query_source::Alias<S>) -> Self::Out {
818 Like {
819 left: self.left.map(alias),
820 right: self.right.map(alias),
821 }
822 }
823 }
824
825 #[diagnostic::on_unimplemented(
826 message = "cannot use the `LIKE` operator with expressions of the type `{ST}` for the backend `{Self}`",
827 note = "expressions of the type `diesel::sql_types::Text` and `diesel::sql_types::Nullable<Text>` are \n\
828 allowed for all backends"
829 )]
830 #[cfg_attr(
831 feature = "postgres_backend",
832 diagnostic::on_unimplemented(
833 note = "expressions of the type `diesel::sql_types::Binary` and `diesel::sql_types::Nullable<Binary>` are \n\
834 allowed for the PostgreSQL backend"
835 )
836 )]
837 pub trait LikeIsAllowedForType<ST>: Backend {}
838
839 impl<DB> LikeIsAllowedForType<crate::sql_types::Text> for DB where DB: Backend {}
840
841 #[cfg(feature = "postgres_backend")]
842 impl LikeIsAllowedForType<crate::pg::sql_types::Citext> for crate::pg::Pg {}
843
844 impl<T, DB> LikeIsAllowedForType<crate::sql_types::Nullable<T>> for DB where
845 DB: Backend + LikeIsAllowedForType<T>
846 {
847 }