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, device, device_route, dldata_buffer, network, network_route,
10
    sqlite::{
11
        application::Model as ApplicationModel,
12
        conn::{self, Options},
13
        device::Model as DeviceModel,
14
        device_route::Model as DeviceRouteModel,
15
        dldata_buffer::Model as DlDataBufferModel,
16
        network::Model as NetworkModel,
17
        network_route::Model as NetworkRouteModel,
18
        unit::Model as UnitModel,
19
    },
20
    unit,
21
};
22

            
23
/// Pure SQLite model.
24
#[derive(Clone)]
25
pub struct Model {
26
    conn: Arc<SqlitePool>,
27
    unit: Arc<UnitModel>,
28
    application: Arc<ApplicationModel>,
29
    network: Arc<NetworkModel>,
30
    device: Arc<DeviceModel>,
31
    device_route: Arc<DeviceRouteModel>,
32
    network_route: Arc<NetworkRouteModel>,
33
    dldata_buffer: Arc<DlDataBufferModel>,
34
}
35

            
36
impl Model {
37
    /// Create an instance.
38
56
    pub async fn new(opts: &Options) -> Result<Self, Box<dyn StdError>> {
39
56
        let conn = Arc::new(conn::connect(opts).await?);
40
        Ok(Model {
41
56
            conn: conn.clone(),
42
56
            unit: Arc::new(UnitModel::new(conn.clone()).await?),
43
56
            application: Arc::new(ApplicationModel::new(conn.clone()).await?),
44
56
            network: Arc::new(NetworkModel::new(conn.clone()).await?),
45
56
            device: Arc::new(DeviceModel::new(conn.clone()).await?),
46
56
            device_route: Arc::new(DeviceRouteModel::new(conn.clone()).await?),
47
56
            network_route: Arc::new(NetworkRouteModel::new(conn.clone()).await?),
48
56
            dldata_buffer: Arc::new(DlDataBufferModel::new(conn.clone()).await?),
49
        })
50
56
    }
51

            
52
    /// Get the raw database connection ([`SqlitePool`]).
53
1110
    pub fn get_connection(&self) -> &SqlitePool {
54
1110
        &self.conn
55
1110
    }
56
}
57

            
58
#[async_trait]
59
impl super::Model for Model {
60
36
    async fn close(&self) -> Result<(), Box<dyn StdError>> {
61
36
        Ok(())
62
72
    }
63

            
64
5594
    fn unit(&self) -> &dyn unit::UnitModel {
65
5594
        self.unit.as_ref()
66
5594
    }
67

            
68
4176
    fn application(&self) -> &dyn application::ApplicationModel {
69
4176
        self.application.as_ref()
70
4176
    }
71

            
72
6608
    fn network(&self) -> &dyn network::NetworkModel {
73
6608
        self.network.as_ref()
74
6608
    }
75

            
76
7048
    fn device(&self) -> &dyn device::DeviceModel {
77
7048
        self.device.as_ref()
78
7048
    }
79

            
80
3526
    fn device_route(&self) -> &dyn device_route::DeviceRouteModel {
81
3526
        self.device_route.as_ref()
82
3526
    }
83

            
84
2790
    fn network_route(&self) -> &dyn network_route::NetworkRouteModel {
85
2790
        self.network_route.as_ref()
86
2790
    }
87

            
88
3048
    fn dldata_buffer(&self) -> &dyn dldata_buffer::DlDataBufferModel {
89
3048
        self.dldata_buffer.as_ref()
90
3048
    }
91
}