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
    application_dldata, application_uldata, coremgr_opdata, network_dldata, network_uldata,
10
    sqlite::{
11
        application_dldata::Model as ApplicationDlDataModel,
12
        application_uldata::Model as ApplicationUlDataModel,
13
        conn::{self, Options},
14
        coremgr_opdata::Model as CoremgrOpDataModel,
15
        network_dldata::Model as NetworkDlDataModel,
16
        network_uldata::Model as NetworkUlDataModel,
17
    },
18
};
19

            
20
/// Pure SQLite model.
21
#[derive(Clone)]
22
pub struct Model {
23
    conn: Arc<SqlitePool>,
24
    application_dldata: Arc<ApplicationDlDataModel>,
25
    application_uldata: Arc<ApplicationUlDataModel>,
26
    coremgr_opdata: Arc<CoremgrOpDataModel>,
27
    network_dldata: Arc<NetworkDlDataModel>,
28
    network_uldata: Arc<NetworkUlDataModel>,
29
}
30

            
31
impl Model {
32
    /// Create an instance.
33
12
    pub async fn new(opts: &Options) -> Result<Self, Box<dyn StdError>> {
34
33
        let conn = Arc::new(conn::connect(opts).await?);
35
        Ok(Model {
36
12
            conn: conn.clone(),
37
24
            application_dldata: Arc::new(ApplicationDlDataModel::new(conn.clone()).await?),
38
47
            application_uldata: Arc::new(ApplicationUlDataModel::new(conn.clone()).await?),
39
23
            coremgr_opdata: Arc::new(CoremgrOpDataModel::new(conn.clone()).await?),
40
24
            network_dldata: Arc::new(NetworkDlDataModel::new(conn.clone()).await?),
41
23
            network_uldata: Arc::new(NetworkUlDataModel::new(conn.clone()).await?),
42
        })
43
12
    }
44

            
45
    /// Get the raw database connection ([`SqlitePool`]).
46
153
    pub fn get_connection(&self) -> &SqlitePool {
47
153
        &self.conn
48
153
    }
49
}
50

            
51
#[async_trait]
52
impl super::Model for Model {
53
8
    async fn close(&self) -> Result<(), Box<dyn StdError>> {
54
8
        Ok(())
55
16
    }
56

            
57
588
    fn application_dldata(&self) -> &dyn application_dldata::ApplicationDlDataModel {
58
588
        self.application_dldata.as_ref()
59
588
    }
60

            
61
582
    fn application_uldata(&self) -> &dyn application_uldata::ApplicationUlDataModel {
62
582
        self.application_uldata.as_ref()
63
582
    }
64

            
65
532
    fn coremgr_opdata(&self) -> &dyn coremgr_opdata::CoremgrOpDataModel {
66
532
        self.coremgr_opdata.as_ref()
67
532
    }
68

            
69
591
    fn network_dldata(&self) -> &dyn network_dldata::NetworkDlDataModel {
70
591
        self.network_dldata.as_ref()
71
591
    }
72

            
73
568
    fn network_uldata(&self) -> &dyn network_uldata::NetworkUlDataModel {
74
568
        self.network_uldata.as_ref()
75
568
    }
76
}