reinhardt-db 0.3.14

Django-style database layer for Reinhardt framework
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
//! Custom Object Manager support for the Reinhardt ORM.
//!
//! This module provides the [`CustomManager`] trait, enabling Django-style
//! customizable object managers.
//!
//! Issue: <https://github.com/kent8192/reinhardt-web/issues/3980>
//! Unified access: <https://github.com/kent8192/reinhardt-web/issues/3984>
//!
//! # Design
//!
//! The `Model` trait has an associated type `Objects` that determines which
//! manager `Model::objects()` returns. By default (when no custom manager is
//! specified), `type Objects = Manager<Self>`. When a custom manager is
//! configured via `#[model(manager = MyManager)]`, the macro sets
//! `type Objects = MyManager`, so `Model::objects()` returns the custom
//! manager directly.
//!
//! All default implementations delegate to the existing inherent methods on
//! [`Manager<M>`], so the runtime semantics of the standard operations are
//! preserved exactly. The blanket `impl<M: Model> CustomManager for Manager<M>`
//! ensures that the existing manager continues to satisfy the trait, allowing
//! generic functions to accept any compatible manager.
//!
//! # Hooks
//!
//! [`CustomManager`] also exposes three hook methods that default to a no-op
//! and that custom implementations can override:
//!
//! - [`CustomManager::before_save`] — invoked before `create`/`update`
//! - [`CustomManager::before_delete`] — invoked before `delete`
//! - [`CustomManager::before_bulk_update`] — invoked before `bulk_update`
//!
//! Returning `Err(_)` from any hook vetoes the operation, mirroring the event
//! veto behavior already present on `Model::save`/`Model::delete`.
//!
//! # Quick Start
//!
//! Define a custom manager with `Default`, implement [`CustomManager`], and
//! use the `#[model(manager = ...)]` attribute:
//!
//! ```ignore
//! use reinhardt_db::orm::CustomManager;
//! use reinhardt_core::exception::Result;
//!
//! #[derive(Default)]
//! struct ActiveUserManager;
//!
//! impl CustomManager for ActiveUserManager {
//!     type Model = User;
//!
//!     fn new() -> Self { Self }
//!
//!     fn before_save(&self, user: &mut User) -> Result<()> {
//!         if user.username.is_empty() {
//!             return Err(reinhardt_core::exception::Error::Database(
//!                 "username must not be empty".into(),
//!             ));
//!         }
//!         Ok(())
//!     }
//! }
//!
//! #[reinhardt_macros::model(table_name = "users", manager = ActiveUserManager)]
//! struct User { /* ... */ }
//!
//! // objects() now returns ActiveUserManager directly
//! let manager = User::objects();
//! ```
//!
//! # Blanket Implementation
//!
//! The blanket `impl<M: Model> CustomManager for Manager<M>` makes every
//! existing manager — the value returned by `Model::objects()` — satisfy
//! [`CustomManager`] automatically. Generic code can therefore accept any
//! compatible manager:
//!
//! ```
//! use reinhardt_db::orm::custom_manager::CustomManager;
//! use reinhardt_db::orm::manager::Manager;
//! use reinhardt_db::orm::model::{FieldSelector, Model};
//! use serde::{Deserialize, Serialize};
//!
//! #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
//! struct Article { id: Option<i64>, title: String }
//!
//! #[derive(Clone)]
//! struct ArticleFields;
//! impl FieldSelector for ArticleFields {
//!     fn with_alias(self, _alias: &str) -> Self { self }
//! }
//!
//! impl Model for Article {
//!     type PrimaryKey = i64;
//!     type Fields = ArticleFields;
//!     type Objects = Manager<Self>;
//!     fn table_name() -> &'static str { "articles" }
//!     fn new_fields() -> Self::Fields { ArticleFields }
//!     fn primary_key(&self) -> Option<Self::PrimaryKey> { self.id }
//!     fn set_primary_key(&mut self, value: Self::PrimaryKey) { self.id = Some(value); }
//! }
//!
//! // Generic helper accepting any `CustomManager` bound to `Article`.
//! fn count_filters<M: CustomManager<Model = Article>>(m: &M) -> usize {
//!     m.all().filters().len()
//! }
//!
//! // `Manager<Article>` satisfies the trait via blanket impl.
//! let m = Manager::<Article>::new();
//! assert_eq!(count_filters(&m), 0);
//! ```

