Skip to main content

diesel/query_builder/
bind_collector.rs

1//! Types related to managing bind parameters during query construction.
2
3use crate::backend::Backend;
4use crate::result::Error::SerializationError;
5use crate::result::QueryResult;
6use crate::serialize::{IsNull, Output, ToSql};
7use crate::sql_types::{HasSqlType, TypeMetadata};
8use alloc::boxed::Box;
9use alloc::vec::Vec;
10
11#[doc(inline)]
12#[doc(inline)]
pub use self::private::ByteWrapper;#[diesel_derives::__diesel_public_if(
13    feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
14)]
15pub(crate) use self::private::ByteWrapper;
16
17/// A type which manages serializing bind parameters during query construction.
18///
19/// The only reason you would ever need to interact with this trait is if you
20/// are adding support for a new backend to Diesel. Plugins which are extending
21/// the query builder will use [`AstPass::push_bind_param`] instead.
22///
23/// [`AstPass::push_bind_param`]: crate::query_builder::AstPass::push_bind_param()
24pub trait BindCollector<'a, DB: TypeMetadata>: Sized {
25    /// The internal buffer type used by this bind collector
26    type Buffer;
27
28    /// Serializes the given bind value, and collects the result.
29    fn push_bound_value<T, U>(
30        &mut self,
31        bind: &'a U,
32        metadata_lookup: &mut DB::MetadataLookup,
33    ) -> QueryResult<()>
34    where
35        DB: Backend + HasSqlType<T>,
36        U: ToSql<T, DB> + ?Sized + 'a;
37
38    /// Push a null value with the given type information onto the bind collector
39    // For backward compatibility reasons we provide a default implementation
40    // but custom backends that want to support `#[derive(MultiConnection)]`
41    // need to provide a customized implementation of this function
42    #[diesel_derives::__diesel_public_if(
43        feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
44    )]
45    fn push_null_value(&mut self, _metadata: DB::TypeMetadata) -> QueryResult<()> {
46        Ok(())
47    }
48}
49
50/// A movable version of the bind collector which allows it to be extracted, moved and refilled.
51///
52/// This is mostly useful in async context where bind data needs to be moved across threads.
53#[doc =
" A movable version of the bind collector which allows it to be extracted, moved and refilled."]
#[doc = ""]
#[doc =
" This is mostly useful in async context where bind data needs to be moved across threads."]
pub trait MoveableBindCollector<DB: TypeMetadata> {
    /// The movable bind data of this bind collector
    type BindData: Send + 'static;
    /// Builds a movable version of the bind collector
    fn moveable(&self)
    -> Self::BindData;
    /// Refill the bind collector with its bind data
    fn append_bind_data(&mut self, from: &Self::BindData);
    /// Push bind data as debug representation
    fn push_debug_binds<'a,
    'b>(bind_data: &Self::BindData,
    f: &'a mut Vec<Box<dyn core::fmt::Debug + 'b>>);
}#[diesel_derives::__diesel_public_if(
54    feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
55)]
56pub trait MoveableBindCollector<DB: TypeMetadata> {
57    /// The movable bind data of this bind collector
58    type BindData: Send + 'static;
59
60    /// Builds a movable version of the bind collector
61    fn moveable(&self) -> Self::BindData;
62
63    /// Refill the bind collector with its bind data
64    fn append_bind_data(&mut self, from: &Self::BindData);
65
66    /// Push bind data as debug representation
67    fn push_debug_binds<'a, 'b>(
68        bind_data: &Self::BindData,
69        f: &'a mut Vec<Box<dyn core::fmt::Debug + 'b>>,
70    );
71}
72
73#[derive(#[automatically_derived]
impl<DB: ::core::fmt::Debug + Backend + TypeMetadata> ::core::fmt::Debug for
    RawBytesBindCollector<DB> where DB::TypeMetadata: ::core::fmt::Debug {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f,
            "RawBytesBindCollector", "metadata", &self.metadata, "binds",
            &&self.binds)
    }
}Debug)]
74/// A bind collector used by backends which transmit bind parameters as an
75/// opaque blob of bytes.
76///
77/// For most backends, this is the concrete implementation of `BindCollector`
78/// that should be used.
79#[doc =
" A bind collector used by backends which transmit bind parameters as an"]
#[doc = " opaque blob of bytes."]
#[doc = ""]
#[doc =
" For most backends, this is the concrete implementation of `BindCollector`"]
#[doc = " that should be used."]
#[non_exhaustive]
pub struct RawBytesBindCollector<DB: Backend + TypeMetadata> {
    #[doc = " The metadata associated with each bind parameter."]
    #[doc = ""]
    #[doc = " This vec is guaranteed to be the same length as `binds`."]
    pub metadata: Vec<DB::TypeMetadata>,
    #[doc = " The serialized bytes for each bind parameter."]
    #[doc = ""]
    #[doc = " This vec is guaranteed to be the same length as `metadata`."]
    pub binds: Vec<Option<Vec<u8>>>,
}#[diesel_derives::__diesel_public_if(
80    feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes",
81    public_fields(metadata, binds)
82)]
83pub struct RawBytesBindCollector<DB: Backend + TypeMetadata> {
84    /// The metadata associated with each bind parameter.
85    ///
86    /// This vec is guaranteed to be the same length as `binds`.
87    pub(crate) metadata: Vec<DB::TypeMetadata>,
88    /// The serialized bytes for each bind parameter.
89    ///
90    /// This vec is guaranteed to be the same length as `metadata`.
91    pub(crate) binds: Vec<Option<Vec<u8>>>,
92}
93
94impl<DB: Backend + TypeMetadata> Default for RawBytesBindCollector<DB> {
95    fn default() -> Self {
96        Self::new()
97    }
98}
99
100#[allow(clippy::new_without_default)]
101impl<DB: Backend + TypeMetadata> RawBytesBindCollector<DB> {
102    /// Construct an empty `RawBytesBindCollector`
103    pub fn new() -> Self {
104        RawBytesBindCollector {
105            metadata: Vec::new(),
106            binds: Vec::new(),
107        }
108    }
109
110    pub(crate) fn reborrow_buffer<'a: 'b, 'b>(b: &'b mut ByteWrapper<'a>) -> ByteWrapper<'b> {
111        ByteWrapper(b.0)
112    }
113}
114
115impl<'a, DB> BindCollector<'a, DB> for RawBytesBindCollector<DB>
116where
117    for<'b> DB: Backend<BindCollector<'b> = Self> + TypeMetadata,
118{
119    type Buffer = ByteWrapper<'a>;
120
121    fn push_bound_value<T, U>(
122        &mut self,
123        bind: &U,
124        metadata_lookup: &mut DB::MetadataLookup,
125    ) -> QueryResult<()>
126    where
127        DB: HasSqlType<T>,
128        U: ToSql<T, DB> + ?Sized,
129    {
130        let mut bytes = Vec::new();
131        let is_null = {
132            let mut to_sql_output = Output::new(ByteWrapper(&mut bytes), metadata_lookup);
133            bind.to_sql(&mut to_sql_output)
134                .map_err(SerializationError)?
135        };
136        let metadata = <DB as HasSqlType<T>>::metadata(metadata_lookup);
137        match is_null {
138            IsNull::No => self.binds.push(Some(bytes)),
139            IsNull::Yes => self.binds.push(None),
140        }
141        self.metadata.push(metadata);
142        Ok(())
143    }
144
145    fn push_null_value(&mut self, metadata: DB::TypeMetadata) -> QueryResult<()> {
146        self.metadata.push(metadata);
147        self.binds.push(None);
148        Ok(())
149    }
150}
151
152impl<DB> MoveableBindCollector<DB> for RawBytesBindCollector<DB>
153where
154    for<'a> DB: Backend<BindCollector<'a> = Self> + TypeMetadata + 'static,
155    <DB as TypeMetadata>::TypeMetadata: core::fmt::Debug + Clone + Send,
156{
157    type BindData = Self;
158
159    fn moveable(&self) -> Self::BindData {
160        RawBytesBindCollector {
161            binds: self.binds.clone(),
162            metadata: self.metadata.clone(),
163        }
164    }
165
166    fn append_bind_data(&mut self, from: &Self::BindData) {
167        self.binds.extend(from.binds.iter().cloned());
168        self.metadata.extend(from.metadata.clone());
169    }
170
171    fn push_debug_binds<'a, 'b>(
172        bind_data: &Self::BindData,
173        f: &'a mut Vec<Box<dyn core::fmt::Debug + 'b>>,
174    ) {
175        f.extend(
176            bind_data
177                .metadata
178                .iter()
179                .map(|m| Box::new(m.clone()) as Box<dyn core::fmt::Debug>),
180        );
181    }
182}
183
184// This is private for now as we may want to add `Into` impls for the wrapper type
185// later on
186mod private {
187    use alloc::vec::Vec;
188
189    /// A type wrapper for raw bytes
190    #[derive(#[automatically_derived]
impl<'a> ::core::fmt::Debug for ByteWrapper<'a> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_tuple_field1_finish(f, "ByteWrapper",
            &&self.0)
    }
}Debug)]
191    pub struct ByteWrapper<'a>(pub(crate) &'a mut Vec<u8>);
192}