sql-orm 0.2.0-rc.1

Public API crate for the sql-orm workspace.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
//! Public API surface for the SQL Server code-first ORM.
//!
//! Most applications should import [`prelude`] and define entities with the
//! derive macros re-exported there. The crate root also exposes advanced
//! modules for users who need direct access to metadata, query ASTs,
//! migrations, SQL Server compilation, or the Tiberius adapter.

extern crate self as sql_orm;

mod active_record;
mod audit_runtime;
mod context;
mod dbset_query;
mod page_request;
mod predicate_composition;
mod query_alias;
mod query_order;
mod query_predicates;
mod query_projection;
mod raw_sql;
mod soft_delete_runtime;
mod tracking;

pub use sql_orm_core as core;
pub use sql_orm_macros as macros;
pub use sql_orm_migrate as migrate;
pub use sql_orm_query as query;
pub use sql_orm_sqlserver as sqlserver;
pub use sql_orm_tiberius as tiberius;
pub use tokio;

pub use active_record::{ActiveRecord, EntityPersist, EntityPersistMode, EntityPrimaryKey};
pub use audit_runtime::{
    AuditContext, AuditOperation, AuditProvider, AuditRequestValues, AuditValues,
    resolve_audit_values,
};
#[cfg(feature = "pool-bb8")]
pub use context::connect_shared_from_pool;
pub use context::{
    ActiveTenant, DbContext, DbContextEntitySet, DbSet, SharedConnection, connect_shared,
    connect_shared_with_config, connect_shared_with_options,
};
pub use dbset_query::{
    AggregateProjections, CollectionIncludeStrategy, DbSetGroupedQuery, DbSetQuery,
    DbSetQueryIncludeMany, DbSetQueryIncludeOne, GroupByExpressions,
};
pub use page_request::PageRequest;
pub use predicate_composition::PredicateCompositionExt;
pub use query_alias::{AliasedEntityColumn, EntityColumnAliasExt};
pub use query_order::EntityColumnOrderExt;
pub use query_predicates::EntityColumnPredicateExt;
pub use query_projection::SelectProjections;
pub use raw_sql::{QueryHint, RawCommand, RawParam, RawParams, RawQuery};
pub use soft_delete_runtime::{
    SoftDeleteContext, SoftDeleteOperation, SoftDeleteProvider, SoftDeleteRequestValues,
    SoftDeleteValues,
};
pub use sql_orm_core::{EntityMetadata, NavigationKind, NavigationMetadata};
pub use sql_orm_query::{AggregateExpr, AggregateOrderBy, AggregatePredicate, AggregateProjection};
pub use sql_orm_tiberius::{
    MssqlConnectionConfig, MssqlHealthCheckOptions, MssqlHealthCheckQuery, MssqlOperationalOptions,
    MssqlParameterLogMode, MssqlPoolBackend, MssqlPoolOptions, MssqlRetryOptions,
    MssqlSlowQueryOptions, MssqlTimeoutOptions, MssqlTracingOptions,
};
#[cfg(feature = "pool-bb8")]
pub use sql_orm_tiberius::{MssqlPool, MssqlPoolBuilder, MssqlPooledConnection};
pub use tracking::{EntityState, Tracked};
#[doc(hidden)]
pub use tracking::{
    SaveChangesOperationPlan, TrackedEntityRegistration, TrackingRegistry, TrackingRegistryHandle,
    save_changes_operation_plan,
};

/// Provides entity metadata for code-first migration snapshot generation.
///
/// `#[derive(DbContext)]` implements this trait for application contexts by
/// returning the metadata for every `DbSet<T>` declared on the context.
pub trait MigrationModelSource {
    /// Returns the static metadata for all entities owned by the context.
    fn entity_metadata() -> &'static [&'static EntityMetadata];
}

/// Runtime metadata hook for entities that declare `#[orm(audit = Audit)]`.
///
/// The derive macro implements this for every entity. Entities without audit
/// policy return `None`; audited entities return the audit-owned columns as an
/// `EntityPolicyMetadata` view without changing the normal entity metadata
/// shape used by snapshots, diffs, and DDL.
pub trait AuditEntity: core::Entity {
    /// Returns audit-owned columns for this entity when audit is enabled.
    fn audit_policy() -> Option<core::EntityPolicyMetadata>;
}

/// Runtime metadata hook for entities that declare
/// `#[orm(soft_delete = SoftDelete)]`.
///
/// The public delete/read behavior lives in the `sql-orm` crate. Lower
/// layers still see ordinary columns and ordinary query/update AST nodes.
pub trait SoftDeleteEntity: core::Entity {
    /// Returns soft-delete-owned columns for this entity when enabled.
    fn soft_delete_policy() -> Option<core::EntityPolicyMetadata>;
}

/// Runtime value shape for the active tenant configured on a context.
///
/// `#[derive(TenantContext)]` implements this trait for user-defined structs
/// with exactly one field. The field defines both the tenant column name and
/// the SQL value used by tenant-scoped reads and writes.
pub trait TenantContext: core::EntityPolicy {
    /// Physical column name used by tenant-scoped entities.
    const COLUMN_NAME: &'static str;