use std::collections::HashMap;
use std::future::Future;

use reinhardt_query::{InsertStatement, SelectStatement};

use super::annotation::Annotation;
use super::composite_pk::PkValue;
use super::connection::{DatabaseBackend, DatabaseConnection};
use super::cte::CTE;
use super::manager::Manager;
use super::model::Model;
use super::query::{FilterCondition, QuerySet};

/// Trait that exposes the full surface area of an object manager and provides
/// extension hooks for custom behavior.
///
/// All builder methods have default implementations that delegate to the
/// canonical [`Manager<M>`] inherent methods, so implementing this trait only
/// requires defining `type Model` and the `new` constructor; every other
/// method may be left to the default to preserve standard behavior, or
/// overridden to inject custom logic.
///
/// # Hooks
///
/// Three hook methods (`before_save`, `before_delete`, `before_bulk_update`)
/// allow custom implementations to validate or veto operations before they
/// reach the database. The default implementations are no-ops returning
/// `Ok(())`.
///
/// # Bounds
///
/// `CustomManager: Sized + Send + Sync` so that managers can be safely
/// constructed via `Default::default()` and shared across asynchronous tasks
/// without additional bounds at every call site.
pub trait CustomManager: Sized + Send + Sync {
	/// The model this manager operates on.
	type Model: Model;

	/// Construct a fresh manager instance.
	///
	/// Custom managers that hold no runtime state can simply return `Self`,
	/// often via `#[derive(Default)]`.
	fn new() -> Self;

	// =========================================================================
	// QuerySet builders (28 methods) — default impls delegate to Manager<M>
	// =========================================================================

	/// Get all records (Django: `Model.objects.all()`).
	fn all(&self) -> QuerySet<Self::Model> {
		Manager::<Self::Model>::new().all()
	}

	/// Filter records by a typed filter expression.
	///
	/// Accepts any value convertible into [`FilterCondition`]. See
	/// [`Manager::filter`] for the recommended fluent builder form
	/// (`Model::field_x().eq(value)`) and composite conditions.
	fn filter(&self, filter: impl Into<FilterCondition>) -> QuerySet<Self::Model> {
		Manager::<Self::Model>::new().filter(filter)
	}

	/// Get a single record by primary key (returns a `QuerySet` for chaining).
	fn get(&self, pk: <Self::Model as Model>::PrimaryKey) -> QuerySet<Self::Model> {
		Manager::<Self::Model>::new().get(pk)
	}

	/// Set a `LIMIT` clause.
	fn limit(&self, limit: usize) -> QuerySet<Self::Model> {
		Manager::<Self::Model>::new().limit(limit)
	}

	/// Set an `ORDER BY` clause; prefix a field with `-` for descending order.
	fn order_by(&self, fields: &[&str]) -> QuerySet<Self::Model> {
		Manager::<Self::Model>::new().order_by(fields)
	}

	/// Add an annotation (computed field) to the query.
	fn annotate(&self, annotation: Annotation) -> QuerySet<Self::Model> {
		Manager::<Self::Model>::new().annotate(annotation)
	}

	/// Defer loading of the specified fields.
	fn defer(&self, fields: &[&str]) -> QuerySet<Self::Model> {
		Manager::<Self::Model>::new().defer(fields)
	}

	/// Restrict loading to only the specified fields.
	fn only(&self, fields: &[&str]) -> QuerySet<Self::Model> {
		Manager::<Self::Model>::new().only(fields)
	}

	/// Project only the specified fields as values.
	fn values(&self, fields: &[&str]) -> QuerySet<Self::Model> {
		Manager::<Self::Model>::new().values(fields)
	}

	/// Eager-load related objects via SQL `JOIN`.
	fn select_related(&self, fields: &[&str]) -> QuerySet<Self::Model> {
		Manager::<Self::Model>::new().select_related(fields)
	}

	/// Set an `OFFSET` clause.
	fn offset(&self, offset: usize) -> QuerySet<Self::Model> {
		Manager::<Self::Model>::new().offset(offset)
	}

	/// Paginate (1-indexed page, fixed page size).
	fn paginate(&self, page: usize, page_size: usize) -> QuerySet<Self::Model> {
		Manager::<Self::Model>::new().paginate(page, page_size)
	}

