1//! Types related to managing bind parameters during query construction.
23use 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;
1011#[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;
1617/// 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
26type Buffer;
2728/// Serializes the given bind value, and collects the result.
29fn push_bound_value<T, U>(
30&mut self,
31 bind: &'a U,
32 metadata_lookup: &mut DB::MetadataLookup,
33 ) -> QueryResult<()>
34where
35DB: Backend + HasSqlType<T>,
36 U: ToSql<T, DB> + ?Sized + 'a;
3738/// 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)]
45fn push_null_value(&mut self, _metadata: DB::TypeMetadata) -> QueryResult<()> {
46Ok(())
47 }
48}
4950/// 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
58type BindData: Send + 'static;
5960/// Builds a movable version of the bind collector
61fn moveable(&self) -> Self::BindData;
6263/// Refill the bind collector with its bind data
64fn append_bind_data(&mut self, from: &Self::BindData);
6566/// Push bind data as debug representation
67fn push_debug_binds<'a, 'b>(
68 bind_data: &Self::BindData,
69 f: &'a mut Vec<Box<dyn core::fmt::Debug + 'b>>,
70 );
71}
7273#[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`.
87pub(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`.
91pub(crate) binds: Vec<Option<Vec<u8>>>,
92}
9394impl<DB: Backend + TypeMetadata> Defaultfor RawBytesBindCollector<DB> {
95fn default() -> Self {
96Self::new()
97 }
98}
99100#[allow(clippy::new_without_default)]
101impl<DB: Backend + TypeMetadata> RawBytesBindCollector<DB> {
102/// Construct an empty `RawBytesBindCollector`
103pub fn new() -> Self {
104RawBytesBindCollector {
105 metadata: Vec::new(),
106 binds: Vec::new(),
107 }
108 }
109110pub(crate) fn reborrow_buffer<'a: 'b, 'b>(b: &'b mut ByteWrapper<'a>) -> ByteWrapper<'b> {
111ByteWrapper(b.0)
112 }
113}
114115impl<'a, DB> BindCollector<'a, DB> for RawBytesBindCollector<DB>
116where
117 for<'b> DB: Backend<BindCollector<'b> = Self> + TypeMetadata,
118{
119type Buffer = ByteWrapper<'a>;
120121fn push_bound_value<T, U>(
122&mut self,
123 bind: &U,
124 metadata_lookup: &mut DB::MetadataLookup,
125 ) -> QueryResult<()>
126where
127DB: HasSqlType<T>,
128 U: ToSql<T, DB> + ?Sized,
129 {
130let mut bytes = Vec::new();
131let is_null = {
132let mut to_sql_output = Output::new(ByteWrapper(&mut bytes), metadata_lookup);
133bind.to_sql(&mut to_sql_output)
134 .map_err(SerializationError)?
135};
136let metadata = <DB as HasSqlType<T>>::metadata(metadata_lookup);
137match is_null {
138 IsNull::No => self.binds.push(Some(bytes)),
139 IsNull::Yes => self.binds.push(None),
140 }
141self.metadata.push(metadata);
142Ok(())
143 }
144145fn push_null_value(&mut self, metadata: DB::TypeMetadata) -> QueryResult<()> {
146self.metadata.push(metadata);
147self.binds.push(None);
148Ok(())
149 }
150}
151152impl<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{
157type BindData = Self;
158159fn moveable(&self) -> Self::BindData {
160RawBytesBindCollector {
161 binds: self.binds.clone(),
162 metadata: self.metadata.clone(),
163 }
164 }
165166fn append_bind_data(&mut self, from: &Self::BindData) {
167self.binds.extend(from.binds.iter().cloned());
168self.metadata.extend(from.metadata.clone());
169 }
170171fn push_debug_binds<'a, 'b>(
172 bind_data: &Self::BindData,
173 f: &'a mut Vec<Box<dyn core::fmt::Debug + 'b>>,
174 ) {
175f.extend(
176bind_data177 .metadata
178 .iter()
179 .map(|m| Box::new(m.clone()) as Box<dyn core::fmt::Debug>),
180 );
181 }
182}
183184// This is private for now as we may want to add `Into` impls for the wrapper type
185// later on
186mod private {
187use alloc::vec::Vec;
188189/// 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)]
191pub struct ByteWrapper<'a>(pub(crate) &'a mut Vec<u8>);
192}