toasty_core/
driver.rs

1mod capability;
2pub use capability::{Capability, StorageTypes};
3
4mod response;
5pub use response::{Response, Rows};
6
7pub mod operation;
8pub use operation::{IsolationLevel, Operation};
9
10use crate::{
11    async_trait,
12    schema::{
13        db::{AppliedMigration, Migration, SchemaDiff},
14        Schema,
15    },
16};
17
18use std::{borrow::Cow, fmt::Debug, sync::Arc};
19
20#[async_trait]
21pub trait Driver: Debug + Send + Sync + 'static {
22    /// Returns the URL this driver is connecting to.
23    fn url(&self) -> Cow<'_, str>;
24
25    /// Describes the driver's capability, which informs the query planner.
26    fn capability(&self) -> &'static Capability;
27
28    /// Creates a new connection to the database.
29    ///
30    /// This method is called by the [`Pool`] whenever a [`Connection`] is requested while none is
31    /// available and there is room to create a new [`Connection`].
32    async fn connect(&self) -> crate::Result<Box<dyn Connection>>;
33
34    /// Returns the maximum number of simultaneous database connections supported. For example,
35    /// this is `Some(1)` for the in-memory SQLite driver which cannot be pooled.
36    fn max_connections(&self) -> Option<usize> {
37        None
38    }
39
40    /// Generates a migration from a [`SchemaDiff`].
41    fn generate_migration(&self, schema_diff: &SchemaDiff<'_>) -> Migration;
42
43    /// Drops the entire database and recreates an empty one without applying migrations.
44    async fn reset_db(&self) -> crate::Result<()>;
45}
46
47#[async_trait]
48pub trait Connection: Debug + Send + 'static {
49    /// Execute a database operation
50    async fn exec(&mut self, schema: &Arc<Schema>, plan: Operation) -> crate::Result<Response>;
51
52    /// Creates tables and indices defined in the schema on the database.
53    /// TODO: This will probably use database introspection in the future.
54    async fn push_schema(&mut self, _schema: &Schema) -> crate::Result<()>;
55
56    /// Returns a list of currently applied database migrations.
57    async fn applied_migrations(&mut self) -> crate::Result<Vec<AppliedMigration>>;
58
59    /// Applies a migration to the database.
60    async fn apply_migration(
61        &mut self,
62        id: u64,
63        name: String,
64        migration: &Migration,
65    ) -> crate::Result<()>;
66}