rust-ef 1.6.0

Rust Entity Framework - An EFCore-inspired ORM for Rust
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
//! Relationship types (`BelongsTo<T>`, `HasMany<T>`, `HasOne<T>`).
//!
//! These types serve as container wrappers for navigation properties,
//! analogous to how EFCore represents navigation properties in the model.
//!
//! They are pure marker/container types and do NOT impose entity trait
//! bounds  - ?the constraint belongs at the usage site (DbContext, builders),
//! not on the container itself.
//!
//! ## Lazy loading (v1.1+)
//!
//! Each container holds an optional [`Arc<dyn LazyContext>`] and a `loaded`
//! flag. When lazy loading is enabled on the `DbContext`, `to_list()`
//! attaches a `LazyContext` to every navigation container on every
//! materialized entity. The user can then call `nav.load().await` to
//! trigger a single-entity navigation query on first access; subsequent
//! accesses read from the in-memory cache.
//!
//! `Clone` intentionally drops the lazy context and resets `loaded` to
//! `false`, matching the v1.0 semantics where cloning a navigation
//! container yields an empty (unloaded) copy.

use crate::entity::{IEntityType, IFromRow, ILazyInit};
use crate::error::{EFError, EFResult};
use crate::lazy::{load_collection_lazy, load_scalar_lazy, LazyContext, MAX_LAZY_DEPTH};
use std::marker::PhantomData;
use std::sync::Arc;

// ---------------------------------------------------------------------------
// BelongsTo<T>
// ---------------------------------------------------------------------------

/// Represents a "belongs-to" navigation  - ?the dependent side of a one-to-many
/// or one-to-one relationship where the foreign key lives on this entity.
///
/// Corresponds to EFCore's reference navigation property.
pub struct BelongsTo<T> {
    inner: Option<Box<T>>,
    lazy_ctx: Option<Arc<dyn LazyContext>>,
    loaded: bool,
    _phantom: PhantomData<T>,
}

impl<T> BelongsTo<T> {
    pub fn new() -> Self {
        Self {
            inner: None,
            lazy_ctx: None,
            loaded: false,
            _phantom: PhantomData,
        }
    }

    pub fn with(entity: T) -> Self {
        Self {
            inner: Some(Box::new(entity)),
            lazy_ctx: None,
            loaded: true,
            _phantom: PhantomData,
        }
    }

    pub fn get(&self) -> Option<&T> {
        self.inner.as_deref()
    }

    pub fn get_mut(&mut self) -> Option<&mut T> {
        self.inner.as_deref_mut()
    }

    /// Extracts the related entity, leaving the container empty and unloaded.
    /// Used by batch nested-include loading to collect related entities
    /// across multiple parents into a single slice for batch SQL.
    pub fn take(&mut self) -> Option<T> {
        let taken = self.inner.take();
        self.loaded = false;
        taken.map(|b| *b)
    }

    /// Returns `true` if the navigation has been loaded (either eagerly
    /// via `Include` or lazily via [`load`]).
    pub fn is_loaded(&self) -> bool {
        self.loaded
    }

    /// Attaches a lazy-loading context to this container.
    ///
    /// Called by the `#[derive(EntityType)]` macro's `ILazyInit`
    /// implementation when lazy loading is enabled on the `DbContext`.
    pub fn set_lazy_context(&mut self, ctx: Arc<dyn LazyContext>) {
        self.lazy_ctx = Some(ctx);
    }

    /// Triggers lazy loading of this reference navigation.
    ///
    /// If the navigation is already loaded, this is a no-op. If no
    /// [`LazyContext`] is attached (lazy loading disabled), returns
    /// `Ok(())` without loading.
    ///
    /// After this call, [`get`] returns the loaded entity (or `None` if
    /// no matching row was found).
    pub async fn load(&mut self) -> EFResult<()>
    where
        T: IFromRow + IEntityType + ILazyInit,
    {
        if self.loaded {
            return Ok(());
        }
        let Some(ctx) = self.lazy_ctx.clone() else {
            // No lazy context attached — lazy loading disabled.
            return Ok(());
        };
        if ctx.depth() >= MAX_LAZY_DEPTH {
            return Err(EFError::other("lazy loading recursion limit exceeded"));
        }
        let maybe_entity = load_scalar_lazy::<T>(ctx.as_ref()).await?;
        if let Some(mut entity) = maybe_entity {
            // Attach lazy contexts to the child for nested lazy loading.
            let provider = ctx.provider().clone();
            let filter_map = ctx.filter_map().map(|m| Arc::new(m.clone()));
            entity.attach_lazy_contexts(provider, filter_map, ctx.depth() + 1);
            self.inner = Some(Box::new(entity));
        }
        self.loaded = true;
        Ok(())
    }
}

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

