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
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use super::{Affected, Desult, Dypes, Params, SelectHolder};
use std;
extern crate chrono;

pub struct Row<'a>(&'a Rowable);

impl<'a> Row<'a> {
    pub fn new<T>(row: &'a T) -> Self
    where
        T: Rowable,
    {
        Row(row)
    }

    pub fn get<T>(&self, key: &str) -> Option<T>
    where
        Option<T>: std::convert::From<Dypes>,
    {
        if key == "user_profile_id" {
            println!("get user_profile_id {:?}", self.0.get_val(key));
        }

        match self.0.get_val(key) {
            Some(x) => <Option<T>>::from(x),
            None => None,
        }
    }

    pub fn get_date_string(&self, key: &str, format: &str) -> Desult<String> {
        self.0.get_date_string(key, format)
    }

    /*
    pub fn get_date_time(&self, key: &str) -> Option<chrono::DateTime<chrono::offset::Local>> {
        let a: mysql::Value = self.0.get(key).unwrap();
        let b: (i32, u32, u32, u32, u32, u32, u32) = match a {
            mysql::Value::Date(a, b, c, d, e, f, g) => (
                a as i32, b as u32, c as u32, d as u32, e as u32, f as u32, g as u32,
            ),
            _ => return None,
        };
        let date = chrono::NaiveDate::from_ymd(b.0, b.1, b.2);
        let time = chrono::NaiveTime::from_hms_milli(b.3, b.4, b.5, b.6);

        let date_time = chrono::NaiveDateTime::new(date, time);

        let offset = chrono::offset::FixedOffset::east(0);

        Some(chrono::DateTime::<chrono::offset::Local>::from_utc(
            date_time, offset,
        ))
    }

    pub fn get_date_string(&self, key: &str, format: &str) -> Option<String> {
        let a: mysql::Value = self.0.get(key).unwrap();
        let b: (i32, u32, u32, u32, u32, u32, u32) = match a {
            mysql::Value::Date(a, b, c, d, e, f, g) => (
                a as i32, b as u32, c as u32, d as u32, e as u32, f as u32, g as u32,
            ),
            _ => return None,
        };

        if b.1 <= 0 || b.2 <= 0 {
            return None;
        }

        let date = chrono::NaiveDate::from_ymd(b.0, b.1, b.2);

        let a = date.format(format);

        Some(format!("{}", a))
    }
    */
}

pub trait Queryable {
    fn new(row: Row) -> Self;
}

///Need to implement for structs to be inserted
///
pub trait Insertable {
    ///List of fields of struct as &str
    fn fields() -> Vec<String>;

    ///List of values of struct as &str
    fn values(&self) -> Vec<String>;
}

pub trait Rowable {
    fn get_val(&self, key: &str) -> Option<Dypes>;

    fn get_date_string(&self, key: &str, format: &str) -> Desult<String>;
}

pub trait Connectionable {
    fn select<T: Queryable + std::fmt::Debug, P: std::clone::Clone>(
        &self,
        sql: &str,
        params: P,
        calc_found_rows: bool,
    ) -> Desult<SelectHolder<T>>
    where
        Params: std::convert::From<P>;

    fn value<T, R>(&self, sql: &str, colum: &str, params: R) -> Desult<T>
    where
        T: std::convert::From<Dypes>,
        R: std::clone::Clone,
        Params: std::convert::From<R>;

    fn row<T, R>(&self, sql: &str, params: R) -> Desult<T>
    where
        T: Queryable,
        R: std::clone::Clone,
        Params: std::convert::From<R>;

    fn array<T: Queryable + std::fmt::Debug, P: std::clone::Clone>(
        &self,
        sql: &str,
        params: P,
        calc_found_rows: bool,
    ) -> Desult<Vec<T>>
    where
        Params: std::convert::From<P>,
    {
        self.select(sql, params, calc_found_rows).map(|r| r.data)
    }

    fn insert_update<T: Insertable>(&self, table: &str, fields: Vec<T>) -> Desult<Affected>;

    fn gen_dupdate(colums: Vec<String>) -> String {
        let mut rt = Vec::new();
        for n in colums {
            rt.push(format!("{} = VALUES({}) ", n, n));
        }
        rt.join(&",")
    }

    fn delete_ids<T>(
        &self,
        table: &str,
        id_colum: &str,
        id_values: Vec<T>,
        in_out: &str,
    ) -> Desult<Affected>
    where
        T: std::clone::Clone,
        Dypes: std::convert::From<T>;

    fn delete_wid<T>(&self, table: &str, id_colum: &str, id_values: Vec<T>) -> Desult<Affected>
    where
        T: std::clone::Clone,
        Dypes: std::convert::From<T>;

    fn delete_nwid<T>(&self, table: &str, id_colum: &str, id_values: Vec<T>) -> Desult<Affected>
    where
        T: std::clone::Clone,
        Dypes: std::convert::From<T>;

    fn concat_colums(colums: Vec<&str>) -> String;
}