Skip to main content

tank_core/
driver.rs

1use crate::{
2    Connection, ConnectionPool, DBConnectionManager, Error, PoolConfig, Prepared, Result,
3    Transaction, writer::SqlWriter,
4};
5use deadpool::managed::Pool;
6use std::{borrow::Cow, fmt::Debug, future::Future};
7
8/// Backend connector and dialect.
9pub trait Driver: Default + Clone + Sync + Send + Debug {
10    /// Concrete connection type.
11    type Connection: Connection<Driver = Self> + Debug;
12    /// Dialect-specific SQL writer.
13    type SqlWriter: SqlWriter;
14    /// Prepared statement implementation.
15    type Prepared: Prepared;
16    /// Transaction implementation.
17    type Transaction<'c>: Transaction<'c>;
18
19    /// Human-readable backend name.
20    const NAME: &'static [&'static str];
21
22    /// Returns the primary name of the driver.
23    fn name(&self) -> &'static str {
24        Self::NAME[0]
25    }
26
27    /// Creates a new connection to the database at the specified URL.
28    fn connect_pool(
29        &self,
30        url: Cow<'static, str>,
31        config: PoolConfig,
32    ) -> impl Future<Output = Result<impl ConnectionPool<Self>>> + Send {
33        let config = config.into();
34        async move {
35            Ok(Pool::builder(DBConnectionManager::new(self.clone(), url))
36                .config(config)
37                .build()
38                .map_err(Error::new)?)
39        }
40    }
41
42    /// Returns a dialect-specific SQL writer for query construction.
43    fn sql_writer(&self) -> Self::SqlWriter;
44}