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
use crate::{
    pool::{ODBCConnection, SharedPool},
    OdbcError,
};
use async_trait::async_trait;
use axum_core::extract::{FromRequest, RequestParts};
use http::{self, StatusCode};
use lazy_static::lazy_static;
use odbc_api::Environment;
use std::fmt;
use std::sync::Arc;

#[derive(Clone)]
pub struct ODBCConnectionManager {
    pub(crate) shared: Arc<SharedPool>,
}

lazy_static! {
    pub(crate) static ref ENV: Environment = Environment::new().unwrap();
}

impl ODBCConnectionManager {
    /// Constructs a ODBCConnectionManager.
    ///
    /// limit is the max size of how many connections we will allow to exist within the pool.
    /// All other connections will get dropped after use if the pool is full.
    ///
    /// # Examples
    /// ```rust no_run
    /// use axum_odbc::{OdbcManagerLayer, ODBCConnectionManager};
    ///
    /// let odbc_manager = ODBCConnectionManager::new("DSN=PostgreSQL", 5);
    /// ```
    ///
    pub fn new<S: Into<String>>(connection_string: S, limit: u32) -> ODBCConnectionManager {
        ODBCConnectionManager {
            shared: SharedPool::new_arc(connection_string.into(), limit),
        }
    }

    /// Aquires an ODBCConnection.
    ///
    /// # Examples
    /// ```rust no_run
    /// use axum_odbc::{OdbcManagerLayer, ODBCConnectionManager};
    ///
    /// let odbc_manager = odbc_manager.aquire().await.unwrap();
    /// ```
    ///
    pub async fn aquire(&self) -> Result<ODBCConnection, OdbcError> {
        let shared = Arc::clone(&self.shared);
        shared.aquire().await
    }
}

#[async_trait]
impl<B> FromRequest<B> for ODBCConnectionManager
where
    B: Send,
{
    type Rejection = (http::StatusCode, &'static str);

    async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
        req.extensions()
            .get::<ODBCConnectionManager>()
            .cloned()
            .ok_or((
                StatusCode::INTERNAL_SERVER_ERROR,
                "Can't extract ODBCConnectionManager. Is `ODBCManagerLayer` enabled?",
            ))
    }
}

impl fmt::Debug for ODBCConnectionManager {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ODBCConnectionManager").finish()
    }
}