1use std::{
2 path::{Path, PathBuf},
3 sync::Arc,
4};
5
6use anyhow::{Context as _, 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 u16 {}
200impl Bind for u16 {
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 u16 {
209 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
210 let result = statement.column_int64(start_index)?;
211 Ok((result as u16, start_index + 1))
212 }
213}
214
215impl StaticColumnCount for usize {}
216impl Bind for usize {
217 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
218 (*self as i64)
219 .bind(statement, start_index)
220 .with_context(|| format!("Failed to bind usize at index {start_index}"))
221 }
222}
223
224impl Column for usize {
225 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
226 let result = statement.column_int64(start_index)?;
227 Ok((result as usize, start_index + 1))
228 }
229}
230
231impl StaticColumnCount for &str {}
232impl Bind for &str {
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 StaticColumnCount for Arc<str> {}
240impl Bind for Arc<str> {
241 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
242 statement.bind_text(start_index, self.as_ref())?;
243 Ok(start_index + 1)
244 }
245}
246
247impl StaticColumnCount for String {}
248impl Bind for String {
249 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
250 statement.bind_text(start_index, self)?;
251 Ok(start_index + 1)
252 }
253}
254
255impl Column for Arc<str> {
256 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
257 let result = statement.column_text(start_index)?;
258 Ok((Arc::from(result), start_index + 1))
259 }
260}
261
262impl Column for String {
263 fn column<'a>(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
264 let result = statement.column_text(start_index)?;
265 Ok((result.to_owned(), start_index + 1))
266 }
267}
268
269impl<T: StaticColumnCount> StaticColumnCount for Option<T> {
270 fn column_count() -> usize {
271 T::column_count()
272 }
273}
274impl<T: Bind + StaticColumnCount> Bind for Option<T> {
275 fn bind(&self, statement: &Statement, mut start_index: i32) -> Result<i32> {
276 if let Some(this) = self {
277 this.bind(statement, start_index)
278 } else {
279 for _ in 0..T::column_count() {
280 statement.bind_null(start_index)?;
281 start_index += 1;
282 }
283 Ok(start_index)
284 }
285 }
286}
287
288impl<T: Column + StaticColumnCount> Column for Option<T> {
289 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
290 if let SqlType::Null = statement.column_type(start_index)? {
291 Ok((None, start_index + T::column_count() as i32))
292 } else {
293 T::column(statement, start_index).map(|(result, next_index)| (Some(result), next_index))
294 }
295 }
296}
297
298impl<T: StaticColumnCount, const COUNT: usize> StaticColumnCount for [T; COUNT] {
299 fn column_count() -> usize {
300 T::column_count() * COUNT
301 }
302}
303impl<T: Bind, const COUNT: usize> Bind for [T; COUNT] {
304 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
305 let mut current_index = start_index;
306 for binding in self {
307 current_index = binding.bind(statement, current_index)?
308 }
309
310 Ok(current_index)
311 }
312}
313
314impl StaticColumnCount for &Path {}
315impl Bind for &Path {
316 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
317 self.as_os_str()
318 .as_encoded_bytes()
319 .bind(statement, start_index)
320 }
321}
322
323impl StaticColumnCount for Arc<Path> {}
324impl Bind for Arc<Path> {
325 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
326 self.as_ref().bind(statement, start_index)
327 }
328}
329
330impl StaticColumnCount for PathBuf {}
331impl Bind for PathBuf {
332 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
333 (self.as_ref() as &Path).bind(statement, start_index)
334 }
335}
336
337impl Column for PathBuf {
338 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
339 let blob = statement.column_blob(start_index)?;
340
341 PathBuf::try_from_bytes(blob).map(|path| (path, start_index + 1))
342 }
343}
344
345impl StaticColumnCount for uuid::Uuid {
346 fn column_count() -> usize {
347 1
348 }
349}
350
351impl Bind for uuid::Uuid {
352 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
353 self.as_bytes().bind(statement, start_index)
354 }
355}
356
357impl Column for uuid::Uuid {
358 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
359 let (bytes, next_index) = Column::column(statement, start_index)?;
360 Ok((uuid::Uuid::from_bytes(bytes), next_index))
361 }
362}
363
364impl StaticColumnCount for () {
365 fn column_count() -> usize {
366 0
367 }
368}
369/// Unit impls do nothing. This simplifies query macros
370impl Bind for () {
371 fn bind(&self, _statement: &Statement, start_index: i32) -> Result<i32> {
372 Ok(start_index)
373 }
374}
375
376impl Column for () {
377 fn column(_statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
378 Ok(((), start_index))
379 }
380}
381
382macro_rules! impl_tuple_row_traits {
383 ( $($local:ident: $type:ident),+ ) => {
384 impl<$($type: StaticColumnCount),+> StaticColumnCount for ($($type,)+) {
385 fn column_count() -> usize {
386 let mut count = 0;
387 $(count += $type::column_count();)+
388 count
389 }
390 }
391
392 impl<$($type: Bind),+> Bind for ($($type,)+) {
393 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
394 let mut next_index = start_index;
395 let ($($local,)+) = self;
396 $(next_index = $local.bind(statement, next_index)?;)+
397 Ok(next_index)
398 }
399 }
400
401 impl<$($type: Column),+> Column for ($($type,)+) {
402 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
403 let mut next_index = start_index;
404 Ok((
405 (
406 $({
407 let value;
408 (value, next_index) = $type::column(statement, next_index)?;
409 value
410 },)+
411 ),
412 next_index,
413 ))
414 }
415 }
416 }
417}
418
419impl_tuple_row_traits!(t1: T1, t2: T2);
420impl_tuple_row_traits!(t1: T1, t2: T2, t3: T3);
421impl_tuple_row_traits!(t1: T1, t2: T2, t3: T3, t4: T4);
422impl_tuple_row_traits!(t1: T1, t2: T2, t3: T3, t4: T4, t5: T5);
423impl_tuple_row_traits!(t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6);
424impl_tuple_row_traits!(t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6, t7: T7);
425impl_tuple_row_traits!(
426 t1: T1,
427 t2: T2,
428 t3: T3,
429 t4: T4,
430 t5: T5,
431 t6: T6,
432 t7: T7,
433 t8: T8
434);
435impl_tuple_row_traits!(
436 t1: T1,
437 t2: T2,
438 t3: T3,
439 t4: T4,
440 t5: T5,
441 t6: T6,
442 t7: T7,
443 t8: T8,
444 t9: T9
445);
446impl_tuple_row_traits!(
447 t1: T1,
448 t2: T2,
449 t3: T3,
450 t4: T4,
451 t5: T5,
452 t6: T6,
453 t7: T7,
454 t8: T8,
455 t9: T9,
456 t10: T10
457);