serde_odbc/
col_binder.rs

1/*
2This file is part of serde-odbc.
3
4serde-odbc is free software: you can redistribute it and/or modify
5it under the terms of the GNU Lesser General Public License as published by
6the Free Software Foundation, either version 3 of the License, or
7(at your option) any later version.
8
9serde-odbc is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12GNU Lesser General Public License for more details.
13
14You should have received a copy of the GNU Lesser General Public License
15along with serde-odbc.  If not, see <http://www.gnu.org/licenses/>.
16*/
17use std::mem::size_of;
18
19use odbc_sys::{SQLBindCol, SQLHSTMT, SQLLEN, SQLPOINTER, SQLUSMALLINT, SQL_C_CHAR};
20use serde::ser::Serialize;
21
22use super::bind_types::BindTypes;
23use super::binder::{Binder, BinderImpl};
24use super::error::{OdbcResult, Result};
25
26struct ColBinder {
27    stmt: SQLHSTMT,
28    col_nr: SQLUSMALLINT,
29}
30
31pub unsafe fn bind_cols<C: Serialize>(stmt: SQLHSTMT, cols: &C) -> Result<()> {
32    Binder::bind(ColBinder { stmt, col_nr: 0 }, cols)
33}
34
35impl BinderImpl for ColBinder {
36    fn bind<T: BindTypes>(
37        &mut self,
38        value_ptr: SQLPOINTER,
39        indicator_ptr: *mut SQLLEN,
40    ) -> Result<()> {
41        self.col_nr += 1;
42
43        unsafe {
44            SQLBindCol(
45                self.stmt,
46                self.col_nr,
47                T::c_data_type(),
48                value_ptr,
49                size_of::<T>() as SQLLEN,
50                indicator_ptr,
51            )
52        }
53        .check()
54    }
55
56    fn bind_str(
57        &mut self,
58        length: usize,
59        value_ptr: SQLPOINTER,
60        indicator_ptr: *mut SQLLEN,
61    ) -> Result<()> {
62        self.col_nr += 1;
63
64        unsafe {
65            SQLBindCol(
66                self.stmt,
67                self.col_nr,
68                SQL_C_CHAR,
69                value_ptr,
70                (length + 1) as SQLLEN,
71                indicator_ptr,
72            )
73        }
74        .check()
75    }
76}