wip
This commit is contained in:
parent
81ed961659
commit
15799f7af6
17 changed files with 481 additions and 148 deletions
|
@ -9,14 +9,37 @@ use anyhow::{Context, Result};
|
|||
|
||||
use crate::statement::{SqlType, Statement};
|
||||
|
||||
pub trait Bind {
|
||||
pub trait StaticRowComponent {
|
||||
fn static_column_count() -> usize {
|
||||
1
|
||||
}
|
||||
}
|
||||
|
||||
pub trait RowComponent {
|
||||
fn column_count(&self) -> usize;
|
||||
}
|
||||
|
||||
impl<T: StaticRowComponent> RowComponent for T {
|
||||
fn column_count(&self) -> usize {
|
||||
T::static_column_count()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: StaticRowComponent> StaticRowComponent for &T {
|
||||
fn static_column_count() -> usize {
|
||||
T::static_column_count()
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Bind: RowComponent {
|
||||
fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32>;
|
||||
}
|
||||
|
||||
pub trait Column: Sized {
|
||||
pub trait Column: Sized + RowComponent {
|
||||
fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)>;
|
||||
}
|
||||
|
||||
impl StaticRowComponent for bool {}
|
||||
impl Bind for bool {
|
||||
fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
|
||||
statement
|
||||
|
@ -33,6 +56,7 @@ impl Column for bool {
|
|||
}
|
||||
}
|
||||
|
||||
impl StaticRowComponent for &[u8] {}
|
||||
impl Bind for &[u8] {
|
||||
fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
|
||||
statement
|
||||
|
@ -42,6 +66,7 @@ impl Bind for &[u8] {
|
|||
}
|
||||
}
|
||||
|
||||
impl<const C: usize> StaticRowComponent for &[u8; C] {}
|
||||
impl<const C: usize> Bind for &[u8; C] {
|
||||
fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
|
||||
statement
|
||||
|
@ -51,6 +76,7 @@ impl<const C: usize> Bind for &[u8; C] {
|
|||
}
|
||||
}
|
||||
|
||||
impl StaticRowComponent for Vec<u8> {}
|
||||
impl Bind for Vec<u8> {
|
||||
fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
|
||||
statement
|
||||
|
@ -70,6 +96,7 @@ impl Column for Vec<u8> {
|
|||
}
|
||||
}
|
||||
|
||||
impl StaticRowComponent for f64 {}
|
||||
impl Bind for f64 {
|
||||
fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
|
||||
statement
|
||||
|
@ -89,6 +116,7 @@ impl Column for f64 {
|
|||
}
|
||||
}
|
||||
|
||||
impl StaticRowComponent for f32 {}
|
||||
impl Bind for f32 {
|
||||
fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
|
||||
statement
|
||||
|
@ -109,6 +137,7 @@ impl Column for f32 {
|
|||
}
|
||||
}
|
||||
|
||||
impl StaticRowComponent for i32 {}
|
||||
impl Bind for i32 {
|
||||
fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
|
||||
statement
|
||||
|
@ -126,6 +155,7 @@ impl Column for i32 {
|
|||
}
|
||||
}
|
||||
|
||||
impl StaticRowComponent for i64 {}
|
||||
impl Bind for i64 {
|
||||
fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
|
||||
statement
|
||||
|
@ -142,6 +172,7 @@ impl Column for i64 {
|
|||
}
|
||||
}
|
||||
|
||||
impl StaticRowComponent for u32 {}
|
||||
impl Bind for u32 {
|
||||
fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
|
||||
(*self as i64)
|
||||
|
@ -157,6 +188,7 @@ impl Column for u32 {
|
|||
}
|
||||
}
|
||||
|
||||
impl StaticRowComponent for usize {}
|
||||
impl Bind for usize {
|
||||
fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
|
||||
(*self as i64)
|
||||
|
@ -172,6 +204,7 @@ impl Column for usize {
|
|||
}
|
||||
}
|
||||
|
||||
impl StaticRowComponent for &str {}
|
||||
impl Bind for &str {
|
||||
fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
|
||||
statement.bind_text(start_index, self)?;
|
||||
|
@ -179,6 +212,7 @@ impl Bind for &str {
|
|||
}
|
||||
}
|
||||
|
||||
impl StaticRowComponent for Arc<str> {}
|
||||
impl Bind for Arc<str> {
|
||||
fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
|
||||
statement.bind_text(start_index, self.as_ref())?;
|
||||
|
@ -186,6 +220,7 @@ impl Bind for Arc<str> {
|
|||
}
|
||||
}
|
||||
|
||||
impl StaticRowComponent for String {}
|
||||
impl Bind for String {
|
||||
fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
|
||||
statement.bind_text(start_index, self)?;
|
||||
|
@ -207,28 +242,41 @@ impl Column for String {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: Bind> Bind for Option<T> {
|
||||
fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
|
||||
impl<T: StaticRowComponent> StaticRowComponent for Option<T> {
|
||||
fn static_column_count() -> usize {
|
||||
T::static_column_count()
|
||||
}
|
||||
}
|
||||
impl<T: Bind + StaticRowComponent> Bind for Option<T> {
|
||||
fn bind(&self, statement: &Statement, mut start_index: i32) -> Result<i32> {
|
||||
if let Some(this) = self {
|
||||
this.bind(statement, start_index)
|
||||
} else {
|
||||
statement.bind_null(start_index)?;
|
||||
Ok(start_index + 1)
|
||||
for _ in 0..T::static_column_count() {
|
||||
statement.bind_null(start_index)?;
|
||||
start_index += 1;
|
||||
}
|
||||
Ok(start_index)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Column> Column for Option<T> {
|
||||
impl<T: Column + StaticRowComponent> Column for Option<T> {
|
||||
fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
|
||||
if let SqlType::Null = statement.column_type(start_index)? {
|
||||
Ok((None, start_index + 1))
|
||||
Ok((None, start_index + T::static_column_count() as i32))
|
||||
} else {
|
||||
T::column(statement, start_index).map(|(result, next_index)| (Some(result), next_index))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Bind, const COUNT: usize> Bind for [T; COUNT] {
|
||||
impl<T: StaticRowComponent, const COUNT: usize> StaticRowComponent for [T; COUNT] {
|
||||
fn static_column_count() -> usize {
|
||||
T::static_column_count() * COUNT
|
||||
}
|
||||
}
|
||||
impl<T: Bind + StaticRowComponent, const COUNT: usize> Bind for [T; COUNT] {
|
||||
fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
|
||||
let mut current_index = start_index;
|
||||
for binding in self {
|
||||
|
@ -239,7 +287,7 @@ impl<T: Bind, const COUNT: usize> Bind for [T; COUNT] {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: Column + Default + Copy, const COUNT: usize> Column for [T; COUNT] {
|
||||
impl<T: Column + StaticRowComponent + Default + Copy, const COUNT: usize> Column for [T; COUNT] {
|
||||
fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
|
||||
let mut array = [Default::default(); COUNT];
|
||||
let mut current_index = start_index;
|
||||
|
@ -250,40 +298,21 @@ impl<T: Column + Default + Copy, const COUNT: usize> Column for [T; COUNT] {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: Bind> Bind for Vec<T> {
|
||||
fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
|
||||
let mut current_index = start_index;
|
||||
for binding in self.iter() {
|
||||
current_index = binding.bind(statement, current_index)?
|
||||
}
|
||||
|
||||
Ok(current_index)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Bind> Bind for &[T] {
|
||||
fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
|
||||
let mut current_index = start_index;
|
||||
for binding in *self {
|
||||
current_index = binding.bind(statement, current_index)?
|
||||
}
|
||||
|
||||
Ok(current_index)
|
||||
}
|
||||
}
|
||||
|
||||
impl StaticRowComponent for &Path {}
|
||||
impl Bind for &Path {
|
||||
fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
|
||||
self.as_os_str().as_bytes().bind(statement, start_index)
|
||||
}
|
||||
}
|
||||
|
||||
impl StaticRowComponent for Arc<Path> {}
|
||||
impl Bind for Arc<Path> {
|
||||
fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
|
||||
self.as_ref().bind(statement, start_index)
|
||||
}
|
||||
}
|
||||
|
||||
impl StaticRowComponent for PathBuf {}
|
||||
impl Bind for PathBuf {
|
||||
fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
|
||||
(self.as_ref() as &Path).bind(statement, start_index)
|
||||
|
@ -301,6 +330,11 @@ impl Column for PathBuf {
|
|||
}
|
||||
}
|
||||
|
||||
impl StaticRowComponent for () {
|
||||
fn static_column_count() -> usize {
|
||||
0
|
||||
}
|
||||
}
|
||||
/// Unit impls do nothing. This simplifies query macros
|
||||
impl Bind for () {
|
||||
fn bind(&self, _statement: &Statement, start_index: i32) -> Result<i32> {
|
||||
|
@ -314,74 +348,80 @@ impl Column for () {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T1: Bind, T2: Bind> Bind for (T1, T2) {
|
||||
fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
|
||||
let next_index = self.0.bind(statement, start_index)?;
|
||||
self.1.bind(statement, next_index)
|
||||
macro_rules! impl_tuple_row_traits {
|
||||
( $($local:ident: $type:ident),+ ) => {
|
||||
impl<$($type: RowComponent),+> RowComponent for ($($type,)+) {
|
||||
fn column_count(&self) -> usize {
|
||||
let ($($local,)+) = self;
|
||||
let mut count = 0;
|
||||
$(count += $local.column_count();)+
|
||||
count
|
||||
}
|
||||
}
|
||||
|
||||
impl<$($type: Bind),+> Bind for ($($type,)+) {
|
||||
fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
|
||||
let mut next_index = start_index;
|
||||
let ($($local,)+) = self;
|
||||
$(next_index = $local.bind(statement, next_index)?;)+
|
||||
Ok(next_index)
|
||||
}
|
||||
}
|
||||
|
||||
impl<$($type: Column),+> Column for ($($type,)+) {
|
||||
fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
|
||||
let mut next_index = start_index;
|
||||
Ok((
|
||||
(
|
||||
$({
|
||||
let value;
|
||||
(value, next_index) = $type::column(statement, next_index)?;
|
||||
value
|
||||
},)+
|
||||
),
|
||||
next_index,
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T1: Column, T2: Column> Column for (T1, T2) {
|
||||
fn column<'a>(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
|
||||
let (first, next_index) = T1::column(statement, start_index)?;
|
||||
let (second, next_index) = T2::column(statement, next_index)?;
|
||||
Ok(((first, second), next_index))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T1: Bind, T2: Bind, T3: Bind> Bind for (T1, T2, T3) {
|
||||
fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
|
||||
let next_index = self.0.bind(statement, start_index)?;
|
||||
let next_index = self.1.bind(statement, next_index)?;
|
||||
self.2.bind(statement, next_index)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T1: Column, T2: Column, T3: Column> Column for (T1, T2, T3) {
|
||||
fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
|
||||
let (first, next_index) = T1::column(statement, start_index)?;
|
||||
let (second, next_index) = T2::column(statement, next_index)?;
|
||||
let (third, next_index) = T3::column(statement, next_index)?;
|
||||
Ok(((first, second, third), next_index))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T1: Bind, T2: Bind, T3: Bind, T4: Bind> Bind for (T1, T2, T3, T4) {
|
||||
fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
|
||||
let next_index = self.0.bind(statement, start_index)?;
|
||||
let next_index = self.1.bind(statement, next_index)?;
|
||||
let next_index = self.2.bind(statement, next_index)?;
|
||||
self.3.bind(statement, next_index)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T1: Column, T2: Column, T3: Column, T4: Column> Column for (T1, T2, T3, T4) {
|
||||
fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
|
||||
let (first, next_index) = T1::column(statement, start_index)?;
|
||||
let (second, next_index) = T2::column(statement, next_index)?;
|
||||
let (third, next_index) = T3::column(statement, next_index)?;
|
||||
let (fourth, next_index) = T4::column(statement, next_index)?;
|
||||
Ok(((first, second, third, fourth), next_index))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T1: Bind, T2: Bind, T3: Bind, T4: Bind, T5: Bind> Bind for (T1, T2, T3, T4, T5) {
|
||||
fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
|
||||
let next_index = self.0.bind(statement, start_index)?;
|
||||
let next_index = self.1.bind(statement, next_index)?;
|
||||
let next_index = self.2.bind(statement, next_index)?;
|
||||
let next_index = self.3.bind(statement, next_index)?;
|
||||
self.4.bind(statement, next_index)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T1: Column, T2: Column, T3: Column, T4: Column, T5: Column> Column for (T1, T2, T3, T4, T5) {
|
||||
fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
|
||||
let (first, next_index) = T1::column(statement, start_index)?;
|
||||
let (second, next_index) = T2::column(statement, next_index)?;
|
||||
let (third, next_index) = T3::column(statement, next_index)?;
|
||||
let (fourth, next_index) = T4::column(statement, next_index)?;
|
||||
let (fifth, next_index) = T5::column(statement, next_index)?;
|
||||
Ok(((first, second, third, fourth, fifth), next_index))
|
||||
}
|
||||
}
|
||||
impl_tuple_row_traits!(t1: T1, t2: T2);
|
||||
impl_tuple_row_traits!(t1: T1, t2: T2, t3: T3);
|
||||
impl_tuple_row_traits!(t1: T1, t2: T2, t3: T3, t4: T4);
|
||||
impl_tuple_row_traits!(t1: T1, t2: T2, t3: T3, t4: T4, t5: T5);
|
||||
impl_tuple_row_traits!(t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6);
|
||||
impl_tuple_row_traits!(t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6, t7: T7);
|
||||
impl_tuple_row_traits!(
|
||||
t1: T1,
|
||||
t2: T2,
|
||||
t3: T3,
|
||||
t4: T4,
|
||||
t5: T5,
|
||||
t6: T6,
|
||||
t7: T7,
|
||||
t8: T8
|
||||
);
|
||||
impl_tuple_row_traits!(
|
||||
t1: T1,
|
||||
t2: T2,
|
||||
t3: T3,
|
||||
t4: T4,
|
||||
t5: T5,
|
||||
t6: T6,
|
||||
t7: T7,
|
||||
t8: T8,
|
||||
t9: T9
|
||||
);
|
||||
impl_tuple_row_traits!(
|
||||
t1: T1,
|
||||
t2: T2,
|
||||
t3: T3,
|
||||
t4: T4,
|
||||
t5: T5,
|
||||
t6: T6,
|
||||
t7: T7,
|
||||
t8: T8,
|
||||
t9: T9,
|
||||
t10: T10
|
||||
);
|
||||
|
|
|
@ -238,11 +238,14 @@ impl<'a> Statement<'a> {
|
|||
|
||||
pub fn bind<T: Bind>(&self, value: T, index: i32) -> Result<i32> {
|
||||
debug_assert!(index > 0);
|
||||
value.bind(self, index)
|
||||
let after = value.bind(self, index)?;
|
||||
debug_assert_eq!((after - index) as usize, value.column_count());
|
||||
Ok(after)
|
||||
}
|
||||
|
||||
pub fn column<T: Column>(&mut self) -> Result<T> {
|
||||
let (result, _) = T::column(self, 0)?;
|
||||
let (result, after) = T::column(self, 0)?;
|
||||
debug_assert_eq!(T::column_count(&result), after as usize);
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
|
@ -260,7 +263,9 @@ impl<'a> Statement<'a> {
|
|||
}
|
||||
|
||||
pub fn with_bindings(&mut self, bindings: impl Bind) -> Result<&mut Self> {
|
||||
self.bind(bindings, 1)?;
|
||||
let column_count = bindings.column_count();
|
||||
let after = self.bind(bindings, 1)?;
|
||||
debug_assert_eq!((after - 1) as usize, column_count);
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue