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
//! Table builder API module
//! 
//! A table in this module represents directly a table in an SQL database. Each
//! table is initialised (for you) with a backend which corresponds to the one
//! selected for your schema in general.
//! 
//! You also don't explicitly have to call `exec` which is (again) done for you
//! by the `schema::exec()` function.
//! 
//! 

use generators::{DatabaseGenerator, TableGenerator};

#[derive(Clone, PartialEq, Eq)]
pub struct Table<T: DatabaseGenerator + TableGenerator + Default>(T, String, Vec<String>);

impl<T: DatabaseGenerator + TableGenerator + Default> Table<T> {
    
    /// Create a new table with a name and a generic backend
    pub fn new(name: &str) -> Self {
        return Table(Default::default(), String::from(name), Vec::new());
    }

    /// Helper function which gets the table name
    pub fn get_name(&self) -> &String {
        return &self.1;
    }

    /// Concatinate the table contents
    pub fn exec(&self) -> String {
        let l = self.2.len();
        if l == 1 {
            return self.2[0].clone();
        }

        /* Otherwise, build multi-instruction table */
        let mut cmd = String::from("(");
        let mut ctr = 0;

        for item in &self.2 {
            cmd.push_str(item);
            ctr += 1;
            if ctr < l {
                cmd.push_str(", ");
            }
        }

        cmd.push(')');
        return cmd;
    }

    /// Drop an existing column from the table
    pub fn drop_column(&mut self, _: &str) {
        unimplemented!();
    }

    /// Rename an existing column
    pub fn rename_column(&mut self, _: &str, _: &str) {
        unimplemented!();
    }

    /// Adds a primary key called id, that auto increments
    pub fn increments(&mut self) {
        self.2.push(T::increments());
    }

    /// Add an integer column
    pub fn integer(&mut self, name: &str) {
        self.2.push(T::integer(name));
    }
    
    /// Add a text column
    pub fn text(&mut self, name: &str) {
        self.2.push(T::text(name));
    }
    
    /// Add a string column
    pub fn string(&mut self, name: &str) {
        self.2.push(T::string(name));
    }
    
    /// Add a timestamp column
    pub fn timestamp(&mut self, name: &str) {
        self.2.push(T::timestamp(name));
    }

}