1use std::{
2 path::{Path, PathBuf},
3 sync::Arc,
4};
5
6use anyhow::{Context, Result};
7use util::paths::PathExt;
8
9use crate::statement::{SqlType, Statement};
10
11pub trait StaticColumnCount {
12 fn column_count() -> usize {
13 1
14 }
15}
16
17pub trait Bind {
18 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32>;
19}
20
21pub trait Column: Sized {
22 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)>;
23}
24
25impl StaticColumnCount for bool {}
26impl Bind for bool {
27 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
28 statement
29 .bind(&self.then_some(1).unwrap_or(0), start_index)
30 .with_context(|| format!("Failed to bind bool at index {start_index}"))
31 }
32}
33
34impl Column for bool {
35 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
36 i32::column(statement, start_index)
37 .map(|(i, next_index)| (i != 0, next_index))
38 .with_context(|| format!("Failed to read bool at index {start_index}"))
39 }
40}
41
42impl StaticColumnCount for &[u8] {}
43impl Bind for &[u8] {
44 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
45 statement
46 .bind_blob(start_index, self)
47 .with_context(|| format!("Failed to bind &[u8] at index {start_index}"))?;
48 Ok(start_index + 1)
49 }
50}
51
52impl<const C: usize> StaticColumnCount for &[u8; C] {}
53impl<const C: usize> Bind for &[u8; C] {
54 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
55 statement
56 .bind_blob(start_index, self.as_slice())
57 .with_context(|| format!("Failed to bind &[u8; C] at index {start_index}"))?;
58 Ok(start_index + 1)
59 }
60}
61
62impl<const C: usize> Column for [u8; C] {
63 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
64 let bytes_slice = statement.column_blob(start_index)?;
65 let array = bytes_slice.try_into()?;
66 Ok((array, start_index + 1))
67 }
68}
69
70impl StaticColumnCount for Vec<u8> {}
71impl Bind for Vec<u8> {
72 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
73 statement
74 .bind_blob(start_index, self)
75 .with_context(|| format!("Failed to bind Vec<u8> at index {start_index}"))?;
76 Ok(start_index + 1)
77 }
78}
79
80impl Column for Vec<u8> {
81 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
82 let result = statement
83 .column_blob(start_index)
84 .with_context(|| format!("Failed to read Vec<u8> at index {start_index}"))?;
85
86 Ok((Vec::from(result), start_index + 1))
87 }
88}
89
90impl StaticColumnCount for f64 {}
91impl Bind for f64 {
92 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
93 statement
94 .bind_double(start_index, *self)
95 .with_context(|| format!("Failed to bind f64 at index {start_index}"))?;
96 Ok(start_index + 1)
97 }
98}
99
100impl Column for f64 {
101 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
102 let result = statement
103 .column_double(start_index)
104 .with_context(|| format!("Failed to parse f64 at index {start_index}"))?;
105
106 Ok((result, start_index + 1))
107 }
108}
109
110impl StaticColumnCount for f32 {}
111impl Bind for f32 {
112 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
113 statement
114 .bind_double(start_index, *self as f64)
115 .with_context(|| format!("Failed to bind f64 at index {start_index}"))?;
116 Ok(start_index + 1)
117 }
118}
119
120impl Column for f32 {
121 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
122 let result = statement
123 .column_double(start_index)
124 .with_context(|| format!("Failed to parse f32 at index {start_index}"))?
125 as f32;
126
127 Ok((result, start_index + 1))
128 }
129}
130
131impl StaticColumnCount for i32 {}
132impl Bind for i32 {
133 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
134 statement
135 .bind_int(start_index, *self)
136 .with_context(|| format!("Failed to bind i32 at index {start_index}"))?;
137
138 Ok(start_index + 1)
139 }
140}
141
142impl Column for i32 {
143 fn column<'a>(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
144 let result = statement.column_int(start_index)?;
145 Ok((result, start_index + 1))
146 }
147}
148
149impl StaticColumnCount for i64 {}
150impl Bind for i64 {
151 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
152 statement
153 .bind_int64(start_index, *self)
154 .with_context(|| format!("Failed to bind i64 at index {start_index}"))?;
155 Ok(start_index + 1)
156 }
157}
158
159impl Column for i64 {
160 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
161 let result = statement.column_int64(start_index)?;
162 Ok((result, start_index + 1))
163 }
164}
165
166impl StaticColumnCount for u64 {}
167impl Bind for u64 {
168 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
169 statement
170 .bind_int64(start_index, (*self) as i64)
171 .with_context(|| format!("Failed to bind i64 at index {start_index}"))?;
172 Ok(start_index + 1)
173 }
174}
175
176impl Column for u64 {
177 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
178 let result = statement.column_int64(start_index)? as u64;
179 Ok((result, start_index + 1))
180 }
181}
182
183impl StaticColumnCount for u32 {}
184impl Bind for u32 {
185 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
186 (*self as i64)
187 .bind(statement, start_index)
188 .with_context(|| format!("Failed to bind usize at index {start_index}"))
189 }
190}
191
192impl Column for u32 {
193 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
194 let result = statement.column_int64(start_index)?;
195 Ok((result as u32, start_index + 1))
196 }
197}
198
199impl StaticColumnCount for usize {}
200impl Bind for usize {
201 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
202 (*self as i64)
203 .bind(statement, start_index)
204 .with_context(|| format!("Failed to bind usize at index {start_index}"))
205 }
206}
207
208impl Column for usize {
209 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
210 let result = statement.column_int64(start_index)?;
211 Ok((result as usize, start_index + 1))
212 }
213}
214
215impl StaticColumnCount for &str {}
216impl Bind for &str {
217 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
218 statement.bind_text(start_index, self)?;
219 Ok(start_index + 1)
220 }
221}
222
223impl StaticColumnCount for Arc<str> {}
224impl Bind for Arc<str> {
225 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
226 statement.bind_text(start_index, self.as_ref())?;
227 Ok(start_index + 1)
228 }
229}
230
231impl StaticColumnCount for String {}
232impl Bind for String {
233 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
234 statement.bind_text(start_index, self)?;
235 Ok(start_index + 1)
236 }
237}
238
239impl Column for Arc<str> {
240 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
241 let result = statement.column_text(start_index)?;
242 Ok((Arc::from(result), start_index + 1))
243 }
244}
245
246impl Column for String {
247 fn column<'a>(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
248 let result = statement.column_text(start_index)?;
249 Ok((result.to_owned(), start_index + 1))
250 }
251}
252
253impl<T: StaticColumnCount> StaticColumnCount for Option<T> {
254 fn column_count() -> usize {
255 T::column_count()
256 }
257}
258impl<T: Bind + StaticColumnCount> Bind for Option<T> {
259 fn bind(&self, statement: &Statement, mut start_index: i32) -> Result<i32> {
260 if let Some(this) = self {
261 this.bind(statement, start_index)
262 } else {
263 for _ in 0..T::column_count() {
264 statement.bind_null(start_index)?;
265 start_index += 1;
266 }
267 Ok(start_index)
268 }
269 }
270}
271
272impl<T: Column + StaticColumnCount> Column for Option<T> {
273 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
274 if let SqlType::Null = statement.column_type(start_index)? {
275 Ok((None, start_index + T::column_count() as i32))
276 } else {
277 T::column(statement, start_index).map(|(result, next_index)| (Some(result), next_index))
278 }
279 }
280}
281
282impl<T: StaticColumnCount, const COUNT: usize> StaticColumnCount for [T; COUNT] {
283 fn column_count() -> usize {
284 T::column_count() * COUNT
285 }
286}
287impl<T: Bind, const COUNT: usize> Bind for [T; COUNT] {
288 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
289 let mut current_index = start_index;
290 for binding in self {
291 current_index = binding.bind(statement, current_index)?
292 }
293
294 Ok(current_index)
295 }
296}
297
298impl StaticColumnCount for &Path {}
299impl Bind for &Path {
300 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
301 self.as_os_str()
302 .as_encoded_bytes()
303 .bind(statement, start_index)
304 }
305}
306
307impl StaticColumnCount for Arc<Path> {}
308impl Bind for Arc<Path> {
309 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
310 self.as_ref().bind(statement, start_index)
311 }
312}
313
314impl StaticColumnCount for PathBuf {}
315impl Bind for PathBuf {
316 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
317 (self.as_ref() as &Path).bind(statement, start_index)
318 }
319}
320
321impl Column for PathBuf {
322 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
323 let blob = statement.column_blob(start_index)?;
324
325 PathBuf::try_from_bytes(blob).map(|path| (path, start_index + 1))
326 }
327}
328
329impl StaticColumnCount for uuid::Uuid {
330 fn column_count() -> usize {
331 1
332 }
333}
334
335impl Bind for uuid::Uuid {
336 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
337 self.as_bytes().bind(statement, start_index)
338 }
339}
340
341impl Column for uuid::Uuid {
342 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
343 let (bytes, next_index) = Column::column(statement, start_index)?;
344 Ok((uuid::Uuid::from_bytes(bytes), next_index))
345 }
346}
347
348impl StaticColumnCount for () {
349 fn column_count() -> usize {
350 0
351 }
352}
353/// Unit impls do nothing. This simplifies query macros
354impl Bind for () {
355 fn bind(&self, _statement: &Statement, start_index: i32) -> Result<i32> {
356 Ok(start_index)
357 }
358}
359
360impl Column for () {
361 fn column(_statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
362 Ok(((), start_index))
363 }
364}
365
366macro_rules! impl_tuple_row_traits {
367 ( $($local:ident: $type:ident),+ ) => {
368 impl<$($type: StaticColumnCount),+> StaticColumnCount for ($($type,)+) {
369 fn column_count() -> usize {
370 let mut count = 0;
371 $(count += $type::column_count();)+
372 count
373 }
374 }
375
376 impl<$($type: Bind),+> Bind for ($($type,)+) {
377 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
378 let mut next_index = start_index;
379 let ($($local,)+) = self;
380 $(next_index = $local.bind(statement, next_index)?;)+
381 Ok(next_index)
382 }
383 }
384
385 impl<$($type: Column),+> Column for ($($type,)+) {
386 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
387 let mut next_index = start_index;
388 Ok((
389 (
390 $({
391 let value;
392 (value, next_index) = $type::column(statement, next_index)?;
393 value
394 },)+
395 ),
396 next_index,
397 ))
398 }
399 }
400 }
401}
402
403impl_tuple_row_traits!(t1: T1, t2: T2);
404impl_tuple_row_traits!(t1: T1, t2: T2, t3: T3);
405impl_tuple_row_traits!(t1: T1, t2: T2, t3: T3, t4: T4);
406impl_tuple_row_traits!(t1: T1, t2: T2, t3: T3, t4: T4, t5: T5);
407impl_tuple_row_traits!(t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6);
408impl_tuple_row_traits!(t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6, t7: T7);
409impl_tuple_row_traits!(
410 t1: T1,
411 t2: T2,
412 t3: T3,
413 t4: T4,
414 t5: T5,
415 t6: T6,
416 t7: T7,
417 t8: T8
418);
419impl_tuple_row_traits!(
420 t1: T1,
421 t2: T2,
422 t3: T3,
423 t4: T4,
424 t5: T5,
425 t6: T6,
426 t7: T7,
427 t8: T8,
428 t9: T9
429);
430impl_tuple_row_traits!(
431 t1: T1,
432 t2: T2,
433 t3: T3,
434 t4: T4,
435 t5: T5,
436 t6: T6,
437 t7: T7,
438 t8: T8,
439 t9: T9,
440 t10: T10
441);