rwf 0.2.1

Framework for building web applications in the Rust programming language
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
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
//! Object-relational mapper (ORM), the **M** in MVC.
//!
//! See [documentation](https://levkk.github.io/rwf/models/) for detailed examples on how to use the ORM.
use crate::colors::MaybeColorize;
use crate::config::get_config;

use pool::ToConnectionRequest;
use std::time::{Duration, Instant};
use tracing::{error, info};

pub mod callbacks;
pub mod column;
pub mod error;
pub mod escape;
pub mod exists;
pub mod explain;
pub mod filter;
pub mod insert;
pub mod join;
pub mod limit;
pub mod lock;
pub mod migrations;
pub mod order_by;
pub mod picked;
pub mod placeholders;
pub mod pool;
pub mod prelude;
pub mod row;
pub mod select;
pub mod update;
pub mod value;

pub use column::{Column, Columns, ToColumn};
pub use error::Error;
pub use escape::Escape;
pub use exists::Exists;
pub use explain::Explain;
pub use filter::{Filter, WhereClause};
pub use insert::Insert;
pub use join::{Association, AssociationType, Join, Joined, Joins};
pub use limit::Limit;
pub use lock::Lock;
pub use migrations::{migrate, rollback, Migrations};
pub use order_by::{OrderBy, OrderColumn, ToOrderBy};
pub use picked::Picked;
pub use placeholders::Placeholders;
pub use pool::{get_connection, get_pool, start_transaction, Connection, ConnectionGuard, Pool};
pub use row::Row;
pub use select::Select;
pub use update::Update;
pub use value::{ToValue, Value};

/// Convert a PostgreSQL row to a Rust struct. Type conversions are handled by `tokio_postgres`. This only
/// creates a mapping between columns and struct fields.
///
/// This trait needs to be implemented for all structs that implement the [`Model`] trait.
///
///
/// # Example
///
/// ```
/// use rwf::model::{FromRow, Error};
///
/// #[derive(Clone)]
/// struct User {
///     id: i64,
///     email: String,
/// }
///
/// impl FromRow for User {
///     fn from_row(row: tokio_postgres::Row) -> Result<Self, Error> {
///         Ok(User {
///             id: row.try_get("id")?,
///             email: row.try_get("email")?
///         })
///     }
/// }
/// ```
pub trait FromRow: Clone + Send {
    /// Convert a [`tokio_postgres::Row`] to [`Self`].
    fn from_row(row: tokio_postgres::Row) -> Result<Self, Error>
    where
        Self: Sized;
}

/// Convert an entity to a valid SQL string.
///
/// This trait can be implemented for pretty much anything,
/// from a single table column to a multi-table join query.
/// It's the implementor's responsibility to make sure
/// all SQL is valid and user input is escaped to avoid SQL injection
/// attacks.
///
/// This trait is used internally by the ORM to generate SQL queries.
///
/// # Examples
///
/// #### Custom query
///
/// ```
/// use rwf::model::{ToSql, Escape};
///
/// struct SelectUser {
///     email: String,
/// }
///
/// impl ToSql for SelectUser {
///     fn to_sql(&self) -> String {
///         format!("SELECT * FROM users WHERE email = '{}'", self.email.escape())
///     }
/// }
///
/// let query = SelectUser { email: "test@test.com".into() }.to_sql();
/// assert_eq!(query, r"SELECT * FROM users WHERE email = 'test@test.com'");
/// ```
///
/// #### Equality check
///
/// ```
/// use rwf::model::{ToSql, Escape};
///
/// struct Equals {
///     column: String,
///     value: i64,
/// }
///
/// impl ToSql for Equals {
///     fn to_sql(&self) -> String {
///         format!("{} = {}", self.column.escape(), self.value)
///     }
/// }
///
/// let equals = Equals { column: "id".into(), value: 5 };
/// assert_eq!(equals.to_sql(), "id = 5");
/// ```
pub trait ToSql {
    /// Convert `self` into a valid SQL entity.
    fn to_sql(&self) -> String;
}

/// The query builder. It constructs the actual queries,
/// executes them against the database, and returns the results
/// converted into Rust types.
///
/// The query builder should not be instantiated directly. Use the [`Model`] trait instead,
/// implemented for your struct.
///
/// # Example
///
/// Let's define a struct implemeting the [`Model`] trait and use it as an example:
///
/// ```
/// # use rwf::macros::Model;
/// #[derive(Clone, Model)]
/// struct User {
///     id: Option<i64>,
///     email: String,
///     admin: bool,
/// }
/// ```
///
/// Calling any [`Model`] method, e.g., [`Model::all`], will return the query builder with the
/// specified Rust type:
///
/// ```
/// # use rwf::macros::Model;
/// # use rwf::model::{Model, Query, Select};
/// # #[derive(Clone, Debug, Model)]
/// # struct User {
/// #    id: Option<i64>,
/// #    email: String,
/// #    admin: bool,
/// # }
/// let query = User::all();
///
/// match query {
///     Query::Select(Select::<User> { table_name, .. }) => {
///         assert_eq!(table_name, User::table_name());
///     }
///     _ => unreachable!(),
/// }
/// ```
///
/// By itself the query builder object will not execute the query until [`Query::execute`] is called. This allows the
/// query builder to be passed around the code base and modified depending on the context.
///
/// ## Scopes
///
/// Since the query builder represents a unexecuted query, it can be saved for later re-use. This allows us to implement
/// "scopes", re-usable queries that can be modified or executed as-is. To make this more user-friendly, the [`Scope`]
/// type is aliased to [`Query`], for example:
///
/// ```
/// # use rwf::macros::Model;
/// # use rwf::model::{Model, Query, Select, Scope};
/// # #[derive(Clone, Debug, Model)]
/// # struct User {
/// #    id: Option<i64>,
/// #    email: String,
/// #    admin: bool,
/// # }
/// impl User {
///     fn admins() -> Scope<User> {
///         Self::filter("admin", true)
///     }
/// }
/// ```
#[derive(Debug, Clone)]
pub enum Query<T: FromRow + ?Sized = Row> {
    /// Represents a `SELECT` query.
    Select(Select<T>),
    /// Represents an `UPDATE` statement.
    Update(Update<T>),
    /// Represents an `INSERT` statement.
    Insert(Insert<T>),
    /// Implements [`Model::find_or_create_by`] by building a `SELECT` and an `INSERT` query.
    InsertIfNotExists {
        select: Select<T>,
        insert: Insert<T>,
        created: bool,
    },
    /// An arbitrary query.
    Raw {
        query: String,
        placeholders: Placeholders,
    },
    /// **WIP**: `SELECT` query with only specific columns.
    Picked(Picked<T>),
}