    /// Converts the active tenant value into the SQL value compared in queries.
    fn tenant_value(&self) -> core::SqlValue;
}

/// Runtime metadata hook for entities that opt into tenant scoping.
///
/// Entities without `#[orm(tenant = CurrentTenant)]` return `None` and remain
/// cross-tenant even when a context has an active tenant configured.
pub trait TenantScopedEntity: core::Entity {
    /// Returns tenant-owned column metadata for tenant-scoped entities.
    fn tenant_policy() -> Option<core::EntityPolicyMetadata>;
}

/// Contract generated for entities that can receive an included single
/// navigation value.
///
/// This is used by `DbSetQuery::include::<T>(...)` for `belongs_to` and
/// `has_one` navigations. Collection includes use a different loading strategy
/// and are intentionally not part of this contract.
pub trait IncludeNavigation<T>: core::Entity {
    /// Attaches a loaded navigation value to the field named by `navigation`.
    fn set_included_navigation(
        &mut self,
        navigation: &str,
        value: Option<T>,
    ) -> Result<(), core::OrmError>;
}

/// Contract generated for entities that can receive an included collection
/// navigation value.
///
/// This is used by `DbSetQuery::include_many::<T>(...)` for `has_many`
/// navigations. The query layer groups duplicate root rows before assigning
/// the loaded collection.
pub trait IncludeCollection<T>: core::Entity {
    /// Attaches loaded collection values to the field named by `navigation`.
    fn set_included_collection(
        &mut self,
        navigation: &str,
        values: Vec<T>,
    ) -> Result<(), core::OrmError>;
}

/// Marker value for a single related entity navigation.
///
/// Navigation fields are not persisted as columns. They exist so
/// `#[derive(Entity)]` can attach navigation metadata to the entity while
/// future loading APIs decide explicitly when related rows are fetched.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Navigation<T> {
    value: Option<T>,
}

impl<T> Navigation<T> {
    /// Creates an empty navigation value.
    pub const fn empty() -> Self {
        Self { value: None }
    }

    /// Creates a navigation value containing a loaded related entity.
    pub fn loaded(value: T) -> Self {
        Self { value: Some(value) }
    }

    /// Creates a navigation value from an optional related entity.
    pub fn from_option(value: Option<T>) -> Self {
        Self { value }
    }

    /// Returns the loaded related entity when one has been attached.
    pub fn as_ref(&self) -> Option<&T> {
        self.value.as_ref()
    }

    /// Replaces the loaded related entity.
    pub fn set(&mut self, value: Option<T>) {
        self.value = value;
    }
}

impl<T> Default for Navigation<T> {
    fn default() -> Self {
        Self::empty()
    }
}

/// Opt-in lazy single navigation wrapper.
///
/// This type never performs I/O by itself. It only records whether a related
/// value has been explicitly loaded by an ORM operation such as `include(...)`
/// or a future explicit lazy-loading API that receives a context-bearing value.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LazyNavigation<T> {
    value: Option<T>,
    loaded: bool,
}

impl<T> LazyNavigation<T> {
    /// Creates an unloaded lazy navigation.
    pub const fn unloaded() -> Self {
        Self {
            value: None,
            loaded: false,
        }
    }

    /// Creates a loaded lazy navigation containing a related entity.
    pub fn loaded(value: T) -> Self {
        Self {
            value: Some(value),
            loaded: true,
        }
    }

    /// Creates a loaded lazy navigation from an optional related entity.
    pub fn from_option(value: Option<T>) -> Self {
        Self {
            value,
            loaded: true,
        }
    }

    /// Returns whether a load operation has populated this wrapper.
    pub fn is_loaded(&self) -> bool {
        self.loaded
    }

    /// Returns the loaded related entity when one is present.
    ///
    /// This is a memory-only accessor. It never executes SQL.
    pub fn as_ref(&self) -> Option<&T> {
        self.value.as_ref()
    }

    /// Replaces the loaded value and marks this wrapper as loaded.
    pub fn set_loaded(&mut self, value: Option<T>) {
        self.value = value;
        self.loaded = true;
    }

    /// Clears the cached value and marks this wrapper as unloaded.
    pub fn clear(&mut self) {
        self.value = None;
        self.loaded = false;
    }
}

impl<T> Default for LazyNavigation<T> {
    fn default() -> Self {
        Self::unloaded()
    }
}

/// Marker value for a collection navigation.
///
/// Collection navigation fields are ignored by column metadata and start empty
/// when an entity is materialized without an explicit include/load operation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Collection<T> {
    values: Vec<T>,
}

impl<T> Collection<T> {
    /// Creates an empty collection navigation.
    pub const fn empty() -> Self {
        Self { values: Vec::new() }
    }

    /// Creates a loaded collection navigation from existing values.
    pub fn from_vec(values: Vec<T>) -> Self {
        Self { values }
    }

    /// Returns the loaded related entities.
    pub fn as_slice(&self) -> &[T] {
        &self.values
    }
}

