gorm 0.1.4

An orm that is simple to use and prevents all mistakes at compile time
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
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
use std::{fmt::Write, marker::PhantomData};

use super::SqlStatement;
use crate::{
    sql::{
        Column, ColumnIsForeignKey, CombineSelectableTables, CombinedSelectableTables,
        FieldNameCharsConsListItem, FieldsConsListItem, ParameterBinder, SelectableTables,
        SelectedValues, SelectedValuesContainsFieldWithName, SqlBool, SqlExpression, SqlType,
        Table, TableHasOneForeignKey, TableMarker,
    },
    util::{TypedBool, TypedFalse, TypedTrue, TypesEqual},
};

/// Represents any type of sql select statement.
pub trait SelectStatement: SqlStatement {
    /// A type identifying the output fields of this select statement.
    type OutputFields: FieldsConsListItem;

    /// The source from which records are being selected.
    type SelectFrom: SelectFrom;

    /// The selected values in this select statement.
    ///
    /// For select statement which don't have custom selected values, this will
    /// be `()`, otherwise it will be some type implementing the
    /// [`SelectedValues`] trait.
    // This is used for allowing ordering by a value that was selected using
    // `select_values!`. In order to check that the value is actually in
    // the list of selected values we need a reference to the
    // `SelectedValues` because it implements the
    // `SelectedValuesContainsFieldWithName` trait which allows checking if it
    // contains a field with a given name.
    //
    // We can't put a constraint on this type to implement `SelectedValues`
    // because that would require adding a generic to the `SelectStatement`
    // trait, which will break some other stuff. Also, there won't be an
    // easy placeholder to use if we constrained it.
    //
    // So we just don't constrain it, and for types that don't have custom
    // values it will be `()` or whatever else, and for types that have
    // custom values it would implement `SelectedValues`, that way we could
    // implement something only when this implements `SelectedValues`.
    type SelectedValues;

    /// Does this select statement have custom selected values?
    type HasSelectedValues: TypedBool;

    /// Does this select statement have a `WHERE` clause?
    type HasWhereClause: TypedBool;

    /// Does this select statement have a `GROUP BY` clause?
    type HasGroupByClause: TypedBool;

    /// Does this select statement have a `ORDER BY` clause?
    type HasOrderByClause: TypedBool;

    /// Writes the custom selected values which are selected by this select
    /// statement.
    fn write_selected_values<'s, 'a>(
        &'s self,
        f: &mut String,
        parameter_binder: &mut ParameterBinder<'a>,
    ) -> std::fmt::Result
    where
        's: 'a;

    /// Writes the `WHERE` clause of this select statement.
    fn write_where_clause<'s, 'a>(
        &'s self,
        f: &mut String,
        parameter_binder: &mut ParameterBinder<'a>,
    ) -> std::fmt::Result
    where
        's: 'a;

    /// Writes the `GROUP BY` clause of this select statement.
    fn write_group_by_clause<'s, 'a>(
        &'s self,
        f: &mut String,
        parameter_binder: &mut ParameterBinder<'a>,
    ) -> std::fmt::Result
    where
        's: 'a;

    /// Writes the `ORDER BY` clause of this select statement.
    fn write_order_by_clause<'s, 'a>(
        &'s self,
        f: &mut String,
        parameter_binder: &mut ParameterBinder<'a>,
    ) -> std::fmt::Result
    where
        's: 'a;

    /// Writes this select statement as an sql string.
    fn write_sql_string<'s, 'a>(
        &'s self,
        f: &mut String,
        parameter_binder: &mut ParameterBinder<'a>,
    ) -> std::fmt::Result
    where
        's: 'a,
    {
        write!(f, "SELECT ")?;
        self.write_selected_values(f, parameter_binder)?;
        write!(f, " FROM ")?;
        <Self as SelectStatement>::SelectFrom::write_sql_from_string(f)?;
        self.write_where_clause(f, parameter_binder)?;
        self.write_group_by_clause(f, parameter_binder)?;
        self.write_order_by_clause(f, parameter_binder)
    }
}