impl<T: FromRow> ToSql for Query<T> {
    fn to_sql(&self) -> String {
        use Query::*;

        match self {
            Select(select) => select.to_sql(),
            Raw { query, .. } => query.clone(),
            Update(update) => update.to_sql(),
            Insert(insert) => insert.to_sql(),
            InsertIfNotExists { select, insert, .. } => {
                format!("{}; {};", select.to_sql(), insert.to_sql())
            }
            Picked(picked) => picked.select.to_sql(),
        }
    }
}

impl<T: Model> Query<T> {
    /// Start a `SELECT` query for the given table. This method is mostly used internally.
    ///
    /// # Arguments
    ///
    /// * `table_name` - The name of the database table.
    ///
    /// # Example
    ///
    /// ```
    /// # use rwf::macros::Model;
    /// # use rwf::model::{Model, Query, Select, ToSql};
    /// # #[derive(Clone, Debug, Model)]
    /// # struct User {
    /// #    id: Option<i64>,
    /// #    email: String,
    /// #    admin: bool,
    /// # }
    /// let query = Query::<User>::select("users");
    /// assert_eq!(query.to_sql(), r#"SELECT * FROM "users""#);
    /// ```
    pub fn select(table_name: impl ToString) -> Self {
        Query::Select(Select::new(
            table_name.to_string().as_str(),
            &T::primary_key(),
        ))
    }

    /// Create a query that selects one row from the relation. The rows are not ordered and any row can be returned.
    ///
    /// # Example
    ///
    /// ```
    /// # use rwf::macros::Model;
    /// # use rwf::model::{Model, Query, Select, ToSql};
    /// # #[derive(Clone, Debug, Model)]
    /// # struct User {
    /// #    id: Option<i64>,
    /// #    email: String,
    /// #    admin: bool,
    /// # }
    /// let query = User::all().take_one();
    /// assert_eq!(query.to_sql(), r#"SELECT * FROM "users" LIMIT 1"#);
    /// ```
    pub fn take_one(self) -> Self {
        use Query::*;

        match self {
            Select(select) => Select(select.limit(1)),
            _ => self,
        }
    }

    /// Create a query that selects _n_ rows from the relation. The order of rows is determined by the database.
    ///
    /// # Example
    ///
    /// ```
    /// # use rwf::macros::Model;
    /// # use rwf::model::{Model, Query, Select, ToSql};
    /// # #[derive(Clone, Debug, Model)]
    /// # struct User {
    /// #    id: Option<i64>,
    /// #    email: String,
    /// #    admin: bool,
    /// # }
    /// let query = User::all().take_many(25);
    /// assert_eq!(query.to_sql(), r#"SELECT * FROM "users" LIMIT 25"#);
    /// ```
    pub fn take_many(self, n: i64) -> Self {
        use Query::*;

        match self {
            Select(select) => Select(select.limit(n)),
            _ => self,
        }
    }

    /// Create a query that selects the first row from the database. The rows are ordered
    ///  by primary key in ascending order.
    ///
    /// # Example
    ///
    /// ```
    /// # use rwf::macros::Model;
    /// # use rwf::model::{Model, Query, Select, ToSql};
    /// # #[derive(Clone, Debug, Model)]
    /// # struct User {
    /// #    id: Option<i64>,
    /// #    email: String,
    /// #    admin: bool,
    /// # }
    /// let query = User::all().first_one();
    /// assert_eq!(query.to_sql(), r#"SELECT * FROM "users" ORDER BY "users"."id" ASC LIMIT 1"#);
    /// ```
    pub fn first_one(self) -> Self {
        use Query::*;

        match self {
            Select(_) => self.first_many(1),
            _ => self,
        }
    }

    /// Creates a query that selects first _n_ rows from the database. Rows are sorted
    /// by primary key in ascending order.
    ///
    /// # Example
    ///
    /// ```
    /// # use rwf::macros::Model;
    /// # use rwf::model::{Model, Query, Select, ToSql};
    /// # #[derive(Clone, Debug, Model)]
    /// # struct User {
    /// #    id: Option<i64>,
    /// #    email: String,
    /// #    admin: bool,
    /// # }
    /// let query = User::all().first_many(25);
    /// assert_eq!(query.to_sql(), r#"SELECT * FROM "users" ORDER BY "users"."id" ASC LIMIT 25"#);
    /// ```
    pub fn first_many(self, n: i64) -> Self {
        use Query::*;

        match self {
            Select(select) => {
                let table_name = select.table_name.clone();
                let order_by = if select.order_by.is_empty() {
                    OrderBy::asc(Column::new(table_name.as_str(), &select.primary_key))
                } else {
                    select.order_by.clone()
                };

                Select(select.limit(n).order_by(order_by))
            }

            _ => self,
        }
    }

    pub fn filter(self, column: impl ToColumn, value: impl ToValue) -> Self {
        use Query::*;

        match self {
            Select(select) => Select(select.filter_and(column, value)),
            _ => self,
        }
    }

    pub fn filter_gt(self, column: impl ToColumn, value: impl ToValue) -> Self {
        use Query::*;
        match self {
            Select(select) => Select(select.filter_gt(column, value)),
            _ => self,
        }
    }

    pub fn filter_gte(self, column: impl ToColumn, value: impl ToValue) -> Self {
        use Query::*;
        match self {
            Select(select) => Select(select.filter_gte(column, value)),
            _ => self,
        }
    }

    pub fn filter_lt(self, column: impl ToColumn, value: impl ToValue) -> Self {
        use Query::*;
        match self {
            Select(select) => Select(select.filter_lt(column, value)),
            _ => self,
        }
    }

    pub fn filter_lte(self, column: impl ToColumn, value: impl ToValue) -> Self {
        use Query::*;
        match self {
            Select(select) => Select(select.filter_lte(column, value)),
            _ => self,
        }
    }

    pub fn or(self, f: fn(Self) -> Self) -> Self {
        use Query::*;
        match self {
            Select(mut select) => {
                let or = select.or();
                let query = f(Select(or));
                match query {
                    Select(or) => {
                        select.where_clause.or(or.where_clause.filter());
                        select.placeholders = or.placeholders;
                        Select(select)
                    }

                    _ => Select(select),
                }
            }
            _ => self,
        }
    }

    pub fn filter_not(self, column: impl ToColumn, value: impl ToValue) -> Self {
        self.not(column, value)
    }

    pub fn not(self, column: impl ToColumn, value: impl ToValue) -> Self {
        use Query::*;

        match self {
            Select(select) => Select(select.filter_not(column, value)),
            _ => self,
        }
    }

    pub fn or_not(self, column: impl ToColumn, value: impl ToValue) -> Self {
        use Query::*;

        match self {
            Select(select) => Select(select.filter_or_not(column, value)),
            _ => self,
        }
    }

    pub fn find_by(mut self, column: impl ToColumn, value: impl ToValue) -> Self {
        use Query::*;

        if let Select(select::Select {
            ref mut where_clause,
            ..
        }) = self
        {
            where_clause.clear();
        }

        self.filter(column, value).take_one()
    }

