diesel_bind_if_some/
lib.rs1use diesel::{
2 backend::Backend,
3 expression::ValidGrouping,
4 query_builder::{AstPass, QueryFragment, QueryId},
5 sql_types::{self, SingleValue},
6 AppearsOnTable, Expression, QueryResult, SelectableExpression,
7};
8
9#[derive(Clone, Copy)]
18pub struct BindIfSome<T>(pub Option<T>);
19
20impl<T> QueryId for BindIfSome<T> {
32 type QueryId = ();
33 const HAS_STATIC_QUERY_ID: bool = false;
34}
35
36impl<T: Expression> Expression for BindIfSome<T>
37where
38 T::SqlType: SingleValue,
39{
40 type SqlType = sql_types::Nullable<T::SqlType>;
41}
42
43impl<DB: Backend, T: QueryFragment<DB>> QueryFragment<DB> for BindIfSome<T> {
44 fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, DB>) -> QueryResult<()> {
45 if let Some(value) = &self.0 {
46 value.walk_ast(out)?;
47 } else {
48 out.push_sql("(NULL)");
49 }
50
51 Ok(())
52 }
53}
54
55impl<QS: ?Sized, T: AppearsOnTable<QS>> AppearsOnTable<QS> for BindIfSome<T> where
56 T::SqlType: SingleValue
57{
58}
59
60impl<QS: ?Sized, T: SelectableExpression<QS>> SelectableExpression<QS> for BindIfSome<T> where
61 T::SqlType: SingleValue
62{
63}
64
65impl<GB, T: ValidGrouping<GB>> ValidGrouping<GB> for BindIfSome<T> {
66 type IsAggregate = T::IsAggregate;
67}