1use std::{
2 ffi::OsStr,
3 os::unix::prelude::OsStrExt,
4 path::{Path, PathBuf},
5 sync::Arc,
6};
7
8use anyhow::Result;
9
10use crate::statement::{SqlType, Statement};
11
12pub trait Bind {
13 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32>;
14}
15
16pub trait Column: Sized {
17 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)>;
18}
19
20impl Bind for bool {
21 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
22 statement.bind(self.then_some(1).unwrap_or(0), start_index)
23 }
24}
25
26impl Column for bool {
27 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
28 i32::column(statement, start_index).map(|(i, next_index)| (i != 0, next_index))
29 }
30}
31
32impl Bind for &[u8] {
33 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
34 statement.bind_blob(start_index, self)?;
35 Ok(start_index + 1)
36 }
37}
38
39impl Bind for Vec<u8> {
40 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
41 statement.bind_blob(start_index, self)?;
42 Ok(start_index + 1)
43 }
44}
45
46impl Column for Vec<u8> {
47 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
48 let result = statement.column_blob(start_index)?;
49 Ok((Vec::from(result), start_index + 1))
50 }
51}
52
53impl Bind for f64 {
54 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
55 statement.bind_double(start_index, *self)?;
56 Ok(start_index + 1)
57 }
58}
59
60impl Column for f64 {
61 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
62 let result = statement.column_double(start_index)?;
63 Ok((result, start_index + 1))
64 }
65}
66
67impl Bind for i32 {
68 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
69 statement.bind_int(start_index, *self)?;
70 Ok(start_index + 1)
71 }
72}
73
74impl Column for i32 {
75 fn column<'a>(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
76 let result = statement.column_int(start_index)?;
77 Ok((result, start_index + 1))
78 }
79}
80
81impl Bind for i64 {
82 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
83 statement.bind_int64(start_index, *self)?;
84 Ok(start_index + 1)
85 }
86}
87
88impl Column for i64 {
89 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
90 let result = statement.column_int64(start_index)?;
91 Ok((result, start_index + 1))
92 }
93}
94
95impl Bind for usize {
96 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
97 (*self as i64).bind(statement, start_index)
98 }
99}
100
101impl Column for usize {
102 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
103 let result = statement.column_int64(start_index)?;
104 Ok((result as usize, start_index + 1))
105 }
106}
107
108impl Bind for () {
109 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
110 statement.bind_null(start_index)?;
111 Ok(start_index + 1)
112 }
113}
114
115impl Bind for &str {
116 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
117 statement.bind_text(start_index, self)?;
118 Ok(start_index + 1)
119 }
120}
121
122impl Bind for Arc<str> {
123 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
124 statement.bind_text(start_index, self.as_ref())?;
125 Ok(start_index + 1)
126 }
127}
128
129impl Bind for String {
130 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
131 statement.bind_text(start_index, self)?;
132 Ok(start_index + 1)
133 }
134}
135
136impl Column for Arc<str> {
137 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
138 let result = statement.column_text(start_index)?;
139 Ok((Arc::from(result), start_index + 1))
140 }
141}
142
143impl Column for String {
144 fn column<'a>(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
145 let result = statement.column_text(start_index)?;
146 Ok((result.to_owned(), start_index + 1))
147 }
148}
149
150impl<T1: Bind, T2: Bind> Bind for (T1, T2) {
151 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
152 let next_index = self.0.bind(statement, start_index)?;
153 self.1.bind(statement, next_index)
154 }
155}
156
157impl<T1: Column, T2: Column> Column for (T1, T2) {
158 fn column<'a>(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
159 let (first, next_index) = T1::column(statement, start_index)?;
160 let (second, next_index) = T2::column(statement, next_index)?;
161 Ok(((first, second), next_index))
162 }
163}
164
165impl<T1: Bind, T2: Bind, T3: Bind> Bind for (T1, T2, T3) {
166 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
167 let next_index = self.0.bind(statement, start_index)?;
168 let next_index = self.1.bind(statement, next_index)?;
169 self.2.bind(statement, next_index)
170 }
171}
172
173impl<T1: Column, T2: Column, T3: Column> Column for (T1, T2, T3) {
174 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
175 let (first, next_index) = T1::column(statement, start_index)?;
176 let (second, next_index) = T2::column(statement, next_index)?;
177 let (third, next_index) = T3::column(statement, next_index)?;
178 Ok(((first, second, third), next_index))
179 }
180}
181
182impl<T1: Bind, T2: Bind, T3: Bind, T4: Bind> Bind for (T1, T2, T3, T4) {
183 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
184 let next_index = self.0.bind(statement, start_index)?;
185 let next_index = self.1.bind(statement, next_index)?;
186 let next_index = self.2.bind(statement, next_index)?;
187 self.3.bind(statement, next_index)
188 }
189}
190
191impl<T1: Column, T2: Column, T3: Column, T4: Column> Column for (T1, T2, T3, T4) {
192 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
193 let (first, next_index) = T1::column(statement, start_index)?;
194 let (second, next_index) = T2::column(statement, next_index)?;
195 let (third, next_index) = T3::column(statement, next_index)?;
196 let (fourth, next_index) = T4::column(statement, next_index)?;
197 Ok(((first, second, third, fourth), next_index))
198 }
199}
200
201impl<T1: Bind, T2: Bind, T3: Bind, T4: Bind, T5: Bind> Bind for (T1, T2, T3, T4, T5) {
202 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
203 let next_index = self.0.bind(statement, start_index)?;
204 let next_index = self.1.bind(statement, next_index)?;
205 let next_index = self.2.bind(statement, next_index)?;
206 let next_index = self.3.bind(statement, next_index)?;
207 self.4.bind(statement, next_index)
208 }
209}
210
211impl<T1: Column, T2: Column, T3: Column, T4: Column, T5: Column> Column for (T1, T2, T3, T4, T5) {
212 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
213 let (first, next_index) = T1::column(statement, start_index)?;
214 let (second, next_index) = T2::column(statement, next_index)?;
215 let (third, next_index) = T3::column(statement, next_index)?;
216 let (fourth, next_index) = T4::column(statement, next_index)?;
217 let (fifth, next_index) = T5::column(statement, next_index)?;
218 Ok(((first, second, third, fourth, fifth), next_index))
219 }
220}
221
222impl<T: Bind> Bind for Option<T> {
223 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
224 if let Some(this) = self {
225 this.bind(statement, start_index)
226 } else {
227 statement.bind_null(start_index)?;
228 Ok(start_index + 1)
229 }
230 }
231}
232
233impl<T: Column> Column for Option<T> {
234 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
235 if let SqlType::Null = statement.column_type(start_index)? {
236 Ok((None, start_index + 1))
237 } else {
238 T::column(statement, start_index).map(|(result, next_index)| (Some(result), next_index))
239 }
240 }
241}
242
243impl<T: Bind, const COUNT: usize> Bind for [T; COUNT] {
244 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
245 let mut current_index = start_index;
246 for binding in self {
247 current_index = binding.bind(statement, current_index)?
248 }
249
250 Ok(current_index)
251 }
252}
253
254impl<T: Column + Default + Copy, const COUNT: usize> Column for [T; COUNT] {
255 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
256 let mut array = [Default::default(); COUNT];
257 let mut current_index = start_index;
258 for i in 0..COUNT {
259 (array[i], current_index) = T::column(statement, current_index)?;
260 }
261 Ok((array, current_index))
262 }
263}
264
265impl<T: Bind> Bind for Vec<T> {
266 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
267 let mut current_index = start_index;
268 for binding in self.iter() {
269 current_index = binding.bind(statement, current_index)?
270 }
271
272 Ok(current_index)
273 }
274}
275
276impl<T: Bind> Bind for &[T] {
277 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
278 let mut current_index = start_index;
279 for binding in *self {
280 current_index = binding.bind(statement, current_index)?
281 }
282
283 Ok(current_index)
284 }
285}
286
287impl Bind for &Path {
288 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
289 self.as_os_str().as_bytes().bind(statement, start_index)
290 }
291}
292
293impl Column for PathBuf {
294 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
295 let blob = statement.column_blob(start_index)?;
296
297 Ok((
298 PathBuf::from(OsStr::from_bytes(blob).to_owned()),
299 start_index + 1,
300 ))
301 }
302}