tank_core/connection.rs
1use crate::{Driver, Executor, Result, Transaction};
2use std::{
3 borrow::Cow,
4 future::{self, Future},
5};
6
7/// A live database handle capable of executing queries and spawning transactions.
8///
9/// This trait extends [`Executor`] adding functionality to acquire a connection
10/// and to begin transactional scopes.
11///
12/// Drivers implement concrete `Connection` types to expose backend-specific
13/// behavior (timeouts, pooling strategies, prepared statement caching, etc.).
14///
15/// # Lifecycle
16/// * `connect` creates (or fetches) an underlying connection. It may eagerly
17/// establish network I/O for validation; always await it.
18/// * `begin` starts a transaction returning an object implementing
19/// [`Transaction`]. Commit / rollback MUST be awaited to guarantee resource
20/// release.
21pub trait Connection: Executor {
22 /// Create a connection (or pool) with at least one underlying session
23 /// established to the given URL.
24 fn connect(
25 url: Cow<'static, str>,
26 ) -> impl Future<Output = Result<<Self::Driver as Driver>::Connection>>;
27
28 /// Begin a transaction scope tied to the current connection.
29 fn begin(&mut self) -> impl Future<Output = Result<impl Transaction<'_>>>;
30
31 fn disconnect(self) -> impl Future<Output = Result<()>> {
32 future::ready(Ok(()))
33 }
34}