/// Implements the [`SqlStatement`] trait for some type which implements
/// [`SelectStatement`]
macro_rules! impl_sql_statement_for_select_statement {
    {} => {
        type OutputFields = <Self as SelectStatement>::OutputFields;

        fn write_sql_string<'s, 'a>(
            &'s self,
            f: &mut String,
            parameter_binder: &mut ParameterBinder<'a>,
        ) -> std::fmt::Result
        where
            's: 'a,
        {
            <Self as SelectStatement>::write_sql_string(&self, f, parameter_binder)
        }
    };
}

/// An sql select statement which loads all records from some table.
///
/// This statement can be created by calling the [`SelectFrom::find`] function.
pub struct EmptySelectStatement<S: SelectFrom>(PhantomData<S>);
impl<S: SelectFrom> EmptySelectStatement<S> {
    pub fn new() -> Self {
        Self(PhantomData)
    }
}

impl<S: SelectFrom + 'static> SelectStatement for EmptySelectStatement<S> {
    type HasGroupByClause = TypedFalse;
    type HasOrderByClause = TypedFalse;
    type HasSelectedValues = TypedFalse;
    type HasWhereClause = TypedFalse;
    type OutputFields = <S::LeftMostTable as Table>::Fields;
    type SelectFrom = S;
    type SelectedValues = ();

    fn write_selected_values<'s, 'a>(
        &'s self,
        f: &mut String,
        _parameter_binder: &mut ParameterBinder<'a>,
    ) -> std::fmt::Result
    where
        's: 'a,
    {
        write!(f, "*")
    }

    fn write_where_clause<'s, 'a>(
        &'s self,
        _f: &mut String,
        _parameter_binder: &mut ParameterBinder<'a>,
    ) -> std::fmt::Result
    where
        's: 'a,
    {
        Ok(())
    }

    fn write_group_by_clause<'s, 'a>(
        &'s self,
        _f: &mut String,
        _parameter_binder: &mut ParameterBinder<'a>,
    ) -> std::fmt::Result
    where
        's: 'a,
    {
        Ok(())
    }

    fn write_order_by_clause<'s, 'a>(
        &'s self,
        _f: &mut String,
        _parameter_binder: &mut ParameterBinder<'a>,
    ) -> std::fmt::Result
    where
        's: 'a,
    {
        Ok(())
    }
}

impl<S: SelectFrom + 'static> SqlStatement for EmptySelectStatement<S> {
    impl_sql_statement_for_select_statement! {}
}

/// A wrapper around an sql select statement which selects custom values from
/// it. for it.
///
/// This wrapper shouldn't be used directly, you should instead use the
/// [`SelectValues::select`] function.
pub struct WithSelectedValues<
    S: SelectFrom,
    T: SelectStatement<HasSelectedValues = TypedFalse>,
    V: SelectedValues<S::SelectableTables>,
> {
    statement: T,
    values: V,
    phantom: PhantomData<S>,
}

impl<
    S: SelectFrom + 'static,
    T: SelectStatement<HasSelectedValues = TypedFalse>,
    V: SelectedValues<S::SelectableTables> + 'static,
> SelectStatement for WithSelectedValues<S, T, V>
{
    type HasGroupByClause = T::HasGroupByClause;
    type HasOrderByClause = T::HasOrderByClause;
    type HasSelectedValues = TypedTrue;
    type HasWhereClause = T::HasWhereClause;
    type OutputFields = V::Fields;
    type SelectFrom = S;
    type SelectedValues = V;

    fn write_selected_values<'s, 'a>(
        &'s self,
        f: &mut String,
        parameter_binder: &mut ParameterBinder<'a>,
    ) -> std::fmt::Result
    where
        's: 'a,
    {
        self.values.write_sql_string(f, parameter_binder)
    }

    fn write_where_clause<'s, 'a>(
        &'s self,
        f: &mut String,
        parameter_binder: &mut ParameterBinder<'a>,
    ) -> std::fmt::Result
    where
        's: 'a,
    {
        self.statement.write_where_clause(f, parameter_binder)
    }

    fn write_group_by_clause<'s, 'a>(
        &'s self,
        f: &mut String,
        parameter_binder: &mut ParameterBinder<'a>,
    ) -> std::fmt::Result
    where
        's: 'a,
    {
        self.statement.write_group_by_clause(f, parameter_binder)
    }

    fn write_order_by_clause<'s, 'a>(
        &'s self,
        f: &mut String,
        parameter_binder: &mut ParameterBinder<'a>,
    ) -> std::fmt::Result
    where
        's: 'a,
    {
        self.statement.write_order_by_clause(f, parameter_binder)
    }
}

