prax-query 0.9.0

Type-safe query builder for the Prax ORM
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
//! Lazy loading utilities for relations.
//!
//! This module provides lazy loading wrappers that defer loading of related
//! data until it is actually accessed, improving performance when relations
//! are not always needed.
//!
//! # Example
//!
//! ```rust,ignore
//! use prax_query::lazy::Lazy;
//!
//! struct User {
//!     id: i64,
//!     name: String,
//!     // Posts are lazily loaded
//!     posts: Lazy<Vec<Post>>,
//! }
//!
//! // Posts are not loaded until accessed
//! let posts = user.posts.load(&engine).await?;
//! ```

use std::cell::UnsafeCell;
use std::fmt;
use std::future::Future;
use std::sync::atomic::{AtomicU8, Ordering};

/// State of a lazy value.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
enum LazyState {
    /// Not yet loaded.
    Unloaded = 0,
    /// Currently loading.
    Loading = 1,
    /// Loaded successfully.
    Loaded = 2,
    /// Failed to load.
    Failed = 3,
}

impl From<u8> for LazyState {
    fn from(v: u8) -> Self {
        match v {
            0 => Self::Unloaded,
            1 => Self::Loading,
            2 => Self::Loaded,
            3 => Self::Failed,
            _ => Self::Unloaded,
        }
    }
}

/// A lazily-loaded value.
///
/// The value is not loaded until explicitly requested, allowing
/// for deferred loading of expensive relations.
pub struct Lazy<T> {
    state: AtomicU8,
    value: UnsafeCell<Option<T>>,
}

// SAFETY: Lazy uses atomic operations for state and only allows
// mutable access when state transitions are valid.
unsafe impl<T: Send> Send for Lazy<T> {}
unsafe impl<T: Sync> Sync for Lazy<T> {}

impl<T> Lazy<T> {
    /// Create a new unloaded lazy value.
    pub const fn new() -> Self {
        Self {
            state: AtomicU8::new(LazyState::Unloaded as u8),
            value: UnsafeCell::new(None),
        }
    }

    /// Create a lazy value that is already loaded.
    pub fn loaded(value: T) -> Self {
        Self {
            state: AtomicU8::new(LazyState::Loaded as u8),
            value: UnsafeCell::new(Some(value)),
        }
    }

    /// Check if the value has been loaded.
    #[inline]
    pub fn is_loaded(&self) -> bool {
        LazyState::from(self.state.load(Ordering::Acquire)) == LazyState::Loaded
    }

    /// Check if the value is currently loading.
    #[inline]
    pub fn is_loading(&self) -> bool {
        LazyState::from(self.state.load(Ordering::Acquire)) == LazyState::Loading
    }

    /// Get the value if it has been loaded.
    pub fn get(&self) -> Option<&T> {
        if self.is_loaded() {
            // SAFETY: We only read when state is Loaded, which means
            // the value has been written and won't change.
            unsafe { (*self.value.get()).as_ref() }
        } else {
            None
        }
    }

    /// Get a mutable reference to the value if loaded.
    pub fn get_mut(&mut self) -> Option<&mut T> {
        if self.is_loaded() {
            self.value.get_mut().as_mut()
        } else {
            None
        }
    }

    /// Set the value directly.
    pub fn set(&self, value: T) {
        // SAFETY: We're transitioning to Loaded state
        unsafe {
            *self.value.get() = Some(value);
        }
        self.state.store(LazyState::Loaded as u8, Ordering::Release);
    }

    /// Take the value, leaving the lazy unloaded.
    pub fn take(&mut self) -> Option<T> {
        if self.is_loaded() {
            self.state
                .store(LazyState::Unloaded as u8, Ordering::Release);
            self.value.get_mut().take()
        } else {
            None
        }
    }

    /// Reset to unloaded state.
    pub fn reset(&mut self) {
        self.state
            .store(LazyState::Unloaded as u8, Ordering::Release);
        *self.value.get_mut() = None;
    }

