1use anyhow::Result;
2
3use crate::statement::{SqlType, Statement};
4
5pub trait Bind {
6 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32>;
7}
8
9pub trait Column: Sized {
10 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)>;
11}
12
13impl Bind for bool {
14 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
15 statement.bind(self.then_some(1).unwrap_or(0), start_index)
16 }
17}
18
19impl Column for bool {
20 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
21 i32::column(statement, start_index).map(|(i, next_index)| (i != 0, next_index))
22 }
23}
24
25impl Bind for &[u8] {
26 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
27 statement.bind_blob(start_index, self)?;
28 Ok(start_index + 1)
29 }
30}
31
32impl Bind for Vec<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 Column for Vec<u8> {
40 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
41 let result = statement.column_blob(start_index)?;
42 Ok((Vec::from(result), start_index + 1))
43 }
44}
45
46impl Bind for f64 {
47 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
48 statement.bind_double(start_index, *self)?;
49 Ok(start_index + 1)
50 }
51}
52
53impl Column for f64 {
54 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
55 let result = statement.column_double(start_index)?;
56 Ok((result, start_index + 1))
57 }
58}
59
60impl Bind for i32 {
61 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
62 statement.bind_int(start_index, *self)?;
63 Ok(start_index + 1)
64 }
65}
66
67impl Column for i32 {
68 fn column<'a>(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
69 let result = statement.column_int(start_index)?;
70 Ok((result, start_index + 1))
71 }
72}
73
74impl Bind for i64 {
75 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
76 statement.bind_int64(start_index, *self)?;
77 Ok(start_index + 1)
78 }
79}
80
81impl Column for i64 {
82 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
83 let result = statement.column_int64(start_index)?;
84 Ok((result, start_index + 1))
85 }
86}
87
88impl Bind for usize {
89 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
90 (*self as i64).bind(statement, start_index)
91 }
92}
93
94impl Column for usize {
95 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
96 let result = statement.column_int64(start_index)?;
97 Ok((result as usize, start_index + 1))
98 }
99}
100
101impl Bind for () {
102 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
103 statement.bind_null(start_index)?;
104 Ok(start_index + 1)
105 }
106}
107
108impl Bind for &str {
109 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
110 statement.bind_text(start_index, self)?;
111 Ok(start_index + 1)
112 }
113}
114
115impl Bind for String {
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 Column for String {
123 fn column<'a>(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
124 let result = statement.column_text(start_index)?;
125 Ok((result.to_owned(), start_index + 1))
126 }
127}
128
129impl<T1: Bind, T2: Bind> Bind for (T1, T2) {
130 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
131 let next_index = self.0.bind(statement, start_index)?;
132 self.1.bind(statement, next_index)
133 }
134}
135
136impl<T1: Column, T2: Column> Column for (T1, T2) {
137 fn column<'a>(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
138 let (first, next_index) = T1::column(statement, start_index)?;
139 let (second, next_index) = T2::column(statement, next_index)?;
140 Ok(((first, second), next_index))
141 }
142}
143
144impl<T1: Bind, T2: Bind, T3: Bind> Bind for (T1, T2, T3) {
145 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
146 let next_index = self.0.bind(statement, start_index)?;
147 let next_index = self.1.bind(statement, next_index)?;
148 self.2.bind(statement, next_index)
149 }
150}
151
152impl<T1: Column, T2: Column, T3: Column> Column for (T1, T2, T3) {
153 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
154 let (first, next_index) = T1::column(statement, start_index)?;
155 let (second, next_index) = T2::column(statement, next_index)?;
156 let (third, next_index) = T3::column(statement, next_index)?;
157 Ok(((first, second, third), next_index))
158 }
159}
160
161impl<T1: Bind, T2: Bind, T3: Bind, T4: Bind> Bind for (T1, T2, T3, T4) {
162 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
163 let next_index = self.0.bind(statement, start_index)?;
164 let next_index = self.1.bind(statement, next_index)?;
165 let next_index = self.2.bind(statement, next_index)?;
166 self.3.bind(statement, next_index)
167 }
168}
169
170impl<T1: Column, T2: Column, T3: Column, T4: Column> Column for (T1, T2, T3, T4) {
171 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
172 let (first, next_index) = T1::column(statement, start_index)?;
173 let (second, next_index) = T2::column(statement, next_index)?;
174 let (third, next_index) = T3::column(statement, next_index)?;
175 let (forth, next_index) = T4::column(statement, next_index)?;
176 Ok(((first, second, third, forth), next_index))
177 }
178}
179
180impl<T: Bind> Bind for Option<T> {
181 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
182 if let Some(this) = self {
183 this.bind(statement, start_index)
184 } else {
185 statement.bind_null(start_index)?;
186 Ok(start_index + 1)
187 }
188 }
189}
190
191impl<T: Column> Column for Option<T> {
192 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
193 if let SqlType::Null = statement.column_type(start_index)? {
194 Ok((None, start_index + 1))
195 } else {
196 T::column(statement, start_index).map(|(result, next_index)| (Some(result), next_index))
197 }
198 }
199}
200
201impl<T: Bind, const COUNT: usize> Bind for [T; COUNT] {
202 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
203 let mut current_index = start_index;
204 for binding in self {
205 current_index = binding.bind(statement, current_index)?
206 }
207
208 Ok(current_index)
209 }
210}
211
212impl<T: Column + Default + Copy, const COUNT: usize> Column for [T; COUNT] {
213 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
214 let mut array = [Default::default(); COUNT];
215 let mut current_index = start_index;
216 for i in 0..COUNT {
217 (array[i], current_index) = T::column(statement, current_index)?;
218 }
219 Ok((array, current_index))
220 }
221}
222
223impl<T: Bind> Bind for Vec<T> {
224 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
225 let mut current_index = start_index;
226 for binding in self.iter() {
227 current_index = binding.bind(statement, current_index)?
228 }
229
230 Ok(current_index)
231 }
232}
233
234impl<T: Bind> Bind for &[T] {
235 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
236 let mut current_index = start_index;
237 for binding in *self {
238 current_index = binding.bind(statement, current_index)?
239 }
240
241 Ok(current_index)
242 }
243}