impl<T> Default for Collection<T> {
    fn default() -> Self {
        Self { values: Vec::new() }
    }
}

/// Opt-in lazy collection navigation wrapper.
///
/// This type stores loaded values and load state, but it never owns a database
/// context and never performs I/O from accessors, formatting, cloning or
/// comparison. Loading must happen through an explicit ORM method.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LazyCollection<T> {
    values: Vec<T>,
    loaded: bool,
}

impl<T> LazyCollection<T> {
    /// Creates an unloaded lazy collection.
    pub const fn unloaded() -> Self {
        Self {
            values: Vec::new(),
            loaded: false,
        }
    }

    /// Creates a loaded lazy collection from existing values.
    pub fn from_vec(values: Vec<T>) -> Self {
        Self {
            values,
            loaded: true,
        }
    }

    /// Returns whether a load operation has populated this wrapper.
    pub fn is_loaded(&self) -> bool {
        self.loaded
    }

    /// Returns loaded related entities.
    ///
    /// This is a memory-only accessor. It never executes SQL.
    pub fn as_slice(&self) -> &[T] {
        &self.values
    }

    /// Replaces the loaded values and marks this wrapper as loaded.
    pub fn set_loaded(&mut self, values: Vec<T>) {
        self.values = values;
        self.loaded = true;
    }

    /// Clears the cached values and marks this wrapper as unloaded.
    pub fn clear(&mut self) {
        self.values.clear();
        self.loaded = false;
    }
}

impl<T> Default for LazyCollection<T> {
    fn default() -> Self {
        Self::unloaded()
    }
}

/// Builds a model snapshot from a context type that exposes entity metadata.
///
/// This is the helper used by consumer snapshot-export binaries.
pub fn model_snapshot_from_source<S: MigrationModelSource>() -> migrate::ModelSnapshot {
    migrate::ModelSnapshot::from_entities(S::entity_metadata())
}

/// Serializes the current model snapshot for a context as pretty JSON.
///
/// Consumer projects can print this from a small binary and pass it to the CLI
/// through `migration add --snapshot-bin`.
pub fn model_snapshot_json_from_source<S: MigrationModelSource>() -> Result<String, core::OrmError>
{
    model_snapshot_from_source::<S>().to_json_pretty()
}

pub mod prelude {
    pub use crate::AliasedEntityColumn;
    #[cfg(feature = "pool-bb8")]
    pub use crate::connect_shared_from_pool;
    pub use crate::{
        ActiveRecord, ActiveTenant, AggregateProjections, AuditEntity, Collection,
        CollectionIncludeStrategy, DbContext, DbContextEntitySet, DbSet, DbSetGroupedQuery,
        DbSetQuery, DbSetQueryIncludeMany, DbSetQueryIncludeOne, EntityColumnAliasExt,
        EntityColumnOrderExt, EntityColumnPredicateExt, EntityState, GroupByExpressions,
        IncludeCollection, IncludeNavigation, LazyCollection, LazyNavigation, MigrationModelSource,
        MssqlConnectionConfig, MssqlHealthCheckOptions, MssqlHealthCheckQuery,
        MssqlOperationalOptions, MssqlParameterLogMode, MssqlPoolBackend, MssqlPoolOptions,
        MssqlRetryOptions, MssqlSlowQueryOptions, MssqlTimeoutOptions, MssqlTracingOptions,
        Navigation, PageRequest, PredicateCompositionExt, QueryHint, RawCommand, RawParam,
        RawParams, RawQuery, SelectProjections, SharedConnection, SoftDeleteContext,
        SoftDeleteEntity, SoftDeleteOperation, SoftDeleteProvider, SoftDeleteRequestValues,
        SoftDeleteValues, TenantContext, TenantScopedEntity, Tracked, model_snapshot_from_source,
        model_snapshot_json_from_source,
    };
    pub use crate::{
        AuditContext, AuditOperation, AuditProvider, AuditRequestValues, AuditValues,
        resolve_audit_values,
    };
    #[cfg(feature = "pool-bb8")]
    pub use crate::{MssqlPool, MssqlPoolBuilder, MssqlPooledConnection};
    pub use sql_orm_core::{
        Changeset, ColumnMetadata, ColumnValue, Entity, EntityColumn, EntityMetadata, EntityPolicy,
        EntityPolicyMetadata, ForeignKeyMetadata, FromRow, IdentityMetadata, IndexColumnMetadata,
        IndexMetadata, Insertable, NavigationKind, NavigationMetadata, OrmError,
        PrimaryKeyMetadata, ReferentialAction, Row, SqlServerType, SqlTypeMapping, SqlValue,
    };
    pub use sql_orm_macros::{
        AuditFields, Changeset, DbContext, Entity, FromRow, Insertable, SoftDeleteFields,
        TenantContext,
    };
    pub use sql_orm_query::{
        AggregateExpr, AggregateOrderBy, AggregatePredicate, AggregateProjection, Join, JoinType,
        SelectProjection,
    };
}

