1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//! Aims to create a common interface to use mysql and sqlite
//! databases.

extern crate deslite;
extern crate mysql;

extern crate serde;

extern crate chrono;
extern crate chrono_tz;

pub mod my_sql;
pub mod sqlite;
mod traits;
mod types;

pub use traits::*;
pub use types::*;

/// Result type
/// Err defaults to Error
pub type Desult<T> = Result<T, Error>;

#[derive(Debug)]
pub struct Affected {
    pub affected_rows: u64,
    pub last_insert_id: u64,
}

impl Affected {
    pub fn new(affected_rows: u64, last_insert_id: u64) -> Self {
        Self {
            affected_rows,
            last_insert_id,
        }
    }
}

#[derive(Debug)]
pub struct DbEngine;

impl DbEngine {
    pub fn new_mysql(
        host: String,
        user_name: String,
        password: String,
        db_name: String,
    ) -> my_sql::Connection {
        my_sql::Connection::new(host, user_name, password, db_name)
    }

    pub fn new_sqlite(db_name: &str) -> sqlite::Connection {
        sqlite::Connection::new(db_name).unwrap()
    }
}

/// Struct returned when the select method is used.
#[derive(Debug, PartialEq, Eq)]
pub struct SelectHolder<T> {
    pub data: Vec<T>,
    pub count: usize,
}

impl<T> SelectHolder<T> {
    pub fn new(data: Vec<T>, count: usize) -> Self {
        Self { data, count }
    }
}

/// Error type for the lib
#[derive(Debug)]
pub enum Error {
    SQLErr(String),
    IndexOutOfBound(String),
    ConversionErr(String),
    LibErr(String),
    Unknown(String),
    ConnectionErr(String),
}

impl Error {
    pub fn date_conv_err(key: &str) -> Self {
        Error::ConversionErr(format!("Failed to convert {} to date string", key))
    }
}