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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//! ODBC support for the `r2d2` connection pool.
extern crate r2d2;
extern crate odbc;
extern crate odbc_safe as safe;

#[macro_use]
extern crate lazy_static;

use std::error::Error;
use std::fmt;
use odbc::*;

#[derive(Debug)]
pub struct ODBCConnectionManager {
    connection_string: String
}

#[derive(Debug)]
pub struct ODBCConnectionManagerTx {
    connection_string: String
}

pub struct ODBCConnection<'a, AC: safe::AutocommitMode>(Connection<'a, AC>);

unsafe impl Send for ODBCConnection<'static, safe::AutocommitOn> {}
unsafe impl Send for ODBCConnection<'static, safe::AutocommitOff> {}

impl <'a, AC: safe::AutocommitMode> ODBCConnection<'a, AC> {
    pub fn raw(&self) -> &Connection<'a, AC> {
        &self.0
    }
}

pub struct ODBCEnv(Environment<Version3>);

unsafe impl Sync for ODBCEnv {}

unsafe impl Send for ODBCEnv {}

#[derive(Debug)]
pub struct ODBCError(Box<dyn Error>);

lazy_static! {
    static ref ENV: ODBCEnv = ODBCEnv(create_environment_v3().unwrap());
}

impl Error for ODBCError {
    fn description(&self) -> &str {
        "Error connecting DB"
    }
}

impl fmt::Display for ODBCError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.0.fmt(f)
    }
}

impl From<DiagnosticRecord> for ODBCError {
    fn from(err: DiagnosticRecord) -> Self {
        println!("ODBC ERROR {}", err);
        ODBCError(Box::new(err))
    }
}

impl <E: 'static> From<std::sync::PoisonError<E>> for ODBCError {
    fn from(err: std::sync::PoisonError<E>) -> Self {
        ODBCError(Box::new(err))
    }
}

impl ODBCConnectionManager {
    /// Creates a new `ODBCConnectionManager`.
    pub fn new<S: Into<String>>(connection_string: S) -> ODBCConnectionManager
    {
        ODBCConnectionManager {
            connection_string: connection_string.into()
        }
    }
}

impl ODBCConnectionManagerTx {
    /// Creates a new `ODBCConnectionManagerTx`.
    pub fn new<S: Into<String>>(connection_string: S) -> ODBCConnectionManagerTx
    {
        ODBCConnectionManagerTx {
            connection_string: connection_string.into()
        }
    }
}

/// An `r2d2::ManageConnection` for ODBC connections.
///
/// ## Example
///
/// ```rust,no_run
/// extern crate r2d2;
/// extern crate r2d2_odbc;
/// extern crate odbc;
///
/// use std::thread;
/// use r2d2_odbc::ODBCConnectionManager;
/// use odbc::*;
///
/// fn main() {
///     let manager = ODBCConnectionManager::new("DSN=PostgreSQL");
///     let pool = r2d2::Pool::new(manager).unwrap();
///
///     let mut children = vec![];
///     for i in 0..10i32 {
///         let pool = pool.clone();
///         children.push(thread::spawn(move || {
///             let pool_conn = pool.get().unwrap();
///             let conn = pool_conn.raw();
///             let stmt = Statement::with_parent(&conn).unwrap();
///             if let Data(mut stmt) = stmt.exec_direct("SELECT version()").unwrap() {
///                 while let Some(mut cursor) = stmt.fetch().unwrap() {
///                     if let Some(val) = cursor.get_data::<&str>(0).unwrap() {
///                         print!("THREAD {} {}", i, val);
///                     }
///                 }
///             }
///         }));
///     }
///     for child in children {
///         let _ = child.join();
///     }
/// }
/// ```
impl r2d2::ManageConnection for ODBCConnectionManager {
    type Connection = ODBCConnection<'static, safe::AutocommitOn>;
    type Error = ODBCError;

    fn connect(&self) -> std::result::Result<Self::Connection, Self::Error> {
        let env = &ENV.0;
        Ok(ODBCConnection(env.connect_with_connection_string(&self.connection_string)?))
    }

    fn is_valid(&self, _conn: &mut Self::Connection) -> std::result::Result<(), Self::Error> {
        //TODO
        Ok(())
    }

    fn has_broken(&self, _conn: &mut Self::Connection) -> bool {
        //TODO
        false
    }
}

impl r2d2::ManageConnection for ODBCConnectionManagerTx {
    type Connection = ODBCConnection<'static, safe::AutocommitOff>;
    type Error = ODBCError;

    fn connect(&self) -> std::result::Result<Self::Connection, Self::Error> {
        let env = &ENV.0;
        let conn = env.connect_with_connection_string(&self.connection_string)?;
        let conn_result = conn.disable_autocommit();
        match conn_result {
            Ok(conn) => Ok(ODBCConnection(conn)),
            _ => Err(ODBCError("Unable to use transactions".into()))
        }
    }

    fn is_valid(&self, _conn: &mut Self::Connection) -> std::result::Result<(), Self::Error> {
        //TODO
        Ok(())
    }

    fn has_broken(&self, _conn: &mut Self::Connection) -> bool {
        //TODO
        false
    }
}