impl<
    S: SelectFrom + 'static,
    T: SelectStatement<HasSelectedValues = TypedFalse>,
    V: SelectedValues<S::SelectableTables> + 'static,
> SqlStatement for WithSelectedValues<S, T, V>
{
    impl_sql_statement_for_select_statement! {}
}

/// A trait which allows selecting custom values from an sql select statement.
pub trait SelectValues: SelectStatement<HasSelectedValues = TypedFalse> {
    /// Selects custom values from this select statement. To provide a list of
    /// selected values use the [`select_values!`] macro.
    ///
    /// [`select_values!`]: crate::select_values
    fn select<V: SelectedValues<<Self::SelectFrom as SelectFrom>::SelectableTables>>(
        self,
        values: V,
    ) -> WithSelectedValues<Self::SelectFrom, Self, V> {
        WithSelectedValues {
            statement: self,
            values,
            phantom: PhantomData,
        }
    }
}
impl<T: SelectStatement<HasSelectedValues = TypedFalse>> SelectValues for T {}

/// A wrapper around an sql select statement which adds a `WHERE` clause to it.
///
/// This wrapper shouldn't be used directly, you should instead use the
/// [`Filter::filter`] function.
pub struct WithWhereClause<
    S: SelectFrom,
    T: SelectStatement<HasWhereClause = TypedFalse>,
    C: SqlExpression<S::SelectableTables, SqlType = SqlBool>,
> {
    statement: T,
    condition: C,
    phantom: PhantomData<S>,
}
impl<
    S: SelectFrom + 'static,
    T: SelectStatement<HasWhereClause = TypedFalse>,
    C: SqlExpression<S::SelectableTables, SqlType = SqlBool> + 'static,
> SelectStatement for WithWhereClause<S, T, C>
{
    type HasGroupByClause = T::HasGroupByClause;
    type HasOrderByClause = T::HasOrderByClause;
    type HasSelectedValues = T::HasSelectedValues;
    type HasWhereClause = TypedTrue;
    type OutputFields = <T as SelectStatement>::OutputFields;
    type SelectFrom = S;
    type SelectedValues = T::SelectedValues;

    fn write_where_clause<'s, 'a>(
        &'s self,
        f: &mut String,
        parameter_binder: &mut ParameterBinder<'a>,
    ) -> std::fmt::Result
    where
        's: 'a,
    {
        write!(f, " WHERE ")?;
        self.condition.write_sql_string(f, parameter_binder)
    }

    fn write_selected_values<'s, 'a>(
        &'s self,
        f: &mut String,
        parameter_binder: &mut ParameterBinder<'a>,
    ) -> std::fmt::Result
    where
        's: 'a,
    {
        self.statement.write_selected_values(f, parameter_binder)
    }

    fn write_group_by_clause<'s, 'a>(
        &'s self,
        f: &mut String,
        parameter_binder: &mut ParameterBinder<'a>,
    ) -> std::fmt::Result
    where
        's: 'a,
    {
        self.statement.write_group_by_clause(f, parameter_binder)
    }

    fn write_order_by_clause<'s, 'a>(
        &'s self,
        f: &mut String,
        parameter_binder: &mut ParameterBinder<'a>,
    ) -> std::fmt::Result
    where
        's: 'a,
    {
        self.statement.write_order_by_clause(f, parameter_binder)
    }
}

impl<
    S: SelectFrom + 'static,
    T: SelectStatement<HasWhereClause = TypedFalse>,
    C: SqlExpression<S::SelectableTables, SqlType = SqlBool> + 'static,