    /// Load the value using the provided async loader function.
    ///
    /// If already loaded, returns the cached value.
    /// If loading fails, the error is returned but the state remains unloaded.
    pub async fn load_with<F, Fut, E>(&self, loader: F) -> Result<&T, E>
    where
        F: FnOnce() -> Fut,
        Fut: Future<Output = Result<T, E>>,
    {
        // Fast path: already loaded
        if self.is_loaded() {
            // SAFETY: State is Loaded, value is immutable
            return Ok(unsafe { (*self.value.get()).as_ref().unwrap() });
        }

        // Try to transition to loading state
        let prev = self.state.compare_exchange(
            LazyState::Unloaded as u8,
            LazyState::Loading as u8,
            Ordering::AcqRel,
            Ordering::Acquire,
        );

        match prev {
            Ok(_) => {
                // We own the loading transition
                match loader().await {
                    Ok(value) => {
                        // SAFETY: We're the only writer (we hold Loading state)
                        unsafe {
                            *self.value.get() = Some(value);
                        }
                        self.state.store(LazyState::Loaded as u8, Ordering::Release);
                        // SAFETY: We just stored the value
                        Ok(unsafe { (*self.value.get()).as_ref().unwrap() })
                    }
                    Err(e) => {
                        self.state
                            .store(LazyState::Unloaded as u8, Ordering::Release);
                        Err(e)
                    }
                }
            }
            Err(current) => {
                // Someone else is loading or already loaded
                match LazyState::from(current) {
                    LazyState::Loaded => {
                        // SAFETY: State is Loaded
                        Ok(unsafe { (*self.value.get()).as_ref().unwrap() })
                    }
                    LazyState::Loading => {
                        // Wait for the other loader (spin with yield)
                        loop {
                            tokio::task::yield_now().await;
                            let state = LazyState::from(self.state.load(Ordering::Acquire));
                            match state {
                                LazyState::Loaded => {
                                    return Ok(unsafe { (*self.value.get()).as_ref().unwrap() });
                                }
                                LazyState::Unloaded | LazyState::Failed => {
                                    // Other loader failed, try again
                                    return Box::pin(self.load_with(loader)).await;
                                }
                                LazyState::Loading => continue,
                            }
                        }
                    }
                    _ => {
                        // Retry loading
                        Box::pin(self.load_with(loader)).await
                    }
                }
            }
        }
    }
}

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

impl<T: Clone> Clone for Lazy<T> {
    fn clone(&self) -> Self {
        if self.is_loaded() {
            Self::loaded(self.get().unwrap().clone())
        } else {
            Self::new()
        }
    }
}

impl<T: fmt::Debug> fmt::Debug for Lazy<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let state = LazyState::from(self.state.load(Ordering::Acquire));
        match state {
            LazyState::Loaded => {
                if let Some(value) = self.get() {
                    f.debug_struct("Lazy")
                        .field("state", &"Loaded")
                        .field("value", value)
                        .finish()
                } else {
                    f.debug_struct("Lazy").field("state", &"Loaded").finish()
                }
            }
            _ => f.debug_struct("Lazy").field("state", &state).finish(),
        }
    }
}

/// A relation that can be lazily loaded.
///
/// This is similar to `Lazy` but includes the loader configuration.
pub struct LazyRelation<T, L> {
    /// The lazy value.
    pub value: Lazy<T>,
    /// The loader (e.g., query parameters).
    pub loader: L,
}

impl<T, L> LazyRelation<T, L> {
    /// Create a new lazy relation.
    pub fn new(loader: L) -> Self {
        Self {
            value: Lazy::new(),
            loader,
        }
    }

    /// Create a lazy relation with a pre-loaded value.
    pub fn loaded(value: T, loader: L) -> Self {
        Self {
            value: Lazy::loaded(value),
            loader,
        }
    }

    /// Check if the relation has been loaded.
    #[inline]
    pub fn is_loaded(&self) -> bool {
        self.value.is_loaded()
    }

    /// Get the loaded value if available.
    pub fn get(&self) -> Option<&T> {
        self.value.get()
    }
}

