use std::time::Duration;
mod connection;
mod db_connection;
#[cfg(feature = "mock")]
#[cfg_attr(docsrs, doc(cfg(feature = "mock")))]
mod mock;
mod statement;
mod stream;
mod transaction;
pub use connection::*;
pub use db_connection::*;
#[cfg(feature = "mock")]
#[cfg_attr(docsrs, doc(cfg(feature = "mock")))]
pub use mock::*;
pub use statement::*;
use std::borrow::Cow;
pub use stream::*;
use tracing::instrument;
pub use transaction::*;
use crate::error::*;
#[derive(Debug, Default)]
pub struct Database;
#[derive(Debug, Clone)]
pub struct ConnectOptions {
pub(crate) url: String,
pub(crate) max_connections: Option<u32>,
pub(crate) min_connections: Option<u32>,
pub(crate) connect_timeout: Option<Duration>,
pub(crate) idle_timeout: Option<Duration>,
pub(crate) acquire_timeout: Option<Duration>,
pub(crate) max_lifetime: Option<Duration>,
pub(crate) sqlx_logging: bool,
pub(crate) sqlx_logging_level: log::LevelFilter,
pub(crate) sqlcipher_key: Option<Cow<'static, str>>,
pub(crate) schema_search_path: Option<String>,
}
impl Database {
#[instrument(level = "trace", skip(opt))]
pub async fn connect<C>(opt: C) -> Result<DatabaseConnection, DbErr>
where
C: Into<ConnectOptions>,
{
let opt: ConnectOptions = opt.into();
#[cfg(feature = "sqlx-mysql")]
if DbBackend::MySql.is_prefix_of(&opt.url) {
return crate::SqlxMySqlConnector::connect(opt).await;
}
#[cfg(feature = "sqlx-postgres")]
if DbBackend::Postgres.is_prefix_of(&opt.url) {
return crate::SqlxPostgresConnector::connect(opt).await;
}
#[cfg(feature = "sqlx-sqlite")]
if DbBackend::Sqlite.is_prefix_of(&opt.url) {
return crate::SqlxSqliteConnector::connect(opt).await;
}
#[cfg(feature = "mock")]
if crate::MockDatabaseConnector::accepts(&opt.url) {
return crate::MockDatabaseConnector::connect(&opt.url).await;
}
Err(conn_err(format!(
"The connection string '{}' has no supporting driver.",
opt.url
)))
}
}
impl<T> From<T> for ConnectOptions
where
T: Into<String>,
{
fn from(s: T) -> ConnectOptions {
ConnectOptions::new(s.into())
}
}
impl ConnectOptions {
pub fn new<T>(url: T) -> Self
where
T: Into<String>,
{
Self {
url: url.into(),
max_connections: None,
min_connections: None,
connect_timeout: None,
idle_timeout: None,
acquire_timeout: None,
max_lifetime: None,
sqlx_logging: true,
sqlx_logging_level: log::LevelFilter::Info,
sqlcipher_key: None,
schema_search_path: None,
}
}
#[cfg(feature = "sqlx-dep")]
pub fn pool_options<DB>(self) -> sqlx::pool::PoolOptions<DB>
where
DB: sqlx::Database,
{
let mut opt = sqlx::pool::PoolOptions::new();
if let Some(max_connections) = self.max_connections {
opt = opt.max_connections(max_connections);
}
if let Some(min_connections) = self.min_connections {
opt = opt.min_connections(min_connections);
}
if let Some(connect_timeout) = self.connect_timeout {
opt = opt.acquire_timeout(connect_timeout);
}
if let Some(idle_timeout) = self.idle_timeout {
opt = opt.idle_timeout(Some(idle_timeout));
}
if let Some(acquire_timeout) = self.acquire_timeout {
opt = opt.acquire_timeout(acquire_timeout);
}
if let Some(max_lifetime) = self.max_lifetime {
opt = opt.max_lifetime(Some(max_lifetime));
}
opt
}
pub fn get_url(&self) -> &str {
&self.url
}
pub fn max_connections(&mut self, value: u32) -> &mut Self {
self.max_connections = Some(value);
self
}
pub fn get_max_connections(&self) -> Option<u32> {
self.max_connections
}
pub fn min_connections(&mut self, value: u32) -> &mut Self {
self.min_connections = Some(value);
self
}
pub fn get_min_connections(&self) -> Option<u32> {
self.min_connections
}
pub fn connect_timeout(&mut self, value: Duration) -> &mut Self {
self.connect_timeout = Some(value);
self
}
pub fn get_connect_timeout(&self) -> Option<Duration> {
self.connect_timeout
}
pub fn idle_timeout(&mut self, value: Duration) -> &mut Self {
self.idle_timeout = Some(value);
self
}
pub fn get_idle_timeout(&self) -> Option<Duration> {
self.idle_timeout
}
pub fn acquire_timeout(&mut self, value: Duration) -> &mut Self {
self.acquire_timeout = Some(value);
self
}
pub fn get_acquire_timeout(&self) -> Option<Duration> {
self.acquire_timeout
}
pub fn max_lifetime(&mut self, lifetime: Duration) -> &mut Self {
self.max_lifetime = Some(lifetime);
self
}
pub fn get_max_lifetime(&self) -> Option<Duration> {
self.max_lifetime
}
pub fn sqlx_logging(&mut self, value: bool) -> &mut Self {
self.sqlx_logging = value;
self
}
pub fn get_sqlx_logging(&self) -> bool {
self.sqlx_logging
}
pub fn sqlx_logging_level(&mut self, level: log::LevelFilter) -> &mut Self {
self.sqlx_logging_level = level;
self
}
pub fn get_sqlx_logging_level(&self) -> log::LevelFilter {
self.sqlx_logging_level
}
pub fn sqlcipher_key<T>(&mut self, value: T) -> &mut Self
where
T: Into<Cow<'static, str>>,
{
self.sqlcipher_key = Some(value.into());
self
}
pub fn set_schema_search_path<T>(&mut self, schema_search_path: T) -> &mut Self
where
T: Into<String>,
{
self.schema_search_path = Some(schema_search_path.into());
self
}
}