Skip to main content

easy_sql/traits/
driver.rs

1use std::{collections::HashMap, fmt::Debug};
2
3use easy_macros::always_context;
4
5use crate::{driver::TableField, traits::EasyExecutor};
6
7pub type DriverRow<D> = <<D as Driver>::InternalDriver as sqlx::database::Database>::Row;
8
9pub type DriverConnection<D> =
10    <<D as Driver>::InternalDriver as sqlx::database::Database>::Connection;
11pub type DriverArguments<'a, D> =
12    <<D as Driver>::InternalDriver as sqlx::database::Database>::Arguments<'a>;
13pub type InternalDriver<D> = <D as Driver>::InternalDriver;
14pub type DriverQueryResult<D> =
15    <<D as Driver>::InternalDriver as sqlx::database::Database>::QueryResult;
16pub type DriverTypeInfo<D> = <<D as Driver>::InternalDriver as sqlx::database::Database>::TypeInfo;
17
18/// Driver backend integration.
19///
20/// Implement this trait for custom drivers to describe the underlying `sqlx::Database` and
21/// database-specific DDL helpers used by the macros and migrations.
22#[always_context]
23pub trait Driver: Debug + Send + Sync + Sized {
24    type InternalDriver: sqlx::Database;
25
26    fn identifier_delimiter() -> &'static str;
27
28    /// Build a parameter placeholder for the driver (`index` is 0-based).
29    fn parameter_placeholder(index: usize) -> String;
30
31    async fn table_exists(
32        conn: &mut (impl EasyExecutor<Self> + Send + Sync),
33        name: &'static str,
34    ) -> anyhow::Result<bool>;
35
36    /// Create a table with the provided schema metadata.
37    ///
38    /// `foreign_keys`:
39    /// - Key: referenced table name
40    /// - Value: local field names, referenced field names, on delete/update cascade flag
41    async fn create_table(
42        conn: &mut (impl EasyExecutor<Self> + Send + Sync),
43        table_name: &'static str,
44        fields: Vec<TableField>,
45        primary_keys: Vec<&'static str>,
46        foreign_keys: HashMap<&'static str, (Vec<&'static str>, Vec<&'static str>, bool)>,
47    ) -> anyhow::Result<()>;
48}