    pub fn limit(self, limit: i64) -> Self {
        self.take_many(limit)
    }

    pub fn offset(self, offset: i64) -> Self {
        if let Query::Select(select) = self {
            Query::Select(select.offset(offset))
        } else {
            self
        }
    }

    pub fn order(self, order: impl ToOrderBy) -> Self {
        if let Query::Select(mut select) = self {
            select.order_by = select.order_by + order.to_order_by();
            Query::Select(select)
        } else {
            self
        }
    }

    /// Join this relation with another relation directly related to it, either
    /// through a foreign key.
    ///
    /// Joined relation must implement [`Association`] for current relation.
    ///
    /// # Example
    ///
    /// ```
    /// use rwf::{model::{Association, Model}, macros::Model};
    ///
    /// #[derive(Clone, Default, Model)]
    /// struct User {}
    ///
    /// #[derive(Clone, Default, Model)]
    /// struct Order {}
    ///
    /// impl Association<Order> for User {}
    /// ```
    pub fn join<F: Association<T>>(self) -> Self {
        match self {
            Query::Select(select) => Query::Select(select.join(F::construct_join())),
            _ => self,
        }
    }

    pub fn join_nested<F: Association<T>, G: Model>(self, joined: Joined<F, G>) -> Self {
        match self {
            Query::Select(select) => Query::Select(select.add_joins(joined.into())),
            _ => self,
        }
    }

    pub fn lock(self) -> Self {
        match self {
            Query::Select(select) => Query::Select(select.lock()),
            _ => self,
        }
    }

    pub fn skip_locked(self) -> Self {
        match self {
            Query::Select(select) => Query::Select(select.skip_locked()),
            _ => self,
        }
    }

    pub fn find_or_create(self) -> Self {
        match self {
            Query::Select(select) => {
                let (columns, values) = select.insert_columns();
                let insert = Insert::from_columns(&columns, &values);
                Query::InsertIfNotExists {
                    select: select.limit(1),
                    insert,
                    created: false,
                }
            }
            _ => self,
        }
    }

    pub fn column(self, column: impl ToColumn) -> Self {
        match self {
            Query::Select(select) => {
                let select = select.select_additional(column);
                Query::Select(select)
            }

            _ => self,
        }
    }

    pub fn update_all(self, attributes: &[(impl ToColumn, impl ToValue)]) -> Self {
        match self {
            Query::Select(select) => {
                let update = Update::<T>::from(select);
                let columns = attributes
                    .iter()
                    .map(|(c, _)| c.to_column())
                    .collect::<Vec<_>>();
                let values = attributes
                    .iter()
                    .map(|(_, v)| v.to_value())
                    .collect::<Vec<_>>();
                Query::Update(update.columns(&columns, &values))
            }
            _ => self,
        }
    }

    pub fn unique_by(self, columns: &[impl ToColumn]) -> Self {
        match self {
            Query::Insert(insert) => Query::Insert(insert.unique_by(columns)),
            Query::InsertIfNotExists {
                select,
                insert,
                created,
            } => {
                let insert = insert.unique_by(columns);
                Query::InsertIfNotExists {
                    select,
                    insert,
                    created,
                }
            }
            _ => self,
        }
    }

    async fn execute_internal(
        &self,
        client: impl ToConnectionRequest<'_>,
    ) -> Result<Vec<tokio_postgres::Row>, Error> {
        let request = client.to_connection_request()?;
        let mut conn = request.get().await?;

        let client = match request.connection() {
            Some(conn) => conn,
            None => conn.as_mut().unwrap(),
        };

        let result = match self {
            Query::Select(select) => {
                let query = self.to_sql();
                let placeholdres = { select.placeholders() };
                let values = placeholdres.values();
                client.query_cached(&query, &values).await
            }

            Query::Raw {
                query,
                placeholders,
            } => {
                let values = placeholders.values();
                client.query_cached(query, &values).await
            }

            Query::Update(update) => {
                let query = self.to_sql();
                let values = update.placeholders.values();
                client.query_cached(&query, &values).await
            }

            Query::Insert(insert) => {
                let query = self.to_sql();
                let values = insert.placeholders.values();
                client.query_cached(&query, &values).await
            }

            Query::InsertIfNotExists { select, insert, .. } => {
                let query = select.to_sql();
                let values = select.placeholders().values();
                let result = client.query_cached(&query, &values).await;

                let result = match result {
                    Ok(result) => result,
                    Err(err) => {
                        self.log_error(&err);
                        return Err(err);
                    }
                };

                if result.is_empty() {
                    let query = insert.to_sql();
                    let values = insert.placeholders.values();
                    client.query_cached(&query, &values).await
                } else {
                    Ok(result)
                }
            }

            Query::Picked(picked) => {
                let select = &picked.select;
                let query = select.to_sql();
                let placeholdres = { select.placeholders() };
                let values = placeholdres.values();
                client.query_cached(&query, &values).await
            }
        };

        match result {
            Ok(rows) => Ok(rows),
            Err(err) => {
                self.log_error(&err);
                Err(err)
            }
        }
    }

    /// Execute the query and fetch the first row from the database.
    pub async fn fetch(self, conn: impl ToConnectionRequest<'_>) -> Result<T, Error> {
        match self.execute(conn).await?.first().cloned() {
            Some(row) => Ok(row),
            None => Err(Error::RecordNotFound),
        }
    }

    pub async fn fetch_optional(
        self,
        conn: impl ToConnectionRequest<'_>,
    ) -> Result<Option<T>, Error> {
        match self.fetch(conn).await {
            Ok(row) => Ok(Some(row)),
            Err(Error::RecordNotFound) => Ok(None),
            Err(err) => Err(err),
        }
    }

    /// Execute the query and fetch all rows from the database.
    pub async fn fetch_all(self, conn: impl ToConnectionRequest<'_>) -> Result<Vec<T>, Error> {
        self.execute(conn).await
    }

    /// Get the query plan from Postgres.
    ///
    /// Take the actual query, prepend `EXPLAIN` and execute.
    pub async fn explain(self, conn: impl ToConnectionRequest<'_>) -> Result<Explain, Error> {
        let query = format!("EXPLAIN {}", self.to_sql());
        let placeholders = match self {
            Query::Select(select) => select.placeholders,
            Query::Update(update) => update.placeholders,
            Query::Insert(insert) => insert.placeholders,
            Query::Picked(picked) => picked.select.placeholders,
            _ => todo!("explain"),
        };

        let query = Query::<Explain>::Raw {
            query,
            placeholders,
        };
        match query.execute_internal(conn).await?.pop() {
            Some(explain) => Ok(Explain::from_row(explain)?),
            None => Err(Error::RecordNotFound),
        }
    }

    pub async fn exists(self, conn: impl ToConnectionRequest<'_>) -> Result<bool, Error> {
        Ok(self.count(conn).await? > 0)
    }

