use std::{
collections::{HashMap, HashSet},
sync::RwLockReadGuard,
};
use toql::{error::ToqlError, alias_format::AliasFormat, prelude::{Cache, Context, SqlArg}, table_mapper_registry::TableMapperRegistry};
use crate::{queryable::Queryable, backend::MySqlAsyncBackend};
pub use mysql_async;
pub use mysql_common::chrono;
#[macro_use]
pub mod access;
pub mod sql_arg;
pub mod error;
pub mod result;
pub mod row;
pub mod backend;
pub mod prelude;
pub mod toql_api;
pub mod queryable;
#[cfg(test)]
mod test;
pub struct MySqlAsync<'a, C>
where
C: Queryable + Send,
{
backend: MySqlAsyncBackend<'a, C>,
}
impl<'a, C> MySqlAsync<'a, C>
where
C: Queryable + Send,
{
pub fn from(conn: C, cache: &'a Cache) -> MySqlAsync<'a, C> {
Self::with_context(conn, cache, Context::default())
}
pub fn conn(&mut self) -> &mut C {
&mut self.backend.conn
}
pub fn into_conn(self) -> C {
self.backend.conn
}
pub fn with_context(conn: C, cache: &'a Cache, context: Context) -> MySqlAsync<'a, C> {
MySqlAsync {
backend: MySqlAsyncBackend {
conn,
cache,
context,
},
}
}
pub fn set_roles(&mut self, roles: HashSet<String>) -> &mut Self {
self.backend.context.roles = roles;
self
}
pub fn registry(
&self,
) -> std::result::Result<RwLockReadGuard<'_, TableMapperRegistry>, ToqlError> {
self.backend.cache.registry.read().map_err(ToqlError::from)
}
pub fn roles(&self) -> &HashSet<String> {
&self.backend.context.roles
}
pub fn alias_format(&self) -> AliasFormat {
self.backend.context.alias_format.to_owned()
}
pub fn aux_params(&self) -> &HashMap<String, SqlArg> {
&self.backend.context.aux_params
}
pub fn set_aux_param(&mut self, name: String, value: SqlArg) {
self.backend.context.aux_params.insert(name, value);
}
}