#[cfg(test)]
mod tests {
    use super::prelude::{
        ActiveRecord, ActiveTenant, AuditContext, AuditEntity, AuditFields, AuditOperation,
        AuditProvider, AuditRequestValues, AuditValues, Changeset, ColumnValue, DbContext,
        DbContextEntitySet, DbSet, Entity, EntityColumn, EntityColumnOrderExt,
        EntityColumnPredicateExt, EntityMetadata, EntityPolicy, EntityPolicyMetadata, EntityState,
        IdentityMetadata, Insertable, LazyCollection, LazyNavigation, MssqlConnectionConfig,
        MssqlOperationalOptions, MssqlPoolBackend, MssqlPoolOptions, MssqlRetryOptions,
        MssqlTimeoutOptions, NavigationKind, NavigationMetadata, OrmError, PageRequest,
        PredicateCompositionExt, PrimaryKeyMetadata, QueryHint, RawCommand, RawParam, RawParams,
        RawQuery, SelectProjection, SelectProjections, SharedConnection, SoftDeleteEntity,
        SoftDeleteFields, SqlServerType, SqlTypeMapping, SqlValue, TenantContext,
        TenantScopedEntity, Tracked,
    };
    use sql_orm_query::{Expr, OrderBy, Predicate, SortDirection, TableRef};
    use std::time::Duration;

    struct PublicEntity;

    static PUBLIC_ENTITY_METADATA: EntityMetadata = EntityMetadata {
        rust_name: "PublicEntity",
        schema: "dbo",
        table: "public_entities",
        renamed_from: None,
        columns: &[],
        primary_key: PrimaryKeyMetadata {
            name: None,
            columns: &[],
        },
        indexes: &[],
        foreign_keys: &[],
        navigations: &[],
    };

