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
28
    pub async fn new(opts: &Options) -> Result<Self, Box<dyn StdError>> {
39
83
        let conn = Arc::new(conn::connect(opts).await?);
40
        Ok(Model {
41
28
            conn: conn.clone(),
42
56
            unit: Arc::new(UnitModel::new(conn.clone()).await?),
43
111
            application: Arc::new(ApplicationModel::new(conn.clone()).await?),
44
52
            network: Arc::new(NetworkModel::new(conn.clone()).await?),
45
56
            device: Arc::new(DeviceModel::new(conn.clone()).await?),
46
55
            device_route: Arc::new(DeviceRouteModel::new(conn.clone()).await?),
47
56
            network_route: Arc::new(NetworkRouteModel::new(conn.clone()).await?),
48
55
            dldata_buffer: Arc::new(DlDataBufferModel::new(conn.clone()).await?),
49
        })
50
28
    }
51

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

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

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

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

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

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

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

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

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