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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
use std::{marker::PhantomData, ops::{Deref, Not}};

use tokio_postgres::types::ToSql;

use crate::query::Where;

/// A wrapper around the [`Column`] struct which includes
/// the rust type of the field.
///
/// For each field of a [`pg_worm::Model`] a `TypedColumn` is automatically
/// generated.
///
/// A `TypedColumn` can be used to access information about
/// the column and create `Filter`s regarding this column.
///
/// # Example
///
/// ```
/// use pg_worm::prelude::*;
///
/// #[derive(Model)]
/// struct Foo {
///     baz: i64
/// }
///
/// assert_eq!(Foo::baz.column_name(), "baz");
///
/// ```
///
#[derive(Clone, Copy, Debug)]
pub struct TypedColumn<T: ToSql + Sync> {
    column: Column,
    rs_type: PhantomData<T>,
}

/// This type represents a column.
///  
/// It can be used to retrieve information about the column.
///
/// It is mostly seen in it's wrapped form [`TypedColumn`].
#[derive(Copy, Clone, Debug)]
pub struct Column {
    /// The name of this column.
    pub column_name: &'static str,
    /// The name of the table this columnn belongs to.
    pub table_name: &'static str,
    nullable: bool,
    unique: bool,
    primary_key: bool,
    generated: bool,
}

macro_rules! impl_prop_typed_col {
    ($($prop:ident),+) => {
        $(
            /// Set this property so `true`.
            pub const fn $prop(mut self) -> TypedColumn<T> {
                self.column.$prop = true;
                self
            }
        )*
    };
}

macro_rules! impl_prop_col {
    ($($prop:ident),+) => {
        $(
            /// Returns this propertie's value.
            pub const fn $prop(&self) -> bool {
                self.$prop
            }
        )*
    };
}

impl<T: ToSql + Sync + Send + 'static> TypedColumn<T> {
    /// Creates anew `TypedColumn<T>`.
    pub const fn new(table_name: &'static str, column_name: &'static str) -> TypedColumn<T> {
        TypedColumn {
            column: Column::new(table_name, column_name),
            rs_type: PhantomData::<T>,
        }
    }

    impl_prop_typed_col!(nullable, unique, primary_key, generated);

    /// Returns a [`Where`] clause which checks whether
    /// this column is equal to some value.
    pub fn eq<'a>(&self, other: &'a T) -> Where<'a> {
        Where::new(
            format!("{}.{} = ?", self.table_name, self.column_name),
            vec![other],
        )
    }
}

impl<T: ToSql + Sync + Send + 'static + PartialOrd> TypedColumn<T> {
    /// Check whether this column's value is **g**reater **t**han some
    /// other value.
    pub fn gt<'a>(&self, other: &'a T) -> Where<'a> {
        Where::new(
            format!("{}.{} > ?", self.table_name, self.column_name),
            vec![other],
        )
    }

    /// Check whether this colum's value is **g**reater **t**han or **e**qual
    /// to another value.
    pub fn gte<'a>(&self, other: &'a T) -> Where<'a> {
        Where::new(
            format!("{}.{} >= ?", self.table_name, self.column_name),
            vec![other],
        )
    }

    /// Check whether this column's value is **l**ess **t**han some
    /// other value.
    pub fn lt<'a>(&self, other: &'a T) -> Where<'a> {
        Where::new(
            format!("{}.{} < ?", self.table_name, self.column_name),
            vec![other],
        )
    }

    /// Check whether this colum's value is **l**ess **t**han or **e**qual
    /// to another value.
    pub fn lte<'a>(&self, other: &'a T) -> Where<'a> {
        Where::new(
            format!("{}.{} <= ?", self.table_name, self.column_name),
            vec![other],
        )
    }
}

impl<'a, T: ToSql + Sync + 'a> TypedColumn<Option<T>> {
    /// Check whether this column's value is `NULL`.
    pub fn null(&self) -> Where<'a> {
        Where::new(
            format!("{}.{} IS NULL", self.table_name, self.column_name), 
            vec![]
        )
    }

    /// Check whether this column's value is `NOT NULL`
    pub fn not_noll(&self) -> Where<'a> {
        self.null().not()
    }
} 

