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
163
use std::mem;
use std::rc;
use std::marker;
use std::fmt;

use super::select_query;
use super::insert_query::{self, ToInsertValue};
use super::from;
use super::predicate;
use super::expression;
use super::sql;
use super::field;

pub trait FieldUpd: sql::ToSql + fmt::Debug {
    fn upcast_field_update(&self) -> SharedFieldUpdate;
}

#[derive(Clone, Debug)]
pub struct FieldUpdate<F, T: fmt::Debug> {
    pub field: F,
    pub value: insert_query::InsertValue<T>
}

impl<F, T: fmt::Debug> FieldUpdate<F, T> {
    pub fn get_field(&self) -> &F {
        &self.field
    }

    pub fn get_value(&self) -> &insert_query::InsertValue<T> {
        &self.value
    }
}

type BoxedFieldUpdate = Box<FieldUpd + 'static>;
type SharedFieldUpdate = rc::Rc<BoxedFieldUpdate>;

impl<F, T> FieldUpd for FieldUpdate<F, T>
    where F: Clone + sql::ToPredicateValue + 'static,
          T: Clone + sql::ToPredicateValue + 'static {
    fn upcast_field_update(&self) -> SharedFieldUpdate {
        rc::Rc::new(Box::new(self.clone()))
    }
}

pub trait ToFieldUpdate<F, T: fmt::Debug> {
    fn set<B: expression::ToExpression<T>>(&self, val: &B) -> FieldUpdate<F, T>;
    fn set_default(&self) -> FieldUpdate<F, T>;
}

impl<T> ToFieldUpdate<field::NamedField<T>, T> for field::NamedField<T> where T: Clone + fmt::Debug {
    fn set<B: expression::ToExpression<T>>(&self, val: &B) -> FieldUpdate<field::NamedField<T>, T> {
        FieldUpdate {
            field: self.clone(),
            value: val.as_expr().to_insert_val()
        }
    }

    fn set_default(&self) -> FieldUpdate<field::NamedField<T>, T> {
        FieldUpdate {
            field: self.clone(),
            value: insert_query::InsertValue::Default
        }
    }
}

impl ToFieldUpdate<expression::RawExpression, expression::RawExpression> for expression::RawExpression {
    fn set<B: expression::ToExpression<expression::RawExpression>>(&self, val: &B) -> FieldUpdate<expression::RawExpression, expression::RawExpression> {
        FieldUpdate {
            field: self.clone(),
            value: val.as_expr().to_insert_val()
        }
    }

    fn set_default(&self) -> FieldUpdate<expression::RawExpression, expression::RawExpression> {
        FieldUpdate {
            field: self.clone(),
            value: insert_query::InsertValue::Default
        }
    }
}

pub trait Updatable<M>: from::Table + Sized {
    fn update(&self) -> UpdateQuery<(), select_query::NoResult, M> {
        UpdateQuery::new(self)
    }
}

#[derive(Clone, Debug)]
pub struct UpdateQuery<T, L, M> {
    only: bool,
    table: from::SharedTable,
    updates: Vec<SharedFieldUpdate>,
    from: Option<Vec<from::SharedFrom>>,
    where_: Option<predicate::SharedPredicate>,
    all: bool,
    returning: Option<select_query::Select>,

    _marker_t: marker::PhantomData<T>,
    _marker_l: marker::PhantomData<L>,
    _marker_m: marker::PhantomData<M>
}

impl<T, L, M> UpdateQuery<T, L, M> {
    pub fn is_only(&self) -> bool { self.only }
    pub fn is_all(&self) -> bool { self.all }

    pub fn get_table(&self) -> &from::SharedTable { &self.table }
    pub fn get_updates(&self) -> &Vec<SharedFieldUpdate> { &self.updates }
    pub fn get_from(&self) -> &Option<Vec<from::SharedFrom>> { &self.from }
    pub fn get_where(&self) -> &Option<predicate::SharedPredicate> { &self.where_ }
    pub fn get_returning(&self) -> &Option<select_query::Select> { &self.returning }
}

impl<T, L, M> UpdateQuery<T, L, M> {
    pub fn new(table: &from::Table) -> UpdateQuery<T, L, M> {
        UpdateQuery {
            only: false,
            table: table.upcast_table(),
            updates: vec![],
            from: None,
            where_: None,
            all: false,
            returning: None,

            _marker_t: marker::PhantomData,
            _marker_l: marker::PhantomData,
            _marker_m: marker::PhantomData,
        }
    }

    pub fn only(mut self) -> UpdateQuery<T, L, M> {
        self.only = true;
        self
    }

    pub fn from(mut self, from: &from::From) -> UpdateQuery<T, L, M> {
        if self.from.is_none() {
            self.from = Some(vec![])
        }

        self.from.as_mut().unwrap().push(from.upcast_from());
        self
    }

    pub fn field<F: FieldUpd>(mut self, update: F) -> UpdateQuery<T, L, M> {
        self.updates.push(update.upcast_field_update());
        self
    }

    pub fn all(mut self) -> UpdateQuery<T, L, M> {
        self.where_ = None;
        self.all = true;
        self
    }
}

returning_for!(UpdateQuery);

impl<T:Clone, L:Clone, M:Clone> select_query::Queryable for UpdateQuery<T, L, M> {
    fn get_where(&self) -> &Option<predicate::SharedPredicate> { &self.where_ }
    fn set_where(&mut self, predicate: predicate::SharedPredicate) { self.where_ = Some(predicate); }
    fn unset_where(&mut self) { self.where_ = None; }
}