	/// Pre-fetch related objects in separate queries.
	fn prefetch_related(&self, fields: &[&str]) -> QuerySet<Self::Model> {
		Manager::<Self::Model>::new().prefetch_related(fields)
	}

	/// Project as tuples of values rather than full models.
	fn values_list(&self, fields: &[&str]) -> QuerySet<Self::Model> {
		Manager::<Self::Model>::new().values_list(fields)
	}

	/// PostgreSQL: filter by array overlap (`&&`).
	fn filter_array_overlap(&self, field: &str, values: &[&str]) -> QuerySet<Self::Model> {
		Manager::<Self::Model>::new().filter_array_overlap(field, values)
	}

	/// PostgreSQL: filter by array contains (`@>`).
	fn filter_array_contains(&self, field: &str, values: &[&str]) -> QuerySet<Self::Model> {
		Manager::<Self::Model>::new().filter_array_contains(field, values)
	}

	/// PostgreSQL: filter by JSONB contains (`@>`).
	fn filter_jsonb_contains(&self, field: &str, json: &str) -> QuerySet<Self::Model> {
		Manager::<Self::Model>::new().filter_jsonb_contains(field, json)
	}

	/// PostgreSQL: filter by JSONB key existence (`?`).
	fn filter_jsonb_key_exists(&self, field: &str, key: &str) -> QuerySet<Self::Model> {
		Manager::<Self::Model>::new().filter_jsonb_key_exists(field, key)
	}

	/// PostgreSQL: filter where a range column contains a value.
	fn filter_range_contains(&self, field: &str, value: &str) -> QuerySet<Self::Model> {
		Manager::<Self::Model>::new().filter_range_contains(field, value)
	}

	/// Filter where a field is `IN` the result of a sub-query.
	fn filter_in_subquery<R: Model, F>(&self, field: &str, subquery_fn: F) -> QuerySet<Self::Model>
	where
		F: FnOnce(QuerySet<R>) -> QuerySet<R>,
	{
		Manager::<Self::Model>::new().filter_in_subquery(field, subquery_fn)
	}

	/// Filter where a field is `NOT IN` the result of a sub-query.
	fn filter_not_in_subquery<R: Model, F>(
		&self,
		field: &str,
		subquery_fn: F,
	) -> QuerySet<Self::Model>
	where
		F: FnOnce(QuerySet<R>) -> QuerySet<R>,
	{
		Manager::<Self::Model>::new().filter_not_in_subquery(field, subquery_fn)
	}

	/// Filter using a correlated `EXISTS (...)` sub-query.
	fn filter_exists<R: Model, F>(&self, subquery_fn: F) -> QuerySet<Self::Model>
	where
		F: FnOnce(QuerySet<R>) -> QuerySet<R>,
	{
		Manager::<Self::Model>::new().filter_exists(subquery_fn)
	}

	/// Filter using a correlated `NOT EXISTS (...)` sub-query.
	fn filter_not_exists<R: Model, F>(&self, subquery_fn: F) -> QuerySet<Self::Model>
	where
		F: FnOnce(QuerySet<R>) -> QuerySet<R>,
	{
		Manager::<Self::Model>::new().filter_not_exists(subquery_fn)
	}

	/// Add a Common Table Expression (`WITH ...`).
	fn with_cte(&self, cte: CTE) -> QuerySet<Self::Model> {
		Manager::<Self::Model>::new().with_cte(cte)
	}

	/// PostgreSQL: full-text search using `to_tsvector` / `to_tsquery`.
	fn full_text_search(&self, field: &str, query: &str) -> QuerySet<Self::Model> {
		Manager::<Self::Model>::new().full_text_search(field, query)
	}

	/// Annotate using a sub-query expression.
	fn annotate_subquery<R, F>(&self, name: &str, builder: F) -> QuerySet<Self::Model>
	where
		R: Model + 'static,
		F: FnOnce(QuerySet<R>) -> QuerySet<R>,
	{
		Manager::<Self::Model>::new().annotate_subquery(name, builder)
	}

	// =========================================================================
	// Async CRUD (8 methods) — default impls delegate to Manager<M>
	// =========================================================================