impl<T> Clone for BelongsTo<T> {
    fn clone(&self) -> Self {
        Self {
            inner: None, // Navigation properties are not deep-cloned
            lazy_ctx: None,
            loaded: false,
            _phantom: PhantomData,
        }
    }
}

impl<T> std::fmt::Debug for BelongsTo<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("BelongsTo")
            .field("loaded", &self.loaded)
            .finish()
    }
}

// ---------------------------------------------------------------------------
// HasMany<T>
// ---------------------------------------------------------------------------

/// Represents a "has-many" navigation  - ?a collection of related entities.
///
/// Corresponds to EFCore's collection navigation property
/// (e.g., `ICollection<Post>`).
pub struct HasMany<T, Join = ()> {
    items: Vec<T>,
    lazy_ctx: Option<Arc<dyn LazyContext>>,
    loaded: bool,
    _phantom: PhantomData<(T, Join)>,
}

impl<T, Join> HasMany<T, Join> {
    pub fn new() -> Self {
        Self {
            items: Vec::new(),
            lazy_ctx: None,
            loaded: false,
            _phantom: PhantomData,
        }
    }

    pub fn with(items: Vec<T>) -> Self {
        Self {
            items,
            lazy_ctx: None,
            loaded: true,
            _phantom: PhantomData,
        }
    }

    pub fn items(&self) -> &[T] {
        &self.items
    }

    pub fn items_mut(&mut self) -> &mut Vec<T> {
        &mut self.items
    }

    pub fn add(&mut self, item: T) {
        self.items.push(item);
    }

    pub fn remove(&mut self, index: usize) -> Option<T> {
        if index < self.items.len() {
            Some(self.items.remove(index))
        } else {
            None
        }
    }

    pub fn len(&self) -> usize {
        self.items.len()
    }

    pub fn is_empty(&self) -> bool {
        self.items.is_empty()
    }

    /// Returns `true` if the navigation has been loaded (either eagerly
    /// via `Include` or lazily via [`load`]).
    pub fn is_loaded(&self) -> bool {
        self.loaded
    }

    /// Attaches a lazy-loading context to this container.
    ///
    /// Called by the `#[derive(EntityType)]` macro's `ILazyInit`
    /// implementation when lazy loading is enabled on the `DbContext`.
    pub fn set_lazy_context(&mut self, ctx: Arc<dyn LazyContext>) {
        self.lazy_ctx = Some(ctx);
    }

    /// Triggers lazy loading of this collection navigation.
    ///
    /// If the navigation is already loaded, this is a no-op. If no
    /// [`LazyContext`] is attached (lazy loading disabled), returns
    /// `Ok(())` without loading.
    ///
    /// After this call, [`items`] returns the loaded collection.
    pub async fn load(&mut self) -> EFResult<()>
    where
        T: IFromRow + IEntityType + ILazyInit,
        Join: 'static,
    {
        if self.loaded {
            return Ok(());
        }
        let Some(ctx) = self.lazy_ctx.clone() else {
            // No lazy context attached — lazy loading disabled.
            return Ok(());
        };
        if ctx.depth() >= MAX_LAZY_DEPTH {
            return Err(EFError::other("lazy loading recursion limit exceeded"));
        }
        let mut entities = load_collection_lazy::<T>(ctx.as_ref()).await?;
        // Attach lazy contexts to each child for nested lazy loading.
        let provider = ctx.provider().clone();
        let filter_map = ctx.filter_map().map(|m| Arc::new(m.clone()));
        let depth = ctx.depth() + 1;
        for entity in &mut entities {
            // Re-bind to split let style for readability.
            let p = provider.clone();
            let fm = filter_map.clone();
            entity.attach_lazy_contexts(p, fm, depth);
        }
        self.items = entities;
        self.loaded = true;
        Ok(())
    }
}

