sequelite 0.2.3

A simple SQLite ORM for Rust
Documentation
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
use std::{marker::PhantomData, fmt::Debug, ops::{BitAnd, BitOr}};

use rusqlite::ToSql;

use crate::{connection::{Queryable, RawQuery, IntoInsertable, Insertable, Executable}, IntoSqlite};

use super::{Model, column::Column};

/// Just a marker type for count queries
pub struct CountQuery;

/// A trait for filtering queries
/// 
/// This allows you to filter, limit, offset, and order elements that you are querying.
/// 
/// # Example
/// ```rs
/// User::select()
///     .filter(User::id.eq(1) & User::name.like("%test%"))
///     .limit(1)
///     .order(User::id.desc())
///     .exec(&conn).unwrap();
/// ```
pub struct ModelQuery<M> {
    model: PhantomData<M>,
    table_name: String,
    query: String,
    joins: Vec<String>,
    params: Vec<Box<dyn ToSql>>,
}

impl<M: Model> Debug for ModelQuery<M> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ModelQuery")
            .field("query", &self.query)
            .finish()
    }
}

impl<M> Default for ModelQuery<M> {
    fn default() -> Self {
        Self {
            model: Default::default(),
            table_name: "unknown".to_string(),
            query: String::new(),
            joins: Vec::new(),
            params: Vec::new(),
        }
    }
}

// Only tables can be queried
impl<M: Model> ModelQuery<M> {
    // ====< Constructors >====
    pub fn select() -> Self {
        let query = format!("SELECT * FROM {}", M::table_name());
        ModelQuery {
            model: PhantomData,
            table_name: M::table_name().to_string(),
            query,
            ..Default::default()
        }
    }

    pub fn count() -> ModelQuery<CountQuery> {
        let query = format!("SELECT COUNT(*) FROM {}", M::table_name());
        ModelQuery {
            model: PhantomData,
            table_name: M::table_name().to_string(),
            query,
            ..Default::default()
        }
    }
}

// Every ModelQuery is a Queryable
impl<M> ModelQuery<M> {

    // ====< Utils >====
    pub fn combine(self, query: String, params: Vec<Box<dyn ToSql>>) -> Self {
        let mut params_old = self.params;
        params_old.extend(params);
        ModelQuery {
            model: PhantomData,
            table_name: self.table_name,
            query: format!("{} {}", self.query, query),
            joins: self.joins,
            params: params_old,
        }
    }

    // ====< Additional Methods >====
    /// Filter the query with the given filter
    /// 
    /// ## Arguments
    /// * `filter` - The filter to apply to the query
    /// 
    /// ## Example
    /// ```rs
    /// let user = User::select()
    ///     .filter(User::id.eq(1) & User::name.like("%test%"))
    ///     .exec(&conn).unwrap();
    /// ```
    pub fn filter(self, mut filter: impl ModelQueryFilter) -> Self {
        let filter_query = filter.get_query();
        ModelQuery::combine(self, format!("WHERE {}", filter_query.sql), filter_query.params)
    }

    /// Select element by id
    /// 
    /// ## Arguments
    /// * `id` - The id of the element to select
    /// 
    /// ## Example
    /// ```rs
    /// let user = User::select()
    ///     .with_id(1)
    ///     .exec(&conn).unwrap();
    /// ```
    /// 
    /// ## Note
    /// This is equivalent to `.filter(User::id.eq(id)).limit(1)` and should not be combined with other filters or limits.
    pub fn with_id(self, id: i64) -> Self {
        let table_name = self.table_name.clone();
        ModelQuery::combine(self, format!("WHERE {}.id = ? LIMIT 1", table_name), vec![Box::new(id)])
    }

    /// Limit the number of elements returned
    /// 
    /// ## Arguments
    /// * `limit` - The maximum number of elements to return
    /// 
    /// ## Example
    /// ```rs
    /// let users = User::select()
    ///     .limit(10)
    ///     .exec(&conn).unwrap();
    /// ```
    pub fn limit(self, limit: u32) -> Self {
        ModelQuery::combine(self, "LIMIT ?".to_string(), vec![Box::new(limit)])
    }

    /// Offset selection by the given number of elements
    /// 
    /// ## Arguments
    /// * `offset` - The number of elements to skip
    /// 
    /// ## Example
    /// ```rs
    /// let users = User::select()
    ///     .offset(10)
    ///     .exec(&conn).unwrap();
    /// ```
    pub fn offset(self, offset: u32) -> Self {
        ModelQuery::combine(self, "OFFSET ?".to_string(), vec![Box::new(offset)])
    }