    pub async fn count(self, conn: impl ToConnectionRequest<'_>) -> Result<i64, Error> {
        let query = match self {
            Query::Select(select) => Query::Select(select.exists()),
            _ => self,
        };
        let start = Instant::now();

        let result = match query.execute_internal(conn).await?.pop() {
            None => Ok(0),
            Some(exists) => Ok(Exists::from_row(exists)?.count),
        };

        query.log(start.elapsed());

        result
    }

    /// Execute a query and return an optional result.
    pub async fn execute(self, conn: impl ToConnectionRequest<'_>) -> Result<Vec<T>, Error> {
        let start = Instant::now();
        let mut results = vec![];
        let rows = self.execute_internal(conn).await?;
        for row in rows {
            results.push(T::from_row(row)?)
        }
        let time = start.elapsed();

        self.log(time);

        Ok(results)
    }

    fn type_name() -> String {
        std::any::type_name::<T>()
            .split("::")
            .skip(1)
            .collect::<Vec<_>>()
            .join("::")
    }

    fn action(&self) -> &'static str {
        match self {
            Query::Select(_) | Query::Picked(_) => "load",
            Query::Update(_) => "save",
            Query::Raw { .. } => "query",
            Query::Insert(_) => "save",
            Query::InsertIfNotExists { .. } => "load/create",
        }
    }

    fn log(&self, duration: Duration) {
        if !get_config().general.log_queries {
            return;
        }

        info!(
            "{} {} ({:.3} ms) {}",
            Self::type_name().green(),
            self.action().purple(),
            duration.as_secs_f64() * 1000.0,
            self.to_sql()
        );
    }

    fn log_error(&self, err: &Error) {
        error!(
            "{} {} {} {}",
            Self::type_name().green(),
            self.action().purple(),
            self.to_sql(),
            err,
        )
    }
}

pub type Scope<T> = Query<T>;

/// Implements Object-relational mapping (ORM) methods for a Rust struct, turning it into a database model. This trait doesn't have to be implemented manually. You can use the [`rwf_macros::Model`] macro instead, for example:
///
/// ```
/// # use rwf::prelude::*;
/// #[derive(Clone, macros::Model)]
/// struct User {
///     id: Option<i64>,
///     email: String,
/// }
/// ```
///
/// Structs that wish to implement this trait need to implement two additional traits:
///
/// - [`FromRow`]
/// - [`Clone`]
///
/// When usindg the [`rwf_macros::Model`] derive, [`FromRow`] is derived automatically.
pub trait Model: FromRow {
    /// Name of the PostgreSQL table where records for this model are stored.
    ///
    /// The name must not be fully qualified
    ///  e.g. `"users"` is correct, while `r#"public"."users"#` won't work.
    ///
    /// This method is implemented automatically by the [`rwf_macros::Model`] derive. If you wish to override
    /// that implementation, use `#[table_name("your_name")]` derive attribute.
    fn table_name() -> &'static str;

    /// Get the fully qualified column name for this table.
    ///
    /// # Example
    ///
    /// ```
    /// # use rwf::prelude::*;
    /// # #[derive(Clone, macros::Model)]
    /// # struct User {
    /// #    id: Option<i64>,
    /// #    email: String,
    /// # }
    /// let column = User::column("email");
    ///
    /// assert_eq!(column.to_string(), r#""users"."email""#);
    /// ```
    fn column(name: &str) -> Column {
        Column::new(Self::table_name(), name)
    }

