diesel/query_builder/
bind_collector.rs1use 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#[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
17pub trait BindCollector<'a, DB: TypeMetadata>: Sized {
25 type Buffer;
27
28 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 #[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#[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 fn len(&self) -> usize;
57
58 fn is_empty(&self) -> bool {
61 self.len() == 0
62 }
63}
64
65#[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 type BindData: BindData;
74
75 fn moveable(&self) -> Self::BindData;
77
78 fn append_bind_data(&mut self, from: &Self::BindData);
80
81 fn push_debug_binds<'a, 'b>(
83 bind_data: &Self::BindData,
84 f: &'a mut Vec<Box<dyn core::fmt::Debug + 'b>>,
85 );
86}
87
88#[derive(Debug)]
89#[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 pub(crate) metadata: Vec<DB::TypeMetadata>,
103 pub(crate) binds: Vec<Option<Vec<u8>>>,
107}
108
109impl<DB: Backend + TypeMetadata> Default for RawBytesBindCollector<DB> {
110 fn default() -> Self {
111 Self::new()
112 }
113}
114
115#[allow(clippy::new_without_default)]
116impl<DB: Backend + TypeMetadata> RawBytesBindCollector<DB> {
117 pub fn new() -> Self {
119 RawBytesBindCollector {
120 metadata: Vec::new(),
121 binds: Vec::new(),
122 }
123 }
124
125 pub(crate) fn reborrow_buffer<'a: 'b, 'b>(b: &'b mut ByteWrapper<'a>) -> ByteWrapper<'b> {
126 ByteWrapper(b.0)
127 }
128}
129
130impl<'a, DB> BindCollector<'a, DB> for RawBytesBindCollector<DB>
131where
132 for<'b> DB: Backend<BindCollector<'b> = Self> + TypeMetadata,
133{
134 type Buffer = ByteWrapper<'a>;
135
136 fn push_bound_value<T, U>(
137 &mut self,
138 bind: &U,
139 metadata_lookup: &mut DB::MetadataLookup,
140 ) -> QueryResult<()>
141 where
142 DB: HasSqlType<T>,
143 U: ToSql<T, DB> + ?Sized,
144 {
145 let mut bytes = Vec::new();
146 let is_null = {
147 let mut to_sql_output = Output::new(ByteWrapper(&mut bytes), metadata_lookup);
148 bind.to_sql(&mut to_sql_output)
149 .map_err(SerializationError)?
150 };
151 let metadata = <DB as HasSqlType<T>>::metadata(metadata_lookup);
152 match is_null {
153 IsNull::No => self.binds.push(Some(bytes)),
154 IsNull::Yes => self.binds.push(None),
155 }
156 self.metadata.push(metadata);
157 Ok(())
158 }
159
160 fn push_null_value(&mut self, metadata: DB::TypeMetadata) -> QueryResult<()> {
161 self.metadata.push(metadata);
162 self.binds.push(None);
163 Ok(())
164 }
165}
166
167impl<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{
172 type BindData = Self;
173
174 fn moveable(&self) -> Self::BindData {
175 RawBytesBindCollector {
176 binds: self.binds.clone(),
177 metadata: self.metadata.clone(),
178 }
179 }
180
181 fn append_bind_data(&mut self, from: &Self::BindData) {
182 self.binds.extend(from.binds.iter().cloned());
183 self.metadata.extend(from.metadata.clone());
184 }
185
186 fn push_debug_binds<'a, 'b>(
187 bind_data: &Self::BindData,
188 f: &'a mut Vec<Box<dyn core::fmt::Debug + 'b>>,
189 ) {
190 f.extend(
191 bind_data
192 .metadata
193 .iter()
194 .map(|m| Box::new(m.clone()) as Box<dyn core::fmt::Debug>),
195 );
196 }
197}
198
199impl<DB> BindData for RawBytesBindCollector<DB>
200where
201 DB: Backend + TypeMetadata,
202 for<'a> DB: Backend<BindCollector<'a> = Self> + TypeMetadata + 'static,
203 <DB as TypeMetadata>::TypeMetadata: core::fmt::Debug + Clone + Send,
204{
205 fn len(&self) -> usize {
206 self.binds.len()
207 }
208}
209
210mod private {
213 use alloc::vec::Vec;
214
215 #[derive(Debug)]
217 pub struct ByteWrapper<'a>(pub(crate) &'a mut Vec<u8>);
218}