database_reflection/adapter/
reflection_adapter.rs

1use crate::reflection::{Database, Table};
2use sqlx::{Error, Pool};
3use std::fmt::{Display, Formatter};
4use std::marker::PhantomData;
5use std::ops::Deref;
6
7#[derive(Clone, Default, Debug)]
8pub struct Uninitialized<DB: sqlx::Database>(PhantomData<DB>);
9impl<DB: sqlx::Database> Uninitialized<DB> {
10    pub fn new() -> Uninitialized<DB> {
11        Uninitialized(PhantomData)
12    }
13}
14#[derive(Clone, Debug)]
15pub struct Connected<DB: sqlx::Database>(Pool<DB>);
16
17pub trait State<DB: sqlx::Database> {}
18
19impl<DB: sqlx::Database> State<DB> for Uninitialized<DB> {}
20
21impl<DB: sqlx::Database> State<DB> for Connected<DB> {}
22impl<DB: sqlx::Database> Deref for Connected<DB> {
23    type Target = Pool<DB>;
24
25    fn deref(&self) -> &Self::Target {
26        &self.0
27    }
28}
29impl<DB: sqlx::Database> Connected<DB> {
30    pub fn new(pool: Pool<DB>) -> Connected<DB> {
31        Connected(pool)
32    }
33}
34
35#[derive(Debug)]
36pub enum ReflectionAdapterError {
37    ConnectionError(Error),
38    DatabaseError(Error),
39    ValidationError(String),
40    IntegrityError(String),
41}
42
43impl Display for ReflectionAdapterError {
44    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
45        match self {
46            ReflectionAdapterError::ConnectionError(e) => {
47                write!(f, "ConnectionError: {}", e)
48            }
49            ReflectionAdapterError::DatabaseError(e) => {
50                write!(f, "DatabaseError: {}", e)
51            }
52            ReflectionAdapterError::ValidationError(e) => write!(f, "ConnectionError: {}", e),
53            ReflectionAdapterError::IntegrityError(e) => write!(f, "ConnectionError: {}", e),
54        }
55    }
56}
57
58impl std::error::Error for ReflectionAdapterError {}
59
60pub trait ReflectionAdapterUninitialized<T: sqlx::Database> {
61    type ValidAdapter: ReflectionAdapter<T>;
62
63    fn set_connection_string(&mut self, connection_string: &str);
64
65    fn connect(
66        self,
67    ) -> impl std::future::Future<Output = Result<Self::ValidAdapter, ReflectionAdapterError>> + Send;
68}
69
70pub trait ReflectionAdapter<T: sqlx::Database> {
71    type InvalidAdapter: ReflectionAdapterUninitialized<T>;
72    fn disconnect(
73        self,
74    ) -> impl std::future::Future<Output = Result<Self::InvalidAdapter, ReflectionAdapterError>> + Send;
75
76    fn set_database_name(
77        &mut self,
78        database_name: &str,
79    ) -> impl std::future::Future<Output = Result<(), ReflectionAdapterError>>;
80
81    fn get_database_name(&self) -> &str;
82
83    fn list_database_names(
84        &self,
85    ) -> impl std::future::Future<Output = Result<Vec<String>, ReflectionAdapterError>> + Send;
86
87    fn list_table_names(
88        &self,
89    ) -> impl std::future::Future<Output = Result<Vec<String>, ReflectionAdapterError>> + Send;
90
91    fn get_table_reflection(
92        &self,
93        table_name: &str,
94    ) -> impl std::future::Future<Output = Result<Table, ReflectionAdapterError>> + Send;
95
96    fn get_reflection(
97        &self,
98    ) -> impl std::future::Future<Output = Result<Database, ReflectionAdapterError>> + Send;
99}