impl<'a, T: ToSql + Sync + 'a> TypedColumn<Vec<T>> {
    /// Check whether this column's array contains some value.
    pub fn contains(&self, value: &'a T) -> Where<'a> {
        Where::new(
            format!("? = ANY({}.{})", self.table_name, self.column_name),
            vec![value]
        )
    }

    /// Check whether this column's array does `NOT` contain some value.
    pub fn contains_not(&self, value: &'a T) -> Where<'a> {
        self.contains(value).not()
    }

    /// Check whether this column's array contains any value of 
    /// another array.
    pub fn contains_any(&self, values: &'a Vec<&'a T>) -> Where<'a> {
        Where::new(
            format!("{}.{} && ?", self.table_name, self.column_name),
            vec![values]
        )
    }

    /// Check whether this column's array contains all values of
    /// another array.
    pub fn contains_all(&self, values: &'a Vec<&'a T>) -> Where<'a> {
        Where::new(
            format!("{}.{} @> ?", self.table_name, self.column_name), 
            vec![values]
        )
    }

    /// Check whether this column's array does not overlap
    /// with another array, i.e. contains none of its values.
    pub fn contains_none(&self, values: &'a Vec<&'a T>) -> Where<'a> {
        self.contains_any(values).not()
    }
}

impl<T: ToSql + Sync> Deref for TypedColumn<T> {
    type Target = Column;

    fn deref(&self) -> &Self::Target {
        &self.column
    }
}

impl Column {
    /// Creates a new column
    const fn new(table_name: &'static str, column_name: &'static str) -> Column {
        Column {
            column_name,
            table_name,
            nullable: false,
            unique: false,
            primary_key: false,
            generated: false,
        }
    }

    impl_prop_col!(unique, nullable, primary_key, generated);

    /// Get the column name.
    pub const fn column_name(&self) -> &'static str {
        self.column_name
    }

    /// Get the name of the table this column
    /// is part of.
    pub const fn table_name(&self) -> &'static str {
        self.table_name
    }

    /// Get the full name of the column.
    ///
    /// # Example
    ///
    /// ```
    /// use pg_worm::prelude::*;
    ///
    /// #[derive(Model)]
    /// struct Foo {
    ///     baz: String
    /// }
    /// assert_eq!(Foo::baz.full_name(), "foo.baz");
    /// ```
    #[inline]
    pub fn full_name(&self) -> String {
        format!("{}.{}", self.table_name, self.column_name)
    }
}

#[cfg(test)]
mod tests {
    #![allow(dead_code)]

    use crate::{
        prelude::*,
        query::{PushChunk, Where},
    };

    impl<'a> Where<'a> {
        /// This is a convieniance function for testing purposes.
        fn to_stmt(&mut self) -> String {
            let mut q = Query::<u64>::default();
            self.push_to_buffer(&mut q);

            q.0
        }
    }

    #[derive(Model)]
    struct Book {
        id: i64,
        title: String,
        pages: Vec<String>
    }

    #[test]
    fn equals() {
        assert_eq!(Book::title.eq(&"ABC".into()).to_stmt(), "book.title = ?")
    }

    #[test]
    fn greater_than() {
        assert_eq!(Book::id.gt(&1).to_stmt(), "book.id > ?");
    }

    #[test]
    fn greater_than_equals() {
        assert_eq!(Book::id.gte(&1).to_stmt(), "book.id >= ?");
    }

    #[test]
    fn less_than() {
        assert_eq!(Book::id.lt(&1).to_stmt(), "book.id < ?")
    }

    #[test]
    fn less_than_equals() {
        assert_eq!(Book::id.lte(&1).to_stmt(), "book.id <= ?")
    }

    #[test]
    fn complete_query() {
        let q = Book::select()
            .where_(Book::title.eq(&"The Communist Manifesto".into()))
            .where_(Book::pages.contains(&"You have nothing to lose but your chains!".into()))
            .where_(Book::id.gt(&3))
            .to_query().0;
        assert_eq!(q, "SELECT book.id, book.title, book.pages FROM book WHERE (book.title = $1) AND ($2 = ANY(book.pages)) AND (book.id > $3)");
    }
}