> SqlStatement for WithWhereClause<S, T, C>
{
    impl_sql_statement_for_select_statement! {}
}

/// A trait which allows filtering a select statement so that it only selects
/// records matching some condition.
pub trait Filter: SelectStatement<HasWhereClause = TypedFalse> {
    /// Filters this select statement, so that it only returns records which
    /// match the given condition.
    fn filter<
        C: SqlExpression<<Self::SelectFrom as SelectFrom>::SelectableTables, SqlType = SqlBool>,
    >(
        self,
        condition: C,
    ) -> WithWhereClause<Self::SelectFrom, Self, C> {
        WithWhereClause {
            statement: self,
            condition,
            phantom: PhantomData,
        }
    }
}
impl<T: SelectStatement<HasWhereClause = TypedFalse>> Filter for T {}

/// A wrapper around an sql select statement which adds a `GROUP BY` clause to
/// it.
///
/// This wrapper shouldn't be used directly, you should instead use the
/// [`GroupBy::group_by`] function.
pub struct WithGroupByClause<
    S: SelectFrom,
    T: SelectStatement<HasGroupByClause = TypedFalse>,
    G: SqlExpression<S::SelectableTables>,
> {
    statement: T,
    group_by: G,
    phantom: PhantomData<S>,
}
impl<
    S: SelectFrom + 'static,
    T: SelectStatement<HasGroupByClause = TypedFalse>,
    G: SqlExpression<S::SelectableTables> + 'static,
> SelectStatement for WithGroupByClause<S, T, G>
{
    type HasGroupByClause = TypedTrue;
    type HasOrderByClause = T::HasOrderByClause;
    type HasSelectedValues = T::HasSelectedValues;
    type HasWhereClause = T::HasWhereClause;
    type OutputFields = <T as SelectStatement>::OutputFields;
    type SelectFrom = S;
    type SelectedValues = T::SelectedValues;

    fn write_group_by_clause<'s, 'a>(
        &'s self,
        f: &mut String,
        parameter_binder: &mut ParameterBinder<'a>,
    ) -> std::fmt::Result
    where
        's: 'a,
    {
        write!(f, " GROUP BY ")?;
        self.group_by.write_sql_string(f, parameter_binder)
    }

    fn write_selected_values<'s, 'a>(
        &'s self,
        f: &mut String,
        parameter_binder: &mut ParameterBinder<'a>,
    ) -> std::fmt::Result
    where
        's: 'a,
    {
        self.statement.write_selected_values(f, parameter_binder)
    }

    fn write_where_clause<'s, 'a>(
        &'s self,
        f: &mut String,
        parameter_binder: &mut ParameterBinder<'a>,
    ) -> std::fmt::Result
    where
        's: 'a,
    {
        self.statement.write_where_clause(f, parameter_binder)
    }

    fn write_order_by_clause<'s, 'a>(
        &'s self,
        f: &mut String,
        parameter_binder: &mut ParameterBinder<'a>,
    ) -> std::fmt::Result
    where
        's: 'a,
    {
        self.statement.write_order_by_clause(f, parameter_binder)
    }
}

impl<
    S: SelectFrom + 'static,
    T: SelectStatement<HasGroupByClause = TypedFalse>,
    G: SqlExpression<S::SelectableTables> + 'static,
> SqlStatement for WithGroupByClause<S, T, G>
{
    impl_sql_statement_for_select_statement! {}
}

/// A trait which allows grouping the results of a select statement which uses
/// aggregate functions by some expression.
pub trait GroupBy: SelectStatement<HasGroupByClause = TypedFalse> {
    /// Groups the results of this select statement which uses aggregate
    /// functions by the given expression.
    fn group_by<G: SqlExpression<<Self::SelectFrom as SelectFrom>::SelectableTables>>(
        self,
        group_by: G,
    ) -> WithGroupByClause<Self::SelectFrom, Self, G> {
        WithGroupByClause {
            statement: self,
            group_by,
            phantom: PhantomData,
        }
    }
}
impl<T: SelectStatement<HasGroupByClause = TypedFalse>> GroupBy for T {}

