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
use std::{fmt, rc};

use super::sql;
use super::select_query;
use super::insert_query;
use super::update_query;
use super::delete_query;
use super::field::{self, Field};

pub trait From: fmt::Debug {
    fn as_sql(&self) -> &sql::FromToSql;
    fn upcast_from(&self) -> SharedFrom;
}

pub type BoxedFrom = Box<From + 'static>;
pub type SharedFrom = rc::Rc<BoxedFrom>;

pub trait Table: fmt::Debug {
    fn upcast_table(&self) -> SharedTable;
    fn get_table_name(&self) -> &String;
    fn get_table_alias(&self) -> &Option<String>;
}

pub type BoxedTable = Box<Table + 'static>;
pub type SharedTable = rc::Rc<BoxedTable>;

#[derive(Clone, Debug)]
pub struct TableDef {
    name: String,
    alias: Option<String>
}

// FIXME: Remove after all stuff in insert_query::InsertQuery will be fixed
macro_rules! insert {
    ($name:ident, $(($t:ident, $arg:ident)),+) => (
        #[doc(hidden)]
        fn $name<$($t:Clone+ 'static,)+>(&self, $($arg: &field::NamedField<$t>,)+) -> insert_query::InsertQuery<($($t,)+), ($(insert_query::InsertValue<$t>,)+), (), (), ()> {
            let mut cols = vec![];
            $(cols.push((*$arg).upcast_field());)+
            insert_query::InsertQuery::new_with_cols(self, cols)
        }
    )
}

impl TableDef {
    pub fn new(name: &str) -> TableDef {
        TableDef { name: name.to_string(), alias: None }
    }

    pub fn new_with_alias(name: &str, alias: &str) -> TableDef {
        TableDef { name: name.to_string(), alias: Some(alias.to_string()) }
    }

    pub fn alias(&self, alias: &str) -> TableDef {
        let mut table_def = self.clone();
        table_def.alias = Some(alias.to_string());
        table_def
    }

    // FIXME: Remove after all stuff in insert_query::InsertQuery will be fixed
    insert!(insert_1, (T0, _t0));

    #[doc(hidden)]
    pub fn insert_1_for_test(&self, name: &field::NamedField<String>) -> insert_query::InsertQuery<(String,), (insert_query::InsertValue<String>,), (), (), ()> {
        self.insert_1(name)
    }
}

impl Table for TableDef {
    fn upcast_table(&self) -> SharedTable {
        rc::Rc::new(Box::new(self.clone()))
    }

    fn get_table_name(&self) -> &String {
        &self.name
    }

    fn get_table_alias(&self) -> &Option<String> {
        &self.alias
    }
}

impl From for TableDef {
    fn as_sql(&self) -> &sql::FromToSql {
        self
    }

    fn upcast_from(&self) -> SharedFrom {
        rc::Rc::new(Box::new(self.clone()))
    }
}

impl select_query::Selectable<()> for TableDef {}
impl insert_query::Insertable<()> for TableDef {}
impl update_query::Updatable<()> for TableDef {}
impl delete_query::Deletable<()> for TableDef {}

#[derive(Clone, Debug)]
pub struct FromSelect<T, L, M> {
    pub select: select_query::SelectQuery<T, L, M>,
    pub alias: String
}

impl<T: Clone + 'static + fmt::Debug, L: Clone + 'static + fmt::Debug, M: Clone + 'static + fmt::Debug> From for FromSelect<T, L, M> {
    fn as_sql(&self) -> &sql::FromToSql {
        self
    }

    fn upcast_from(&self) -> SharedFrom {
        rc::Rc::new(Box::new(self.clone()))
    }
}

impl<T: Clone + 'static + fmt::Debug, L: Clone + 'static + fmt::Debug, M: Clone + 'static + fmt::Debug> select_query::Selectable<M> for FromSelect<T, L, M> {}