tank_core/
connection.rs

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