/// An ordering, used in an `ORDER BY` clause.
pub trait Ordering {
    /// The sql string which should be appended at the end of the `ORDER BY`
    /// clause for this ordering to be applied.
    const ORDER_STR: &'static str;
}

/// Order the results of a query in ascending order.
pub struct AscendingOrder;
impl Ordering for AscendingOrder {
    const ORDER_STR: &'static str = "";
}

/// Order the results of a query in descending order.
pub struct DescendingOrder;
impl Ordering for DescendingOrder {
    const ORDER_STR: &'static str = " DESC";
}

/// A wrapper around an sql select statement which adds an `ORDER BY` clause to
/// it and orders it by some expression.
///
/// This wrapper shouldn't be used directly, you should instead use the
/// [`OrderBy::order_by_ascending`] and [`OrderBy::order_by_descending`]
/// functions.
pub struct WithOrderByClause<
    S: SelectFrom,
    T: SelectStatement<HasOrderByClause = TypedFalse>,
    B: SqlExpression<S::SelectableTables>,
    O: Ordering,
> {
    statement: T,
    order_by: B,
    _phantom: (PhantomData<S>, PhantomData<O>),
}
impl<
    S: SelectFrom + 'static,
    T: SelectStatement<HasOrderByClause = TypedFalse>,
    B: SqlExpression<S::SelectableTables> + 'static,
    O: Ordering + 'static,
> SelectStatement for WithOrderByClause<S, T, B, O>
{
    type HasGroupByClause = T::HasGroupByClause;
    type HasOrderByClause = TypedTrue;
    type HasSelectedValues = T::HasSelectedValues;
    type HasWhereClause = T::HasWhereClause;
    type OutputFields = <T as SelectStatement>::OutputFields;
    type SelectFrom = S;
    type SelectedValues = T::SelectedValues;

    fn write_group_by_clause<'s, 'a>(
        &'s self,
        f: &mut String,
        parameter_binder: &mut ParameterBinder<'a>,
    ) -> std::fmt::Result
    where
        's: 'a,
    {
        self.statement.write_group_by_clause(f, parameter_binder)
    }

    fn write_selected_values<'s, 'a>(
        &'s self,
        f: &mut String,
        parameter_binder: &mut ParameterBinder<'a>,
    ) -> std::fmt::Result
    where
        's: 'a,
    {
        self.statement.write_selected_values(f, parameter_binder)
    }

    fn write_where_clause<'s, 'a>(
        &'s self,
        f: &mut String,
        parameter_binder: &mut ParameterBinder<'a>,
    ) -> std::fmt::Result
    where
        's: 'a,
    {
        self.statement.write_where_clause(f, parameter_binder)
    }

    fn write_order_by_clause<'s, 'a>(
        &'s self,
        f: &mut String,
        parameter_binder: &mut ParameterBinder<'a>,
    ) -> std::fmt::Result
    where
        's: 'a,
    {
        write!(f, " ORDER BY ")?;
        self.order_by.write_sql_string(f, parameter_binder)?;
        write!(f, "{}", O::ORDER_STR)
    }
}

impl<
    S: SelectFrom + 'static,
    T: SelectStatement<HasOrderByClause = TypedFalse>,
    B: SqlExpression<S::SelectableTables> + 'static,
    O: Ordering + 'static,
> SqlStatement for WithOrderByClause<S, T, B, O>
{
    impl_sql_statement_for_select_statement! {}
}

/// A trait which allows ordering the results of a select statement by some
/// expression in an ascending or descending order.
pub trait OrderBy: SelectStatement<HasOrderByClause = TypedFalse> {
    /// Orders the results of this select statement by the given expression in
    /// an ascending order.
    fn order_by_ascending<B: SqlExpression<<Self::SelectFrom as SelectFrom>::SelectableTables>>(
        self,
        order_by: B,
    ) -> WithOrderByClause<Self::SelectFrom, Self, B, AscendingOrder> {
        WithOrderByClause {
            statement: self,
            order_by,
            _phantom: (PhantomData, PhantomData),
        }
    }

