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
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
use super::connection::{DatabaseBackend, DatabaseConnection};
use super::{Model, QuerySet};
use reinhardt_query::prelude::{
Alias, ColumnRef, DeleteStatement, Expr, ExprTrait, Func, InsertStatement, MySqlQueryBuilder,
PostgresQueryBuilder, Query, QueryBuilder, SelectStatement, SqliteQueryBuilder,
UpdateStatement, Values,
};
use std::collections::HashMap;
use std::marker::PhantomData;
use std::sync::Arc;
use tokio::sync::RwLock;
use uuid::Uuid;
/// Build SQL with values from an INSERT statement based on database backend
fn build_insert_sql(stmt: &InsertStatement, backend: DatabaseBackend) -> (String, Values) {
match backend {
DatabaseBackend::Postgres => PostgresQueryBuilder.build_insert(stmt),
DatabaseBackend::MySql => MySqlQueryBuilder.build_insert(stmt),
DatabaseBackend::Sqlite => SqliteQueryBuilder.build_insert(stmt),
}
}
/// Build SQL with values from an UPDATE statement based on database backend
fn build_update_sql(stmt: &UpdateStatement, backend: DatabaseBackend) -> (String, Values) {
match backend {
DatabaseBackend::Postgres => PostgresQueryBuilder.build_update(stmt),
DatabaseBackend::MySql => MySqlQueryBuilder.build_update(stmt),
DatabaseBackend::Sqlite => SqliteQueryBuilder.build_update(stmt),
}
}
/// Build SQL with values from a SELECT statement based on database backend
fn build_select_sql(stmt: &SelectStatement, backend: DatabaseBackend) -> (String, Values) {
match backend {
DatabaseBackend::Postgres => PostgresQueryBuilder.build_select(stmt),
DatabaseBackend::MySql => MySqlQueryBuilder.build_select(stmt),
DatabaseBackend::Sqlite => SqliteQueryBuilder.build_select(stmt),
}
}
/// Convert a SELECT statement to SQL string based on database backend
fn select_to_string(stmt: &SelectStatement, backend: DatabaseBackend) -> String {
build_select_sql(stmt, backend).0
}
/// Convert an INSERT statement to SQL string based on database backend
fn insert_to_string(stmt: &InsertStatement, backend: DatabaseBackend) -> String {
build_insert_sql(stmt, backend).0
}
/// Build SQL with values from a DELETE statement based on database backend
fn build_delete_sql(stmt: &DeleteStatement, backend: DatabaseBackend) -> (String, Values) {
match backend {
DatabaseBackend::Postgres => PostgresQueryBuilder.build_delete(stmt),
DatabaseBackend::MySql => MySqlQueryBuilder.build_delete(stmt),
DatabaseBackend::Sqlite => SqliteQueryBuilder.build_delete(stmt),
}
}
/// Global database connection state
static DB: once_cell::sync::OnceCell<Arc<RwLock<Option<DatabaseConnection>>>> =
once_cell::sync::OnceCell::new();
/// Initialize the global database connection
///
/// # Arguments
///
/// * `url` - Database connection URL
///
/// # Examples
///
/// ```no_run
/// # async fn example() {
/// use reinhardt_db::orm::manager::init_database;
///
/// init_database("postgres://localhost/mydb").await.unwrap();
/// # }
/// # tokio::runtime::Runtime::new().unwrap().block_on(example());
/// ```
pub async fn init_database(url: &str) -> reinhardt_core::exception::Result<()> {
init_database_with_pool_size(url, None).await
}
/// Initialize the global database connection with a specific pool size
///
/// # Arguments
///
/// * `url` - Database connection URL
/// * `pool_size` - Maximum number of connections in the pool (None = use default)
///
/// # Examples
///
/// ```no_run
/// # async fn example() {
/// use reinhardt_db::orm::manager::init_database_with_pool_size;
///
/// // Use larger pool for high-concurrency tests
/// init_database_with_pool_size("postgres://localhost/mydb", Some(50)).await.unwrap();
/// # }
/// # tokio::runtime::Runtime::new().unwrap().block_on(example());
/// ```
pub async fn init_database_with_pool_size(
url: &str,
pool_size: Option<u32>,
) -> reinhardt_core::exception::Result<()> {
let conn = DatabaseConnection::connect_with_pool_size(url, pool_size).await?;
DB.get_or_init(|| Arc::new(RwLock::new(Some(conn))));
Ok(())
}
/// Reinitialize the global database connection (for testing)
///
/// This function replaces the existing database connection with a new one.
/// Useful for test scenarios where each test needs a fresh connection pool.
///
/// # Arguments
///
/// * `url` - Database connection URL
///
/// # Examples
///
/// ```no_run
/// # async fn example() {
/// use reinhardt_db::orm::manager::reinitialize_database;
///
/// reinitialize_database("postgres://localhost/mydb").await.unwrap();
/// # }
/// # tokio::runtime::Runtime::new().unwrap().block_on(example());
/// ```
pub async fn reinitialize_database(url: &str) -> reinhardt_core::exception::Result<()> {
reinitialize_database_with_pool_size(url, None).await
}
/// Reinitialize the global database connection with a specific pool size (for testing)
///
/// # Arguments
///
/// * `url` - Database connection URL
/// * `pool_size` - Maximum number of connections in the pool (None = use default)
///
/// # Examples
///
/// ```no_run
/// # async fn example() {
/// use reinhardt_db::orm::manager::reinitialize_database_with_pool_size;
///
/// // Use larger pool for concurrent tests
/// reinitialize_database_with_pool_size("postgres://localhost/mydb", Some(30)).await.unwrap();
/// # }
/// # tokio::runtime::Runtime::new().unwrap().block_on(example());
/// ```
pub async fn reinitialize_database_with_pool_size(
url: &str,
pool_size: Option<u32>,
) -> reinhardt_core::exception::Result<()> {
let conn = DatabaseConnection::connect_with_pool_size(url, pool_size).await?;
if let Some(db_cell) = DB.get() {
// Replace existing connection
let mut guard = db_cell.write().await;
*guard = Some(conn);
} else {
// First time initialization
DB.get_or_init(|| Arc::new(RwLock::new(Some(conn))));
}
Ok(())
}
/// Get a reference to the global database connection
pub async fn get_connection() -> reinhardt_core::exception::Result<DatabaseConnection> {
let db = DB.get().ok_or_else(|| {
reinhardt_core::exception::Error::Database("Database not initialized".to_string())
})?;
let guard = db.read().await;
guard.clone().ok_or_else(|| {
reinhardt_core::exception::Error::Database("Database connection not available".to_string())
})
}
/// Model manager (similar to Django's Manager)
/// Provides an interface for database operations
pub struct Manager<M: Model> {
_marker: PhantomData<M>,
}
impl<M: Model> Manager<M> {
/// Creates a new instance.
pub fn new() -> Self {
Self {
_marker: PhantomData,
}
}
/// Get all records
pub fn all(&self) -> QuerySet<M> {
QuerySet::new()
}
/// Filter records by field, operator, and value
///
/// Accepts both string literals and FieldRef for type-safe field references.
///
/// # Examples
///
/// ```ignore
/// // String literal
/// User::objects().filter("email", FilterOperator::Eq, FilterValue::String("alice@example.com".to_string()))
///
/// // Type-safe FieldRef
/// User::objects().filter(User::field_email(), FilterOperator::Eq, FilterValue::String("alice@example.com".to_string()))
/// ```
pub fn filter<F: Into<String>>(
&self,
field: F,
operator: super::query::FilterOperator,
value: super::query::FilterValue,
) -> QuerySet<M> {
let filter = super::query::Filter::new(field.into(), operator, value);
QuerySet::new().filter(filter)
}
/// Filter records by a Filter object (Django-style)
///
/// # Example
///
/// ```ignore
/// let users = User::objects()
/// .filter_by(User::field_name().eq("Alice"))
/// .all(&db)
/// .await?;
/// ```
pub fn filter_by(&self, filter: super::query::Filter) -> QuerySet<M> {
QuerySet::new().filter(filter)
}
/// Get a single record by primary key
/// Returns a QuerySet filtered by the primary key field
pub fn get(&self, pk: M::PrimaryKey) -> QuerySet<M> {
let pk_field = M::primary_key_field();
let pk_str = pk.to_string();
// Try to parse as i64 first (common for primary keys), fallback to string
let pk_value = if let Ok(int_value) = pk_str.parse::<i64>() {
super::query::FilterValue::Integer(int_value)
} else {
super::query::FilterValue::String(pk_str)
};
let filter = super::query::Filter::new(
pk_field.to_string(),
super::query::FilterOperator::Eq,
pk_value,
);
QuerySet::new().filter(filter)
}
/// Set LIMIT clause
///
/// Limits the number of records returned by the QuerySet.
/// Corresponds to Django's `QuerySet[:n]`.
///
/// # Examples
///
/// ```ignore
/// let users = User::objects().limit(10).all().await?;
/// ```
pub fn limit(&self, limit: usize) -> QuerySet<M> {
QuerySet::new().limit(limit)
}
/// Set ORDER BY clause
///
/// Sorts the QuerySet results by the specified fields.
/// Use "-" prefix for descending order (e.g., "-created_at").
/// Corresponds to Django's QuerySet.order_by().
///
/// # Examples
///
/// ```ignore
/// // Ascending by name
/// let users = User::objects().order_by(&["name"]).all().await?;
///
/// // Descending by created_at
/// let users = User::objects().order_by(&["-created_at"]).all().await?;
///
/// // Multiple fields
/// let users = User::objects().order_by(&["department", "-salary"]).all().await?;
/// ```
pub fn order_by(&self, fields: &[&str]) -> QuerySet<M> {
QuerySet::new().order_by(fields)
}
/// Add annotation to QuerySet
///
/// Adds a computed field to each record using SQL expressions or aggregations.
/// Corresponds to Django's QuerySet.annotate().
///
/// # Examples
///
/// ```ignore
/// use reinhardt_db::orm::annotation::{Annotation, AnnotationValue};
/// use reinhardt_db::orm::aggregation::Aggregate;
///
/// let users = User::objects()
/// .annotate(Annotation::new("total_orders",
/// AnnotationValue::Aggregate(Aggregate::count(Some("orders")))))
/// .all()
/// .await?;
/// ```
pub fn annotate(&self, annotation: super::annotation::Annotation) -> QuerySet<M> {
QuerySet::new().annotate(annotation)
}
/// Defer loading of specified fields
///
/// Excludes the specified fields from the initial query, loading them only when accessed.
/// Corresponds to Django's QuerySet.defer().
///
/// # Examples
///
/// ```ignore
/// let users = User::objects().defer(&["bio", "profile_picture"]).all().await?;
/// ```
pub fn defer(&self, fields: &[&str]) -> QuerySet<M> {
QuerySet::new().defer(fields)
}
/// Load only specified fields
///
/// Loads only the specified fields, excluding all others.
/// Corresponds to Django's QuerySet.only().
///
/// # Examples
///
/// ```ignore
/// let users = User::objects().only(&["id", "username"]).all().await?;
/// ```
pub fn only(&self, fields: &[&str]) -> QuerySet<M> {
QuerySet::new().only(fields)
}
/// Select specific fields (values)
///
/// Returns records with only the specified fields.
/// Corresponds to Django's QuerySet.values().
///
/// # Examples
///
/// ```ignore
/// let user_data = User::objects().values(&["id", "username", "email"]).all().await?;
/// ```
pub fn values(&self, fields: &[&str]) -> QuerySet<M> {
QuerySet::new().values(fields)
}
/// Eager load related objects using JOIN
///
/// Performs SQL JOINs to load related objects in a single query.
/// Corresponds to Django's QuerySet.select_related().
///
/// # Examples
///
/// ```ignore
/// let posts = Post::objects().select_related(&["author", "category"]).all().await?;
/// ```
pub fn select_related(&self, fields: &[&str]) -> QuerySet<M> {
QuerySet::new().select_related(fields)
}
/// Set OFFSET clause
///
/// Skips the specified number of records before returning results.
/// Corresponds to Django's QuerySet slicing `[offset:]`.
///
/// # Examples
///
/// ```ignore
/// let users = User::objects().offset(20).all().await?;
/// ```
pub fn offset(&self, offset: usize) -> QuerySet<M> {
QuerySet::new().offset(offset)
}
/// Paginate results (LIMIT + OFFSET)
///
/// Convenience method that combines LIMIT and OFFSET for pagination.
/// Corresponds to Django's Paginator.
///
/// # Examples
///
/// ```ignore
/// let users = User::objects().paginate(3, 10).all().await?; // page 3, 10 items per page
/// ```
pub fn paginate(&self, page: usize, page_size: usize) -> QuerySet<M> {
QuerySet::new().paginate(page, page_size)
}
/// Prefetch related objects using separate queries
///
/// Performs separate queries to load related objects, reducing N+1 queries.
/// Corresponds to Django's QuerySet.prefetch_related().
///
/// # Examples
///
/// ```ignore
/// let posts = Post::objects().prefetch_related(&["comments", "tags"]).all().await?;
/// ```
pub fn prefetch_related(&self, fields: &[&str]) -> QuerySet<M> {
QuerySet::new().prefetch_related(fields)
}
/// Select specific fields (values_list)
///
/// Alias for `values()`. Returns records with only the specified fields.
/// Corresponds to Django's QuerySet.values_list().
///
/// # Examples
///
/// ```ignore
/// let user_data = User::objects().values_list(&["id", "username"]).all().await?;
/// ```
pub fn values_list(&self, fields: &[&str]) -> QuerySet<M> {
QuerySet::new().values_list(fields)
}
/// Filter by array overlap (PostgreSQL)
///
/// Filters rows where the array field overlaps with the provided values.
/// Uses the `&&` operator in PostgreSQL.
///
/// # Examples
///
/// ```ignore
/// let posts = Post::objects().filter_array_overlap("tags", &["rust", "web"]).all().await?;
/// ```
pub fn filter_array_overlap(&self, field: &str, values: &[&str]) -> QuerySet<M> {
QuerySet::new().filter_array_overlap(field, values)
}
/// Filter by array contains (PostgreSQL)
///
/// Filters rows where the array field contains all provided values.
/// Uses the `@>` operator in PostgreSQL.
///
/// # Examples
///
/// ```ignore
/// let posts = Post::objects().filter_array_contains("tags", &["rust", "web"]).all().await?;
/// ```
pub fn filter_array_contains(&self, field: &str, values: &[&str]) -> QuerySet<M> {
QuerySet::new().filter_array_contains(field, values)
}
/// Filter by JSONB contains (PostgreSQL)
///
/// Filters rows where the JSONB field contains the provided JSON.
/// Uses the `@>` operator in PostgreSQL.
///
/// # Examples
///
/// ```ignore
/// let users = User::objects().filter_jsonb_contains("metadata", r#"{"role": "admin"}"#).all().await?;
/// ```
pub fn filter_jsonb_contains(&self, field: &str, json: &str) -> QuerySet<M> {
QuerySet::new().filter_jsonb_contains(field, json)
}
/// Filter by JSONB key exists (PostgreSQL)
///
/// Filters rows where the JSONB field has the specified key.
/// Uses the `?` operator in PostgreSQL.
///
/// # Examples
///
/// ```ignore
/// let users = User::objects().filter_jsonb_key_exists("metadata", "email").all().await?;
/// ```
pub fn filter_jsonb_key_exists(&self, field: &str, key: &str) -> QuerySet<M> {
QuerySet::new().filter_jsonb_key_exists(field, key)
}
/// Filter by range contains (PostgreSQL)
///
/// Filters rows where the range field contains the provided value.
/// Uses the `@>` operator in PostgreSQL.
///
/// # Examples
///
/// ```ignore
/// let events = Event::objects().filter_range_contains("date_range", "2024-01-15").all().await?;
/// ```
pub fn filter_range_contains(&self, field: &str, value: &str) -> QuerySet<M> {
QuerySet::new().filter_range_contains(field, value)
}
/// Filter by IN subquery
///
/// Filters rows where the field value is in the result of a subquery.
///
/// # Examples
///
/// ```ignore
/// let authors = Author::objects()
/// .filter_in_subquery("id", |subq: QuerySet<Book>| {
/// subq.filter(Filter::new("price", FilterOperator::Gt, FilterValue::Int(1500)))
/// .values(&["author_id"])
/// })
/// .all()
/// .await?;
/// ```
pub fn filter_in_subquery<R: super::Model, F>(&self, field: &str, subquery_fn: F) -> QuerySet<M>
where
F: FnOnce(QuerySet<R>) -> QuerySet<R>,
{
QuerySet::new().filter_in_subquery(field, subquery_fn)
}
/// Filter by NOT IN subquery
///
/// Filters rows where the field value is not in the result of a subquery.
///
/// # Examples
///
/// ```ignore
/// let authors = Author::objects()
/// .filter_not_in_subquery("id", |subq: QuerySet<Book>| {
/// subq.filter(Filter::new("status", FilterOperator::Eq, FilterValue::String("archived".into())))
/// .values(&["author_id"])
/// })
/// .all()
/// .await?;
/// ```
pub fn filter_not_in_subquery<R: super::Model, F>(
&self,
field: &str,
subquery_fn: F,
) -> QuerySet<M>
where
F: FnOnce(QuerySet<R>) -> QuerySet<R>,
{
QuerySet::new().filter_not_in_subquery(field, subquery_fn)
}
/// Filter by EXISTS subquery
///
/// Filters rows where the subquery returns at least one row.
///
/// # Examples
///
/// ```ignore
/// let authors = Author::objects()
/// .filter_exists(|subq: QuerySet<Book>| {
/// subq.filter(Filter::new("author_id", FilterOperator::Eq, FilterValue::FieldRef(F::new("authors.id"))))
/// })
/// .all()
/// .await?;
/// ```
pub fn filter_exists<R: super::Model, F>(&self, subquery_fn: F) -> QuerySet<M>
where
F: FnOnce(QuerySet<R>) -> QuerySet<R>,
{
QuerySet::new().filter_exists(subquery_fn)
}
/// Filter by NOT EXISTS subquery
///
/// Filters rows where the subquery returns no rows.
///
/// # Examples
///
/// ```ignore
/// let authors = Author::objects()
/// .filter_not_exists(|subq: QuerySet<Book>| {
/// subq.filter(Filter::new("author_id", FilterOperator::Eq, FilterValue::FieldRef(F::new("authors.id"))))
/// })
/// .all()
/// .await?;
/// ```
pub fn filter_not_exists<R: super::Model, F>(&self, subquery_fn: F) -> QuerySet<M>
where
F: FnOnce(QuerySet<R>) -> QuerySet<R>,
{
QuerySet::new().filter_not_exists(subquery_fn)
}
/// Add Common Table Expression (WITH clause)
///
/// Adds a CTE that can be referenced in the main query.
///
/// # Examples
///
/// ```ignore
/// let high_earners = CTE::new("high_earners", "SELECT * FROM employees WHERE salary > 100000");
/// let results = Employee::objects()
/// .with_cte(high_earners)
/// .all()
/// .await?;
/// ```
pub fn with_cte(&self, cte: super::cte::CTE) -> QuerySet<M> {
QuerySet::new().with_cte(cte)
}
/// Full-text search (PostgreSQL)
///
/// Filters rows using PostgreSQL's full-text search.
///
/// # Examples
///
/// ```ignore
/// let articles = Article::objects()
/// .full_text_search("search_vector", "rust programming")
/// .all()
/// .await?;
/// ```
pub fn full_text_search(&self, field: &str, query: &str) -> QuerySet<M> {
QuerySet::new().full_text_search(field, query)
}
/// Annotate with subquery
///
/// Adds a scalar subquery to the SELECT clause.
///
/// # Examples
///
/// ```ignore
/// let authors = Author::objects()
/// .annotate_subquery::<Book, _>("book_count", |subq| {
/// subq.filter("author_id", FilterOperator::Eq, FilterValue::OuterRef(OuterRef::new("authors.id")))
/// .values(&["COUNT(*)"])
/// })
/// .all()
/// .await?;
/// ```
pub fn annotate_subquery<R, F>(&self, name: &str, builder: F) -> QuerySet<M>
where
R: super::Model + 'static,
F: FnOnce(QuerySet<R>) -> QuerySet<R>,
{
QuerySet::new().annotate_subquery(name, builder)
}
/// Get a record by composite primary key
///
/// Retrieves a single object using all fields of a composite primary key.
///
/// # Examples
///
/// ```ignore
/// let mut pk_values = HashMap::new();
/// pk_values.insert("post_id".to_string(), PkValue::Int(1));
/// pk_values.insert("tag_id".to_string(), PkValue::Int(5));
/// let post_tag = PostTag::objects().get_composite(&pk_values).await?;
/// ```
pub async fn get_composite(
&self,
pk_values: &std::collections::HashMap<String, super::composite_pk::PkValue>,
) -> reinhardt_core::exception::Result<M>
where
M: Clone + serde::de::DeserializeOwned,
{
QuerySet::new().get_composite(pk_values).await
}
/// Create a new record using reinhardt-query for SQL injection protection
pub async fn create(&self, model: &M) -> reinhardt_core::exception::Result<M> {
let conn = get_connection().await?;
self.create_with_conn(&conn, model).await
}
/// Create a new record with an explicit database connection
///
/// This method allows using a specific connection, which is essential for
/// transaction support. When operations are performed within a transaction,
/// the same connection must be used throughout.
///
/// # Arguments
///
/// * `conn` - The database connection to use
/// * `model` - The model to create
///
/// # Examples
///
/// ```no_run
/// # use reinhardt_db::orm::{Model, Manager, TransactionScope};
/// # async fn example<M: Model>(manager: Manager<M>, model: &M) -> reinhardt_core::exception::Result<()> {
/// use reinhardt_db::orm::manager::get_connection;
///
/// let conn = get_connection().await?;
/// let tx = TransactionScope::begin(&conn).await?;
///
/// // Create within transaction
/// let created = manager.create_with_conn(&conn, model).await?;
///
/// tx.commit().await?;
/// # Ok(())
/// # }
/// ```
pub async fn create_with_conn(
&self,
conn: &DatabaseConnection,
model: &M,
) -> reinhardt_core::exception::Result<M> {
let json = serde_json::to_value(model)
.map_err(|e| reinhardt_core::exception::Error::Database(e.to_string()))?;
// Extract fields and values from model
let obj = json.as_object().ok_or_else(|| {
reinhardt_core::exception::Error::Database("Model must serialize to object".to_string())
})?;
// Build reinhardt-query INSERT statement
let mut stmt = Query::insert();
stmt.into_table(Alias::new(M::table_name()));
// Get the primary key field name to filter out auto-increment fields
let pk_field = M::primary_key_field();
// Filter out primary key fields and null datetime fields.
// Null datetime fields are skipped to let database DEFAULT apply
// (e.g., created_at, updated_at with DEFAULT CURRENT_TIMESTAMP).
let (fields, values): (Vec<_>, Vec<_>) = obj
.iter()
.filter(|(k, v)| {
let key = k.as_str();
// Exclude primary key field if it's null or 0 (auto-increment)
if key == pk_field {
if v.is_null() {
return false;
}
if let Some(n) = v.as_i64() {
return n != 0;
}
}
// Skip null datetime fields to let database DEFAULT apply
if v.is_null()
&& (key == "created_at"
|| key == "updated_at"
|| key.ends_with("_date")
|| key.ends_with("_time")
|| key.ends_with("_at"))
{
return false;
}
true
})
.map(|(k, v)| {
// Convert null values to SQL NULL for proper insertion
let value = if v.is_null() {
reinhardt_query::value::Value::Int(None)
} else {
Self::json_to_sea_value(v)
};
(Alias::new(k.as_str()), value)
})
.unzip();
stmt.columns(fields);
stmt.values_panic(values);
// Add RETURNING clause with explicit column names from JSON object
// Note: Using Asterisk in columns() may not work correctly with reinhardt-query
let all_columns: Vec<_> = obj.keys().map(|k| Alias::new(k.as_str())).collect();
stmt.returning(all_columns);
let (sql, values) = build_insert_sql(&stmt, conn.backend());
let values: Vec<_> = values
.0
.into_iter()
.map(Self::sea_value_to_query_value)
.collect();
let row = conn.query_one(&sql, values).await?;
// row.data is already serde_json::Value::Object so deserialize directly
serde_json::from_value(row.data.clone())
.map_err(|e| reinhardt_core::exception::Error::Database(e.to_string()))
}
/// Convert serde_json::Value to reinhardt_query::value::Value for parameter binding
fn json_to_sea_value(v: &serde_json::Value) -> reinhardt_query::value::Value {
match v {
serde_json::Value::Null => reinhardt_query::value::Value::Int(None),
serde_json::Value::Bool(b) => reinhardt_query::value::Value::Bool(Some(*b)),
serde_json::Value::Number(n) => {
if let Some(i) = n.as_i64() {
reinhardt_query::value::Value::BigInt(Some(i))
} else if let Some(f) = n.as_f64() {
reinhardt_query::value::Value::Double(Some(f))
} else {
reinhardt_query::value::Value::Int(None)
}
}
serde_json::Value::String(s) => {
// 1. Try to parse as UUID (format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
// UUIDs are often serialized as strings via serde
if let Ok(uuid) = Uuid::parse_str(s) {
return reinhardt_query::value::Value::Uuid(Some(Box::new(uuid)));
}
// 2. Try to parse as ISO 8601 datetime (chrono::DateTime<Utc>)
// This handles timestamps serialized by serde_json from chrono::DateTime
// 2.1 Try RFC3339 strict format first (e.g., "2024-01-01T00:00:00+00:00")
if let Ok(dt) = chrono::DateTime::parse_from_rfc3339(s) {
return reinhardt_query::value::Value::ChronoDateTimeUtc(Some(Box::new(
dt.with_timezone(&chrono::Utc),
)));
}
// 2.2 Try chrono's FromStr trait for DateTime<Utc>
// This handles formats like "2024-01-01T00:00:00Z" with optional subseconds
if let Ok(dt) = s.parse::<chrono::DateTime<chrono::Utc>>() {
return reinhardt_query::value::Value::ChronoDateTimeUtc(Some(Box::new(dt)));
}
// 2.3 Try parsing with FixedOffset timezone then convert to UTC
// Handles formats like "2024-01-01T00:00:00.123456789+00:00"
if let Ok(dt) = s.parse::<chrono::DateTime<chrono::FixedOffset>>() {
return reinhardt_query::value::Value::ChronoDateTimeUtc(Some(Box::new(
dt.with_timezone(&chrono::Utc),
)));
}
// Fallback: treat as regular string (non-datetime, non-UUID values)
reinhardt_query::value::Value::String(Some(Box::new(s.clone())))
}
serde_json::Value::Array(arr) => {
// Convert JSON array to reinhardt_query::value::Value array
// Array(ArrayType, Option<Box<Vec<Value>>>)
let values: Vec<reinhardt_query::value::Value> =
arr.iter().map(|v| Self::json_to_sea_value(v)).collect();
reinhardt_query::value::Value::Array(
reinhardt_query::value::ArrayType::String,
Some(Box::new(values)),
)
}
serde_json::Value::Object(_obj) => {
// Use reinhardt-query's Json type for PostgreSQL JSONB/JSON columns
// Json expects Box<serde_json::Value>
reinhardt_query::value::Value::Json(Some(Box::new(v.clone())))
}
}
}
/// Convert reinhardt_query::value::Value to QueryValue for database parameter binding
fn sea_value_to_query_value(v: reinhardt_query::value::Value) -> super::connection::QueryValue {
use super::connection::QueryValue;
match v {
reinhardt_query::value::Value::Bool(Some(b)) => QueryValue::Bool(b),
reinhardt_query::value::Value::Bool(None) => QueryValue::Null,
reinhardt_query::value::Value::TinyInt(Some(i)) => QueryValue::Int(i as i64),
reinhardt_query::value::Value::TinyInt(None) => QueryValue::Null,
reinhardt_query::value::Value::SmallInt(Some(i)) => QueryValue::Int(i as i64),
reinhardt_query::value::Value::SmallInt(None) => QueryValue::Null,
reinhardt_query::value::Value::Int(Some(i)) => QueryValue::Int(i as i64),
reinhardt_query::value::Value::Int(None) => QueryValue::Null,
reinhardt_query::value::Value::BigInt(Some(i)) => QueryValue::Int(i),
reinhardt_query::value::Value::BigInt(None) => QueryValue::Null,
reinhardt_query::value::Value::TinyUnsigned(Some(u)) => QueryValue::Int(u as i64),
reinhardt_query::value::Value::TinyUnsigned(None) => QueryValue::Null,
reinhardt_query::value::Value::SmallUnsigned(Some(u)) => QueryValue::Int(u as i64),
reinhardt_query::value::Value::SmallUnsigned(None) => QueryValue::Null,
reinhardt_query::value::Value::Unsigned(Some(u)) => QueryValue::Int(u as i64),
reinhardt_query::value::Value::Unsigned(None) => QueryValue::Null,
reinhardt_query::value::Value::BigUnsigned(Some(u)) => QueryValue::Int(u as i64),
reinhardt_query::value::Value::BigUnsigned(None) => QueryValue::Null,
reinhardt_query::value::Value::Float(Some(f)) => QueryValue::Float(f as f64),
reinhardt_query::value::Value::Float(None) => QueryValue::Null,
reinhardt_query::value::Value::Double(Some(f)) => QueryValue::Float(f),
reinhardt_query::value::Value::Double(None) => QueryValue::Null,
reinhardt_query::value::Value::String(Some(s)) => QueryValue::String((*s).clone()),
reinhardt_query::value::Value::String(None) => QueryValue::Null,
reinhardt_query::value::Value::Bytes(Some(b)) => QueryValue::Bytes((*b).clone()),
reinhardt_query::value::Value::Bytes(None) => QueryValue::Null,
// Timestamp handling
// ChronoDateTime contains NaiveDateTime, convert to UTC
reinhardt_query::value::Value::ChronoDateTime(Some(dt)) => {
QueryValue::Timestamp(dt.and_utc())
}
reinhardt_query::value::Value::ChronoDateTime(None) => QueryValue::Null,
reinhardt_query::value::Value::ChronoDateTimeUtc(Some(dt)) => {
QueryValue::Timestamp(*dt)
}
reinhardt_query::value::Value::ChronoDateTimeUtc(None) => QueryValue::Null,
// UUID handling
reinhardt_query::value::Value::Uuid(Some(u)) => QueryValue::Uuid(*u),
reinhardt_query::value::Value::Uuid(None) => QueryValue::Null,
// JSON types - serialize to string
reinhardt_query::value::Value::Json(Some(json)) => QueryValue::String(json.to_string()),
reinhardt_query::value::Value::Json(None) => QueryValue::Null,
// For complex types or unsupported types, convert to null
// This is a safe fallback that won't cause runtime errors
_ => QueryValue::Null,
}
}
/// Serialize a JSON value to SQL-compatible string representation
// Allow dead_code: internal helper for JSON-to-SQL serialization in manager operations
#[allow(dead_code)]
fn serialize_value(v: &serde_json::Value) -> String {
match v {
serde_json::Value::Null => "NULL".to_string(),
serde_json::Value::Bool(b) => b.to_string().to_uppercase(),
serde_json::Value::Number(n) => n.to_string(),
serde_json::Value::String(s) => {
// Escape single quotes and wrap in quotes
format!("'{}'", s.replace('\'', "''"))
}
serde_json::Value::Array(arr) => {
// Convert to PostgreSQL array syntax: ARRAY['a', 'b', 'c']
let items: Vec<String> = arr.iter().map(Self::serialize_value).collect();
format!("ARRAY[{}]", items.join(", "))
}
serde_json::Value::Object(obj) => {
// Convert to JSON string for JSONB columns
let json_str = serde_json::to_string(obj).unwrap_or_else(|_| "{}".to_string());
format!("'{}'::jsonb", json_str.replace('\'', "''"))
}
}
}
/// Update an existing record using reinhardt-query for SQL injection protection
pub async fn update(&self, model: &M) -> reinhardt_core::exception::Result<M> {
let conn = get_connection().await?;
self.update_with_conn(&conn, model).await
}
/// Update an existing record with an explicit database connection
///
/// This method allows using a specific connection, which is essential for
/// transaction support.
///
/// # Arguments
///
/// * `conn` - The database connection to use
/// * `model` - The model to update (must have primary key set)
///
/// # Examples
///
/// ```no_run
/// # use reinhardt_db::orm::{Model, Manager, TransactionScope};
/// # async fn example<M: Model>(manager: Manager<M>, model: &M) -> reinhardt_core::exception::Result<()> {
/// use reinhardt_db::orm::manager::get_connection;
///
/// let conn = get_connection().await?;
/// let tx = TransactionScope::begin(&conn).await?;
///
/// // Update within transaction
/// let updated = manager.update_with_conn(&conn, model).await?;
///
/// tx.commit().await?;
/// # Ok(())
/// # }
/// ```
pub async fn update_with_conn(
&self,
conn: &DatabaseConnection,
model: &M,
) -> reinhardt_core::exception::Result<M> {
let pk = model.primary_key().ok_or_else(|| {
reinhardt_core::exception::Error::Database("Model must have primary key".to_string())
})?;
let json = serde_json::to_value(model)
.map_err(|e| reinhardt_core::exception::Error::Database(e.to_string()))?;
let obj = json.as_object().ok_or_else(|| {
reinhardt_core::exception::Error::Database("Model must serialize to object".to_string())
})?;
// Build reinhardt-query UPDATE statement
let mut stmt = Query::update();
stmt.table(Alias::new(M::table_name()));
// Add SET clauses for all fields except primary key
for (k, v) in obj
.iter()
.filter(|(k, _)| k.as_str() != M::primary_key_field())
{
if v.is_null() {
// Use untyped NULL to avoid PostgreSQL type mismatch errors
// (e.g., setting timestamp column to NULL would fail with Int(None))
stmt.value_expr(Alias::new(k.as_str()), Expr::cust("NULL"));
} else {
stmt.value(Alias::new(k.as_str()), Self::json_to_sea_value(v));
}
}
// Add WHERE clause for primary key
// Try to parse as i64 first (common for primary keys), fallback to string
let pk_str = pk.to_string();
let pk_value = if let Ok(int_value) = pk_str.parse::<i64>() {
reinhardt_query::value::Value::BigInt(Some(int_value))
} else if let Ok(uuid) = Uuid::parse_str(&pk_str) {
reinhardt_query::value::Value::Uuid(Some(Box::new(uuid)))
} else {
reinhardt_query::value::Value::String(Some(Box::new(pk_str)))
};
stmt.and_where(Expr::col(Alias::new(M::primary_key_field())).eq(pk_value));
// Add RETURNING clause with explicit column names from JSON object
// Note: Using Asterisk in columns() may not work correctly with reinhardt-query
let all_columns: Vec<_> = obj.keys().map(|k| Alias::new(k.as_str())).collect();
stmt.returning(all_columns);
let (sql, values) = build_update_sql(&stmt, conn.backend());
let values: Vec<_> = values
.0
.into_iter()
.map(Self::sea_value_to_query_value)
.collect();
let row = conn.query_one(&sql, values).await?;
// row.data is already serde_json::Value::Object so deserialize directly
serde_json::from_value(row.data.clone())
.map_err(|e| reinhardt_core::exception::Error::Database(e.to_string()))
}
/// Delete a record using reinhardt-query for SQL injection protection
pub async fn delete(&self, pk: M::PrimaryKey) -> reinhardt_core::exception::Result<()> {
let conn = get_connection().await?;
self.delete_with_conn(&conn, pk).await
}
/// Delete a record with an explicit database connection
///
/// This method allows using a specific connection, which is essential for
/// transaction support.
///
/// # Arguments
///
/// * `conn` - The database connection to use
/// * `pk` - The primary key of the record to delete
///
/// # Examples
///
/// ```no_run
/// # use reinhardt_db::orm::{Model, Manager, TransactionScope};
/// # async fn example<M: Model>(manager: Manager<M>, pk: M::PrimaryKey) -> reinhardt_core::exception::Result<()> {
/// use reinhardt_db::orm::manager::get_connection;
///
/// let conn = get_connection().await?;
/// let tx = TransactionScope::begin(&conn).await?;
///
/// // Delete within transaction
/// manager.delete_with_conn(&conn, pk).await?;
///
/// tx.commit().await?;
/// # Ok(())
/// # }
/// ```
pub async fn delete_with_conn(
&self,
conn: &DatabaseConnection,
pk: M::PrimaryKey,
) -> reinhardt_core::exception::Result<()> {
// Build reinhardt-query DELETE statement
let mut stmt = Query::delete();
// Try to parse as i64 first (common for primary keys), fallback to string
let pk_str = pk.to_string();
let pk_value = if let Ok(int_value) = pk_str.parse::<i64>() {
reinhardt_query::value::Value::BigInt(Some(int_value))
} else if let Ok(uuid) = Uuid::parse_str(&pk_str) {
reinhardt_query::value::Value::Uuid(Some(Box::new(uuid)))
} else {
reinhardt_query::value::Value::String(Some(Box::new(pk_str)))
};
stmt.from_table(Alias::new(M::table_name()))
.and_where(Expr::col(Alias::new(M::primary_key_field())).eq(pk_value));
let (sql, values) = build_delete_sql(&stmt, conn.backend());
let values: Vec<_> = values
.0
.into_iter()
.map(Self::sea_value_to_query_value)
.collect();
conn.execute(&sql, values).await?;
Ok(())
}
/// Count records using reinhardt-query
pub async fn count(&self) -> reinhardt_core::exception::Result<i64> {
let conn = get_connection().await?;
self.count_with_conn(&conn).await
}
/// Count records with an explicit database connection
///
/// This method allows using a specific connection, which is essential for
/// verifying data within a transaction before commit/rollback.
///
/// # Arguments
///
/// * `conn` - The database connection to use
///
/// # Examples
///
/// ```no_run
/// # use reinhardt_db::orm::{Model, Manager, TransactionScope};
/// # async fn example<M: Model>(manager: Manager<M>) -> reinhardt_core::exception::Result<()> {
/// use reinhardt_db::orm::manager::get_connection;
///
/// let conn = get_connection().await?;
/// let tx = TransactionScope::begin(&conn).await?;
///
/// // Count within transaction (sees uncommitted data)
/// let count = manager.count_with_conn(&conn).await?;
///
/// tx.commit().await?;
/// # Ok(())
/// # }
/// ```
pub async fn count_with_conn(
&self,
conn: &DatabaseConnection,
) -> reinhardt_core::exception::Result<i64> {
// Build reinhardt-query SELECT COUNT(*) statement with explicit alias
let stmt = Query::select()
.from(Alias::new(M::table_name()))
.expr_as(Func::count(Expr::asterisk().into()), Alias::new("count"))
.to_owned();
let (sql, values) = build_select_sql(&stmt, conn.backend());
let values: Vec<_> = values
.0
.into_iter()
.map(Self::sea_value_to_query_value)
.collect();
let row = conn.query_one(&sql, values).await?;
row.get::<i64>("count").ok_or_else(|| {
reinhardt_core::exception::Error::Database("Failed to get count".to_string())
})
}
/// Bulk create multiple records using reinhardt-query (similar to Django's bulk_create())
pub fn bulk_create_query(&self, models: &[M]) -> Option<InsertStatement> {
if models.is_empty() {
return None;
}
// Convert all models to JSON and extract field names from first model
let json_values: Vec<serde_json::Value> = models
.iter()
.filter_map(|m| serde_json::to_value(m).ok())
.collect();
if json_values.is_empty() {
return None;
}
// Get field names from first model
let first_obj = json_values[0].as_object()?;
let fields: Vec<_> = first_obj.keys().map(|k| Alias::new(k.as_str())).collect();
// Build reinhardt-query INSERT statement
let mut stmt = Query::insert();
stmt.into_table(Alias::new(M::table_name())).columns(fields);
// Add value rows for each model
for val in &json_values {
if let Some(obj) = val.as_object() {
let values: Vec<reinhardt_query::value::Value> = first_obj
.keys()
.map(|field| {
obj.get(field)
.map(|v| {
if v.is_null() {
// Use untyped NULL to avoid PostgreSQL type mismatch errors
reinhardt_query::value::Value::Int(None)
} else {
Self::json_to_sea_value(v)
}
})
// Use untyped NULL for missing fields
.unwrap_or(reinhardt_query::value::Value::Int(None))
})
.collect();
stmt.values_panic(values);
}
}
Some(stmt.to_owned())
}
/// Generate bulk create SQL (convenience method)
///
/// # Arguments
///
/// * `models` - Models to insert
/// * `backend` - Database backend to generate SQL for
pub fn bulk_create_sql(&self, models: &[M], backend: DatabaseBackend) -> String {
if let Some(stmt) = self.bulk_create_query(models) {
insert_to_string(&stmt, backend)
} else {
String::new()
}
}
/// Generate UPDATE query for QuerySet
pub fn update_queryset(
&self,
queryset: &QuerySet<M>,
updates: &[(&str, &str)],
) -> (String, Vec<String>) {
use crate::orm::query::UpdateValue;
use std::collections::HashMap;
// Convert &[(&str, &str)] to HashMap<String, UpdateValue>
let updates_map: HashMap<String, UpdateValue> = updates
.iter()
.map(|(key, value)| (key.to_string(), UpdateValue::String(value.to_string())))
.collect();
queryset.update_sql(&updates_map)
}
/// Generate DELETE query for QuerySet
pub fn delete_queryset(&self, queryset: &QuerySet<M>) -> (String, Vec<String>) {
queryset.delete_sql()
}
/// Get or create a record (Django's get_or_create)
/// Returns (model, created) where created is true if a new record was created
///
/// Django equivalent:
/// ```python
/// obj, created = Model.objects.get_or_create(
/// field1=value1,
/// defaults={'field2': value2}
/// )
/// ```
pub async fn get_or_create(
&self,
lookup_fields: HashMap<String, String>,
defaults: Option<HashMap<String, String>>,
) -> reinhardt_core::exception::Result<(M, bool)> {
let conn = get_connection().await?;
// Try to find existing record
let (select_sql, _) = self.get_or_create_sql(
&lookup_fields,
&defaults.clone().unwrap_or_default(),
conn.backend(),
);
if let Ok(Some(row)) = conn.query_optional(&select_sql, vec![]).await {
// row.data is already serde_json::Value::Object so deserialize directly
let model: M = serde_json::from_value(row.data.clone())
.map_err(|e| reinhardt_core::exception::Error::Database(e.to_string()))?;
return Ok((model, false));
}
// Record not found, create new one
let mut all_fields = lookup_fields.clone();
if let Some(defs) = defaults {
all_fields.extend(defs);
}
let fields: Vec<String> = all_fields.keys().cloned().collect();
let values: Vec<String> = all_fields.values().map(|v| format!("'{}'", v)).collect();
let insert_sql = format!(
"INSERT INTO {} ({}) VALUES ({}) RETURNING *",
M::table_name(),
fields.join(", "),
values.join(", ")
);
let row = conn.query_one(&insert_sql, vec![]).await?;
// row.data is already serde_json::Value::Object so deserialize directly
let model: M = serde_json::from_value(row.data.clone())
.map_err(|e| reinhardt_core::exception::Error::Database(e.to_string()))?;
Ok((model, true))
}
/// Bulk create multiple records efficiently (Django's bulk_create)
/// Inserts multiple records in a single query for performance
///
/// Django equivalent:
/// ```python
/// Model.objects.bulk_create([
/// Model(field1=value1),
/// Model(field2=value2),
/// ])
/// ```
///
/// Options:
/// - batch_size: Split into multiple batches if needed
/// - ignore_conflicts: Skip records that would violate constraints
/// - update_conflicts: Update existing records instead of failing
pub async fn bulk_create(
&self,
models: Vec<M>,
batch_size: Option<usize>,
ignore_conflicts: bool,
_update_conflicts: bool,
) -> reinhardt_core::exception::Result<Vec<M>> {
if models.is_empty() {
return Ok(vec![]);
}
let conn = get_connection().await?;
let batch_size = batch_size.unwrap_or(models.len());
let mut results = Vec::new();
for chunk in models.chunks(batch_size) {
// Extract fields from first model
let json = serde_json::to_value(&chunk[0])
.map_err(|e| reinhardt_core::exception::Error::Database(e.to_string()))?;
let obj = json.as_object().ok_or_else(|| {
reinhardt_core::exception::Error::Database(
"Model must serialize to object".to_string(),
)
})?;
// Exclude primary key field if it's Null (for auto-increment)
let pk_field = M::primary_key_field();
let field_names: Vec<String> = obj
.iter()
.filter_map(|(k, v)| {
if k == pk_field && v.is_null() {
None
} else {
Some(k.clone())
}
})
.collect();
// Extract values for all models in chunk
let value_rows: Vec<Vec<serde_json::Value>> = chunk
.iter()
.map(|model| {
let json = serde_json::to_value(model).unwrap();
let obj = json.as_object().unwrap();
field_names.iter().map(|field| obj[field].clone()).collect()
})
.collect();
let sql = self.bulk_create_sql_detailed(&field_names, &value_rows, ignore_conflicts);
// Execute and get results
if ignore_conflicts {
conn.execute(&sql, vec![]).await?;
// Note: Can't get RETURNING with DO NOTHING, skip results
// Return empty vec for ignored conflicts
} else {
let sql_with_returning = sql + " RETURNING *";
let rows = conn.query(&sql_with_returning, vec![]).await?;
for row in rows {
// row.data is already serde_json::Value::Object so deserialize directly
let model: M = serde_json::from_value(row.data.clone())
.map_err(|e| reinhardt_core::exception::Error::Database(e.to_string()))?;
results.push(model);
}
}
}
Ok(results)
}
/// Bulk update multiple records efficiently (Django's bulk_update)
/// Updates specified fields for multiple records in optimized queries
///
/// Django equivalent:
/// ```python
/// Model.objects.bulk_update(
/// [obj1, obj2, obj3],
/// ['field1', 'field2'],
/// batch_size=100
/// )
/// ```
pub async fn bulk_update(
&self,
models: Vec<M>,
fields: Vec<String>,
batch_size: Option<usize>,
) -> reinhardt_core::exception::Result<usize> {
if models.is_empty() || fields.is_empty() {
return Ok(0);
}
let conn = get_connection().await?;
let batch_size = batch_size.unwrap_or(models.len());
let mut total_updated = 0;
for chunk in models.chunks(batch_size) {
// Build updates structure
let updates: Vec<(M::PrimaryKey, HashMap<String, serde_json::Value>)> = chunk
.iter()
.filter_map(|model| {
let pk = model.primary_key()?.clone();
let json = serde_json::to_value(model).ok()?;
let obj = json.as_object()?;
let mut field_map = HashMap::new();
for field in &fields {
if let Some(val) = obj.get(field) {
field_map.insert(field.clone(), val.clone());
}
}
Some((pk, field_map))
})
.collect();
if !updates.is_empty() {
let sql = self.bulk_update_sql_detailed(&updates, &fields, conn.backend());
let rows_affected = conn.execute(&sql, vec![]).await?;
total_updated += rows_affected as usize;
}
}
Ok(total_updated)
}
/// Get or create - SQL generation using reinhardt-query (for testing)
pub fn get_or_create_queries(
&self,
lookup_fields: &HashMap<String, String>,
defaults: &HashMap<String, String>,
) -> (SelectStatement, InsertStatement) {
// Generate SELECT query with reinhardt-query
let mut select_stmt = Query::select();
select_stmt
.from(Alias::new(M::table_name()))
.column(ColumnRef::Asterisk);
for (k, v) in lookup_fields.iter() {
select_stmt.and_where(Expr::col(Alias::new(k.as_str())).eq(v.as_str()));
}
// Generate INSERT query with reinhardt-query
let mut insert_fields = lookup_fields.clone();
insert_fields.extend(defaults.clone());
let mut insert_stmt = Query::insert();
insert_stmt.into_table(Alias::new(M::table_name()));
let columns: Vec<_> = insert_fields
.keys()
.map(|k| Alias::new(k.as_str()))
.collect();
let values: Vec<reinhardt_query::prelude::Expr> = insert_fields
.values()
.map(|v| Expr::val(v.clone()))
.collect();
insert_stmt.columns(columns);
insert_stmt.values_panic(values);
(select_stmt.to_owned(), insert_stmt.to_owned())
}
/// Get or create - SQL generation (convenience method for testing)
///
/// # Arguments
///
/// * `lookup_fields` - Fields to lookup
/// * `defaults` - Default values for creation
/// * `backend` - Database backend to generate SQL for
pub fn get_or_create_sql(
&self,
lookup_fields: &HashMap<String, String>,
defaults: &HashMap<String, String>,
backend: DatabaseBackend,
) -> (String, String) {
let (select_stmt, insert_stmt) = self.get_or_create_queries(lookup_fields, defaults);
(
select_to_string(&select_stmt, backend),
insert_to_string(&insert_stmt, backend),
)
}
/// Bulk create - SQL generation only (for testing)
pub fn bulk_create_sql_detailed(
&self,
field_names: &[String],
value_rows: &[Vec<serde_json::Value>],
ignore_conflicts: bool,
) -> String {
if value_rows.is_empty() {
return String::new();
}
let values_clause: Vec<String> = value_rows
.iter()
.map(|row| {
let values = row
.iter()
.map(|v| match v {
serde_json::Value::Null => "NULL".to_string(),
serde_json::Value::Number(n) => n.to_string(),
serde_json::Value::String(s) => {
// SQL injection prevention: Escape single quotes
format!("'{}'", s.replace("'", "''"))
}
serde_json::Value::Bool(b) => if *b { "TRUE" } else { "FALSE" }.to_string(),
serde_json::Value::Array(_) | serde_json::Value::Object(_) => {
// Treat arrays and objects as JSON strings
format!("'{}'", v.to_string().replace("'", "''"))
}
})
.collect::<Vec<_>>()
.join(", ");
format!("({})", values)
})
.collect();
let mut sql = format!(
"INSERT INTO {} ({}) VALUES {}",
M::table_name(),
field_names.join(", "),
values_clause.join(", ")
);
if ignore_conflicts {
sql.push_str(" ON CONFLICT DO NOTHING");
}
sql
}
/// Bulk update SQL generation using CASE expressions
///
/// Generates raw SQL because reinhardt-query's `UpdateStatement` does not support
/// expression-based SET values (e.g., CASE WHEN ... END).
pub fn bulk_update_sql_detailed(
&self,
updates: &[(M::PrimaryKey, HashMap<String, serde_json::Value>)],
fields: &[String],
_backend: DatabaseBackend,
) -> String
where
M::PrimaryKey: std::fmt::Display + Clone,
{
if updates.is_empty() || fields.is_empty() {
return String::new();
}
let table_name = M::table_name();
let mut set_clauses = Vec::new();
for field in fields {
let mut when_clauses = Vec::new();
for (pk, field_map) in updates.iter() {
if let Some(value) = field_map.get(field) {
let val_str = match value {
serde_json::Value::Null => "NULL".to_string(),
serde_json::Value::Bool(b) => b.to_string().to_uppercase(),
serde_json::Value::Number(n) => n.to_string(),
serde_json::Value::String(s) => format!("'{}'", s.replace('\'', "''")),
serde_json::Value::Array(_) | serde_json::Value::Object(_) => {
format!("'{}'", value.to_string().replace('\'', "''"))
}
};
when_clauses.push(format!(
"WHEN \"id\" = '{}' THEN {}",
pk.to_string().replace('\'', "''"),
val_str
));
}
}
if !when_clauses.is_empty() {
set_clauses.push(format!(
"\"{}\" = CASE {} END",
field,
when_clauses.join(" ")
));
}
}
let ids: Vec<String> = updates
.iter()
.map(|(pk, _)| format!("'{}'", pk.to_string().replace('\'', "''")))
.collect();
format!(
"UPDATE \"{}\" SET {} WHERE \"id\" IN ({})",
table_name,
set_clauses.join(", "),
ids.join(", ")
)
}
}
impl<M: Model> Default for Manager<M> {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use crate::orm::FieldSelector;
use crate::orm::Model;
use crate::orm::connection::DatabaseBackend;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Clone, Serialize, Deserialize)]
struct TestUser {
id: Option<i64>,
name: String,
email: String,
}
impl TestUser {
// Allow dead_code: test helper constructor for manager tests
#[allow(dead_code)]
fn new(name: String, email: String) -> Self {
Self {
id: None,
name,
email,
}
}
}
#[derive(Debug, Clone)]
struct TestUserFields;
impl FieldSelector for TestUserFields {
fn with_alias(self, _alias: &str) -> Self {
self
}
}
impl Model for TestUser {
type PrimaryKey = i64;
type Fields = TestUserFields;
fn table_name() -> &'static str {
"test_user"
}
fn primary_key(&self) -> Option<Self::PrimaryKey> {
self.id
}
fn set_primary_key(&mut self, value: Self::PrimaryKey) {
self.id = Some(value);
}
fn primary_key_field() -> &'static str {
"id"
}
fn new_fields() -> Self::Fields {
TestUserFields
}
}
#[test]
fn test_get_or_create_sql() {
let manager = TestUser::objects();
let mut lookup = HashMap::new();
lookup.insert("email".to_string(), "test@example.com".to_string());
let mut defaults = HashMap::new();
defaults.insert("name".to_string(), "Test User".to_string());
let (select_sql, insert_sql) =
manager.get_or_create_sql(&lookup, &defaults, DatabaseBackend::Postgres);
// reinhardt-query uses quoted identifiers and TestUser table is "test_user"
assert!(select_sql.contains("SELECT") && select_sql.contains("FROM"));
assert!(select_sql.contains("test_user"));
assert!(select_sql.contains("email"));
// reinhardt-query produces parameterized SQL with $1 placeholder instead of inline values
assert!(select_sql.contains("$1"));
assert!(insert_sql.contains("INSERT"));
assert!(insert_sql.contains("test_user"));
assert!(insert_sql.contains("email"));
assert!(insert_sql.contains("name"));
}
#[test]
fn test_bulk_create_sql() {
use serde_json::json;
let manager = TestUser::objects();
let fields = vec!["name".to_string(), "email".to_string()];
let values = vec![
vec![json!("Alice"), json!("alice@example.com")],
vec![json!("Bob"), json!("bob@example.com")],
];
let sql = manager.bulk_create_sql_detailed(&fields, &values, false);
// reinhardt-query uses quoted identifiers and TestUser table is "test_user"
assert!(sql.contains("INSERT"));
assert!(sql.contains("test_user"));
assert!(sql.contains("name"));
assert!(sql.contains("email"));
assert!(sql.contains("Alice"));
assert!(sql.contains("alice@example.com"));
assert!(sql.contains("Bob"));
assert!(sql.contains("bob@example.com"));
}
#[test]
fn test_bulk_create_sql_with_conflict() {
use serde_json::json;
let manager = TestUser::objects();
let fields = vec!["name".to_string(), "email".to_string()];
let values = vec![vec![json!("Alice"), json!("alice@example.com")]];
let sql = manager.bulk_create_sql_detailed(&fields, &values, true);
assert!(sql.contains("ON CONFLICT DO NOTHING"));
}
#[test]
fn test_bulk_update_sql() {
use serde_json::json;
let manager = TestUser::objects();
let mut updates = Vec::new();
let mut user1_fields = HashMap::new();
user1_fields.insert("name".to_string(), json!("Alice Updated"));
user1_fields.insert("email".to_string(), json!("alice_new@example.com"));
updates.push((1i64, user1_fields));
let mut user2_fields = HashMap::new();
user2_fields.insert("name".to_string(), json!("Bob Updated"));
user2_fields.insert("email".to_string(), json!("bob_new@example.com"));
updates.push((2i64, user2_fields));
let fields = vec!["name".to_string(), "email".to_string()];
let sql = manager.bulk_update_sql_detailed(&updates, &fields, DatabaseBackend::Postgres);
// reinhardt-query uses quoted identifiers and TestUser table is "test_user"
assert!(sql.contains("UPDATE"));
assert!(sql.contains("test_user"));
assert!(sql.contains("SET"));
assert!(sql.contains("name"));
assert!(sql.contains("CASE"));
assert!(sql.contains("email"));
assert!(sql.contains("Alice Updated"));
assert!(sql.contains("Bob Updated"));
assert!(sql.contains("WHERE"));
}
#[test]
fn test_bulk_create_empty() {
use serde_json::Value;
let manager = TestUser::objects();
let fields: Vec<String> = vec![];
let values: Vec<Vec<Value>> = vec![];
let sql = manager.bulk_create_sql_detailed(&fields, &values, false);
assert!(sql.is_empty());
}
#[test]
fn test_bulk_update_empty() {
use serde_json::Value;
let manager = TestUser::objects();
let updates: Vec<(i64, HashMap<String, Value>)> = vec![];
let fields = vec!["name".to_string()];
let sql = manager.bulk_update_sql_detailed(&updates, &fields, DatabaseBackend::Postgres);
assert!(sql.is_empty());
}
// ──────────────────────────────────────────────────────────────
// Additional manager tests
// ──────────────────────────────────────────────────────────────
#[test]
fn test_manager_new() {
let manager = super::Manager::<TestUser>::new();
// Manager is just a phantom type wrapper, so this just ensures it compiles
let _ = manager;
}
#[test]
fn test_manager_default() {
let manager = super::Manager::<TestUser>::default();
// Default should work the same as new
let _ = manager;
}
#[test]
fn test_get_or_create_sql_empty_lookup() {
let manager = TestUser::objects();
let lookup: HashMap<String, String> = HashMap::new();
let defaults: HashMap<String, String> = HashMap::new();
let (select_sql, insert_sql) =
manager.get_or_create_sql(&lookup, &defaults, DatabaseBackend::Postgres);
// Empty lookup still produces valid SQL structure
assert!(select_sql.contains("SELECT") || select_sql.contains("select"));
assert!(insert_sql.contains("INSERT") || insert_sql.contains("insert"));
}
#[test]
fn test_get_or_create_sql_with_multiple_lookups() {
let manager = TestUser::objects();
let mut lookup = HashMap::new();
lookup.insert("email".to_string(), "test@example.com".to_string());
lookup.insert("name".to_string(), "Test User".to_string());
let defaults: HashMap<String, String> = HashMap::new();
let (select_sql, _insert_sql) =
manager.get_or_create_sql(&lookup, &defaults, DatabaseBackend::Postgres);
// Should have both conditions in WHERE clause
assert!(select_sql.contains("email"));
assert!(select_sql.contains("name"));
}
#[test]
fn test_bulk_create_sql_single_row() {
use serde_json::json;
let manager = TestUser::objects();
let fields = vec!["name".to_string()];
let values = vec![vec![json!("SingleUser")]];
let sql = manager.bulk_create_sql_detailed(&fields, &values, false);
assert!(sql.contains("INSERT"));
assert!(sql.contains("test_user"));
assert!(sql.contains("SingleUser"));
}
#[test]
fn test_bulk_update_sql_single_field() {
use serde_json::json;
let manager = TestUser::objects();
let mut updates = Vec::new();
let mut user1_fields = HashMap::new();
user1_fields.insert("name".to_string(), json!("Updated Name"));
updates.push((1i64, user1_fields));
let fields = vec!["name".to_string()];
let sql = manager.bulk_update_sql_detailed(&updates, &fields, DatabaseBackend::Postgres);
assert!(sql.contains("UPDATE"));
assert!(sql.contains("name"));
assert!(sql.contains("Updated Name"));
assert!(!sql.contains("email"));
}
#[test]
fn test_json_to_sea_value_string() {
use serde_json::json;
let value = json!("hello");
let sea_value = super::Manager::<TestUser>::json_to_sea_value(&value);
// reinhardt-query Value should contain the string
let debug_str = format!("{:?}", sea_value);
assert!(debug_str.contains("hello") || debug_str.contains("String"));
}
#[test]
fn test_json_to_sea_value_integer() {
use serde_json::json;
let value = json!(42);
let sea_value = super::Manager::<TestUser>::json_to_sea_value(&value);
let debug_str = format!("{:?}", sea_value);
assert!(debug_str.contains("42") || debug_str.contains("Int"));
}
#[test]
fn test_json_to_sea_value_float() {
use serde_json::json;
let value = json!(1.5);
let sea_value = super::Manager::<TestUser>::json_to_sea_value(&value);
let debug_str = format!("{:?}", sea_value);
assert!(debug_str.contains("1.5") || debug_str.contains("Double"));
}
#[test]
fn test_json_to_sea_value_bool() {
use serde_json::json;
let value = json!(true);
let sea_value = super::Manager::<TestUser>::json_to_sea_value(&value);
let debug_str = format!("{:?}", sea_value);
assert!(debug_str.contains("true") || debug_str.contains("Bool"));
}
#[test]
fn test_json_to_sea_value_null() {
use serde_json::json;
let value = json!(null);
let sea_value = super::Manager::<TestUser>::json_to_sea_value(&value);
// Null should be represented somehow
let debug_str = format!("{:?}", sea_value);
assert!(!debug_str.is_empty());
}
#[test]
fn test_json_to_sea_value_array() {
use serde_json::json;
let value = json!([1, 2, 3]);
let sea_value = super::Manager::<TestUser>::json_to_sea_value(&value);
// Array should be converted (typically to JSON string)
let debug_str = format!("{:?}", sea_value);
assert!(!debug_str.is_empty());
}
#[test]
fn test_json_to_sea_value_object() {
use serde_json::json;
let value = json!({"key": "value"});
let sea_value = super::Manager::<TestUser>::json_to_sea_value(&value);
// Object should be converted (typically to JSON string)
let debug_str = format!("{:?}", sea_value);
assert!(!debug_str.is_empty());
}
#[test]
fn test_serialize_value_string() {
use serde_json::json;
let value = json!("test_string");
let serialized = super::Manager::<TestUser>::serialize_value(&value);
// Should return the string representation
assert!(serialized.contains("test_string"));
}
#[test]
fn test_serialize_value_number() {
use serde_json::json;
let value = json!(123);
let serialized = super::Manager::<TestUser>::serialize_value(&value);
assert!(serialized.contains("123"));
}
}