tank_core/
driver.rs

1use crate::{Connection, Prepared, Result, Transaction, writer::SqlWriter};
2use std::{borrow::Cow, future::Future};
3
4/// A backend implementation providing connection + SQL dialect services.
5///
6/// The `Driver` forms the bridge between high-level Tank abstractions and
7/// engine-specific details (quoting rules, type names, upsert syntax, etc.).
8///
9/// # Associated Types
10/// * `Connection` – concrete type implementing [`Connection`].
11/// * `SqlWriter` – dialect printer translating Tank's semantic AST into SQL.
12/// * `Prepared` – owned prepared statement handle used by `Query::Prepared`.
13///
14/// # Notes
15/// * `connect` delegates to the associated `Connection::connect` – drivers may
16///   wrap pooling or additional validation around it.
17/// * `NAME` is a human readable identifier (e.g. "postgres", "sqlite").
18pub trait Driver {
19    /// Concrete connection type.
20    type Connection: Connection;
21    /// Dialect aware SQL writer.
22    type SqlWriter: SqlWriter;
23    /// Prepared statement wrapper binding values.
24    type Prepared: Prepared;
25
26    /// Human-readable backend name.
27    const NAME: &'static str;
28
29    /// Establish a connection given a URL.
30    fn connect(&self, url: Cow<'static, str>) -> impl Future<Output = Result<impl Connection>> {
31        Self::Connection::connect(url)
32    }
33
34    /// Obtain a SQL writer object (cheap to construct).
35    fn sql_writer(&self) -> Self::SqlWriter;
36}
37
38/// Extension trait for drivers supporting transactions.
39///
40/// Separates transactional capabilities so drivers can avoid the complexity
41/// when transactions are not supported.
42pub trait DriverTransactional: Driver {
43    /// Concrete transaction type, parameterized by connection borrow lifetime.
44    type Transaction<'c>: Transaction<'c>;
45}