    /// Orders the results of this select statement by the given expression in
    /// an descending order.
    fn order_by_descending<B: SqlExpression<<Self::SelectFrom as SelectFrom>::SelectableTables>>(
        self,
        order_by: B,
    ) -> WithOrderByClause<Self::SelectFrom, Self, B, DescendingOrder> {
        WithOrderByClause {
            statement: self,
            order_by,
            _phantom: (PhantomData, PhantomData),
        }
    }
}
impl<T: SelectStatement<HasOrderByClause = TypedFalse>> OrderBy for T {}

/// A selected value which is used to order by.
pub trait SelectedValueToOrderBy {
    /// A type used to identify the name of this selected value in the list of
    /// selected values.
    type Name: FieldNameCharsConsListItem;

    /// The name of this selected value as a string.
    const NAME_STR: &'static str;
}

/// A wrapper around an sql select statement which adds an `ORDER BY` clause to
/// it and orders it by some value which is in the list of custom selected
/// values.
///
/// This wrapper shouldn't be used directly, you should instead use the
/// [`OrderBySelectedValue::order_by_selected_value_ascending`] and
/// [`OrderBySelectedValue::order_by_selected_value_descending`]
/// functions.
pub struct WithOrderBySelectedValueClause<
    S: SelectFrom,
    T: SelectStatement<HasOrderByClause = TypedFalse>,
    B: SelectedValueToOrderBy,
    O: Ordering,
> {
    statement: T,
    _order_by: B,
    _phantom: (PhantomData<S>, PhantomData<O>),
}
impl<
    S: SelectFrom + 'static,
    T: SelectStatement<HasOrderByClause = TypedFalse>,
    B: SelectedValueToOrderBy + 'static,
    O: Ordering + 'static,
> SelectStatement for WithOrderBySelectedValueClause<S, T, B, O>
{
    type HasGroupByClause = T::HasGroupByClause;
    type HasOrderByClause = TypedTrue;
    type HasSelectedValues = T::HasSelectedValues;
    type HasWhereClause = T::HasWhereClause;
    type OutputFields = <T as SelectStatement>::OutputFields;
    type SelectFrom = S;
    type SelectedValues = T::SelectedValues;

    fn write_group_by_clause<'s, 'a>(
        &'s self,
        f: &mut String,
        parameter_binder: &mut ParameterBinder<'a>,
    ) -> std::fmt::Result
    where
        's: 'a,
    {
        self.statement.write_group_by_clause(f, parameter_binder)
    }

    fn write_selected_values<'s, 'a>(
        &'s self,
        f: &mut String,
        parameter_binder: &mut ParameterBinder<'a>,
    ) -> std::fmt::Result
    where
        's: 'a,
    {
        self.statement.write_selected_values(f, parameter_binder)
    }

    fn write_where_clause<'s, 'a>(
        &'s self,
        f: &mut String,
        parameter_binder: &mut ParameterBinder<'a>,
    ) -> std::fmt::Result
    where
        's: 'a,
    {
        self.statement.write_where_clause(f, parameter_binder)
    }

    fn write_order_by_clause<'s, 'a>(
        &'s self,
        f: &mut String,
        _parameter_binder: &mut ParameterBinder<'a>,
    ) -> std::fmt::Result
    where
        's: 'a,
    {
        write!(f, " ORDER BY {}{}", B::NAME_STR, O::ORDER_STR)
    }
}

impl<
    S: SelectFrom + 'static,
    T: SelectStatement<HasOrderByClause = TypedFalse>,
    B: SelectedValueToOrderBy + 'static,
    O: Ordering + 'static,
> SqlStatement for WithOrderBySelectedValueClause<S, T, B, O>
{
    impl_sql_statement_for_select_statement! {}
}

/// A trait which allows ordering the results of a select statement by some
/// value which is in the list of custom selected values of this statement in an
/// ascending or descending order.
pub trait OrderBySelectedValue<S: SelectableTables>:
    SelectStatement<HasOrderByClause = TypedFalse>
