bindable.rs

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