1use 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}