quackdb_internal/conversion/
bind.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use std::ffi::CStr;

use super::IntoDuckDb;
use crate::ffi;

use chrono::prelude::*;
use paste::paste;

/// Values that can bind to prepared statements
pub unsafe trait BindParam {
    /// # Safety
    /// Does not need to check whether the type is correct or whether index is in bounds.
    unsafe fn bind_param_unchecked(
        self,
        stmt: ffi::duckdb_prepared_statement,
        param_idx: u64,
    ) -> Result<(), &'static str>;
}

/// `Option<T>` corresponds to nullable columns
unsafe impl<T> BindParam for Option<T>
where
    T: BindParam,
{
    unsafe fn bind_param_unchecked(
        self,
        stmt: ffi::duckdb_prepared_statement,
        param_idx: u64,
    ) -> Result<(), &'static str> {
        match self {
            Some(t) => t.bind_param_unchecked(stmt, param_idx),
            None => match ffi::duckdb_bind_null(stmt, param_idx) {
                ffi::DuckDBSuccess => Ok(()),
                ffi::DuckDBError => Err("duckdb_bind_null()"),
                _ => unreachable!(),
            },
        }
    }
}

macro_rules! impl_bind_param_for_primitive {
    ($ty:ty, $duck_ty:ty) => {
        paste! {
            impl_bind_param_for_primitive! {$ty, $duck_ty, [<duckdb_bind_ $duck_ty>]}
        }
    };
    ($ty:ty, $duck_ty:ty, $method:ident) => {
        paste! {
            impl_bind_param_for_primitive! {$ty, $duck_ty, $method, stringify!([<duckdb_ $method>]())}
        }
    };
    ($ty:ty, $duck_ty:ty, $method:ident, $err_msg:expr) => {
        unsafe impl BindParam for $ty {
            unsafe fn bind_param_unchecked(
                self,
                stmt: ffi::duckdb_prepared_statement,
                param_idx: u64,
            ) -> Result<(), &'static str> {
                match ffi::$method(stmt, param_idx, self) {
                    ffi::DuckDBSuccess => Ok(()),
                    ffi::DuckDBError => Err($err_msg),
                    _ => unreachable!()
                }
            }
        }
    };
}

macro_rules! impl_bind_param {
    ($ty:ty, $duck_ty:ty) => {
        paste! {
            impl_bind_param! {$ty, $duck_ty, [<duckdb_bind_ $duck_ty>]}
        }
    };
    ($ty:ty, $duck_ty:ty, $method:ident) => {
        paste! {
            impl_bind_param! {$ty, $duck_ty, $method, stringify!([<duckdb_ $method>]())}
        }
    };
    ($ty:ty, $duck_ty:ty, $method:ident, $err_msg:expr) => {
        unsafe impl BindParam for $ty {
            unsafe fn bind_param_unchecked(
                self,
                stmt: ffi::duckdb_prepared_statement,
                param_idx: u64,
            ) -> Result<(), &'static str> {
                match ffi::$method(stmt, param_idx, self.into_duckdb()) {
                    ffi::DuckDBSuccess => Ok(()),
                    ffi::DuckDBError => Err($err_msg),
                    _ => unreachable!(),
                }
            }
        }
    };
}

impl_bind_param_for_primitive! {bool, boolean}
impl_bind_param_for_primitive! {i8, int8}
impl_bind_param_for_primitive! {i16, int16}
impl_bind_param_for_primitive! {i32, int32}
impl_bind_param_for_primitive! {i64, int64}
impl_bind_param! {i128, hugeint}
impl_bind_param_for_primitive! {u8, uint8}
impl_bind_param_for_primitive! {u16, uint16}
impl_bind_param_for_primitive! {u32, uint32}
impl_bind_param_for_primitive! {u64, uint64}
impl_bind_param_for_primitive! {f32, float}
impl_bind_param_for_primitive! {f64, double}
impl_bind_param! {NaiveDate, date}
impl_bind_param! {NaiveTime, time}
impl_bind_param! {NaiveDateTime, timestamp}

unsafe impl BindParam for &CStr {
    unsafe fn bind_param_unchecked(
        self,
        stmt: ffi::duckdb_prepared_statement,
        param_idx: u64,
    ) -> Result<(), &'static str> {
        match ffi::duckdb_bind_varchar(stmt, param_idx, self.as_ptr()) {
            ffi::DuckDBSuccess => Ok(()),
            ffi::DuckDBError => Err("duckdb_bind_varchar()"),
            _ => unreachable!(),
        }
    }
}

unsafe impl BindParam for &str {
    unsafe fn bind_param_unchecked(
        self,
        stmt: ffi::duckdb_prepared_statement,
        param_idx: u64,
    ) -> Result<(), &'static str> {
        match ffi::duckdb_bind_varchar_length(
            stmt,
            param_idx,
            self.as_ptr().cast(),
            self.len() as u64,
        ) {
            ffi::DuckDBSuccess => Ok(()),
            ffi::DuckDBError => Err("duckdb_bind_varchar_length()"),
            _ => unreachable!(),
        }
    }
}

unsafe impl BindParam for &[u8] {
    unsafe fn bind_param_unchecked(
        self,
        stmt: ffi::duckdb_prepared_statement,
        param_idx: u64,
    ) -> Result<(), &'static str> {
        match ffi::duckdb_bind_blob(stmt, param_idx, self.as_ptr().cast(), self.len() as u64) {
            ffi::DuckDBSuccess => Ok(()),
            ffi::DuckDBError => Err("duckdb_bind_blob()"),
            _ => unreachable!(),
        }
    }
}

unsafe impl BindParam for String {
    unsafe fn bind_param_unchecked(
        self,
        stmt: ffi::duckdb_prepared_statement,
        param_idx: u64,
    ) -> Result<(), &'static str> {
        self.as_str().bind_param_unchecked(stmt, param_idx)
    }
}

unsafe impl<Tz: TimeZone> BindParam for DateTime<Tz> {
    unsafe fn bind_param_unchecked(
        self,
        stmt: ffi::duckdb_prepared_statement,
        param_idx: u64,
    ) -> Result<(), &'static str> {
        match ffi::duckdb_bind_timestamp(stmt, param_idx, self.into_duckdb()) {
            ffi::DuckDBSuccess => Ok(()),
            ffi::DuckDBError => Err("duckdb_bind_timestamp()"),
            _ => unreachable!(),
        }
    }
}