	/// Fetch a single record by composite primary key.
	fn get_composite<'a>(
		&'a self,
		pk_values: &'a HashMap<String, PkValue>,
	) -> impl Future<Output = reinhardt_core::exception::Result<Self::Model>> + Send + 'a
	where
		Self::Model: Clone + serde::de::DeserializeOwned,
	{
		async move { Manager::<Self::Model>::new().get_composite(pk_values).await }
	}

	/// Insert a new record.
	fn create<'a>(
		&'a self,
		model: &'a Self::Model,
	) -> impl Future<Output = reinhardt_core::exception::Result<Self::Model>> + Send + 'a {
		async move {
			let mut model = model.clone();
			self.before_save(&mut model)?;
			Manager::<Self::Model>::new().create(&model).await
		}
	}

	/// Insert a new record using an explicit connection (for transactions).
	fn create_with_conn<'a>(
		&'a self,
		conn: &'a DatabaseConnection,
		model: &'a Self::Model,
	) -> impl Future<Output = reinhardt_core::exception::Result<Self::Model>> + Send + 'a {
		async move {
			let mut model = model.clone();
			self.before_save(&mut model)?;
			Manager::<Self::Model>::new()
				.create_with_conn(conn, &model)
				.await
		}
	}

	/// Update an existing record (must have a primary key set).
	fn update<'a>(
		&'a self,
		model: &'a Self::Model,
	) -> impl Future<Output = reinhardt_core::exception::Result<Self::Model>> + Send + 'a {
		async move {
			let mut model = model.clone();
			self.before_save(&mut model)?;
			Manager::<Self::Model>::new().update(&model).await
		}
	}

	/// Update an existing record using an explicit connection.
	fn update_with_conn<'a>(
		&'a self,
		conn: &'a DatabaseConnection,
		model: &'a Self::Model,
	) -> impl Future<Output = reinhardt_core::exception::Result<Self::Model>> + Send + 'a {
		async move {
			let mut model = model.clone();
			self.before_save(&mut model)?;
			Manager::<Self::Model>::new()
				.update_with_conn(conn, &model)
				.await
		}
	}

	/// Delete a record by primary key.
	fn delete<'a>(
		&'a self,
		pk: <Self::Model as Model>::PrimaryKey,
	) -> impl Future<Output = reinhardt_core::exception::Result<()>> + Send + 'a {
		async move {
			let conn = super::manager::get_connection().await?;
			self.delete_with_conn(&conn, pk).await
		}
	}

	/// Delete a record by primary key using an explicit connection.
	fn delete_with_conn<'a>(
		&'a self,
		conn: &'a DatabaseConnection,
		pk: <Self::Model as Model>::PrimaryKey,
	) -> impl Future<Output = reinhardt_core::exception::Result<()>> + Send + 'a {
		async move {
			let manager = Manager::<Self::Model>::new();
			if let Some(model) = manager.get(pk.clone()).first_with_db(conn).await? {
				self.before_delete(&model)?;
			}
			manager.delete_with_conn(conn, pk).await
		}
	}

	/// Count records.
	fn count<'a>(
		&'a self,
	) -> impl Future<Output = reinhardt_core::exception::Result<i64>> + Send + 'a {
		async move { Manager::<Self::Model>::new().count().await }
	}

	/// Count records using an explicit connection.
	fn count_with_conn<'a>(
		&'a self,
		conn: &'a DatabaseConnection,
	) -> impl Future<Output = reinhardt_core::exception::Result<i64>> + Send + 'a {
		async move { Manager::<Self::Model>::new().count_with_conn(conn).await }
	}

	/// Retrieve a record matching `lookup_fields`, or insert with `defaults`.
	fn get_or_create<'a>(
		&'a self,
		lookup_fields: HashMap<String, String>,
		defaults: Option<HashMap<String, String>>,
	) -> impl Future<Output = reinhardt_core::exception::Result<(Self::Model, bool)>> + Send + 'a {
		async move {
			Manager::<Self::Model>::new()
				.get_or_create(lookup_fields, defaults)
				.await
		}
	}

	/// Bulk-insert multiple records (Django: `bulk_create`).
	fn bulk_create<'a>(
		&'a self,
		models: Vec<Self::Model>,
		batch_size: Option<usize>,
		ignore_conflicts: bool,
		update_conflicts: bool,
	) -> impl Future<Output = reinhardt_core::exception::Result<Vec<Self::Model>>> + Send + 'a
	where
		Self::Model: 'a,
	{
		async move {
			Manager::<Self::Model>::new()
				.bulk_create(models, batch_size, ignore_conflicts, update_conflicts)
				.await
		}
	}

	/// Bulk-update multiple records (Django: `bulk_update`).
	fn bulk_update<'a>(
		&'a self,
		models: Vec<Self::Model>,
		fields: Vec<String>,
		batch_size: Option<usize>,
	) -> impl Future<Output = reinhardt_core::exception::Result<usize>> + Send + 'a
	where
		Self::Model: 'a,
	{
		async move {
			if models.is_empty() || fields.is_empty() {
				return Ok(0);
			}

			let mut models = models;
			self.before_bulk_update(&mut models)?;
			Manager::<Self::Model>::new()
				.bulk_update(models, fields, batch_size)
				.await
		}
	}

	// =========================================================================
	// SQL builder utilities (8 methods) — default impls delegate to Manager<M>
	// =========================================================================

	/// Build the `INSERT` statement for a bulk-create call.
	fn bulk_create_query(&self, models: &[Self::Model]) -> Option<InsertStatement> {
		Manager::<Self::Model>::new().bulk_create_query(models)
	}

	/// Render the bulk-create SQL for a backend.
	fn bulk_create_sql(&self, models: &[Self::Model], backend: DatabaseBackend) -> String {
		Manager::<Self::Model>::new().bulk_create_sql(models, backend)
	}

	/// Build the `UPDATE` SQL for a `QuerySet`.
	fn update_queryset(
		&self,
		queryset: &QuerySet<Self::Model>,
		updates: &[(&str, &str)],
	) -> (String, Vec<String>) {
		Manager::<Self::Model>::new().update_queryset(queryset, updates)
	}

	/// Build the `DELETE` SQL for a `QuerySet`.
	fn delete_queryset(&self, queryset: &QuerySet<Self::Model>) -> (String, Vec<String>) {
		Manager::<Self::Model>::new().delete_queryset(queryset)
	}

	/// Build the `(SELECT, INSERT)` statement pair used by `get_or_create`.
	fn get_or_create_queries(
		&self,
		lookup_fields: &HashMap<String, String>,
		defaults: &HashMap<String, String>,
	) -> (SelectStatement, InsertStatement) {
		Manager::<Self::Model>::new().get_or_create_queries(lookup_fields, defaults)
	}

	/// Build the SQL strings used by `get_or_create`.
	fn get_or_create_sql(
		&self,
		lookup_fields: &HashMap<String, String>,
		defaults: &HashMap<String, String>,
		backend: DatabaseBackend,
	) -> (String, String) {
		Manager::<Self::Model>::new().get_or_create_sql(lookup_fields, defaults, backend)
	}

	/// Build the bulk-create SQL given pre-extracted `field_names` and rows.
	fn bulk_create_sql_detailed(
		&self,
		field_names: &[String],
		value_rows: &[Vec<serde_json::Value>],
		ignore_conflicts: bool,
	) -> String {
		Manager::<Self::Model>::new().bulk_create_sql_detailed(
			field_names,
			value_rows,
			ignore_conflicts,
		)
	}

	/// Build the bulk-update SQL using `CASE` expressions.
	///
	/// The `(PrimaryKey, HashMap<String, Value>)` slice mirrors the shape used
	/// by [`Manager::bulk_update_sql_detailed`]; routing it through an
	/// associated-type projection trips `clippy::type_complexity`, which we
	/// silence here because the signature is fixed by the underlying inherent
	/// method we delegate to.
	#[allow(clippy::type_complexity)]
	fn bulk_update_sql_detailed(
		&self,
		updates: &[(
			<Self::Model as Model>::PrimaryKey,
			HashMap<String, serde_json::Value>,
		)],
		fields: &[String],
		backend: DatabaseBackend,
	) -> String
	where
		<Self::Model as Model>::PrimaryKey: std::fmt::Display + Clone,
	{
		Manager::<Self::Model>::new().bulk_update_sql_detailed(updates, fields, backend)
	}

	// =========================================================================
	// Hooks (3 methods) — default to no-op
	// =========================================================================

	/// Hook invoked before a `create` or `update`. Returning `Err(_)` vetoes
	/// the write.
	fn before_save(&self, _model: &mut Self::Model) -> reinhardt_core::exception::Result<()> {
		Ok(())
	}

	/// Hook invoked before a `delete`. Returning `Err(_)` vetoes the delete.
	fn before_delete(&self, _model: &Self::Model) -> reinhardt_core::exception::Result<()> {
		Ok(())
	}

	/// Hook invoked before a `bulk_update`. Returning `Err(_)` vetoes the
	/// entire batch; mutating `models` in place lets implementations rewrite
	/// records before the update is built.
	fn before_bulk_update(
		&self,
		_models: &mut [Self::Model],
	) -> reinhardt_core::exception::Result<()> {
		Ok(())
	}
}

