pub mod config;
pub mod execute;
pub mod introspect;
pub mod pool;
pub mod routes;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
use introspect::Schema;
use pool::ConnectionPool;
pub struct SqlConnection {
pub engine: config::Engine,
pub pool: ConnectionPool,
}
#[derive(Clone)]
pub struct SqlGateway {
inner: Arc<SqlGatewayInner>,
}
struct SqlGatewayInner {
connections: HashMap<String, SqlConnection>,
#[allow(dead_code)]
schema_cache: RwLock<HashMap<String, Schema>>,
auth_token: String,
}
impl SqlGateway {
#[must_use]
pub fn new(connections: HashMap<String, SqlConnection>, auth_token: String) -> Self {
Self {
inner: Arc::new(SqlGatewayInner {
connections,
schema_cache: RwLock::new(HashMap::new()),
auth_token,
}),
}
}
#[must_use]
pub fn connection(&self, name: &str) -> Option<&SqlConnection> {
self.inner.connections.get(name)
}
#[must_use]
pub fn check_token(&self, presented: &str) -> bool {
use subtle::ConstantTimeEq;
presented
.as_bytes()
.ct_eq(self.inner.auth_token.as_bytes())
.into()
}
#[allow(dead_code)]
pub(crate) fn cache(&self) -> &RwLock<HashMap<String, Schema>> {
&self.inner.schema_cache
}
}