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 helper trait indicating that this is a bind data container
51#[doc = " A helper trait indicating that this is a bind data container"]
pub trait BindData: Send + 'static {
/// How many bind values does this container contain
fn len(&self)
-> usize;
/// Is this set of bind data empty
fn is_empty(&self) -> bool { self.len() == 0 }
}#[diesel_derives::__diesel_public_if(
52 feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
53)]54pub trait BindData: Send + 'static {
55/// How many bind values does this container contain
56fn len(&self) -> usize;
5758/// Is this set of bind data empty
59// This mostly exists to satisfy clippy::len_without_is_empty
60fn is_empty(&self) -> bool {
61self.len() == 0
62}
63}
6465/// A movable version of the bind collector which allows it to be extracted, moved and refilled.
66///
67/// This is mostly useful in async context where bind data needs to be moved across threads.
68#[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: BindData;
/// 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(
69 feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
70)]71pub trait MoveableBindCollector<DB: TypeMetadata> {
72/// The movable bind data of this bind collector
73type BindData: BindData;
7475/// Builds a movable version of the bind collector
76fn moveable(&self) -> Self::BindData;
7778/// Refill the bind collector with its bind data
79fn append_bind_data(&mut self, from: &Self::BindData);
8081/// Push bind data as debug representation
82fn push_debug_binds<'a, 'b>(
83 bind_data: &Self::BindData,
84 f: &'a mut Vec<Box<dyn core::fmt::Debug + 'b>>,
85 );
86}
8788#[derive(#[automatically_derived]impl<DB: ::core::fmt::Debug + Backend + TypeMetadata>
::core::fmt::Debug for RawBytesBindCollector<DB> where
DB::TypeMetadata: ::core::fmt::Debug {
#[inline]
#[rustc_trivial_field_reads]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f,
"RawBytesBindCollector metadata binds", &self.metadata,
&&self.binds)
}
}Debug)]
89/// A bind collector used by backends which transmit bind parameters as an
90/// opaque blob of bytes.
91///
92/// For most backends, this is the concrete implementation of `BindCollector`
93/// that should be used.
94#[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(
95 feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes",
96 public_fields(metadata, binds)
97)]98pub struct RawBytesBindCollector<DB: Backend + TypeMetadata> {
99/// The metadata associated with each bind parameter.
100 ///
101 /// This vec is guaranteed to be the same length as `binds`.
102pub(crate) metadata: Vec<DB::TypeMetadata>,
103/// The serialized bytes for each bind parameter.
104 ///
105 /// This vec is guaranteed to be the same length as `metadata`.
106pub(crate) binds: Vec<Option<Vec<u8>>>,
107}
108109impl<DB: Backend + TypeMetadata> Defaultfor RawBytesBindCollector<DB> {
110fn default() -> Self {
111Self::new()
112 }
113}
114115#[allow(clippy::new_without_default)]
116impl<DB: Backend + TypeMetadata> RawBytesBindCollector<DB> {
117/// Construct an empty `RawBytesBindCollector`
118pub fn new() -> Self {
119RawBytesBindCollector {
120 metadata: Vec::new(),
121 binds: Vec::new(),
122 }
123 }
124125pub(crate) fn reborrow_buffer<'a: 'b, 'b>(b: &'b mut ByteWrapper<'a>) -> ByteWrapper<'b> {
126ByteWrapper(b.0)
127 }
128}
129130impl<'a, DB> BindCollector<'a, DB> for RawBytesBindCollector<DB>
131where
132 for<'b> DB: Backend<BindCollector<'b> = Self> + TypeMetadata,
133{
134type Buffer = ByteWrapper<'a>;
135136fn push_bound_value<T, U>(
137&mut self,
138 bind: &U,
139 metadata_lookup: &mut DB::MetadataLookup,
140 ) -> QueryResult<()>
141where
142DB: HasSqlType<T>,
143 U: ToSql<T, DB> + ?Sized,
144 {
145let mut bytes = Vec::new();
146let is_null = {
147let mut to_sql_output = Output::new(ByteWrapper(&mut bytes), metadata_lookup);
148 bind.to_sql(&mut to_sql_output)
149 .map_err(SerializationError)?
150};
151let metadata = <DB as HasSqlType<T>>::metadata(metadata_lookup);
152match is_null {
153 IsNull::No => self.binds.push(Some(bytes)),
154 IsNull::Yes => self.binds.push(None),
155 }
156self.metadata.push(metadata);
157Ok(())
158 }
159160fn push_null_value(&mut self, metadata: DB::TypeMetadata) -> QueryResult<()> {
161self.metadata.push(metadata);
162self.binds.push(None);
163Ok(())
164 }
165}
166167impl<DB> MoveableBindCollector<DB> for RawBytesBindCollector<DB>
168where
169 for<'a> DB: Backend<BindCollector<'a> = Self> + TypeMetadata + 'static,
170 <DB as TypeMetadata>::TypeMetadata: core::fmt::Debug + Clone + Send,
171{
172type BindData = Self;
173174fn moveable(&self) -> Self::BindData {
175RawBytesBindCollector {
176 binds: self.binds.clone(),
177 metadata: self.metadata.clone(),
178 }
179 }
180181fn append_bind_data(&mut self, from: &Self::BindData) {
182self.binds.extend(from.binds.iter().cloned());
183self.metadata.extend(from.metadata.clone());
184 }
185186fn push_debug_binds<'a, 'b>(
187 bind_data: &Self::BindData,
188 f: &'a mut Vec<Box<dyn core::fmt::Debug + 'b>>,
189 ) {
190f.extend(
191bind_data192 .metadata
193 .iter()
194 .map(|m| Box::new(m.clone()) as Box<dyn core::fmt::Debug>),
195 );
196 }
197}
198199impl<DB> BindDatafor RawBytesBindCollector<DB>
200where
201DB: Backend + TypeMetadata,
202for<'a> DB: Backend<BindCollector<'a> = Self> + TypeMetadata + 'static,
203 <DB as TypeMetadata>::TypeMetadata: core::fmt::Debug + Clone + Send,
204{
205fn len(&self) -> usize {
206self.binds.len()
207 }
208}
209210// This is private for now as we may want to add `Into` impls for the wrapper type
211// later on
212mod private {
213use alloc::vec::Vec;
214215/// A type wrapper for raw bytes
216#[derive(#[automatically_derived]impl<'a> ::core::fmt::Debug for ByteWrapper<'a> {
#[inline]
#[rustc_trivial_field_reads]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field1_finish(f, "ByteWrapper",
&&self.0)
}
}Debug)]
217pub struct ByteWrapper<'a>(pub(crate) &'a mut Vec<u8>);
218}