    /// Order the elements by the given order
    /// 
    /// ## Arguments
    /// * `order` - The order to apply to the elements
    /// 
    /// ## Example
    /// ```rs
    /// let users = User::select()
    ///     .order(User::id.desc())
    ///     .exec(&conn).unwrap();
    /// ```
    pub fn order_by(self, order: ColumnQueryOrder) -> Self {
        ModelQuery::combine(self, format!("ORDER BY {}", order.into_sqlite()), Vec::new())
    }

    /// **WARNING:** This is highly experimental and may not work as expected
    /// Use Relation::get() or Relation::take() instead
    /// 
    /// ## Arguments
    /// * `relation` - The relation to join
    /// 
    /// ## Example
    /// ```rs
    /// let users = Post::select()
    ///     .join_relation(Post::author)
    ///     .exec(&conn).unwrap();
    /// ```
    pub fn join_relation(mut self, relation: Column<'static>) -> Self {
        // Ensure that the relation is a relation
        match relation.get_relation() {
            Some(relation) => {
                // Left join the relation table
                let query = format!("{} LEFT JOIN {} ON {}.{} = {}.{}", self.query, relation.table, relation.table, relation.foreign_key_column.name_const(), relation.local_table, relation.local_key_column_name );

                self.joins.push(relation.local_key_column_name.to_string());
                // Add the relation to the joins
                ModelQuery {
                    model: PhantomData,
                    table_name: self.table_name,
                    query,
                    joins: self.joins,
                    params: self.params,
                }
            },
            None => panic!("Cannot join a non-relation column"),
        }
    }

    /// Select only the given columns (do not use this if you want to map to a model column which is not an `Option<T>`)
    /// 
    /// ## Arguments
    /// * `columns` - The columns to select
    /// 
    /// ## Example
    /// ```rs
    /// let users = User::select()
    ///     .columns(&[User::id, User::name])
    ///     .exec(&conn).unwrap();
    /// ```
    pub fn columns(self, columns: &[Column<'static>]) -> Self {
        let columns = columns.iter().map(|c| c.name()).collect::<Vec<_>>().join(", ");
        // Replace first SELECT * with the given columns
        let query = self.query.replacen('*', &columns, 1);
        ModelQuery {
            model: PhantomData,
            table_name: self.table_name,
            query,
            joins: self.joins,
            params: self.params,
        }
    }
}

impl<M: Model> Queryable<Vec<M>> for ModelQuery<M> {
    fn get_query(&mut self) -> crate::connection::RawQuery {
        crate::connection::RawQuery::new(self.query.clone(), self.params.drain(..).collect())
    }

    fn parse_result(&mut self, rows: rusqlite::Rows) -> Vec<M> {
        M::parse_rows(rows, 0, &self.joins)
    }
}

impl<M: Model> Executable<Vec<M>> for ModelQuery<M> {
    fn exec(self, conn: &crate::prelude::Connection) -> Result<Vec<M>, rusqlite::Error> {
        conn.query(self)
    }
}

impl Queryable<usize> for ModelQuery<CountQuery> {
    fn get_query(&mut self) -> crate::connection::RawQuery {
        crate::connection::RawQuery::new(self.query.clone(), self.params.drain(..).collect())
    }

    fn parse_result(&mut self, mut rows: rusqlite::Rows) -> usize {
        rows.next().unwrap().unwrap().get(0).unwrap()
    }
}

impl Executable<usize> for ModelQuery<CountQuery> {
    fn exec(self, conn: &crate::prelude::Connection) -> Result<usize, rusqlite::Error> {
        conn.query(self)
    }
}

pub trait ModelQueryFilter {
    fn get_query(&mut self) -> crate::connection::RawQuery;
}

pub struct InQueryFilter {
    sql: RawQuery,
}

impl ModelQueryFilter for InQueryFilter {
    fn get_query(&mut self) -> RawQuery {
        self.sql.move_clone()
    }
}

pub struct ColumnQueryFilter {
    column: String,
    value: Option<Box<dyn ToSql>>,
    op: &'static str,
}

impl ModelQueryFilter for ColumnQueryFilter {
    fn get_query(&mut self) -> RawQuery {
        let sql = format!("{} {} ?", self.column, self.op);
        let params = vec![self.value.take().unwrap()];
        RawQuery::new(sql, params)
    }
}

pub struct ColumnQueryFilterUnary {
    column: String,
    op: &'static str,
}

impl ModelQueryFilter for ColumnQueryFilterUnary {
    fn get_query(&mut self) -> RawQuery {
        let sql = format!("{} {}", self.column, self.op);
        RawQuery::new(sql, Vec::new())
    }
}

macro_rules! trait_column_filter {
    ($fn:ident) => {
        fn $fn<V: ToSql + 'static>(self, value: V) -> ColumnQueryFilter;
    };
}

macro_rules! impl_column_filter {
    ($fn:ident, $op:literal, $doc:expr) => {
        #[doc = $doc]
        fn $fn<V: ToSql + 'static>(self, value: V) -> ColumnQueryFilter {
            ColumnQueryFilter {
                column: format!("{}.{}", self.table_name, self.name()),
                op: $op,
                value: Some(Box::new(value)),
            }
        }
    };

