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
use crate::ODBCConnectionManager;
use crate::SharedPool;
use std::ops::{Deref, DerefMut};
use std::sync::Arc;

pub type Connection = odbc_api::force_send_sync::Send<odbc_api::Connection<'static>>;
const DEREF_ERR: &str = "Connection already released to pool";

pub struct ODBCConnection {
    pub connection: Option<Connection>,
    pub(crate) pool: Option<Arc<SharedPool>>,
}

impl ODBCConnection {
    pub(crate) fn new(pool: Arc<SharedPool>, connection: Connection) -> Self {
        Self {
            connection: Some(connection),
            pool: Some(pool),
        }
    }

    pub fn detach(mut self) -> Connection {
        self.connection
            .take()
            .expect("PoolConnection double-dropped")
    }
}

impl Drop for ODBCConnection {
    fn drop(&mut self) {
        if self.connection.is_some() && self.pool.is_some() {
            let shared = self.pool.take().unwrap();
            let connection = self.connection.take().unwrap();
            let pool = shared.pool.lock().unwrap();

            if pool.len() < pool.capacity() {
                // pool has space lets insert it back into the Pool
                if pool.push(connection).is_err() {
                    panic!("Queue was maxed out");
                }
            }
        }
    }
}

impl Deref for ODBCConnection {
    type Target = Connection;

    fn deref(&self) -> &Self::Target {
        self.connection.as_ref().expect(DEREF_ERR)
    }
}

impl DerefMut for ODBCConnection {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.connection.as_mut().expect(DEREF_ERR)
    }
}

impl AsRef<Connection> for ODBCConnection {
    fn as_ref(&self) -> &Connection {
        self
    }
}

impl AsMut<Connection> for ODBCConnection {
    fn as_mut(&mut self) -> &mut Connection {
        self
    }
}

pub trait AttachODBC {
    fn attach(self, manager: ODBCConnectionManager) -> ODBCConnection;
}

impl AttachODBC for Connection {
    fn attach(self, manager: ODBCConnectionManager) -> ODBCConnection {
        ODBCConnection::new(Arc::clone(&manager.shared), self)
    }
}