Skip to content

Commit

Permalink
add ConditionalNullability support to postfix_operator
Browse files Browse the repository at this point in the history
  • Loading branch information
meeshal committed Nov 14, 2024
1 parent 039a541 commit fedd2b7
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 11 deletions.
77 changes: 77 additions & 0 deletions diesel/src/expression/operators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,83 @@ macro_rules! postfix_operator {
$crate::postfix_operator!($name, $operator, $crate::sql_types::Bool, backend: $backend);
};

($name:ident, $operator:expr, ConditionalNullability $($return_ty:tt)::*) => {
$crate::postfix_operator!(
name = $name,
operator = $operator,
return_ty = NullableBasedOnArgs ($($return_ty)::*),
);
};

($name:ident, $operator:expr, ConditionalNullability $($return_ty:tt)::*, backend: $backend:ty) => {
$crate::postfix_operator!(
$name,
$operator,
return_ty = NullableBasedOnArgs ($($return_ty)::*),
backend: $backend
);
};

($name:ident, $operator:expr, return_ty = NullableBasedOnArgs($return_ty:ty)) => {
$crate::__diesel_operator_body!(
notation = postfix,
struct_name = $name,
operator = $operator,
return_ty = (
$crate::sql_types::is_nullable::MaybeNullable<
$crate::sql_types::is_nullable::IsOneNullable<
<Expr as $crate::expression::Expression>::SqlType,
$return_ty
>,
$return_ty
>
),
ty_params = (Expr,),
field_names = (expr,),
backend_ty_params = (DB,),
backend_ty = DB,
expression_ty_params = (),
expression_bounds = (
Expr: $crate::expression::Expression,
<Expr as $crate::expression::Expression>::SqlType: $crate::sql_types::SqlType,
$crate::sql_types::is_nullable::IsOneNullable<
<Expr as $crate::expression::Expression>::SqlType,
$return_ty
>: $crate::sql_types::MaybeNullableType<$return_ty>,
),
);
};

($name:ident, $operator:expr, return_ty = NullableBasedOnArgs($return_ty:ty), backend: $backend:ty) => {
$crate::__diesel_operator_body!(
notation = postfix,
struct_name = $name,
operator = $operator,
return_ty = (
$crate::sql_types::is_nullable::MaybeNullable<
$crate::sql_types::is_nullable::IsOneNullable<
<Expr as $crate::expression::Expression>::SqlType,
$return_ty
>,
$return_ty
>
),
ty_params = (Expr,),
field_names = (expr,),
backend_ty_params = (),
backend_ty = $backend,
expression_ty_params = (),
expression_bounds = (
Expr: $crate::expression::Expression,
<Expr as $crate::expression::Expression>::SqlType: $crate::sql_types::SqlType,
$crate::sql_types::is_nullable::IsOneNullable<
<Expr as $crate::expression::Expression>::SqlType,
$return_ty
>: $crate::sql_types::MaybeNullableType<$return_ty>,
),
);
};

($name:ident, $operator:expr, $return_ty:ty) => {
$crate::__diesel_operator_body!(
notation = postfix,
Expand Down
4 changes: 1 addition & 3 deletions diesel/src/pg/expression/expression_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1003,10 +1003,8 @@ pub trait PgTextExpressionMethods: Expression + Sized {
/// assert_eq!(res, false);
/// let res = diesel::select(("\"abc\"".into_sql::<Text>().is_json_scalar())).get_result::<bool>(conn)?;
/// assert_eq!(res, true);
/// let res = diesel::select(sql::<Nullable<Text>>("NULL").is_json_scalar().nullable()).get_result::<Option<bool>>(conn)?;
/// let res = diesel::select(sql::<Nullable<Text>>("NULL").is_json_scalar()).get_result::<Option<bool>>(conn)?;
/// assert!(res.is_none());
/// let res = diesel::select(sql::<Nullable<Text>>("123").is_json_scalar().nullable()).get_result::<Option<bool>>(conn)?;
/// assert_eq!(res, Some(true));
/// # Ok(())
/// # }
/// ```
Expand Down
16 changes: 8 additions & 8 deletions diesel/src/pg/expression/operators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ infix_operator!(SimilarTo, " SIMILAR TO ", backend: Pg);
infix_operator!(NotSimilarTo, " NOT SIMILAR TO ", backend: Pg);
postfix_operator!(NullsFirst, " NULLS FIRST", NotSelectable, backend: Pg);
postfix_operator!(NullsLast, " NULLS LAST", NotSelectable, backend: Pg);
postfix_operator!(IsJson, " IS JSON", backend: Pg);
postfix_operator!(IsNotJson, " IS NOT JSON", backend: Pg);
postfix_operator!(IsJsonObject, " IS JSON OBJECT", backend: Pg);
postfix_operator!(IsNotJsonObject, " IS NOT JSON OBJECT", backend: Pg);
postfix_operator!(IsJsonArray, " IS JSON ARRAY", backend: Pg);
postfix_operator!(IsNotJsonArray, " IS NOT JSON ARRAY", backend: Pg);
postfix_operator!(IsJsonScalar, " IS JSON SCALAR", backend: Pg);
postfix_operator!(IsNotJsonScalar, " IS NOT JSON SCALAR", backend: Pg);
postfix_operator!(IsJson, " IS JSON", ConditionalNullability Bool, backend: Pg);
postfix_operator!(IsNotJson, " IS NOT JSON", ConditionalNullability Bool, backend: Pg);
postfix_operator!(IsJsonObject, " IS JSON OBJECT", ConditionalNullability Bool, backend: Pg);
postfix_operator!(IsNotJsonObject, " IS NOT JSON OBJECT", ConditionalNullability Bool, backend: Pg);
postfix_operator!(IsJsonArray, " IS JSON ARRAY", ConditionalNullability Bool, backend: Pg);
postfix_operator!(IsNotJsonArray, " IS NOT JSON ARRAY", ConditionalNullability Bool, backend: Pg);
postfix_operator!(IsJsonScalar, " IS JSON SCALAR", ConditionalNullability Bool, backend: Pg);
postfix_operator!(IsNotJsonScalar, " IS NOT JSON SCALAR", ConditionalNullability Bool, backend: Pg);
infix_operator!(ContainsNet, " >> ", backend: Pg);
infix_operator!(ContainsNetLoose, " >>= ", backend: Pg);
infix_operator!(IsContainedByNet, " << ", backend: Pg);
Expand Down

0 comments on commit fedd2b7

Please sign in to comment.