    /// List of names of columns stored in the PostgreSQL table.
    ///
    /// Names must not be fully qualified or contain
    /// double quotes, e.g. `"id"` is correct, while `"users"."id"` won't work.
    ///
    /// This method is implemented automatically by the [`rwf_macros::Model`] derive.
    ///
    /// # Example
    /// ```
    /// fn column_names() -> &'static [&'static str] {
    ///     &["id", "email"]
    /// }
    /// ```
    fn column_names() -> &'static [&'static str];

    /// The primary key value, if one exists, for the instance of a model.
    ///
    /// Primary keys are not required to use the ORM, but are generally needed to perform
    /// updates and deletes. If the model doesn't have a primary key, you can return [`Value::Null`].
    ///
    /// This method is implemented automatically by the [`rwf_macros::Model`] derive.
    ///
    /// # Example
    ///
    /// ```
    /// # use rwf::prelude::*;
    /// # use rwf::model::Value;
    /// # use rwf::macros::FromRow;
    /// # #[derive(Clone, FromRow)]
    /// # struct User {
    /// #    id: Option<i64>,
    /// #    email: String,
    /// # }
    /// # impl Model for User {
    /// # fn table_name() -> &'static str { "" }
    /// # fn values(&self) -> Vec<Value> { vec![] }
    /// # fn column_names() -> &'static [&'static str] { &[] }
    /// # fn foreign_key() -> &'static str { "" }
    /// fn id(&self) -> Value {
    ///     self.id.to_value()
    /// }
    /// # }
    /// ```
    ///
    fn id(&self) -> Value;

    /// List of column values for a particular instance of a model. The values must be in the same
    /// order as the columns in [`Model::column_names`].
    ///
    /// The values are Rust types converted to the [`Value`] enum. This conversion is required so
    /// the ORM can then convert them to PostgreSQL types automatically. Most Rust types can be converted
    /// to [`Value`] automatically as well with [`ToValue::to_value`].
    ///
    /// This method is implemented automatically by the [`rwf_macros::Model`] derive.
    ///
    /// # Example
    ///
    /// ```
    /// # use rwf::prelude::*;
    /// # use rwf::model::Value;
    /// # use rwf::macros::FromRow;
    /// # #[derive(Clone, FromRow)]
    /// # struct User {
    /// #    id: Option<i64>,
    /// #    email: String,
    /// # }
    /// # impl Model for User {
    /// # fn table_name() -> &'static str { "" }
    /// # fn id(&self) -> Value { Value::Null }
    /// # fn column_names() -> &'static [&'static str] { &[] }
    /// # fn foreign_key() -> &'static str { "" }
    /// fn values(&self) -> Vec<Value> {
    ///     vec![
    ///         1_i64.to_value(),
    ///         "test@test.com".to_value(),
    ///     ]
    /// }
    /// # }
    /// ```
    fn values(&self) -> Vec<Value>;

    /// The name of a column in another PostgreSQL table which refers to this model.
    ///
    /// For example, if the table name for this model is `"users"`, this method could return `"user_id"`.
    ///
    /// This method is implemented automatically by the [`rwf_macros::Model`] derive. If you wish to override
    /// that implementation, use `#[foreign_key("your_fk")]` derive attribute.
    ///
    /// # Example
    ///
    /// ```
    /// fn foreign_key() -> &'static str {
    ///     "user_id"
    /// }
    /// ```
    fn foreign_key() -> &'static str;

    /// Name of the primary key column in the database.
    ///
    /// This is typically `"id"`, but can be any other column as long as it has a `UNIQUE NOT NULL` constraint
    /// and a default value produced from a sequence.
    fn primary_key() -> &'static str {
        "id"
    }

    /// Select one record from the table. The row returned is determined by the database.
    ///
    /// # Example
    /// ```
    /// # use rwf::prelude::*;
    /// # use rwf::model::ToSql;
    /// # #[derive(Clone, macros::Model)]
    /// # struct User {
    /// #    id: Option<i64>,
    /// #    email: String,
    /// # }
    /// let query = User::take_one();
    ///
    /// assert_eq!(query.to_sql(), r#"SELECT * FROM "users" LIMIT 1"#);
    /// ```
    fn take_one() -> Query<Self> {
        Query::select(Self::table_name()).take_one()
    }

    /// Select _n_ records from the table. The order of rows returned is determined by the database.
    ///
    /// # Example
    /// ```
    /// # use rwf::prelude::*;
    /// # use rwf::model::ToSql;
    /// # #[derive(Clone, macros::Model)]
    /// # struct User {
    /// #    id: Option<i64>,
    /// #    email: String,
    /// # }
    /// let query = User::take_many(25);
    ///
    /// assert_eq!(query.to_sql(), r#"SELECT * FROM "users" LIMIT 25"#);
    /// ```
    fn take_many(n: i64) -> Query<Self> {
        Query::select(Self::table_name()).take_many(n)
    }

    /// Select the first record from the table, ordered by primary key.
    ///
    /// # Example
    /// ```
    /// # use rwf::prelude::*;
    /// # use rwf::model::ToSql;
    /// # #[derive(Clone, macros::Model)]
    /// # struct User {
    /// #    id: Option<i64>,
    /// #    email: String,
    /// # }
    /// let query = User::first_one();
    ///
    /// assert_eq!(query.to_sql(), r#"SELECT * FROM "users" ORDER BY "users"."id" ASC LIMIT 1"#);
    /// ```
    fn first_one() -> Query<Self> {
        Query::select(Self::table_name()).first_one()
    }

    /// Select the first _n_ records from the table, ordered by primary key.
    ///
    /// # Example
    /// ```
    /// # use rwf::prelude::*;
    /// # use rwf::model::ToSql;
    /// # #[derive(Clone, macros::Model)]
    /// # struct User {
    /// #    id: Option<i64>,
    /// #    email: String,
    /// # }
    /// let query = User::first_many(25);
    ///
    /// assert_eq!(query.to_sql(), r#"SELECT * FROM "users" ORDER BY "users"."id" ASC LIMIT 25"#);
    /// ```
    fn first_many(n: i64) -> Query<Self> {
        Query::select(Self::table_name()).first_many(n)
    }

    /// Select all records from the table. Typically this is a starting point for filtering
    /// by some column(s), but all records can also be returned. Order of records returned is
    /// determined by the database, unless additional filters are specified.
    ///
    /// # Example
    /// ```
    /// # use rwf::prelude::*;
    /// # use rwf::model::ToSql;
    /// # #[derive(Clone, macros::Model)]
    /// # struct User {
    /// #    id: Option<i64>,
    /// #    email: String,
    /// # }
    /// let query = User::all();
    ///
    /// assert_eq!(query.to_sql(), r#"SELECT * FROM "users""#);
    /// ```
    fn all() -> Query<Self> {
        Query::select(Self::table_name())
    }

    /// Filter table records by a column. Multiple calls to filter can be chained to filter by multiple columns.
    ///
    /// # Example
    /// ```
    /// # use rwf::prelude::*;
    /// # use rwf::model::ToSql;
    /// # #[derive(Clone, macros::Model)]
    /// # struct User {
    /// #    id: Option<i64>,
    /// #    email: String,
    /// # }
    /// let query = User::filter("email", "test@test.com");
    ///
    /// assert_eq!(query.to_sql(), r#"SELECT * FROM "users" WHERE "users"."email" = $1"#);
    /// ```
    fn filter(column: impl ToColumn, value: impl ToValue) -> Query<Self> {
        Query::select(Self::table_name()).filter(column, value)
    }

    /// Filter table records by a column and fetch the first matching record. The record will match
    /// the filter, but if there are mulitple records that match, any one of them can be returned.
    ///
    /// # Example
    /// ```
    /// # use rwf::prelude::*;
    /// # use rwf::model::ToSql;
    /// # #[derive(Clone, macros::Model)]
    /// # struct User {
    /// #    id: Option<i64>,
    /// #    email: String,
    /// # }
    /// let query = User::find_by("email", "test@test.com");
    ///
    /// assert_eq!(query.to_sql(), r#"SELECT * FROM "users" WHERE "users"."email" = $1 LIMIT 1"#);
    /// ```
    fn find_by(column: impl ToColumn, value: impl ToValue) -> Query<Self> {
        Query::select(Self::table_name())
            .find_by(column, value.to_value())
            .take_one()
    }

    /// Filter by primary key and return the matching row, if any.
    ///
    /// # Example
    /// ```
    /// # use rwf::prelude::*;
    /// # use rwf::model::ToSql;
    /// # #[derive(Clone, macros::Model)]
    /// # struct User {
    /// #    id: Option<i64>,
    /// #    email: String,
    /// # }
    /// let query = User::find(1);
    ///
    /// assert_eq!(query.to_sql(), r#"SELECT * FROM "users" WHERE "users"."id" = $1 LIMIT 1"#);
    /// ```
    fn find(value: impl ToValue) -> Query<Self> {
        Query::select(Self::table_name())
            .find_by(Self::primary_key(), value.to_value())
            .take_one()
    }

    /// Find records using an arbitrary SQL query. The caller must ensure that all required columns
    /// are returned.
    ///
    /// # Example
    /// ```
    /// # use rwf::prelude::*;
    /// # use rwf::model::ToSql;
    /// # #[derive(Clone, macros::Model)]
    /// # struct User {
    /// #    id: Option<i64>,
    /// #    email: String,
    /// # }
    /// let query = User::find_by_sql("SELECT * FROM users WHERE email = ANY($1, $2) ORDER BY RANDOM()",
    ///     &[
    ///         "bob@test.com".into(),
    ///         "alice@test.com".into(),
    ///     ]
    /// );
    ///
    /// assert_eq!(query.to_sql(), r#"SELECT * FROM users WHERE email = ANY($1, $2) ORDER BY RANDOM()"#);
    /// ```
    fn find_by_sql(query: impl ToString, values: &[Value]) -> Query<Self> {
        Query::Raw {
            query: query.to_string(),
            placeholders: values
                .iter()
                .map(|v| v.to_value())
                .collect::<Vec<_>>()
                .into(),
        }
    }

    /// Order records by a column. This method accepts any input type which implement
    /// the [`ToOrderBy`] trait. Chaining this function allows to order by multiple columns.
    ///
    /// # Example
    /// ```
    /// # use rwf::prelude::*;
    /// # use rwf::model::ToSql;
    /// # #[derive(Clone, macros::Model)]
    /// # struct User {
    /// #    id: Option<i64>,
    /// #    email: String,
    /// # }
    /// let q1 = User::order("email");
    /// let q2 = User::order("email DESC");
    /// let q3 = User::order(("email", "DESC"));
    /// let q4 = User::order((User::column("email"), "DESC"));
    ///
    /// assert_eq!(q1.to_sql(), r#"SELECT * FROM "users" ORDER BY email"#);
    /// assert_eq!(q2.to_sql(), r#"SELECT * FROM "users" ORDER BY email DESC"#);
    /// assert_eq!(q3.to_sql(), r#"SELECT * FROM "users" ORDER BY "email" DESC"#);
    /// assert_eq!(q4.to_sql(), r#"SELECT * FROM "users" ORDER BY "users"."email" DESC"#);
    /// ```
    fn order(order: impl ToOrderBy) -> Query<Self> {
        Self::all().order(order)
    }

    /// Join this model to another model with which it has a relationship. The relationship should be declared
    /// in advance using an annotation.
    ///
    /// # Example
    /// ```
    /// # use rwf::prelude::*;
    /// # use rwf::model::ToSql;
    /// #[derive(Clone, macros::Model)]
    /// #[has_many(Project)]
    /// struct User {
    ///    id: Option<i64>,
    ///    email: String,
    /// }
    /// #[derive(Clone, macros::Model)]
    /// #[belongs_to(User)]
    /// struct Project {
    ///     user_id: i64,
    ///     name: String,
    /// }
    ///
    /// let join = User::join::<Project>();
    /// ```
    fn join<F: Association<Self>>() -> Joined<Self, F> {
        Joined::new(F::construct_join())
    }

    /// Filter all records which have a relationship to this model. Used for fetching multiple records at once
    /// in order to avoid N+1 queries.
    ///
    /// # Example
    /// ```
    /// # use rwf::prelude::*;
    /// # use rwf::model::ToSql;
    /// #[derive(Clone, macros::Model)]
    /// #[has_many(Project)]
    /// struct User {
    ///    id: Option<i64>,
    ///    email: String,
    /// }
    /// #[derive(Clone, macros::Model)]
    /// #[belongs_to(User)]
    /// struct Project {
    ///     user_id: i64,
    ///     name: String,
    /// }
    ///
    /// let alice = User { id: Some(1), email: "alice@test.com".into() };
    /// let bob = User { id: Some(2), email: "bob@test.com".into() };
    ///
    /// let projects = User::related::<Project>(&[alice, bob]);
    ///
    /// assert_eq!(
    ///     projects.to_sql(),
    ///     r#"SELECT * FROM "projects" WHERE "projects"."user_id" = ANY($1)"#
    /// );
    /// ```
    fn related<F: Association<Self>>(models: &[impl Model]) -> Query<F> {
        let fks = models
            .iter()
            .filter(|model| !model.id().is_null())
            .map(|fk| fk.id())
            .collect::<Vec<_>>();
        F::all().filter(Self::foreign_key(), fks.as_slice())
    }

    /// Save a model into the database. If a record already exists, it will be updated. If this is a new record,
    /// it will be inserted.
    ///
    /// # Example
    /// ```
    /// # use rwf::prelude::*;
    /// # use rwf::model::ToSql;
    /// # #[derive(Clone, macros::Model)]
    /// # struct User {
    /// #    id: Option<i64>,
    /// #    email: String,
    /// # }
    /// // Save an existing user.
    /// let user = User { id: Some(1), email: "test@test.com".into() };
    /// assert_eq!(
    ///     user.save().to_sql(),
    ///     r#"UPDATE "users" SET "email" = $2 WHERE "id" = $1 RETURNING *"#,
    /// );
    ///
    /// // Create new user.
    /// let new_user = User { id: None, email: "alice@test.com".into() };
    /// assert_eq!(
    ///     new_user.save().to_sql(),
    ///     r#"INSERT INTO "users" ("email") VALUES ($1) RETURNING *"#,
    /// );
    /// ```
    fn save(self) -> Query<Self> {
        match self.id().is_null() {
            false => Query::Update(Update::new(self)),
            true => Query::Insert(Insert::new(self)),
        }
    }

    /// Create new record of this model. All columns that have a `NOT NULL` constraint and
    /// no default value should be provided.
    ///
    /// # Example
    /// ```
    /// # use rwf::prelude::*;
    /// # use rwf::model::ToSql;
    /// # #[derive(Clone, macros::Model)]
    /// # struct User {
    /// #    id: Option<i64>,
    /// #    email: String,
    /// # }
    /// let user = User::create(&[
    ///     ("email", "bob@test.com"),
    /// ]);
    ///
    /// assert_eq!(
    ///     user.to_sql(),
    ///     r#"INSERT INTO "users" ("email") VALUES ($1) RETURNING *"#,
    /// );
    /// ```
    fn create(attributes: &[(impl ToColumn, impl ToValue)]) -> Query<Self> {
        let columns = attributes
            .iter()
            .map(|(c, _)| c.to_column())
            .collect::<Vec<_>>();
        let values = attributes
            .iter()
            .map(|(_, v)| v.to_value())
            .collect::<Vec<_>>();

        Query::Insert(Insert::from_columns(&columns, &values))
    }

    /// Find an existing record matching the column filters or create a new one
    /// if none already exist. It's is equivalent to running [`Model::filter`]
    /// and [`Model::create`] manually.
    ///
    /// # Example
    /// ```
    /// # use rwf::prelude::*;
    /// # use rwf::model::ToSql;
    /// # #[derive(Clone, macros::Model)]
    /// # struct User {
    /// #    id: Option<i64>,
    /// #    email: String,
    /// # }
    /// User::find_or_create_by(&[
    ///     ("email", "john@test.com"),
    /// ]);
    /// ```
    fn find_or_create_by(attributes: &[(impl ToColumn, impl ToValue)]) -> Query<Self> {
        let columns = attributes
            .iter()
            .map(|(c, _)| c.to_column())
            .collect::<Vec<_>>();
        let values = attributes
            .iter()
            .map(|(_, v)| v.to_value())
            .collect::<Vec<_>>();

        let mut select = Query::<Self>::select(Self::table_name());

        for (column, value) in columns.iter().zip(values.iter()) {
            select = select.filter(column.clone(), value.clone());
        }

        let select = match select {
            Query::Select(select) => select,
            _ => unreachable!(),
        };

        let insert = Insert::<Self>::from_columns(&columns, &values);

        Query::InsertIfNotExists {
            select,
            insert,
            created: false,
        }
    }

    /// Lock all records for the duration of the transaction. No other transaction will be able
    /// to access those records until the current one is finished.
    ///
    /// It's not common to lock all rows in a table, so this function is typically chained with
    /// [`Model::filter`].
    ///
    /// # Example
    /// ```
    /// # use rwf::prelude::*;
    /// # use rwf::model::ToSql;
    /// #[derive(Clone, macros::Model)]
    /// # struct User {
    /// #    id: Option<i64>,
    /// #    email: String,
    /// # }
    ///
    /// let lock = User::lock()
    ///     .filter("email", "test@test.com");
    ///
    /// assert_eq!(
    ///     lock.to_sql(),
    ///     r#"SELECT * FROM "users" WHERE "users"."email" = $1 FOR UPDATE"#,
    /// );
    /// ```
    fn lock() -> Query<Self> {
        Self::all().lock()
    }

    /// Refresh the record from the database. This is equivalent to calling [`Model::find`] with
    /// the primary key as the parameter.
    ///
    /// # Example
    /// ```
    /// # use rwf::prelude::*;
    /// # use rwf::model::ToSql;
    /// # #[derive(Clone, macros::Model)]
    /// # struct User {
    /// #    id: Option<i64>,
    /// #    email: String,
    /// # }
    /// let user = User { id: Some(1), email: "test@test.com".into() };
    ///
    /// assert_eq!(
    ///     User::find(1).to_sql(),
    ///     user.reload().to_sql()
    /// );
    /// ```
    fn reload(self) -> Query<Self> {
        Self::find(self.id())
    }

    /// Convert the model to JSON representation.
    ///
    /// # Example
    /// ```
    /// # use rwf::prelude::*;
    /// # use rwf::model::ToSql;
    /// # #[derive(Clone, macros::Model)]
    /// # struct User {
    /// #    id: Option<i64>,
    /// #    email: String,
    /// # }
    /// use serde_json::json;
    ///
    /// let user = User { id: Some(1), email: "test@test.com".into() };
    ///
    /// assert_eq!(
    ///     user.to_json().unwrap(),
    ///     json!({
    ///         "id": 1,
    ///         "email": "test@test.com",
    ///     }),
    /// );
    /// ```
    fn to_json(&self) -> Result<serde_json::Value, Error> {
        let columns = Self::column_names();
        let values = self.values();

        let mut map = serde_json::Map::new();
        for (column, value) in columns.iter().zip(values.iter()) {
            map.insert(column.to_string(), value.clone().into());
        }

        map.insert("id".into(), self.id().into());

        Ok(serde_json::Value::Object(map))
    }
}