    impl Entity for PublicEntity {
        fn metadata() -> &'static EntityMetadata {
            &PUBLIC_ENTITY_METADATA
        }
    }

    struct PublicPolicy;

    impl EntityPolicy for PublicPolicy {
        const POLICY_NAME: &'static str = "public_policy";
        const COLUMN_NAMES: &'static [&'static str] = &[];

        fn columns() -> &'static [super::core::ColumnMetadata] {
            &[]
        }
    }

    #[allow(dead_code)]
    #[derive(SoftDeleteFields)]
    struct PublicSoftDelete {
        #[orm(sql_type = "datetime2")]
        deleted_at: Option<String>,

        #[orm(nullable)]
        #[orm(length = 120)]
        deleted_by: Option<String>,
    }

    #[allow(dead_code)]
    #[derive(AuditFields)]
    struct PublicAudit {
        #[orm(created_at)]
        #[orm(default_sql = "SYSUTCDATETIME()")]
        #[orm(sql_type = "datetime2")]
        #[orm(updatable = false)]
        created_at: String,

        #[orm(created_by)]
        #[orm(column = "created_by_user_id")]
        created_by: Option<i64>,

        #[orm(updated_by)]
        #[orm(nullable)]
        #[orm(length = 120)]
        updated_by: Option<String>,
    }

    #[allow(dead_code)]
    #[derive(TenantContext)]
    struct PublicTenant {
        #[orm(column = "company_id")]
        tenant_id: i64,
    }

    #[test]
    fn exposes_public_prelude() {
        let error = OrmError::new("public-api");
        let raw_query_type = core::any::type_name::<RawQuery<PublicEntity>>();
        let raw_command_type = core::any::type_name::<RawCommand>();
        let projection_type = core::any::type_name::<SelectProjection>();
        let query_hint = QueryHint::Recompile;
        fn assert_raw_param<T: RawParam>() {}
        fn assert_raw_params<T: RawParams>() {}
        fn assert_select_projections<T: SelectProjections>() {}

        assert!(raw_query_type.contains("RawQuery"));
        assert!(raw_command_type.contains("RawCommand"));
        assert!(projection_type.contains("SelectProjection"));
        assert_raw_param::<i64>();
        assert_raw_param::<SqlValue>();
        assert_raw_params::<(bool, i64)>();
        assert_select_projections::<(EntityColumn<PublicEntity>,)>();
        assert_eq!(query_hint, QueryHint::Recompile);
        assert_eq!(error.message(), "public-api");
        assert_eq!(
            ColumnValue::new("email", SqlValue::String("ana@example.com".to_string())),
            ColumnValue {
                column_name: "email",
                value: SqlValue::String("ana@example.com".to_string()),
            }
        );
        assert_eq!(String::SQL_SERVER_TYPE, SqlServerType::NVarChar);
        assert_eq!(PageRequest::new(2, 25).page, 2);
    }

    #[test]
    fn exposes_entity_contract_in_prelude() {
        assert_eq!(PublicEntity::metadata().table, "public_entities");
    }

    #[test]
    fn exposes_navigation_metadata_contract_in_prelude() {
        let navigation = NavigationMetadata::new(
            "owner",
            NavigationKind::BelongsTo,
            "User",
            "auth",
            "users",
            &["owner_id"],
            &["id"],
            Some("fk_posts_owner_id_users"),
        );

        assert_eq!(navigation.rust_field, "owner");
        assert_eq!(navigation.kind, NavigationKind::BelongsTo);
        assert!(navigation.targets_table("auth", "users"));
        assert!(navigation.uses_foreign_key("fk_posts_owner_id_users"));
    }

    #[test]
    fn lazy_navigation_wrappers_are_memory_only_state_containers() {
        let mut owner = LazyNavigation::unloaded();
        assert!(!owner.is_loaded());
        assert_eq!(owner.as_ref(), None);

        owner.set_loaded(Some(7_i64));
        assert!(owner.is_loaded());
        assert_eq!(owner.as_ref(), Some(&7_i64));

        let cloned = owner.clone();
        assert_eq!(
            format!("{:?}", cloned),
            "LazyNavigation { value: Some(7), loaded: true }"
        );

        owner.clear();
        assert!(!owner.is_loaded());
        assert_eq!(owner.as_ref(), None);

        let mut children = LazyCollection::unloaded();
        assert!(!children.is_loaded());
        assert!(children.as_slice().is_empty());

        children.set_loaded(vec![1_i64, 2_i64]);
        assert!(children.is_loaded());
        assert_eq!(children.as_slice(), &[1_i64, 2_i64]);

        let cloned = children.clone();
        assert_eq!(
            format!("{:?}", cloned),
            "LazyCollection { values: [1, 2], loaded: true }"
        );

        children.clear();
        assert!(!children.is_loaded());
        assert!(children.as_slice().is_empty());
    }

    #[test]
    fn exposes_entity_policy_contract_in_prelude() {
        assert_eq!(
            PublicPolicy::metadata(),
            EntityPolicyMetadata::new("public_policy", &[])
        );
    }

    #[test]
    fn exposes_audit_entity_contract_in_prelude() {
        struct PublicAuditEntity;

        impl Entity for PublicAuditEntity {
            fn metadata() -> &'static EntityMetadata {
                &PUBLIC_ENTITY_METADATA
            }
        }

        impl AuditEntity for PublicAuditEntity {
            fn audit_policy() -> Option<EntityPolicyMetadata> {
                Some(EntityPolicyMetadata::new("audit", &[]))
            }
        }

        assert_eq!(
            PublicAuditEntity::audit_policy(),
            Some(EntityPolicyMetadata::new("audit", &[]))
        );
    }

    #[test]
    fn exposes_soft_delete_contract_in_prelude() {
        struct PublicSoftDeleteEntity;

        impl Entity for PublicSoftDeleteEntity {
            fn metadata() -> &'static EntityMetadata {
                &PUBLIC_ENTITY_METADATA
            }
        }

        impl SoftDeleteEntity for PublicSoftDeleteEntity {
            fn soft_delete_policy() -> Option<EntityPolicyMetadata> {
                Some(EntityPolicyMetadata::new("soft_delete", &[]))
            }
        }

        assert_eq!(
            PublicSoftDeleteEntity::soft_delete_policy(),
            Some(EntityPolicyMetadata::new("soft_delete", &[]))
        );
    }

    #[test]
    fn exposes_tenant_contract_in_prelude() {
        struct PublicTenantEntity;

        impl Entity for PublicTenantEntity {
            fn metadata() -> &'static EntityMetadata {
                &PUBLIC_ENTITY_METADATA
            }
        }

        impl TenantScopedEntity for PublicTenantEntity {
            fn tenant_policy() -> Option<EntityPolicyMetadata> {
                Some(EntityPolicyMetadata::new("tenant", &[]))
            }
        }

        assert_eq!(
            PublicTenantEntity::tenant_policy(),
            Some(EntityPolicyMetadata::new("tenant", &[]))
        );
    }

    #[test]
    fn exposes_audit_runtime_contract_in_prelude() {
        struct PublicAuditProvider;

        impl AuditProvider for PublicAuditProvider {
            fn values(&self, context: AuditContext<'_>) -> Result<Vec<ColumnValue>, OrmError> {
                assert_eq!(context.operation, AuditOperation::Update);
                assert!(context.request_values.is_some());

                Ok(vec![ColumnValue::new(
                    "updated_at",
                    SqlValue::String("provider-updated-at".to_string()),
                )])
            }
        }

        let request_values = AuditRequestValues::new(vec![ColumnValue::new(
            "updated_by",
            SqlValue::String("request-updated-by".to_string()),
        )]);
        let context = AuditContext {
            entity: PublicEntity::metadata(),
            operation: AuditOperation::Update,
            request_values: Some(&request_values),
        };

        let provider = PublicAuditProvider;
        let values = provider.values(context).unwrap();

        assert_eq!(request_values.values()[0].column_name, "updated_by");
        assert_eq!(values[0].column_name, "updated_at");
    }

    #[test]
    fn derives_audit_fields_policy_metadata_from_public_prelude() {
        let metadata = PublicAudit::metadata();

        assert_eq!(metadata.name, "audit");
        assert_eq!(metadata.columns.len(), 3);
        assert_eq!(metadata.columns[0].rust_field, "created_at");
        assert_eq!(metadata.columns[0].column_name, "created_at");
        assert_eq!(metadata.columns[0].sql_type, SqlServerType::DateTime2);
        assert_eq!(metadata.columns[0].default_sql, Some("SYSUTCDATETIME()"));
        assert!(metadata.columns[0].insertable);
        assert!(!metadata.columns[0].updatable);
        assert_eq!(metadata.columns[1].column_name, "created_by_user_id");
        assert!(metadata.columns[1].nullable);
        assert_eq!(metadata.columns[1].sql_type, SqlServerType::BigInt);
        assert_eq!(metadata.columns[2].max_length, Some(120));
        assert!(metadata.columns[2].updatable);
        assert_eq!(
            <PublicAudit as EntityPolicy>::COLUMN_NAMES,
            &["created_at", "created_by_user_id", "updated_by"]
        );

        let audit_values = PublicAudit {
            created_at: "2026-04-28T00:00:00Z".to_string(),
            created_by: Some(7),
            updated_by: None,
        }
        .audit_values();

        assert_eq!(
            audit_values,
            vec![
                ColumnValue::new(
                    "created_at",
                    SqlValue::String("2026-04-28T00:00:00Z".to_string())
                ),
                ColumnValue::new("created_by_user_id", SqlValue::I64(7)),
                ColumnValue::new("updated_by", SqlValue::TypedNull(SqlServerType::NVarChar)),
            ]
        );
    }

    #[test]
    fn derives_tenant_context_policy_metadata_from_public_prelude() {
        let metadata = PublicTenant::metadata();
        let tenant = PublicTenant { tenant_id: 42 };
        let active_tenant = ActiveTenant::from_context(&tenant);

        assert_eq!(metadata.name, "tenant");
        assert_eq!(metadata.columns.len(), 1);
        assert_eq!(metadata.columns[0].rust_field, "tenant_id");
        assert_eq!(metadata.columns[0].column_name, "company_id");
        assert_eq!(metadata.columns[0].sql_type, SqlServerType::BigInt);
        assert!(metadata.columns[0].insertable);
        assert!(!metadata.columns[0].updatable);
        assert_eq!(
            <PublicTenant as EntityPolicy>::COLUMN_NAMES,
            &["company_id"]
        );
        assert_eq!(PublicTenant::COLUMN_NAME, "company_id");
        assert_eq!(tenant.tenant_value(), SqlValue::I64(42));
        assert_eq!(active_tenant.column_name, "company_id");
        assert_eq!(active_tenant.value, SqlValue::I64(42));
    }

    #[test]
    fn exposes_operational_configuration_surface() {
        let options = MssqlOperationalOptions::new()
            .with_timeouts(MssqlTimeoutOptions::new().with_query_timeout(Duration::from_secs(30)))
            .with_retry(MssqlRetryOptions::enabled(
                2,
                Duration::from_millis(50),
                Duration::from_secs(1),
            ))
            .with_pool(MssqlPoolOptions::bb8(12));
        let config = MssqlConnectionConfig::from_connection_string_with_options(
            "server=tcp:localhost,1433;database=master;user=sa;password=Password123;TrustServerCertificate=true",
            options,
        )
        .unwrap();

        assert_eq!(config.options().pool.backend, MssqlPoolBackend::Bb8);
        assert_eq!(config.options().pool.max_size, 12);
    }

    #[cfg(feature = "pool-bb8")]
    #[test]
    fn exposes_pool_surface_when_feature_is_enabled() {
        let builder = super::MssqlPool::builder().max_size(8);

        assert_eq!(builder.options().max_size, 8);
    }

    #[cfg(feature = "pool-bb8")]
    #[test]
    fn exposes_dbcontext_pool_wiring_when_feature_is_enabled() {
        let _from_pool = DerivedDbContext::from_pool;
        let _shared_from_pool = super::connect_shared_from_pool;
    }

    #[test]
    fn exposes_dbcontext_entity_set_contract_in_prelude() {
        fn require_trait<C, E>()
        where
            C: DbContextEntitySet<E>,
            E: Entity,
        {
        }

        require_trait::<DerivedDbContext, DerivedUser>();
    }

    #[test]
    fn exposes_dbcontext_health_check_contract_in_prelude() {
        let _health_check = DerivedDbContext::health_check;
        let _trait_health_check = <DerivedDbContext as DbContext>::health_check;
    }

    #[test]
    fn exposes_dbcontext_soft_delete_runtime_helpers() {
        let _with_soft_delete_provider = DerivedDbContext::with_soft_delete_provider;
        let _with_soft_delete_request_values = DerivedDbContext::with_soft_delete_request_values;
        let _with_soft_delete_values = DerivedDbContext::with_soft_delete_values::<SoftDelete>;
        let _clear_soft_delete_request_values = DerivedDbContext::clear_soft_delete_request_values;
        let _shared_with_soft_delete_values =
            SharedConnection::with_soft_delete_values::<SoftDelete>;
    }

    #[test]
    fn exposes_dbcontext_audit_runtime_helpers() {
        let _with_audit_provider = DerivedDbContext::with_audit_provider;
        let _with_audit_request_values = DerivedDbContext::with_audit_request_values;
        let _clear_audit_request_values = DerivedDbContext::clear_audit_request_values;
        let _shared_with_audit_provider = SharedConnection::with_audit_provider;
        let _shared_with_audit_request_values = SharedConnection::with_audit_request_values;
        let _shared_clear_audit_request_values = SharedConnection::clear_audit_request_values;
    }

    #[test]
    fn exposes_dbcontext_tenant_runtime_helpers() {
        let _with_tenant = DerivedDbContext::with_tenant::<PublicTenant>;
        let _clear_tenant = DerivedDbContext::clear_tenant;
        let _shared_with_tenant = SharedConnection::with_tenant::<PublicTenant>;
        let _shared_clear_tenant = SharedConnection::clear_tenant;
    }

    #[test]
    fn exposes_migration_model_source_contract_in_prelude() {
        fn require_trait<C: super::MigrationModelSource>() {}

        require_trait::<DerivedDbContext>();
        assert_eq!(
            <DerivedDbContext as super::MigrationModelSource>::entity_metadata()
                .iter()
                .map(|metadata| metadata.table)
                .collect::<Vec<_>>(),
            vec!["users", "audit_entries"]
        );
    }

    #[test]
    fn exposes_model_snapshot_export_helpers() {
        let snapshot = super::model_snapshot_from_source::<DerivedDbContext>();
        let json = super::model_snapshot_json_from_source::<DerivedDbContext>().unwrap();

        assert_eq!(
            snapshot
                .schemas
                .iter()
                .flat_map(|schema| schema.tables.iter().map(|table| table.name.as_str()))
                .collect::<Vec<_>>(),
            vec!["users", "audit_entries"]
        );
        assert!(json.contains("\"name\": \"auth\""));
        assert!(json.contains("\"name\": \"users\""));
    }

    #[test]
    fn exposes_active_record_contract_in_prelude() {
        fn require_trait<E: ActiveRecord>() {}

        require_trait::<PublicEntity>();
    }

    #[test]
    fn exposes_tracking_surface_in_prelude() {
        let tracked = Tracked::from_loaded(String::from("tracked"));

        assert_eq!(tracked.state(), EntityState::Unchanged);
        assert_eq!(tracked.current(), "tracked");
    }

    #[allow(dead_code)]
    #[derive(Entity, Debug, Clone)]
    #[orm(table = "users", schema = "auth")]
    #[orm(index(name = "ix_users_email_created_by", columns(email, created_by)))]
    struct DerivedUser {
        #[orm(primary_key)]
        #[orm(identity)]
        id: i64,

        #[orm(length = 180)]
        #[orm(unique)]
        email: String,

        #[orm(nullable)]
        #[orm(index(name = "ix_users_display_name"))]
        display_name: Option<String>,

        #[orm(default_sql = "'system'")]
        created_by: String,

        #[orm(rowversion)]
        version: Vec<u8>,
    }

    #[allow(dead_code)]
    #[derive(Entity, Debug, Clone)]
    struct AuditEntry {
        id: i64,
        payload: String,
    }

    #[allow(dead_code)]
    #[derive(SoftDeleteFields)]
    struct SoftDelete {
        #[orm(deleted_at)]
        deleted_at: Option<String>,
    }

    #[derive(Insertable, Debug, Clone)]
    #[orm(entity = DerivedUser)]
    struct NewDerivedUser {
        email: String,
        display_name: Option<String>,
        #[orm(column = "created_by")]
        author: String,
    }

    #[derive(Changeset, Debug, Clone)]
    #[orm(entity = DerivedUser)]
    struct UpdateDerivedUser {
        email: Option<String>,
        display_name: Option<Option<String>>,
        #[orm(column = "created_by")]
        author: Option<String>,
    }

    #[allow(dead_code)]
    #[derive(DbContext, Debug, Clone)]
    struct DerivedDbContext {
        pub users: DbSet<DerivedUser>,
        pub audit_entries: DbSet<AuditEntry>,
    }

    #[test]
    fn derives_entity_metadata_from_struct_attributes() {
        let metadata = DerivedUser::metadata();

        assert_eq!(metadata.rust_name, "DerivedUser");
        assert_eq!(metadata.schema, "auth");
        assert_eq!(metadata.table, "users");
        assert_eq!(metadata.primary_key.columns, &["id"]);
        assert_eq!(metadata.indexes.len(), 3);

        let id = metadata.field("id").expect("id column metadata");
        assert_eq!(id.sql_type, SqlServerType::BigInt);
        assert_eq!(id.identity, Some(IdentityMetadata::new(1, 1)));
        assert!(!id.insertable);
        assert!(!id.updatable);

        let email = metadata.field("email").expect("email column metadata");
        assert_eq!(email.sql_type, SqlServerType::NVarChar);
        assert_eq!(email.max_length, Some(180));
        assert!(!email.nullable);

        let display_name = metadata
            .field("display_name")
            .expect("display_name column metadata");
        assert!(display_name.nullable);
        assert_eq!(display_name.max_length, Some(255));

        let created_by = metadata
            .field("created_by")
            .expect("created_by column metadata");
        assert_eq!(created_by.default_sql, Some("'system'"));

        let version = metadata.field("version").expect("version column metadata");
        assert_eq!(version.sql_type, SqlServerType::RowVersion);
        assert!(version.rowversion);
        assert!(!version.insertable);
        assert!(!version.updatable);

        assert_eq!(metadata.indexes[0].name, "ux_users_email");
        assert!(metadata.indexes[0].unique);
        assert_eq!(metadata.indexes[1].name, "ix_users_display_name");
        assert!(!metadata.indexes[1].unique);
        assert_eq!(metadata.indexes[2].name, "ix_users_email_created_by");
        assert_eq!(metadata.indexes[2].columns.len(), 2);
        assert_eq!(metadata.indexes[2].columns[0].column_name, "email");
        assert_eq!(metadata.indexes[2].columns[1].column_name, "created_by");
        assert!(!metadata.indexes[2].columns[0].descending);
        assert!(!metadata.indexes[2].columns[1].descending);
    }

    #[test]
    fn derives_default_table_and_primary_key_convention() {
        let metadata = AuditEntry::metadata();

        assert_eq!(metadata.schema, "dbo");
        assert_eq!(metadata.table, "audit_entries");
        assert_eq!(metadata.primary_key.columns, &["id"]);

        let payload = metadata.field("payload").expect("payload column metadata");
        assert_eq!(payload.sql_type, SqlServerType::NVarChar);
        assert_eq!(payload.max_length, Some(255));
        assert!(payload.insertable);
        assert!(payload.updatable);
    }

    #[test]
    fn exposes_static_columns_for_future_query_builder() {
        let email: EntityColumn<DerivedUser> = DerivedUser::email;
        let version = DerivedUser::version;
        let payload = AuditEntry::payload;

        assert_eq!(email.rust_field(), "email");
        assert_eq!(email.column_name(), "email");
        assert_eq!(email.entity_metadata().table, "users");
        assert_eq!(email.metadata().max_length, Some(180));

        assert_eq!(version.column_name(), "version");
        assert_eq!(version.metadata().sql_type, SqlServerType::RowVersion);
        assert!(!version.metadata().insertable);

        assert_eq!(payload.entity_metadata().table, "audit_entries");
        assert_eq!(payload.metadata().column_name, "payload");
    }

    #[test]
    fn exposes_public_column_predicate_extensions() {
        assert_eq!(
            DerivedUser::email.eq("ana@example.com".to_string()),
            Predicate::eq(
                Expr::from(DerivedUser::email),
                Expr::value(SqlValue::String("ana@example.com".to_string()))
            )
        );
        assert_eq!(
            DerivedUser::display_name.is_null(),
            Predicate::is_null(Expr::from(DerivedUser::display_name))
        );
        assert_eq!(
            DerivedUser::email.contains("@example.com"),
            Predicate::like(
                Expr::from(DerivedUser::email),
                Expr::value(SqlValue::String("%@example.com%".to_string()))
            )
        );
        assert_eq!(
            DerivedUser::email.asc(),
            OrderBy::new(TableRef::new("auth", "users"), "email", SortDirection::Asc)
        );
        assert_eq!(
            DerivedUser::email
                .contains("@example.com")
                .and(DerivedUser::display_name.is_not_null()),
            Predicate::and(vec![
                Predicate::like(
                    Expr::from(DerivedUser::email),
                    Expr::value(SqlValue::String("%@example.com%".to_string()))
                ),
                Predicate::is_not_null(Expr::from(DerivedUser::display_name))
            ])
        );
    }

    #[test]
    fn derives_insertable_values_from_named_fields() {
        let insertable = NewDerivedUser {
            email: "ana@example.com".to_string(),
            display_name: None,
            author: "system".to_string(),
        };

        let values = <NewDerivedUser as Insertable<DerivedUser>>::values(&insertable);

        assert_eq!(
            values,
            vec![
                ColumnValue::new("email", SqlValue::String("ana@example.com".to_string())),
                ColumnValue::new("display_name", SqlValue::TypedNull(SqlServerType::NVarChar)),
                ColumnValue::new("created_by", SqlValue::String("system".to_string())),
            ]
        );
    }

    #[test]
    fn derives_changeset_with_outer_option_semantics() {
        let changeset = UpdateDerivedUser {
            email: Some("ana.maria@example.com".to_string()),
            display_name: Some(None),
            author: None,
        };

        let changes = <UpdateDerivedUser as Changeset<DerivedUser>>::changes(&changeset);

        assert_eq!(
            changes,
            vec![
                ColumnValue::new(
                    "email",
                    SqlValue::String("ana.maria@example.com".to_string())
                ),
                ColumnValue::new("display_name", SqlValue::TypedNull(SqlServerType::NVarChar)),
            ]
        );
    }
}