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
/*
This file is part of serde-odbc.

serde-odbc is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

serde-odbc is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with serde-odbc.  If not, see <http://www.gnu.org/licenses/>.
*/
use std::ptr::null_mut;

use odbc_sys::{
    SQLAllocHandle, SQLExecute, SQLFetch, SQLFreeHandle, SQLFreeStmt, SQLPrepare, SQLHANDLE,
    SQLHSTMT, SQLINTEGER, SQL_CLOSE, SQL_HANDLE_STMT, SQL_NO_DATA,
};
use serde::ser::Serialize;

use super::col_binding::{ColBinding, RowSet};
use super::connection::Connection;
use super::error::{OdbcResult, Result};
use super::param_binding::ParamBinding;

pub struct Statement<P: ParamBinding, C: ColBinding> {
    stmt: SQLHSTMT,
    is_positioned: bool,
    params: P,
    cols: C,
}

impl<P: ParamBinding, C: ColBinding> Statement<P, C> {
    pub fn new(conn: &Connection, stmt_str: &str) -> Result<Self> {
        let mut stmt: SQLHANDLE = null_mut();

        unsafe { SQLAllocHandle(SQL_HANDLE_STMT, conn.handle(), &mut stmt) }.check()?;

        let stmt = stmt as SQLHSTMT;

        unsafe { SQLPrepare(stmt, stmt_str.as_ptr(), stmt_str.len() as SQLINTEGER) }.check()?;

        Ok(Statement {
            stmt,
            is_positioned: false,
            params: P::new(),
            cols: C::new(),
        })
    }

    pub fn handle(&self) -> SQLHANDLE {
        self.stmt as SQLHANDLE
    }

    pub fn params(&mut self) -> &mut P::Params {
        self.params.params()
    }

    pub fn cols(&self) -> &C::Cols {
        self.cols.cols()
    }

    pub fn exec(&mut self) -> Result<()> {
        if self.is_positioned {
            unsafe { SQLFreeStmt(self.stmt, SQL_CLOSE) }.check()?;

            self.is_positioned = false;
        }

        unsafe {
            self.params.bind(self.stmt)?;
            self.cols.bind(self.stmt)?;
        }

        unsafe { SQLExecute(self.stmt) }.check()
    }

    pub fn fetch(&mut self) -> Result<bool> {
        let rc = unsafe { SQLFetch(self.stmt) };

        rc.check()?;

        self.is_positioned = true;

        Ok(rc != SQL_NO_DATA && self.cols.fetch())
    }
}

impl<P: ParamBinding, C: Default + Copy + Serialize> Statement<P, RowSet<C>> {
    pub fn with_fetch_size(conn: &Connection, stmt_str: &str, fetch_size: usize) -> Result<Self> {
        let mut stmt = Self::new(conn, stmt_str)?;

        stmt.set_fetch_size(fetch_size);

        Ok(stmt)
    }

    pub fn fetch_size(&self) -> usize {
        self.cols.fetch_size()
    }

    pub fn set_fetch_size(&mut self, size: usize) {
        self.cols.set_fetch_size(size)
    }
}

impl<P: ParamBinding, C: ColBinding> Drop for Statement<P, C> {
    fn drop(&mut self) {
        let _ = unsafe { SQLFreeHandle(SQL_HANDLE_STMT, self.handle()) };
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use crate::{
        col_binding::Cols, connection::Environment, param_binding::Params, tests::CONN_STR,
    };

    #[test]
    fn exec_stmt() {
        let env = Environment::new().unwrap();
        let conn = Connection::new(&env, CONN_STR).unwrap();

        let mut stmt: Statement<Params<i32>, Cols<i32>> =
            Statement::new(&conn, "SELECT ?").unwrap();
        *stmt.params() = 42;
        stmt.exec().unwrap();
        assert!(stmt.fetch().unwrap());
        assert_eq!(42, *stmt.cols());
        assert!(!stmt.fetch().unwrap());
    }
}