tank_core/
driver.rs

1use crate::{Connection, Prepared, Result, Transaction, writer::SqlWriter};
2use std::{borrow::Cow, fmt::Debug, future::Future};
3
4/// Backend connector and SQL dialect provider.
5pub trait Driver: Default + Debug {
6    /// Concrete connection.
7    type Connection: Connection<Driver = Self>;
8    /// SQL dialect writer.
9    type SqlWriter: SqlWriter;
10    /// Prepared statement handle.
11    type Prepared: Prepared;
12    /// Transaction type.
13    type Transaction<'c>: Transaction<'c>;
14
15    /// Human-readable backend name.
16    const NAME: &'static [&'static str];
17
18    /// Driver name (used in URLs).
19    fn name(&self) -> &'static str {
20        Self::NAME[0]
21    }
22
23    /// Connect to database `url`.
24    ///
25    /// The returned future must be awaited to obtain the connection object.
26    /// Implementations may perform I/O or validation during connection.
27    fn connect(&self, url: Cow<'static, str>) -> impl Future<Output = Result<Self::Connection>> {
28        Self::Connection::connect(url)
29    }
30
31    /// Create a SQL writer.
32    ///
33    /// Returns a writer capable of rendering SQL for this driver's dialect.
34    /// Writers are expected to be cheap to construct as they are usually stateless.
35    fn sql_writer(&self) -> Self::SqlWriter;
36}