odbc_safe/
c_data_type.rs

1use super::*;
2use sys::*;
3use std::mem::size_of;
4use std::os::raw::c_void;
5use std::ptr::{null, null_mut};
6
7/// See [C Data Types in ODBC][1]
8/// [1]: https://docs.microsoft.com/sql/odbc/reference/develop-app/c-data-types-in-odbc
9pub unsafe trait CDataType {
10    /// C Data type of the buffer returned by `mut_sql_ptr()`.
11    fn c_data_type() -> SqlCDataType;
12    /// Const sql pointer
13    fn sql_ptr(&self) -> *const c_void;
14    /// Pointer to the buffer in which should be filled with data.
15    fn mut_sql_ptr(&mut self) -> SQLPOINTER;
16    /// Length of the buffer returned by `mut_sql_ptr()` in bytes.
17    fn buffer_len(&self) -> SQLLEN;
18}
19
20unsafe impl CDataType for [SQLCHAR] {
21    fn c_data_type() -> SqlCDataType {
22        SQL_C_BINARY
23    }
24
25    fn sql_ptr(&self) -> *const c_void {
26        if self.is_empty() {
27            null()
28        } else {
29            self.as_ptr() as SQLPOINTER
30        }
31    }
32
33    fn mut_sql_ptr(&mut self) -> SQLPOINTER {
34        if self.is_empty() {
35            null_mut()
36        } else {
37            self.as_mut_ptr() as SQLPOINTER
38        }
39    }
40
41    fn buffer_len(&self) -> SQLLEN {
42        self.buf_len()
43    }
44}
45
46unsafe impl CDataType for SQLSMALLINT {
47    fn c_data_type() -> SqlCDataType {
48        SQL_C_SSHORT
49    }
50
51    fn sql_ptr(&self) -> *const c_void {
52        let ptr: *const Self = self;
53        ptr as *const c_void
54    }
55
56    fn mut_sql_ptr(&mut self) -> SQLPOINTER {
57        let ptr: *mut Self = self;
58        ptr as SQLPOINTER
59    }
60
61    fn buffer_len(&self) -> SQLLEN {
62        size_of::<Self>() as SQLLEN
63    }
64}
65
66unsafe impl CDataType for SQLUSMALLINT {
67    fn c_data_type() -> SqlCDataType {
68        SQL_C_USHORT
69    }
70
71    fn sql_ptr(&self) -> *const c_void {
72        let ptr: *const Self = self;
73        ptr as *const c_void
74    }
75
76    fn mut_sql_ptr(&mut self) -> SQLPOINTER {
77        let ptr: *mut Self = self;
78        ptr as SQLPOINTER
79    }
80
81    fn buffer_len(&self) -> SQLLEN {
82        size_of::<Self>() as SQLLEN
83    }
84}
85
86unsafe impl CDataType for SQLINTEGER {
87    fn c_data_type() -> SqlCDataType {
88        SQL_C_SLONG
89    }
90
91    fn sql_ptr(&self) -> *const c_void {
92        let ptr: *const Self = self;
93        ptr as *const c_void
94    }
95
96    fn mut_sql_ptr(&mut self) -> SQLPOINTER {
97        let ptr: *mut Self = self;
98        ptr as SQLPOINTER
99    }
100
101    fn buffer_len(&self) -> SQLLEN {
102        size_of::<Self>() as SQLLEN
103    }
104}
105
106unsafe impl CDataType for SQLUINTEGER {
107    fn c_data_type() -> SqlCDataType {
108        SQL_C_ULONG
109    }
110
111    fn sql_ptr(&self) -> *const c_void {
112        let ptr: *const Self = self;
113        ptr as *const c_void
114    }
115
116    fn mut_sql_ptr(&mut self) -> SQLPOINTER {
117        let ptr: *mut Self = self;
118        ptr as SQLPOINTER
119    }
120
121    fn buffer_len(&self) -> SQLLEN {
122        size_of::<Self>() as SQLLEN
123    }
124}
125
126unsafe impl CDataType for f32 {
127    fn c_data_type() -> SqlCDataType {
128        SQL_C_FLOAT
129    }
130
131    fn sql_ptr(&self) -> *const c_void {
132        let ptr: *const Self = self;
133        ptr as *const c_void
134    }
135
136    fn mut_sql_ptr(&mut self) -> SQLPOINTER {
137        let ptr: *mut Self = self;
138        ptr as SQLPOINTER
139    }
140
141    fn buffer_len(&self) -> SQLLEN {
142        size_of::<Self>() as SQLLEN
143    }
144}
145
146unsafe impl CDataType for f64 {
147    fn c_data_type() -> SqlCDataType {
148        SQL_C_DOUBLE
149    }
150
151    fn sql_ptr(&self) -> *const c_void {
152        let ptr: *const Self = self;
153        ptr as *const c_void
154    }
155
156    fn mut_sql_ptr(&mut self) -> SQLPOINTER {
157        let ptr: *mut Self = self;
158        ptr as SQLPOINTER
159    }
160
161    fn buffer_len(&self) -> SQLLEN {
162        size_of::<Self>() as SQLLEN
163    }
164}
165
166unsafe impl CDataType for i8 {
167    fn c_data_type() -> SqlCDataType {
168        SQL_C_STINYINT
169    }
170
171    fn sql_ptr(&self) -> *const c_void {
172        let ptr: *const Self = self;
173        ptr as *const c_void
174    }
175
176    fn mut_sql_ptr(&mut self) -> SQLPOINTER {
177        let ptr: *mut Self = self;
178        ptr as SQLPOINTER
179    }
180
181    fn buffer_len(&self) -> SQLLEN {
182        size_of::<Self>() as SQLLEN
183    }
184}
185
186unsafe impl CDataType for SQLCHAR {
187    fn c_data_type() -> SqlCDataType {
188        SQL_C_UTINYINT
189    }
190
191    fn sql_ptr(&self) -> *const c_void {
192        let ptr: *const Self = self;
193        ptr as *const c_void
194    }
195
196    fn mut_sql_ptr(&mut self) -> SQLPOINTER {
197        let ptr: *mut Self = self;
198        ptr as SQLPOINTER
199    }
200
201    fn buffer_len(&self) -> SQLLEN {
202        size_of::<Self>() as SQLLEN
203    }
204}
205
206unsafe impl CDataType for i64 {
207    fn c_data_type() -> SqlCDataType {
208        SQL_C_SBIGINT
209    }
210
211    fn sql_ptr(&self) -> *const c_void {
212        let ptr: *const Self = self;
213        ptr as *const c_void
214    }
215
216    fn mut_sql_ptr(&mut self) -> SQLPOINTER {
217        let ptr: *mut Self = self;
218        ptr as SQLPOINTER
219    }
220
221    fn buffer_len(&self) -> SQLLEN {
222        size_of::<Self>() as SQLLEN
223    }
224}
225
226unsafe impl CDataType for u64 {
227    fn c_data_type() -> SqlCDataType {
228        SQL_C_UBIGINT
229    }
230
231    fn sql_ptr(&self) -> *const c_void {
232        let ptr: *const Self = self;
233        ptr as *const c_void
234    }
235
236    fn mut_sql_ptr(&mut self) -> SQLPOINTER {
237        let ptr: *mut Self = self;
238        ptr as SQLPOINTER
239    }
240
241    fn buffer_len(&self) -> SQLLEN {
242        size_of::<Self>() as SQLLEN
243    }
244}