    ($fn:ident, $op:literal) => {
        impl_column_filter!($fn, $op, "There is no documentation for this filter");
    };
}

pub struct ColumnQueryOrder {
    column: String,
    order: ColumnQueryOrdering,
}

impl IntoSqlite for ColumnQueryOrder {
    fn into_sqlite(&self) -> String {
        format!("{} {}", self.column, self.order.into_sqlite())
    }
}

pub enum ColumnQueryOrdering {
    Ascending,
    Descending,
}

impl IntoSqlite for ColumnQueryOrdering {
    fn into_sqlite(&self) -> String {
        match self {
            ColumnQueryOrdering::Ascending => "ASC".to_string(),
            ColumnQueryOrdering::Descending => "DESC".to_string(),
        }
    }
}

pub trait ColumnQueryFilterImpl {
    trait_column_filter!(eq);
    trait_column_filter!(ne);
    trait_column_filter!(gt);
    trait_column_filter!(lt);
    trait_column_filter!(ge);
    trait_column_filter!(le);
    
    trait_column_filter!(like);
    trait_column_filter!(not_like);

    fn is_null(self) -> ColumnQueryFilterUnary;
    fn is_not_null(self) -> ColumnQueryFilterUnary;

    fn in_(self, values: impl ColumnInQuery) -> InQueryFilter;
    fn not_in(self, values: impl ColumnInQuery) -> InQueryFilter;

    fn asc(self) -> ColumnQueryOrder;
    fn desc(self) -> ColumnQueryOrder;
}