impl<T: Clone, L: Clone> Clone for LazyRelation<T, L> {
    fn clone(&self) -> Self {
        Self {
            value: self.value.clone(),
            loader: self.loader.clone(),
        }
    }
}

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

/// Configuration for a one-to-many relation loader.
#[derive(Debug, Clone)]
pub struct OneToManyLoader {
    /// The foreign key column in the related table.
    pub foreign_key: String,
    /// The local key value.
    pub local_key_value: crate::filter::FilterValue,
    /// The related table name.
    pub table: String,
}

impl OneToManyLoader {
    /// Create a new one-to-many loader.
    pub fn new(
        table: impl Into<String>,
        foreign_key: impl Into<String>,
        local_key_value: impl Into<crate::filter::FilterValue>,
    ) -> Self {
        Self {
            table: table.into(),
            foreign_key: foreign_key.into(),
            local_key_value: local_key_value.into(),
        }
    }
}

/// Configuration for a many-to-one relation loader.
#[derive(Debug, Clone)]
pub struct ManyToOneLoader {
    /// The foreign key value.
    pub foreign_key_value: crate::filter::FilterValue,
    /// The related table name.
    pub table: String,
    /// The primary key column in the related table.
    pub primary_key: String,
}

impl ManyToOneLoader {
    /// Create a new many-to-one loader.
    pub fn new(
        table: impl Into<String>,
        primary_key: impl Into<String>,
        foreign_key_value: impl Into<crate::filter::FilterValue>,
    ) -> Self {
        Self {
            table: table.into(),
            primary_key: primary_key.into(),
            foreign_key_value: foreign_key_value.into(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_lazy_new() {
        let lazy: Lazy<i32> = Lazy::new();
        assert!(!lazy.is_loaded());
        assert!(lazy.get().is_none());
    }

    #[test]
    fn test_lazy_loaded() {
        let lazy = Lazy::loaded(42);
        assert!(lazy.is_loaded());
        assert_eq!(lazy.get(), Some(&42));
    }

    #[test]
    fn test_lazy_set() {
        let lazy: Lazy<i32> = Lazy::new();
        lazy.set(42);
        assert!(lazy.is_loaded());
        assert_eq!(lazy.get(), Some(&42));
    }

    #[test]
    fn test_lazy_take() {
        let mut lazy = Lazy::loaded(42);
        let value = lazy.take();
        assert_eq!(value, Some(42));
        assert!(!lazy.is_loaded());
    }

    #[test]
    fn test_lazy_reset() {
        let mut lazy = Lazy::loaded(42);
        lazy.reset();
        assert!(!lazy.is_loaded());
        assert!(lazy.get().is_none());
    }

    #[test]
    fn test_lazy_clone() {
        let lazy = Lazy::loaded(42);
        let cloned = lazy.clone();
        assert!(cloned.is_loaded());
        assert_eq!(cloned.get(), Some(&42));
    }

    #[test]
    fn test_lazy_clone_unloaded() {
        let lazy: Lazy<i32> = Lazy::new();
        let cloned = lazy.clone();
        assert!(!cloned.is_loaded());
    }

    #[tokio::test]
    async fn test_lazy_load_with() {
        let lazy: Lazy<i32> = Lazy::new();

        let result = lazy.load_with(|| async { Ok::<_, &str>(42) }).await;

        assert!(result.is_ok());
        assert_eq!(result.unwrap(), &42);
        assert!(lazy.is_loaded());
    }

    #[tokio::test]
    async fn test_lazy_load_cached() {
        let lazy = Lazy::loaded(42);

        // Should return cached value
        let result = lazy.load_with(|| async { Ok::<_, &str>(100) }).await;

        assert!(result.is_ok());
        assert_eq!(result.unwrap(), &42); // Original value, not 100
    }

    #[test]
    fn test_lazy_relation() {
        let relation: LazyRelation<Vec<i32>, OneToManyLoader> =
            LazyRelation::new(OneToManyLoader::new("posts", "user_id", 1i64));

        assert!(!relation.is_loaded());
        assert!(relation.get().is_none());
    }
}