dengine/
lib.rs

1//! Aims to create a common interface to use mysql and sqlite
2//! databases.
3
4extern crate deslite;
5extern crate mysql;
6
7extern crate serde;
8
9extern crate chrono;
10extern crate chrono_tz;
11
12pub mod my_sql;
13pub mod sqlite;
14mod traits;
15mod types;
16
17pub use traits::*;
18pub use types::*;
19
20/// Result type
21/// Err defaults to Error
22pub type Desult<T> = Result<T, Error>;
23
24#[derive(Debug)]
25pub struct Affected {
26    pub affected_rows: u64,
27    pub last_insert_id: u64,
28}
29
30impl Affected {
31    pub fn new(affected_rows: u64, last_insert_id: u64) -> Self {
32        Self {
33            affected_rows,
34            last_insert_id,
35        }
36    }
37}
38
39#[derive(Debug)]
40pub struct DbEngine;
41
42impl DbEngine {
43    pub fn new_mysql(
44        host: String,
45        user_name: String,
46        password: String,
47        db_name: String,
48    ) -> my_sql::Connection {
49        my_sql::Connection::new(host, user_name, password, db_name)
50    }
51
52    pub fn new_sqlite(db_name: &str) -> sqlite::Connection {
53        sqlite::Connection::new(db_name).unwrap()
54    }
55}
56
57/// Struct returned when the select method is used.
58#[derive(Debug, PartialEq, Eq)]
59pub struct SelectHolder<T> {
60    pub data: Vec<T>,
61    pub count: usize,
62}
63
64impl<T> SelectHolder<T> {
65    pub fn new(data: Vec<T>, count: usize) -> Self {
66        Self { data, count }
67    }
68}
69
70/// Error type for the lib
71#[derive(Debug)]
72pub enum Error {
73    SQLErr(String),
74    IndexOutOfBound(String),
75    ConversionErr(String),
76    LibErr(String),
77    Unknown(String),
78    ConnectionErr(String),
79}
80
81impl Error {
82    pub fn date_conv_err(key: &str) -> Self {
83        Error::ConversionErr(format!("Failed to convert {} to date string", key))
84    }
85}