impl<T, Join> Default for HasMany<T, Join> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T, Join> Clone for HasMany<T, Join> {
    fn clone(&self) -> Self {
        Self {
            items: Vec::new(), // Navigation collections are not deep-cloned
            lazy_ctx: None,
            loaded: false,
            _phantom: PhantomData,
        }
    }
}

impl<T, Join> std::fmt::Debug for HasMany<T, Join> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("HasMany")
            .field("loaded", &self.loaded)
            .field("len", &self.items.len())
            .finish()
    }
}

/// Type alias for HasMany with an explicit join entity (many-to-many).
pub type Through<Join> = Join;

// ---------------------------------------------------------------------------
// HasOne<T>
// ---------------------------------------------------------------------------

/// Represents a "has-one" navigation  - ?a single related entity
/// where the foreign key lives on the other side.
pub struct HasOne<T> {
    inner: Option<Box<T>>,
    lazy_ctx: Option<Arc<dyn LazyContext>>,
    loaded: bool,
    _phantom: PhantomData<T>,
}

impl<T> HasOne<T> {
    pub fn new() -> Self {
        Self {
            inner: None,
            lazy_ctx: None,
            loaded: false,
            _phantom: PhantomData,
        }
    }

    pub fn with(entity: T) -> Self {
        Self {
            inner: Some(Box::new(entity)),
            lazy_ctx: None,
            loaded: true,
            _phantom: PhantomData,
        }
    }

    pub fn get(&self) -> Option<&T> {
        self.inner.as_deref()
    }

    pub fn get_mut(&mut self) -> Option<&mut T> {
        self.inner.as_deref_mut()
    }

    /// Extracts the related entity, leaving the container empty and unloaded.
    /// Used by batch nested-include loading to collect related entities
    /// across multiple parents into a single slice for batch SQL.
    pub fn take(&mut self) -> Option<T> {
        let taken = self.inner.take();
        self.loaded = false;
        taken.map(|b| *b)
    }

    /// Returns `true` if the navigation has been loaded.
    pub fn is_loaded(&self) -> bool {
        self.loaded
    }

    /// Attaches a lazy-loading context to this container.
    pub fn set_lazy_context(&mut self, ctx: Arc<dyn LazyContext>) {
        self.lazy_ctx = Some(ctx);
    }

    /// Triggers lazy loading of this reference navigation.
    pub async fn load(&mut self) -> EFResult<()>
    where
        T: IFromRow + IEntityType + ILazyInit,
    {
        if self.loaded {
            return Ok(());
        }
        let Some(ctx) = self.lazy_ctx.clone() else {
            return Ok(());
        };
        if ctx.depth() >= MAX_LAZY_DEPTH {
            return Err(EFError::other("lazy loading recursion limit exceeded"));
        }
        let maybe_entity = load_scalar_lazy::<T>(ctx.as_ref()).await?;
        if let Some(mut entity) = maybe_entity {
            let provider = ctx.provider().clone();
            let filter_map = ctx.filter_map().map(|m| Arc::new(m.clone()));
            entity.attach_lazy_contexts(provider, filter_map, ctx.depth() + 1);
            self.inner = Some(Box::new(entity));
        }
        self.loaded = true;
        Ok(())
    }
}

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

impl<T> Clone for HasOne<T> {
    fn clone(&self) -> Self {
        Self {
            inner: None,
            lazy_ctx: None,
            loaded: false,
            _phantom: PhantomData,
        }
    }
}

impl<T> std::fmt::Debug for HasOne<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("HasOne")
            .field("loaded", &self.loaded)
            .finish()
    }
}

// ---------------------------------------------------------------------------
// DeleteBehavior (for cascade configuration)
// ---------------------------------------------------------------------------

/// Specifies the delete behavior for a relationship.
/// Corresponds to EFCore's `DeleteBehavior`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DeleteBehavior {
    Cascade,
    Restrict,
    SetNull,
    NoAction,
}

impl DeleteBehavior {
    /// Maps to the SQL `ON DELETE` clause keyword.
    pub fn to_sql_clause(self) -> &'static str {
        match self {
            DeleteBehavior::Cascade => "CASCADE",
            DeleteBehavior::Restrict => "RESTRICT",
            DeleteBehavior::SetNull => "SET NULL",
            DeleteBehavior::NoAction => "NO ACTION",
        }
    }
}