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
use crate::{
handles::{CData, DataType, HasDataType},
parameter::{InputParameter, StableCData},
};
use odbc_sys::{CDataType, Date, Numeric, Time, Timestamp};
use std::{ffi::c_void, ptr::null};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Ord, PartialOrd)]
pub struct Bit(pub u8);
impl Bit {
pub fn as_bool(self) -> bool {
match self.0 {
0 => false,
1 => true,
_ => panic!("Invalid boolean representation in Bit."),
}
}
}
pub unsafe trait Pod: Default + Copy + CData {
const C_DATA_TYPE: CDataType;
}
macro_rules! impl_fixed_sized {
($t:ident, $c_data_type:expr) => {
unsafe impl CData for $t {
fn cdata_type(&self) -> CDataType {
$c_data_type
}
fn indicator_ptr(&self) -> *const isize {
null()
}
fn value_ptr(&self) -> *const c_void {
self as *const $t as *const c_void
}
fn buffer_length(&self) -> isize {
0
}
}
unsafe impl Pod for $t {
const C_DATA_TYPE: CDataType = $c_data_type;
}
};
}
impl_fixed_sized!(f64, CDataType::Double);
impl_fixed_sized!(f32, CDataType::Float);
impl_fixed_sized!(Date, CDataType::TypeDate);
impl_fixed_sized!(Timestamp, CDataType::TypeTimestamp);
impl_fixed_sized!(Time, CDataType::TypeTime);
impl_fixed_sized!(Numeric, CDataType::Numeric);
impl_fixed_sized!(i16, CDataType::SShort);
impl_fixed_sized!(u16, CDataType::UShort);
impl_fixed_sized!(i32, CDataType::SLong);
impl_fixed_sized!(u32, CDataType::ULong);
impl_fixed_sized!(i8, CDataType::STinyInt);
impl_fixed_sized!(u8, CDataType::UTinyInt);
impl_fixed_sized!(Bit, CDataType::Bit);
impl_fixed_sized!(i64, CDataType::SBigInt);
impl_fixed_sized!(u64, CDataType::UBigInt);
macro_rules! impl_input_fixed_sized {
($t:ident, $data_type:expr) => {
impl HasDataType for $t {
fn data_type(&self) -> DataType {
$data_type
}
}
unsafe impl InputParameter for $t {}
unsafe impl StableCData for $t {}
};
}
impl_input_fixed_sized!(f64, DataType::Double);
impl_input_fixed_sized!(f32, DataType::Real);
impl_input_fixed_sized!(Date, DataType::Date);
impl_input_fixed_sized!(i16, DataType::SmallInt);
impl_input_fixed_sized!(i32, DataType::Integer);
impl_input_fixed_sized!(i8, DataType::TinyInt);
impl_input_fixed_sized!(Bit, DataType::Bit);
impl_input_fixed_sized!(i64, DataType::BigInt);
#[cfg(test)]
mod tests {
use super::Bit;
#[test]
#[should_panic(expected = "Invalid boolean representation in Bit.")]
fn invalid_bit() {
let bit = Bit(2);
bit.as_bool();
}
}