where
    Self::SelectedValues: SelectedValues<S>,
{
    /// Orders the results of this select statement in an ascending order by the
    /// given value from the list of custom selected values of this statment.
    /// To provide a value to this function you should use the
    /// [`selected_value_to_order_by!`] macro
    ///
    /// [`selected_value_to_order_by!`]: crate::selected_value_to_order_by
    fn order_by_selected_value_ascending<B: SelectedValueToOrderBy>(
        self,
        order_by: B,
    ) -> WithOrderBySelectedValueClause<Self::SelectFrom, Self, B, AscendingOrder>
    where
        Self::SelectedValues: SelectedValuesContainsFieldWithName<B::Name>,
    {
        WithOrderBySelectedValueClause {
            statement: self,
            _order_by: order_by,
            _phantom: (PhantomData, PhantomData),
        }
    }

    /// Orders the results of this select statement in an descending order by
    /// the given value from the list of custom selected values of this
    /// statment. To provide a value to this function you should use the
    /// [`selected_value_to_order_by!`] macro
    ///
    /// [`selected_value_to_order_by!`]: crate::selected_value_to_order_by
    fn order_by_selected_value_descending<B: SelectedValueToOrderBy>(
        self,
        order_by: B,
    ) -> WithOrderBySelectedValueClause<Self::SelectFrom, Self, B, DescendingOrder>
    where
        Self::SelectedValues: SelectedValuesContainsFieldWithName<B::Name>,
    {
        WithOrderBySelectedValueClause {
            statement: self,
            _order_by: order_by,
            _phantom: (PhantomData, PhantomData),
        }
    }
}
impl<S: SelectableTables, T: SelectStatement<HasOrderByClause = TypedFalse>> OrderBySelectedValue<S>
    for T
where
    T::SelectedValues: SelectedValues<S>,
{
}

/// Some source which you can select from.
///
/// This can be a table or multiple tables joined together.
pub trait SelectFrom: Sized {
    /// The tables from which columns can be selected in a statement which
    /// selects values from this source.
    type SelectableTables: SelectableTables;

    /// The left-most table of this source.
    /// This is the table whose values will be selected by default in cause of
    /// inner joined tables.
    type LeftMostTable: Table;

    /// Writes the `from` part of the sql query as an sql string.
    fn write_sql_from_string(f: &mut String) -> std::fmt::Result;

    /// Writes the `from` part of the sql query without its left part an sql
    /// string. For example for `T: Table` this will write an empty string
    /// (`""`),  and for `InnerJoin<T1: Table, T2: Table>` this will write
    /// `"INNER JOIN T2 ON .."`.
    fn write_sql_from_string_without_left(f: &mut String) -> std::fmt::Result;

    /// Creates a select statement which finds records in this source.
    fn find(self) -> EmptySelectStatement<Self> {
        EmptySelectStatement::new()
    }
}

// We can select records from a table.
impl<T: TableMarker> SelectFrom for T {
    type LeftMostTable = T::Table;
    type SelectableTables = T::Table;

    fn write_sql_from_string(f: &mut String) -> std::fmt::Result {
        write!(f, "\"{}\"", T::Table::TABLE_NAME)
    }

    fn write_sql_from_string_without_left(_f: &mut String) -> std::fmt::Result {
        Ok(())
    }
}

/// Represents the inner join of 2 selection sources.
pub struct InnerJoined<
    A: SelectFrom,
    B: SelectFrom,
    C: Column<Table = A::LeftMostTable> + ColumnIsForeignKey<B::LeftMostTable>,
>(PhantomData<A>, PhantomData<B>, PhantomData<C>)
where
    A::SelectableTables: CombineSelectableTables<B::SelectableTables>,
    (
        <<C as Column>::SqlType as SqlType>::NonNullSqlType,
        <<B::LeftMostTable as Table>::IdColumn as Column>::SqlType,
    ): TypesEqual;

impl<
    A: SelectFrom,
    B: SelectFrom,
    C: Column<Table = A::LeftMostTable> + ColumnIsForeignKey<B::LeftMostTable>,