/// Blanket implementation: every existing [`Manager<M>`] is also a
/// [`CustomManager`].
///
/// This means functions generic over `CustomManager<Model = M>` can accept the
/// vanilla manager that `Model::objects()` returns today, preserving full
/// backward compatibility. Custom implementations can be substituted in via
/// the `#[model(manager = MyManager)]` attribute.
impl<M: Model> CustomManager for Manager<M> {
	type Model = M;

	fn new() -> Self {
		Manager::new()
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use crate::orm::fields::{CharField, Field};
	use crate::orm::inspection::FieldInfo;
	use crate::orm::model::FieldSelector;
	use crate::orm::query::{Filter, FilterOperator, FilterValue};
	use serde::{Deserialize, Serialize};
	use std::collections::HashMap;

	#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
	struct Article {
		id: Option<i64>,
		title: String,
	}

	#[derive(Clone, Debug, PartialEq, Eq)]
	struct ArticleFields;

	impl FieldSelector for ArticleFields {
		fn with_alias(self, _alias: &str) -> Self {
			self
		}
	}

	impl Model for Article {
		type PrimaryKey = i64;
		type Fields = ArticleFields;
		type Objects = ArticleManager;

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

		fn new_fields() -> Self::Fields {
			ArticleFields
		}

		fn primary_key(&self) -> Option<Self::PrimaryKey> {
			self.id
		}

		fn set_primary_key(&mut self, value: Self::PrimaryKey) {
			self.id = Some(value);
		}

		fn field_metadata() -> Vec<FieldInfo> {
			let mut id = CharField::new(20);
			id.set_attributes_from_name("id");
			let mut title = CharField::new(255);
			title.set_attributes_from_name("title");
			vec![FieldInfo::from_field(&id), FieldInfo::from_field(&title)]
		}
	}

	#[derive(Default)]
	struct ArticleManager;

	impl CustomManager for ArticleManager {
		type Model = Article;

		fn new() -> Self {
			Self
		}
	}

	#[derive(Default)]
	struct VetoArticleManager;

	impl CustomManager for VetoArticleManager {
		type Model = Article;

		fn new() -> Self {
			Self
		}

		fn before_save(&self, _model: &mut Article) -> reinhardt_core::exception::Result<()> {
			Err(reinhardt_core::exception::Error::Database(
				"save vetoed".to_string(),
			))
		}

		fn before_delete(&self, _model: &Article) -> reinhardt_core::exception::Result<()> {
			Err(reinhardt_core::exception::Error::Database(
				"delete vetoed".to_string(),
			))
		}

		fn before_bulk_update(
			&self,
			_models: &mut [Article],
		) -> reinhardt_core::exception::Result<()> {
			Err(reinhardt_core::exception::Error::Database(
				"bulk update vetoed".to_string(),
			))
		}
	}

	#[test]
	fn custom_manager_get_preserves_the_primary_key_filter() {
		let query = ArticleManager::new().get(42);
		assert_eq!(query.filters().len(), 1);
		assert!(matches!(query.filters()[0].value, FilterValue::Integer(42)));
	}

	#[test]
	fn custom_manager_builder_delegation_preserves_query_output() {
		let manager = ArticleManager::new();
		let filter = manager.filter(Filter::new(
			"title",
			FilterOperator::Eq,
			FilterValue::String("Rust".to_string()),
		));

		assert_eq!(manager.all().to_sql(), "SELECT * FROM \"articles\"");
		assert_eq!(filter.filters().len(), 1);
		assert_eq!(
			filter.to_sql(),
			"SELECT * FROM \"articles\" WHERE \"title\" = 'Rust'"
		);
		assert_eq!(
			manager.limit(3).to_sql(),
			"SELECT * FROM \"articles\" LIMIT 3"
		);
		assert_eq!(
			manager.offset(2).to_sql(),
			"SELECT * FROM \"articles\" OFFSET 2"
		);
		assert_eq!(
			manager.paginate(2, 5).to_sql(),
			"SELECT * FROM \"articles\" LIMIT 5 OFFSET 5"
		);
		assert_eq!(
			manager.order_by(&["-title", "id"]).to_sql(),
			"SELECT * FROM \"articles\" ORDER BY \"title\" DESC, \"id\" ASC"
		);
		assert_eq!(
			manager.defer(&["title"]).to_sql(),
			"SELECT \"id\" FROM \"articles\""
		);
		assert_eq!(
			manager.only(&["id", "title"]).to_sql(),
			"SELECT \"id\", \"title\" FROM \"articles\""
		);
		assert_eq!(
			manager.values(&["title"]).to_sql(),
			"SELECT \"title\" FROM \"articles\""
		);
		assert_eq!(
			manager.values_list(&["id", "title"]).to_sql(),
			"SELECT \"id\", \"title\" FROM \"articles\""
		);
		assert_eq!(
			manager
				.filter_array_overlap("tags", &["rust", "orm"])
				.to_sql(),
			"SELECT * FROM \"articles\" WHERE \"tags\" && ARRAY['rust', 'orm']"
		);
		assert_eq!(
			manager
				.filter_array_contains("tags", &["rust", "orm"])
				.to_sql(),
			"SELECT * FROM \"articles\" WHERE \"tags\" @> ARRAY['rust', 'orm']"
		);
		assert_eq!(
			manager
				.filter_jsonb_contains("metadata", r#"{"published":true}"#)
				.to_sql(),
			"SELECT * FROM \"articles\" WHERE \"metadata\" @> '{\"published\":true}'::jsonb"
		);
		assert_eq!(
			manager
				.filter_jsonb_key_exists("metadata", "published")
				.to_sql(),
			"SELECT * FROM \"articles\" WHERE \"metadata\" ? 'published'"
		);
		assert_eq!(
			manager
				.filter_range_contains("published_range", "2026-08-06")
				.to_sql(),
			"SELECT * FROM \"articles\" WHERE \"published_range\" @> '2026-08-06'"
		);
		assert_eq!(
			manager
				.filter_in_subquery::<Article, _>("id", |query| query.only(&["id"]))
				.to_sql(),
			"SELECT * FROM \"articles\" WHERE \"id\" IN (SELECT \"id\" FROM \"articles\")"
		);
		assert_eq!(
			manager
				.filter_not_in_subquery::<Article, _>("id", |query| query.only(&["id"]))
				.to_sql(),
			"SELECT * FROM \"articles\" WHERE \"id\" NOT IN (SELECT \"id\" FROM \"articles\")"
		);
		assert_eq!(
			manager
				.filter_exists::<Article, _>(|query| query.filter(Filter::new(
					"title",
					FilterOperator::Eq,
					FilterValue::String("Rust".to_string()),
				)))
				.to_sql(),
			"SELECT * FROM \"articles\" WHERE EXISTS (SELECT * FROM \"articles\" WHERE \"title\" = 'Rust')"
		);
		assert_eq!(
			manager
				.filter_not_exists::<Article, _>(|query| query.filter(Filter::new(
					"title",
					FilterOperator::Eq,
					FilterValue::String("Rust".to_string()),
				)))
				.to_sql(),
			"SELECT * FROM \"articles\" WHERE NOT EXISTS (SELECT * FROM \"articles\" WHERE \"title\" = 'Rust')"
		);
		assert_eq!(
			manager
				.with_cte(CTE::new("published_articles", "SELECT id FROM articles"))
				.to_sql(),
			"WITH published_articles AS (SELECT id FROM articles) SELECT * FROM \"articles\""
		);
		assert_eq!(
			manager.full_text_search("title", "rust orm").to_sql(),
			"SELECT * FROM \"articles\" WHERE \"title\" @@ plainto_tsquery('english', 'rust orm')"
		);
	}

	#[test]
	fn custom_manager_sql_utilities_preserve_complete_statements() {
		let manager = ArticleManager::new();
		let queryset = manager.filter(Filter::new(
			"title",
			FilterOperator::Eq,
			FilterValue::String("Rust".to_string()),
		));
		let mut lookup_fields = HashMap::new();
		lookup_fields.insert("title".to_string(), "Rust".to_string());
		let mut defaults = HashMap::new();
		defaults.insert("title".to_string(), "Rust ORM".to_string());

		assert_eq!(
			manager.update_queryset(&queryset, &[("title", "Rust ORM")]),
			(
				"UPDATE \"articles\" SET \"title\" = $1 WHERE \"title\" = $2".to_string(),
				vec!["Rust ORM".to_string(), "Rust".to_string()],
			)
		);
		assert_eq!(
			manager.delete_queryset(&queryset),
			(
				"DELETE FROM \"articles\" WHERE \"title\" = $1".to_string(),
				vec!["Rust".to_string()],
			)
		);
		assert_eq!(
			manager.get_or_create_sql(&lookup_fields, &defaults, DatabaseBackend::Sqlite),
			(
				"SELECT * FROM \"articles\" WHERE \"title\" = ?".to_string(),
				"INSERT INTO \"articles\" (\"title\") VALUES (?)".to_string(),
			)
		);
	}

	#[test]
	fn custom_manager_default_hooks_are_noops() {
		let manager = ArticleManager::new();
		let mut article = Article {
			id: Some(1),
			title: "unchanged".to_string(),
		};
		assert_eq!(Article::new_fields().with_alias("articles"), ArticleFields);
		assert_eq!(article.primary_key(), Some(1));
		article.set_primary_key(2);
		assert_eq!(article.primary_key(), Some(2));

		assert!(manager.before_save(&mut article).is_ok());
		assert!(manager.before_delete(&article).is_ok());
		assert!(
			manager
				.before_bulk_update(std::slice::from_mut(&mut article))
				.is_ok()
		);
	}

	#[cfg(feature = "sqlite")]
	#[tokio::test]
	async fn custom_manager_vetoes_create_before_writing_to_sqlite() {
		let connection = DatabaseConnection::connect_sqlite("sqlite::memory:")
			.await
			.expect("in-memory SQLite connection should be available");
		connection
			.execute(
				"CREATE TABLE articles (id INTEGER PRIMARY KEY, title TEXT NOT NULL)",
				Vec::new(),
			)
			.await
			.expect("articles table should be created");
		let article = Article {
			id: None,
			title: "blocked".to_string(),
		};

		let result = VetoArticleManager::new()
			.create_with_conn(&connection, &article)
			.await;

		assert!(matches!(
			result,
			Err(reinhardt_core::exception::Error::Database(message)) if message == "save vetoed"
		));
		let row = connection
			.query_one("SELECT COUNT(*) AS count FROM articles", Vec::new())
			.await
			.expect("article count should be queryable");
		assert_eq!(row.get::<i64>("count"), Some(0));
	}

	#[cfg(feature = "sqlite")]
	#[tokio::test]
	async fn custom_manager_vetoes_delete_without_removing_the_sqlite_row() {
		let connection = DatabaseConnection::connect_sqlite("sqlite::memory:")
			.await
			.expect("in-memory SQLite connection should be available");
		connection
			.execute(
				"CREATE TABLE articles (id INTEGER PRIMARY KEY, title TEXT NOT NULL)",
				Vec::new(),
			)
			.await
			.expect("articles table should be created");
		connection
			.execute(
				"INSERT INTO articles (id, title) VALUES (1, 'retained')",
				Vec::new(),
			)
			.await
			.expect("article should be inserted");

		let result = VetoArticleManager::new()
			.delete_with_conn(&connection, 1)
			.await;

		assert!(matches!(
			result,
			Err(reinhardt_core::exception::Error::Database(message)) if message == "delete vetoed"
		));
		let row = connection
			.query_one("SELECT COUNT(*) AS count FROM articles", Vec::new())
			.await
			.expect("article count should be queryable");
		assert_eq!(row.get::<i64>("count"), Some(1));
	}

	#[tokio::test]
	async fn custom_manager_vetoes_bulk_update_before_global_database_lookup() {
		let articles = vec![Article {
			id: Some(1),
			title: "blocked".to_string(),
		}];

		let result = VetoArticleManager::new()
			.bulk_update(articles, vec!["title".to_string()], None)
			.await;

		assert!(matches!(
			result,
			Err(reinhardt_core::exception::Error::Database(message)) if message == "bulk update vetoed"
		));
	}
}