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 connection pool from a string literal, owned `String`, or `Cow` URL.
28    fn connect_pool(
29        &self,
30        url: impl Into<Cow<'static, str>>,
31        config: PoolConfig,
32    ) -> impl Future<Output = Result<impl ConnectionPool<Self>>> + Send {
33        let url = url.into();
34        let config = config.into();
35        async move {
36            Ok(Pool::builder(DBConnectionManager::new(self.clone(), url))
37                .config(config)
38                .build()
39                .map_err(Error::new)?)
40        }
41    }
42
43    /// Returns a dialect-specific SQL writer for query construction.
44    fn sql_writer(&self) -> Self::SqlWriter;
45}