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 /// Concrete transaction type, parameterized by connection borrow lifetime.
26 type Transaction<'c>: Transaction<'c>;
27
28 /// Human-readable backend name.
29 const NAME: &'static str;
30
31 /// Establish a connection given a URL.
32 fn connect(&self, url: Cow<'static, str>) -> impl Future<Output = Result<impl Connection>> {
33 Self::Connection::connect(url)
34 }
35
36 /// Obtain a SQL writer object (cheap to construct).
37 fn sql_writer(&self) -> Self::SqlWriter;
38}