> InnerJoined<A, B, C>
where
    A::SelectableTables: CombineSelectableTables<B::SelectableTables>,
    (
        <<C as Column>::SqlType as SqlType>::NonNullSqlType,
        <<B::LeftMostTable as Table>::IdColumn as Column>::SqlType,
    ): TypesEqual,
{
    /// Creates a new source which represents the inner join of 2 selection
    /// sources.
    pub fn new() -> Self {
        Self(PhantomData, PhantomData, PhantomData)
    }
}

// We can select from an inner joined source if there is a foreign key
// constraint using which we can join the 2 sources.
impl<
    A: SelectFrom,
    B: SelectFrom,
    C: Column<Table = A::LeftMostTable> + ColumnIsForeignKey<B::LeftMostTable>,
> SelectFrom for InnerJoined<A, B, C>
where
    A::SelectableTables: CombineSelectableTables<B::SelectableTables>,
    (
        <<C as Column>::SqlType as SqlType>::NonNullSqlType,
        <<B::LeftMostTable as Table>::IdColumn as Column>::SqlType,
    ): TypesEqual,
{
    type LeftMostTable = A::LeftMostTable;
    type SelectableTables = CombinedSelectableTables<A::SelectableTables, B::SelectableTables>;

    fn write_sql_from_string(f: &mut String) -> std::fmt::Result {
        A::write_sql_from_string(f)?;
        Self::write_sql_from_string_without_left(f)
    }

    fn write_sql_from_string_without_left(f: &mut String) -> std::fmt::Result {
        write!(
            f,
            " INNER JOIN \"{}\" ON \"{}\".\"{}\" = \"{}\".\"id\"",
            B::LeftMostTable::TABLE_NAME,
            A::LeftMostTable::TABLE_NAME,
            <C as Column>::COLUMN_NAME,
            B::LeftMostTable::TABLE_NAME
        )?;

        B::write_sql_from_string_without_left(f)
    }
}

/// A trait which allows inner joining 2 selection sources using a foreign key.
pub trait InnerJoinTrait: Sized + SelectFrom {
    /// Inner joins this selection source with another selection source, if this
    /// source has a foreign key to the other one.
    fn inner_join<S: SelectFrom>(self, _with: S) -> InnerJoined<Self, S, <Self::LeftMostTable as TableHasOneForeignKey<S::LeftMostTable>>::ForeignKeyColumn>
    where
        Self::LeftMostTable: TableHasOneForeignKey<S::LeftMostTable>,
        <Self as SelectFrom>::SelectableTables: CombineSelectableTables<<S as SelectFrom>::SelectableTables>,
        (<<<Self::LeftMostTable as TableHasOneForeignKey<S::LeftMostTable>>::ForeignKeyColumn as Column>::SqlType as SqlType>::NonNullSqlType, <<S::LeftMostTable as Table>::IdColumn as Column>::SqlType): TypesEqual
    {
        InnerJoined::new()
    }
}

impl<S: SelectFrom> InnerJoinTrait for S {}

/// A trait which allows inner joining 2 selection sources using a foreign keys
/// on a specific column.
pub trait InnerJoinOnTrait: Sized + SelectFrom {
    /// Inner joins this selection source with another selection source, if this
    /// source has a foreign key to the other one.
    fn inner_join_on_column<
        S: SelectFrom,
        C: Column<Table = Self::LeftMostTable> + ColumnIsForeignKey<S::LeftMostTable>,
    >(
        self,
        _column: C,
        _with: S,
    ) -> InnerJoined<Self, S, C>
    where
        <Self as SelectFrom>::SelectableTables:
            CombineSelectableTables<<S as SelectFrom>::SelectableTables>,
        (
            <<C as Column>::SqlType as SqlType>::NonNullSqlType,
            <<S::LeftMostTable as Table>::IdColumn as Column>::SqlType,
        ): TypesEqual,
    {
        InnerJoined::new()
    }
}

impl<S: SelectFrom> InnerJoinOnTrait for S {}