#[cfg(test)]
mod test {
    use super::join::AssociationType;
    use super::*;
    use tokio_postgres::row::Row;

    #[derive(Debug, Clone, Default)]
    struct User {
        id: i64,
        email: String,
        password: String,
    }

    impl Model for User {
        fn id(&self) -> Value {
            Value::Integer(self.id)
        }

        fn table_name() -> &'static str {
            "users"
        }

        fn foreign_key() -> &'static str {
            "user_id"
        }

        fn column_names() -> &'static [&'static str] {
            &["email", "password"]
        }

        fn values(&self) -> Vec<Value> {
            vec![self.email.to_value(), self.password.to_value()]
        }
    }

    #[derive(Debug, Clone, Default)]
    struct Order {
        id: i64,
        user_id: i64,
        amount: f64,
    }

    impl Model for Order {
        fn id(&self) -> Value {
            Value::Integer(self.id)
        }

        fn table_name() -> &'static str {
            "orders"
        }

        fn foreign_key() -> &'static str {
            "order_id"
        }

        fn column_names() -> &'static [&'static str] {
            &["user_id", "amount"]
        }

        fn values(&self) -> Vec<Value> {
            vec![self.user_id.to_value(), self.amount.to_value()]
        }
    }

    #[derive(Debug, Clone, Default)]
    struct OrderItem {
        id: i64,
        order_id: i64,
        product_id: i64,
    }

    impl Model for OrderItem {
        fn id(&self) -> Value {
            Value::Integer(self.id)
        }

        fn table_name() -> &'static str {
            "order_items"
        }

        fn foreign_key() -> &'static str {
            "order_item_id"
        }

        fn column_names() -> &'static [&'static str] {
            &["order_id", "product_id"]
        }

        fn values(&self) -> Vec<Value> {
            vec![self.order_id.to_value(), self.product_id.to_value()]
        }
    }

    #[derive(Debug, Clone, Default)]
    struct Product {
        id: i64,
        name: String,
    }

    impl Model for Product {
        fn id(&self) -> Value {
            Value::Integer(self.id)
        }

        fn table_name() -> &'static str {
            "products"
        }

        fn foreign_key() -> &'static str {
            "product_id"
        }

        fn column_names() -> &'static [&'static str] {
            &["name"]
        }

        fn values(&self) -> Vec<Value> {
            vec![self.name.to_value()]
        }
    }

    impl Association<User> for Order {}

    impl Association<Order> for User {
        fn association_type() -> AssociationType {
            AssociationType::HasMany
        }
    }

    impl Association<Order> for OrderItem {}

    impl Association<OrderItem> for Order {
        fn association_type() -> AssociationType {
            AssociationType::HasMany
        }
    }

    impl Association<Product> for OrderItem {}
    impl Association<OrderItem> for Product {
        fn association_type() -> AssociationType {
            AssociationType::HasMany
        }
    }

    impl FromRow for User {
        fn from_row(row: Row) -> Result<Self, Error> {
            let id: i64 = row.get("id");
            let email: String = row.get("email");
            let password: String = row.get("password");

            Ok(User {
                id,
                email,
                password,
            })
        }
    }

    impl FromRow for Order {
        fn from_row(row: Row) -> Result<Self, Error> {
            let id: i64 = row.get("id");
            let user_id: i64 = row.get("user_id");
            let amount: f64 = row.get("amount");

            Ok(Order {
                id,
                user_id,
                amount,
            })
        }
    }

    impl FromRow for OrderItem {
        fn from_row(row: Row) -> Result<Self, Error> {
            let id: i64 = row.get("id");
            let order_id: i64 = row.get("order_id");
            let product_id: i64 = row.get("product_id");

            Ok(OrderItem {
                id,
                order_id,
                product_id,
            })
        }
    }

    impl FromRow for Product {
        fn from_row(row: Row) -> Result<Self, Error> {
            let id: i64 = row.get("id");
            let name: String = row.get("name");

            Ok(Product { id, name })
        }
    }

    #[test]
    fn test_join() {
        let query = User::all().join::<Order>().first_one();

        assert_eq!(
            query.to_sql(),
            r#"SELECT "users".* FROM "users" INNER JOIN "orders" ON "users"."id" = "orders"."user_id" ORDER BY "users"."id" ASC LIMIT 1"#
        );

        let query = Order::all().join::<User>();
        assert_eq!(
            query.to_sql(),
            r#"SELECT "orders".* FROM "orders" INNER JOIN "users" ON "orders"."user_id" = "users"."id""#
        );

        // Order that have a user with id = 5.
        let query = Order::all()
            .join::<User>()
            .find_by(User::column("id"), 5_i64.to_value());

        assert_eq!(
            query.to_sql(),
            r#"SELECT "orders".* FROM "orders" INNER JOIN "users" ON "orders"."user_id" = "users"."id" WHERE "users"."id" = $1 LIMIT 1"#
        );

        let query = User::all()
            .join::<Order>()
            .filter("id", 5)
            .filter(Order::column("amount"), 42.0);

        assert_eq!(
            query.to_sql(),
            r#"SELECT "users".* FROM "users" INNER JOIN "orders" ON "users"."id" = "orders"."user_id" WHERE "users"."id" = $1 AND "orders"."amount" = $2"#
        );

        let query = User::all()
            .join::<Order>()
            .join_nested(Order::join::<OrderItem>().join::<Product>())
            .filter(Product::column("name"), "test_product");

        println!("{}", query.to_sql());
    }

    #[test]
    fn test_related() {
        // let query = User::related::<Order>([1, 2].as_slice());
        // println!("{}", query.to_sql());
    }

    #[test]
    fn test_take_one() {
        let query = User::take_one().to_sql();

        assert_eq!(query, r#"SELECT * FROM "users" LIMIT 1"#);
    }

    #[test]
    fn test_take_many() {
        let query = User::take_many(25).to_sql();

        assert_eq!(query, r#"SELECT * FROM "users" LIMIT 25"#);
    }

    #[test]
    fn test_first_one() {
        let query = User::first_one().to_sql();

        assert_eq!(
            query,
            r#"SELECT * FROM "users" ORDER BY "users"."id" ASC LIMIT 1"#
        );
    }

    #[test]
    fn test_first_many() {
        let query = User::first_many(25).to_sql();

        assert_eq!(
            query,
            r#"SELECT * FROM "users" ORDER BY "users"."id" ASC LIMIT 25"#
        );
    }

    #[test]
    fn test_all() {
        let query = User::all().to_sql();

        assert_eq!(query, r#"SELECT * FROM "users""#);
    }

    #[test]
    fn test_filter() {
        let query = User::filter("email", "test@test.com")
            .filter("password", "not_encrypted")
            .filter("id", 5)
            .filter("id", [1_i64, 2, 3].as_slice());

        assert_eq!(
            query.to_sql(),
            r#"SELECT * FROM "users" WHERE "users"."email" = $1 AND "users"."password" = $2 AND "users"."id" = $3 AND "users"."id" = ANY($4)"#
        );
    }

    #[test]
    fn test_find_by() {
        let query = User::find_by("email", "test@test.com");
        assert_eq!(
            query.to_sql(),
            r#"SELECT * FROM "users" WHERE "users"."email" = $1 LIMIT 1"#
        );
    }

    #[tokio::test]
    async fn test_fetch() -> Result<(), Error> {
        let pool = Pool::from_env();
        let mut transaction = pool.transaction().await?;

        transaction
            .client()
            .query("DROP TABLE IF EXISTS users CASCADE", &[])
            .await?;

        transaction
            .client()
            .query(
                "CREATE TABLE IF NOT EXISTS users (id BIGINT, email VARCHAR, password VARCHAR);",
                &[],
            )
            .await?;
        transaction
            .client()
            .query(
                "INSERT INTO users VALUES (1, 'test@test.com', 'not_encrypted');",
                &[],
            )
            .await?;

        let user = User::order(("email", "ASC"))
            .first_one()
            .fetch(&mut transaction)
            .await?;

        assert_eq!(user.email, "test@test.com");

        let users = User::all().fetch_all(&mut transaction).await?;

        assert_eq!(users.len(), 1);

        Ok(())
    }

    #[tokio::test]
    async fn test_explain() -> Result<(), Error> {
        let pool = Pool::from_env();
        let mut transaction = pool.transaction().await?;

        transaction
            .client()
            .execute("CREATE TABLE IF NOT EXISTS users (id BIGINT);", &[])
            .await?;

        let explain = User::all().explain(&mut transaction).await?;
        assert!(explain.to_string().starts_with("Seq Scan on users"));

        Ok(())
    }

    #[tokio::test]
    async fn test_find_or_create() -> Result<(), Error> {
        let pool = Pool::from_env();

        let mut transaction = pool.transaction().await?;

        transaction
            .client()
            .execute("DROP TABLE IF EXISTS users CASCADE", &[])
            .await?;
        transaction
            .client()
            .execute("CREATE TABLE IF NOT EXISTS users (id BIGSERIAL PRIMARY KEY, email VARCHAR NOT NULL, password VARCHAR NOT NULL);", &[])
            .await?;

        let query = User::all()
            .filter("email", "test@test.com")
            .filter("password", "password")
            .find_or_create();
        let sql = query.to_sql();
        assert_eq!(
            sql,
            r#"SELECT * FROM "users" WHERE "users"."email" = $1 AND "users"."password" = $2 LIMIT 1; INSERT INTO "users" ("email", "password") VALUES ($1, $2) RETURNING *;"#
        );

        query.fetch(&mut transaction).await?;

        Ok(())
    }

    #[test]
    fn test_unique_by() {
        let query = User::create(&[("email", "test@test.com")])
            .unique_by(&["email"])
            .to_sql();
        assert_eq!(
            query,
            r#"INSERT INTO "users" ("email") VALUES ($1) ON CONFLICT ("email") DO UPDATE SET "email" = EXCLUDED."email" RETURNING *"#
        );

        let query = User::filter("email", "test@test.com")
            .find_or_create()
            .unique_by(&["email"])
            .to_sql();
        assert_eq!(
            query,
            r#"SELECT * FROM "users" WHERE "users"."email" = $1 LIMIT 1; INSERT INTO "users" ("email") VALUES ($1) ON CONFLICT ("email") DO UPDATE SET "email" = EXCLUDED."email" RETURNING *;"#
        );
    }

    // #[test]
    // fn test_or() {
    //     let query = User::all()
    //         .filter(&[("email", "test@test.com")])
    //         .filter(&[("password", "not_encrypted")])
    //         .or(User::all().filter(&[("email", "another@test.com")]));

    //     assert_eq!(
    //         query.to_sql(),
    //         r#"SELECT * FROM "users" WHERE ("users"."email" = $1 AND "users"."password" = $2) OR ("users"."email" = $3)"#
    //     );

    //     let query = User::all()
    //         .not(&[("email", "test@test.com")])
    //         .or_not(&[("email", "another@test.com")]);

    //     assert_eq!(
    //         query.to_sql(),
    //         r#"SELECT * FROM "users" WHERE ("users"."email" <> $1) OR ("users"."email" <> $2)"#
    //     );
    // }
}