impl ColumnQueryFilterImpl for Column<'_> {
    impl_column_filter!(eq, "=", "
        Checks if the column is equal to the given value.
        ## Example
        ```rust
        User::select().filter(User::name.eq(\"John\")).exec(conn);
        ```
        This will generate the following SQL query:
        ```sql
        -- ? is a parameter
        SELECT * FROM users WHERE users.name = ?;
        ```
    ");
    impl_column_filter!(ne, "!=", "
        Checks if the column is not equal to the given value.
        ## Example
        ```rust
        User::select().filter(User::name.ne(\"John\")).exec(conn);
        ```
        This will generate the following SQL query:
        ```sql
        -- ? is a parameter
        SELECT * FROM users WHERE users.name != ?;
        ```
    ");
    impl_column_filter!(gt, ">", "
        Checks if the column is greater than the given value.
        ## Example
        ```rust
        User::select().filter(User::age.gt(18)).exec(conn);
        ```
        This will generate the following SQL query:
        ```sql
        -- ? is a parameter
        SELECT * FROM users WHERE users.age > ?;
        ```
    ");
    impl_column_filter!(lt, "<", "
        Checks if the column is less than the given value.
        ## Example
        ```rust
        User::select().filter(User::age.lt(18)).exec(conn);
        ```
        This will generate the following SQL query:
        ```sql
        -- ? is a parameter
        SELECT * FROM users WHERE users.age < ?;
        ```
    ");
    impl_column_filter!(ge, ">=", "
        Checks if the column is greater than or equal to the given value.
        ## Example
        ```rust
        User::select().filter(User::age.ge(18)).exec(conn);
        ```
        This will generate the following SQL query:
        ```sql
        -- ? is a parameter
        SELECT * FROM users WHERE users.age >= ?;
        ```
    ");
    impl_column_filter!(le, "<=", "
        Checks if the column is less than or equal to the given value.
        ## Example
        ```rust
        User::select().filter(User::age.le(18)).exec(conn);
        ```
        This will generate the following SQL query:
        ```sql
        -- ? is a parameter
        SELECT * FROM users WHERE users.age <= ?;
        ```
    ");

    impl_column_filter!(like, "LIKE", "
        Checks if the column is like the given value.
        ## Example
        ```rust
        User::select().filter(User::name.like(\"%John%\")).exec(conn);
        ```
        This will generate the following SQL query:
        ```sql
        -- ? is a parameter
        SELECT * FROM users WHERE users.name LIKE ?;
        ```
    ");
    impl_column_filter!(not_like, "NOT LIKE", "
        Checks if the column is not like the given value.
        ## Example
        ```rust
        User::select().filter(User::name.not_like(\"%John%\")).exec(conn);
        ```
        This will generate the following SQL query:
        ```sql
        SELECT * FROM users WHERE users.name NOT LIKE ?;
        ```
    ");

    /// Check if the column is null (only for nullable columns)
    /// ## Example
    /// ```rust
    /// User::select().filter(User::name.is_null()).exec(conn);
    /// ```
    /// This will generate the following SQL query:
    /// ```sql
    /// SELECT * FROM users WHERE users.name IS NULL;
    /// ```
    fn is_null(self) -> ColumnQueryFilterUnary {
        ColumnQueryFilterUnary {
            column: format!("{}.{}", self.table_name, self.name()),
            op: "IS NULL",
        }
    }

    /// Check if the column is not null (only for nullable columns)
    /// ## Example
    /// ```rust
    /// User::select().filter(User::name.is_not_null()).exec(conn);
    /// ```
    /// This will generate the following SQL query:
    /// ```sql
    /// SELECT * FROM users WHERE users.name IS NOT NULL;
    /// ```
    fn is_not_null(self) -> ColumnQueryFilterUnary {
        ColumnQueryFilterUnary {
            column: format!("{}.{}", self.table_name, self.name()),
            op: "IS NOT NULL",
        }
    }

    /// Check if the column is in the list of values
    /// ## Example
    /// ```rust
    /// User::select().filter(User::name.in_(vec!["John", "Jane"])).exec(conn);
    /// ```
    /// This will generate the following SQL query:
    /// ```sql
    /// -- ? is a parameter
    /// SELECT * FROM users WHERE users.name IN (?, ?);
    /// ```
    fn in_(self, values: impl ColumnInQuery) -> InQueryFilter {
        let q = values.to_query();
        let sql = format!("{}.{} IN {}", self.table_name, self.name(), q.sql);

        InQueryFilter { sql: RawQuery::new(sql, q.params) }
    }

    /// Check if the column is not in the list of values
    /// ## Example
    /// ```rust
    /// User::select().filter(User::name.not_in(vec!["John", "Jane"])).exec(conn);
    /// ```
    /// This will generate the following SQL query:
    /// ```sql
    /// -- ? is a parameter
    /// SELECT * FROM users WHERE users.name NOT IN (?, ?);
    /// ```
    fn not_in(self, values: impl ColumnInQuery) -> InQueryFilter {
        let q = values.to_query();
        let sql = format!("{}.{} NOT IN {}", self.table_name, self.name(), q.sql);

        InQueryFilter { sql: RawQuery::new(sql, q.params) }
    }

    /// Order the query by the column in ascending order
    /// ## Example
    /// ```rust
    /// User::select().order_by(User::name.asc()).exec(conn);
    /// ```
    /// This will generate the following SQL query:
    /// ```sql
    /// SELECT * FROM users ORDER BY users.name ASC;
    /// ```
    fn asc(self) -> ColumnQueryOrder {
        ColumnQueryOrder {
            column: self.name(),
            order: ColumnQueryOrdering::Ascending,
        }
    }

    /// Order the query by the column in descending order
    /// ## Example
    /// ```rust
    /// User::select().order_by(User::name.desc()).exec(conn);
    /// ```
    /// This will generate the following SQL query:
    /// ```sql
    /// SELECT * FROM users ORDER BY users.name DESC;
    /// ```
    fn desc(self) -> ColumnQueryOrder {
        ColumnQueryOrder {
            column: self.name(),
            order: ColumnQueryOrdering::Descending,
        }
    }
}

pub trait ColumnInQuery {
    fn to_query(self) -> RawQuery;
}

impl<M: Model> ColumnInQuery for ModelQuery<M> {
    fn to_query(mut self) -> RawQuery {
        let mut query = self.get_query();
        let sql = format!("({})", query.sql);
        query.sql = sql;
        query
    }
}

impl<T: ToSql + 'static> ColumnInQuery for Vec<T> {
    fn to_query(self) -> RawQuery {
        let mut params = Vec::new();
        let mut sql = String::from("(");

        for (i, v) in self.into_iter().enumerate() {
            if i > 0 {
                sql.push_str(", ");
            }

            sql.push('?');
            params.push(Box::new(v) as Box<dyn ToSql + 'static>);
        }

        sql.push(')');

        RawQuery::new(sql, params)
    }
}

impl<T: ToSql + 'static> ColumnInQuery for &'static [T] {
    fn to_query(self) -> RawQuery {
        let mut params = Vec::new();
        let mut sql = String::from("(");

        for (i, v) in self.iter().enumerate() {
            if i > 0 {
                sql.push_str(", ");
            }

            sql.push('?');
            params.push(Box::new(v) as Box<dyn ToSql + 'static>);
        }

        sql.push(')');

        RawQuery::new(sql, params)
    }
}
impl<T: ToSql + 'static, const N: usize> ColumnInQuery for &'static [T; N] {
    fn to_query(self) -> RawQuery {
        let mut params = Vec::new();
        let mut sql = String::from("(");

        for (i, v) in self.iter().enumerate() {
            if i > 0 {
                sql.push_str(", ");
            }

            sql.push('?');
            params.push(Box::new(v) as Box<dyn ToSql + 'static>);
        }

        sql.push(')');

        RawQuery::new(sql, params)
    }
}

pub trait ModelQueryFilterExt: ModelQueryFilter {
    fn and<F: ModelQueryFilter>(self, filter: F) -> ModelQueryFilterAnd<Self, F>
    where
        Self: Sized;

    fn or<F: ModelQueryFilter>(self, filter: F) -> ModelQueryFilterOr<Self, F>
    where
        Self: Sized;
}

impl<F: ModelQueryFilter> ModelQueryFilterExt for F {
    /// Combine two filters with an AND operator
    /// ## Example
    /// ```rust
    /// User::select().filter(User::name.eq("John").and(User::age.gt(18))).exec(conn);
    /// ```
    /// This will generate the following SQL query:
    /// ```sql
    /// SELECT * FROM users WHERE users.name = ? AND users.age > ?;
    /// ```
    /// 
    /// ## Note
    /// This is not a beautiful way to write this query, so you should use '&' instead:
    /// ```rust
    /// User::select().filter(User::name.eq("John") & User::age.gt(18)).exec(conn);
    /// ```
    fn and<F1: ModelQueryFilter>(self, filter: F1) -> ModelQueryFilterAnd<Self, F1>
    where
        Self: Sized,
    {
        ModelQueryFilterAnd {
            filter0: self,
            filter1: filter,
        }
    }

    /// Combine two filters with an OR operator
    /// ## Example
    /// ```rust
    /// User::select().filter(User::name.eq("John").or(User::age.gt(18))).exec(conn);
    /// ```
    /// This will generate the following SQL query:
    /// ```sql
    /// SELECT * FROM users WHERE users.name = ? OR users.age > ?;
    /// ```
    /// 
    /// ## Note
    /// This is not a beautiful way to write this query, so you should use '|' instead:
    /// ```rust
    /// User::select().filter(User::name.eq("John") | User::age.gt(18)).exec(conn);
    /// ```
    fn or<F1: ModelQueryFilter>(self, filter: F1) -> ModelQueryFilterOr<Self, F1>
    where
        Self: Sized,
    {
        ModelQueryFilterOr {
            filter0: self,
            filter1: filter,
        }
    }
}

pub struct ModelQueryFilterAnd<F0: ModelQueryFilter, F1: ModelQueryFilter> {
    filter0: F0,
    filter1: F1,
}

pub struct ModelQueryFilterOr<F0: ModelQueryFilter, F1: ModelQueryFilter> {
    filter0: F0,
    filter1: F1,
}

impl<F0: ModelQueryFilter, F1: ModelQueryFilter> ModelQueryFilter for ModelQueryFilterAnd<F0, F1> {
    fn get_query(&mut self) -> crate::connection::RawQuery {
        let mut query = self.filter0.get_query();
        let mut query1 = self.filter1.get_query();
        query.sql = format!("{} AND {}", query.sql, query1.sql);
        query.params.append(&mut query1.params);
        query
    }
}

impl<F0: ModelQueryFilter, F1: ModelQueryFilter> ModelQueryFilter for ModelQueryFilterOr<F0, F1> {
    fn get_query(&mut self) -> crate::connection::RawQuery {
        let mut query = self.filter0.get_query();
        let mut query1 = self.filter1.get_query();
        query.sql = format!("{} OR {}", query.sql, query1.sql);
        query.params.append(&mut query1.params);
        query
    }
}

macro_rules! impl_op {
    ($op:ident ($fn:ident), $target:ident => $result:ident, $doc:expr) => {
        impl<T: ModelQueryFilter> $op<T> for $target {
            type Output = $result<Self, T>;

            #[doc = $doc]
            fn $fn(self, rhs: T) -> Self::Output {
                $result {
                    filter0: self,
                    filter1: rhs,
                }
            }
        }
    };
}

impl_op!(BitAnd (bitand), ColumnQueryFilter => ModelQueryFilterAnd, "Alternative to [ModelQueryFilterExt::and]");
impl_op!(BitOr (bitor), ColumnQueryFilter => ModelQueryFilterOr, "Alternative to [ModelQueryFilterExt::or]");

impl_op!(BitAnd (bitand), InQueryFilter => ModelQueryFilterAnd, "Alternative to [ModelQueryFilterExt::and]");
impl_op!(BitOr (bitor), InQueryFilter => ModelQueryFilterOr, "Alternative to [ModelQueryFilterExt::or]");

impl_op!(BitAnd (bitand), ColumnQueryFilterUnary => ModelQueryFilterAnd, "Alternative to [ModelQueryFilterExt::and]");
impl_op!(BitOr (bitor), ColumnQueryFilterUnary => ModelQueryFilterOr, "Alternative to [ModelQueryFilterExt::or]");


pub struct ModelInsertQuery<M: Model> {
    model: PhantomData<M>,
    columns: Vec<String>,
    values: Vec<Vec<Box<dyn ToSql>>>,
}

impl<M: Model> Insertable for ModelInsertQuery<M> {
    fn get_query(&mut self) -> RawQuery {
        let mut sql = format!("INSERT INTO {} (", M::table_name());
        for column in &self.columns {
            sql.push_str(column);
            sql.push_str(", ");
        }
        sql.pop();
        sql.pop();
        sql.push_str(") VALUES ");

        let mut params = Vec::new();
        for values in &mut self.values {
            sql.push('(');
            for value in values.drain(..) {
                sql.push_str("?, ");
                params.push(value);
            }
            sql.pop();
            sql.pop();
            sql.push(')');
            sql.push(',');
        }
        sql.pop();

        RawQuery {
            sql,
            params,
        }
    }
}

impl<M: Model> IntoInsertable for M {
    type Insertable = ModelInsertQuery<M>;

    fn into_insertable(&self) -> ModelInsertQuery<M> {
        let mut columns = Vec::new();
        let mut values = Vec::new();
        for column in M::columns() {
            let cv = self.column_value(column);

            if !column.can_insert_null() && cv.is_none() {
                panic!("Column '{}' is not nullable", column.name());
            }

            if let Some(value) = cv {
                columns.push(column.name().to_string());
                values.push(value);
            }
        }

        ModelInsertQuery {
            model: PhantomData,
            columns,
            values: vec![values], // Only one row
        }
    }
}

impl<M: Model, const N: usize, I: IntoInsertable<Insertable = ModelInsertQuery<M>>> IntoInsertable for &[I; N] {
    type Insertable = ModelInsertQuery<M>;

    fn into_insertable(&self) -> Self::Insertable {
        let mut columns = Vec::new();
        let mut values = Vec::new();
        
        for v in self.iter() {
            let mut insertable = v.into_insertable();
           
            if columns.is_empty() {
                columns.append(&mut insertable.columns);
            } else {
                assert_eq!(columns, insertable.columns);
            }

            let v = insertable.values.pop().unwrap();
            values.push(v);
        }

        ModelInsertQuery {
            model: PhantomData,
            columns,
            values,
        }
    }
}

impl<M: Model, I: IntoInsertable<Insertable = ModelInsertQuery<M>>> IntoInsertable for &[I] {
    type Insertable = ModelInsertQuery<M>;

    fn into_insertable(&self) -> Self::Insertable {
        let mut columns = Vec::new();
        let mut values = Vec::new();
        
        for v in self.iter() {
            let mut insertable = v.into_insertable();
            
            if columns.is_empty() {
                columns.append(&mut insertable.columns);
            } else {
                assert_eq!(columns, insertable.columns);
            }

            let v = insertable.values.pop().unwrap();
            values.push(v);
        }

        ModelInsertQuery {
            model: PhantomData,
            columns,
            values,
        }
    }
}