1
//! Pure SQLite model.
2

            
3
use std::{error::Error as StdError, sync::Arc};
4

            
5
use async_trait::async_trait;
6
use sqlx::SqlitePool;
7

            
8
use super::{
9
    access_token, authorization_code, client, login_session, refresh_token,
10
    sqlite::{
11
        access_token::Model as AccessTokenModel,
12
        authorization_code::Model as AuthorizationCodeModel,
13
        client::Model as ClientModel,
14
        conn::{self, Options},
15
        login_session::Model as LoginSessionModel,
16
        refresh_token::Model as RefreshTokenModel,
17
        user::Model as UserModel,
18
    },
19
    user,
20
};
21

            
22
/// Pure SQLite model.
23
#[derive(Clone)]
24
pub struct Model {
25
    conn: Arc<SqlitePool>,
26
    user: Arc<UserModel>,
27
    client: Arc<ClientModel>,
28
    login_session: Arc<LoginSessionModel>,
29
    authorization_code: Arc<AuthorizationCodeModel>,
30
    access_token: Arc<AccessTokenModel>,
31
    refresh_token: Arc<RefreshTokenModel>,
32
}
33

            
34
impl Model {
35
    /// Create an instance.
36
11
    pub async fn new(opts: &Options) -> Result<Self, Box<dyn StdError>> {
37
32
        let conn = Arc::new(conn::connect(opts).await?);
38
        Ok(Model {
39
11
            conn: conn.clone(),
40
22
            user: Arc::new(UserModel::new(conn.clone()).await?),
41
44
            client: Arc::new(ClientModel::new(conn.clone()).await?),
42
21
            login_session: Arc::new(LoginSessionModel::new(conn.clone()).await?),
43
22
            authorization_code: Arc::new(AuthorizationCodeModel::new(conn.clone()).await?),
44
21
            access_token: Arc::new(AccessTokenModel::new(conn.clone()).await?),
45
22
            refresh_token: Arc::new(RefreshTokenModel::new(conn.clone()).await?),
46
        })
47
11
    }
48

            
49
    /// Get the raw database connection ([`SqlitePool`]).
50
146
    pub fn get_connection(&self) -> &SqlitePool {
51
146
        &self.conn
52
146
    }
53
}
54

            
55
#[async_trait]
56
impl super::Model for Model {
57
6
    async fn close(&self) -> Result<(), Box<dyn StdError>> {
58
9
        self.conn.close().await;
59
6
        Ok(())
60
12
    }
61

            
62
1054
    fn user(&self) -> &dyn user::UserModel {
63
1054
        self.user.as_ref()
64
1054
    }
65

            
66
1419
    fn client(&self) -> &dyn client::ClientModel {
67
1419
        self.client.as_ref()
68
1419
    }
69

            
70
432
    fn login_session(&self) -> &dyn login_session::LoginSessionModel {
71
432
        self.login_session.as_ref()
72
432
    }
73

            
74
418
    fn authorization_code(&self) -> &dyn authorization_code::AuthorizationCodeModel {
75
418
        self.authorization_code.as_ref()
76
418
    }
77

            
78
417
    fn access_token(&self) -> &dyn access_token::AccessTokenModel {
79
417
        self.access_token.as_ref()
80
417
    }
81

            
82
181
    fn refresh_token(&self) -> &dyn refresh_token::RefreshTokenModel {
83
181
        self.refresh_token.as_ref()
84
181
    }
85
}