rok-fluent 0.4.1

Eloquent-inspired async ORM for Rust (PostgreSQL, MySQL, SQLite)
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
//! Polymorphic relationship types.
//!
//! - [`MorphTo`] — "belongs to" one of many parent types via `{key}_type` + `{key}_id`
//! - [`MorphMany`] — parent side "has many" via a morph key
//! - [`MorphOne`]  — parent side "has one" via a morph key
//! - [`MorphToMany`] — polymorphic pivot (taggable pattern)

use std::marker::PhantomData;

use sqlx::postgres::PgRow;
use sqlx::PgPool;

use crate::core::condition::SqlValue;
use crate::core::model::Model;
use crate::core::sqlx::pg as sqlx_pg;
use crate::orm::postgres::pool;

// ── shared helpers ────────────────────────────────────────────────────────────

fn current_pool() -> Result<PgPool, sqlx::Error> {
    pool::try_current_pool().ok_or_else(|| {
        sqlx::Error::Configuration(
            "no database pool in scope — add OrmLayer to your router or \
             call pool::with_pool() in tests"
                .to_string()
                .into(),
        )
    })
}

/// Singularise a table name: `users → user`, `categories → category`.
pub(crate) fn naive_singular(table: &str) -> String {
    if let Some(s) = table.strip_suffix("ies") {
        format!("{s}y")
    } else if let Some(s) = table.strip_suffix('s') {
        s.to_string()
    } else {
        table.to_string()
    }
}

// ── MorphTo ───────────────────────────────────────────────────────────────────

/// A polymorphic "belongs to" reference stored as `(type_name, id)`.
///
/// Build from a model's morph fields, then call `.resolve::<T>()` to fetch the
/// parent row.  Returns `Err` when the stored type doesn't match `T`.
///
/// # Example
///
/// ```rust,no_run
/// # use rok_fluent::orm::morph::MorphTo;
/// # use rok_fluent::core::model::Model;
/// # #[derive(Debug, sqlx::FromRow)]
/// # pub struct Post { pub id: i64 }
/// # impl Model for Post {
/// #     fn table_name() -> &'static str { "posts" }
/// #     fn columns() -> &'static [&'static str] { &["id"] }
/// # }
/// # async fn example() -> Result<(), sqlx::Error> {
/// let morph = MorphTo::new("post", 42i64);
/// let post: Option<Post> = morph.resolve::<Post>().await?;
/// # Ok(())
/// # }
/// ```
pub struct MorphTo {
    type_name: String,
    id: SqlValue,
}

impl MorphTo {
    /// Create a new `MorphTo` from a stored type name and ID.
    pub fn new(type_name: impl Into<String>, id: impl Into<SqlValue>) -> Self {
        Self {
            type_name: type_name.into(),
            id: id.into(),
        }
    }

    /// The stored type name (e.g., `"post"` or `"video"`).
    pub fn type_name(&self) -> &str {
        &self.type_name
    }

    /// The stored ID value.
    pub fn id(&self) -> &SqlValue {
        &self.id
    }

    /// Fetch the parent row of type `T`.
    ///
    /// Returns `Err` if `type_name` != `naive_singular(T::table_name())`.
    pub async fn resolve<T>(&self) -> Result<Option<T>, sqlx::Error>
    where
        T: Model + for<'r> sqlx::FromRow<'r, PgRow> + Send + Unpin,
    {
        let expected = naive_singular(T::table_name());
        if self.type_name != expected {
            return Err(sqlx::Error::Configuration(
                format!(
                    "MorphTo: stored type '{}' does not match resolved type '{}' (table '{}')",
                    self.type_name,
                    expected,
                    T::table_name()
                )
                .into(),
            ));
        }
        let pool = current_pool()?;
        let sql = format!(
            "SELECT * FROM {} WHERE {} = $1 LIMIT 1",
            T::table_name(),
            T::primary_key()
        );
        sqlx_pg::fetch_optional_as::<T>(&pool, &sql, vec![self.id.clone()]).await
    }
}

// ── MorphMany ─────────────────────────────────────────────────────────────────

/// A polymorphic "has many" relationship query.
///
/// Fetches all child rows of type `T` where `{key}_type = owner_type AND {key}_id = owner_id`.
pub struct MorphMany<T> {
    owner_type: &'static str,
    owner_id: SqlValue,
    key: &'static str,
    _marker: PhantomData<T>,
}

impl<T: Model> MorphMany<T> {
    /// Create a new `MorphMany`.
    pub fn new(owner_type: &'static str, owner_id: impl Into<SqlValue>, key: &'static str) -> Self {
        Self {
            owner_type,
            owner_id: owner_id.into(),
            key,
            _marker: PhantomData,
        }
    }

    pub(crate) fn select_sql(&self) -> (String, Vec<SqlValue>) {
        let table = T::table_name();
        let sql = format!(
            "SELECT * FROM {table} WHERE {key}_type = $1 AND {key}_id = $2",
            key = self.key
        );
        (
            sql,
            vec![
                SqlValue::Text(self.owner_type.to_string()),
                self.owner_id.clone(),
            ],
        )
    }

    pub(crate) fn count_sql(&self) -> (String, Vec<SqlValue>) {
        let table = T::table_name();
        let sql = format!(
            "SELECT COUNT(*) FROM {table} WHERE {key}_type = $1 AND {key}_id = $2",
            key = self.key
        );
        (
            sql,
            vec![
                SqlValue::Text(self.owner_type.to_string()),
                self.owner_id.clone(),
            ],
        )
    }
}

impl<T> MorphMany<T>
where
    T: Model + for<'r> sqlx::FromRow<'r, PgRow> + Send + Unpin,
{
    /// Fetch all child rows.
    pub async fn get(self) -> Result<Vec<T>, sqlx::Error> {
        let pool = current_pool()?;
        let (sql, params) = self.select_sql();
        sqlx_pg::fetch_all_as::<T>(&pool, &sql, params).await
    }

    /// Fetch the first child row, or `None`.
    pub async fn first(self) -> Result<Option<T>, sqlx::Error> {
        let pool = current_pool()?;
        let (base_sql, params) = self.select_sql();
        let sql = format!("{base_sql} LIMIT 1");
        sqlx_pg::fetch_optional_as::<T>(&pool, &sql, params).await
    }

    /// Return the count of child rows.
    pub async fn count(self) -> Result<i64, sqlx::Error> {
        let pool = current_pool()?;
        let (sql, params) = self.count_sql();
        let row = sqlx_pg::build_query(&sql, params).fetch_one(&pool).await?;
        use sqlx::Row;
        row.try_get::<i64, _>(0)
    }

    /// Return `true` if at least one child row exists.
    pub async fn exists(self) -> Result<bool, sqlx::Error> {
        Ok(self.count().await? > 0)
    }
}

// ── MorphOne ──────────────────────────────────────────────────────────────────

/// A polymorphic "has one" relationship query.
///
/// Same as [`MorphMany`] but always returns a single row.
pub struct MorphOne<T> {
    owner_type: &'static str,
    owner_id: SqlValue,
    key: &'static str,
    _marker: PhantomData<T>,
}

impl<T: Model> MorphOne<T> {
    /// Create a new `MorphOne`.
    pub fn new(owner_type: &'static str, owner_id: impl Into<SqlValue>, key: &'static str) -> Self {
        Self {
            owner_type,
            owner_id: owner_id.into(),
            key,
            _marker: PhantomData,
        }
    }

    pub(crate) fn select_sql(&self) -> (String, Vec<SqlValue>) {
        let table = T::table_name();
        let sql = format!(
            "SELECT * FROM {table} WHERE {key}_type = $1 AND {key}_id = $2 LIMIT 1",
            key = self.key
        );
        (
            sql,
            vec![
                SqlValue::Text(self.owner_type.to_string()),
                self.owner_id.clone(),
            ],
        )
    }
}

impl<T> MorphOne<T>
where
    T: Model + for<'r> sqlx::FromRow<'r, PgRow> + Send + Unpin,
{
    /// Fetch the single related row, or `None`.
    pub async fn get(self) -> Result<Option<T>, sqlx::Error> {
        let pool = current_pool()?;
        let (sql, params) = self.select_sql();
        sqlx_pg::fetch_optional_as::<T>(&pool, &sql, params).await
    }

    /// Return `true` if the related row exists.
    pub async fn exists(self) -> Result<bool, sqlx::Error> {
        let pool = current_pool()?;
        let table = T::table_name();
        let sql = format!(
            "SELECT COUNT(*) FROM {table} WHERE {key}_type = $1 AND {key}_id = $2 LIMIT 1",
            key = self.key
        );
        let params = vec![SqlValue::Text(self.owner_type.to_string()), self.owner_id];
        let row = sqlx_pg::build_query(&sql, params).fetch_one(&pool).await?;
        use sqlx::Row;
        Ok(row.try_get::<i64, _>(0)? > 0)
    }
}

// ── MorphToMany ───────────────────────────────────────────────────────────────

/// A polymorphic many-to-many relationship query (taggable pattern).
///
/// SQL pattern (from owner's side):
/// ```sql
/// SELECT t.* FROM t
/// INNER JOIN pivot ON pivot.related_fk = t.pk
/// WHERE pivot.{key}_type = $1 AND pivot.{key}_id = $2
/// ```
pub struct MorphToMany<T> {
    owner_type: &'static str,
    owner_id: SqlValue,
    pivot: &'static str,
    key: &'static str,
    related_fk: &'static str,
    _marker: PhantomData<T>,
}

impl<T: Model> MorphToMany<T> {
    /// Create a new `MorphToMany`.
    pub fn new(
        owner_type: &'static str,
        owner_id: impl Into<SqlValue>,
        pivot: &'static str,
        key: &'static str,
        related_fk: &'static str,
    ) -> Self {
        Self {
            owner_type,
            owner_id: owner_id.into(),
            pivot,
            key,
            related_fk,
            _marker: PhantomData,
        }
    }

    pub(crate) fn select_sql(&self) -> (String, Vec<SqlValue>) {
        let t = T::table_name();
        let pk = T::primary_key();
        let pivot = self.pivot;
        let key = self.key;
        let related_fk = self.related_fk;
        let sql = format!(
            "SELECT {t}.* FROM {t} \
             INNER JOIN {pivot} ON {pivot}.{related_fk} = {t}.{pk} \
             WHERE {pivot}.{key}_type = $1 AND {pivot}.{key}_id = $2"
        );
        (
            sql,
            vec![
                SqlValue::Text(self.owner_type.to_string()),
                self.owner_id.clone(),
            ],
        )
    }

    pub(crate) fn count_sql(&self) -> (String, Vec<SqlValue>) {
        let t = T::table_name();
        let pk = T::primary_key();
        let pivot = self.pivot;
        let key = self.key;
        let related_fk = self.related_fk;
        let sql = format!(
            "SELECT COUNT(*) FROM {t} \
             INNER JOIN {pivot} ON {pivot}.{related_fk} = {t}.{pk} \
             WHERE {pivot}.{key}_type = $1 AND {pivot}.{key}_id = $2"
        );
        (
            sql,
            vec![
                SqlValue::Text(self.owner_type.to_string()),
                self.owner_id.clone(),
            ],
        )
    }
}

impl<T> MorphToMany<T>
where
    T: Model + for<'r> sqlx::FromRow<'r, PgRow> + Send + Unpin,
{
    // ── read terminals ────────────────────────────────────────────────────────

    /// Fetch all related rows.
    pub async fn get(self) -> Result<Vec<T>, sqlx::Error> {
        let pool = current_pool()?;
        let (sql, params) = self.select_sql();
        sqlx_pg::fetch_all_as::<T>(&pool, &sql, params).await
    }

    /// Fetch the first related row, or `None`.
    pub async fn first(self) -> Result<Option<T>, sqlx::Error> {
        let pool = current_pool()?;
        let (base_sql, params) = self.select_sql();
        let sql = format!("{base_sql} LIMIT 1");
        sqlx_pg::fetch_optional_as::<T>(&pool, &sql, params).await
    }

    /// Return the count of related rows.
    pub async fn count(self) -> Result<i64, sqlx::Error> {
        let pool = current_pool()?;
        let (sql, params) = self.count_sql();
        let row = sqlx_pg::build_query(&sql, params).fetch_one(&pool).await?;
        use sqlx::Row;
        row.try_get::<i64, _>(0)
    }

    /// Return `true` if at least one related row exists.
    pub async fn exists(self) -> Result<bool, sqlx::Error> {
        Ok(self.count().await? > 0)
    }

    // ── write operations ──────────────────────────────────────────────────────

    /// Attach `related_id` to the owner via the morph pivot. Idempotent.
    pub async fn attach(self, related_id: impl Into<SqlValue>) -> Result<u64, sqlx::Error> {
        let pool = current_pool()?;
        let sql = format!(
            "INSERT INTO {pivot} ({rfk}, {key}_id, {key}_type) \
             VALUES ($1, $2, $3) ON CONFLICT DO NOTHING",
            pivot = self.pivot,
            rfk = self.related_fk,
            key = self.key,
        );
        let params = vec![
            related_id.into(),
            self.owner_id,
            SqlValue::Text(self.owner_type.to_string()),
        ];
        let result = sqlx_pg::build_query(&sql, params).execute(&pool).await?;
        Ok(result.rows_affected())
    }

    /// Detach `related_id` from the owner.
    pub async fn detach(self, related_id: impl Into<SqlValue>) -> Result<u64, sqlx::Error> {
        let pool = current_pool()?;
        let sql = format!(
            "DELETE FROM {pivot} WHERE {rfk} = $1 AND {key}_id = $2 AND {key}_type = $3",
            pivot = self.pivot,
            rfk = self.related_fk,
            key = self.key,
        );
        let params = vec![
            related_id.into(),
            self.owner_id,
            SqlValue::Text(self.owner_type.to_string()),
        ];
        let result = sqlx_pg::build_query(&sql, params).execute(&pool).await?;
        Ok(result.rows_affected())
    }

    /// Remove all pivot rows for this owner (detach everything).
    pub async fn detach_all(self) -> Result<u64, sqlx::Error> {
        let pool = current_pool()?;
        let sql = format!(
            "DELETE FROM {pivot} WHERE {key}_id = $1 AND {key}_type = $2",
            pivot = self.pivot,
            key = self.key,
        );
        let params = vec![self.owner_id, SqlValue::Text(self.owner_type.to_string())];
        let result = sqlx_pg::build_query(&sql, params).execute(&pool).await?;
        Ok(result.rows_affected())
    }

    /// Sync: detach IDs not in `ids`, attach missing ones. Idempotent.
    pub async fn sync(self, ids: &[impl Into<SqlValue> + Clone]) -> Result<(), sqlx::Error> {
        let pool = current_pool()?;
        let owner_type = SqlValue::Text(self.owner_type.to_string());

        if ids.is_empty() {
            let sql = format!(
                "DELETE FROM {pivot} WHERE {key}_id = $1 AND {key}_type = $2",
                pivot = self.pivot,
                key = self.key,
            );
            sqlx_pg::build_query(&sql, vec![self.owner_id, owner_type])
                .execute(&pool)
                .await?;
            return Ok(());
        }

        let id_vals: Vec<SqlValue> = ids.iter().map(|v| v.clone().into()).collect();

        let ph: Vec<String> = (3..=id_vals.len() + 2).map(|i| format!("${i}")).collect();
        let del_sql = format!(
            "DELETE FROM {pivot} WHERE {key}_id = $1 AND {key}_type = $2 AND {rfk} NOT IN ({ph})",
            pivot = self.pivot,
            key = self.key,
            rfk = self.related_fk,
            ph = ph.join(", "),
        );
        let mut del_params = vec![self.owner_id.clone(), owner_type.clone()];
        del_params.extend(id_vals.iter().cloned());
        sqlx_pg::build_query(&del_sql, del_params)
            .execute(&pool)
            .await?;

        for id_val in id_vals {
            let ins_sql = format!(
                "INSERT INTO {pivot} ({rfk}, {key}_id, {key}_type) \
                 VALUES ($1, $2, $3) ON CONFLICT DO NOTHING",
                pivot = self.pivot,
                rfk = self.related_fk,
                key = self.key,
            );
            sqlx_pg::build_query(
                &ins_sql,
                vec![id_val, self.owner_id.clone(), owner_type.clone()],
            )
            .execute(&pool)
            .await?;
        }

        Ok(())
    }

    /// Toggle: attach IDs not present, detach IDs already present.
    pub async fn toggle(self, ids: &[impl Into<SqlValue> + Clone]) -> Result<(), sqlx::Error> {
        let pool = current_pool()?;
        let owner_type = SqlValue::Text(self.owner_type.to_string());
        let id_vals: Vec<SqlValue> = ids.iter().map(|v| v.clone().into()).collect();

        if id_vals.is_empty() {
            return Ok(());
        }

        let ph: Vec<String> = (3..=id_vals.len() + 2).map(|i| format!("${i}")).collect();
        let sel_sql = format!(
            "SELECT {rfk} FROM {pivot} \
             WHERE {key}_id = $1 AND {key}_type = $2 AND {rfk} IN ({ph})",
            pivot = self.pivot,
            rfk = self.related_fk,
            key = self.key,
            ph = ph.join(", "),
        );
        let mut sel_params = vec![self.owner_id.clone(), owner_type.clone()];
        sel_params.extend(id_vals.iter().cloned());

        let rows = sqlx_pg::build_query(&sel_sql, sel_params)
            .fetch_all(&pool)
            .await?;
        use sqlx::Row;
        let existing: Vec<i64> = rows
            .iter()
            .filter_map(|r| r.try_get::<i64, _>(0).ok())
            .collect();

        for id_val in id_vals {
            let already = match &id_val {
                SqlValue::Integer(n) => existing.contains(n),
                _ => false,
            };

            if already {
                let del = format!(
                    "DELETE FROM {pivot} WHERE {rfk} = $1 AND {key}_id = $2 AND {key}_type = $3",
                    pivot = self.pivot,
                    rfk = self.related_fk,
                    key = self.key,
                );
                sqlx_pg::build_query(
                    &del,
                    vec![id_val, self.owner_id.clone(), owner_type.clone()],
                )
                .execute(&pool)
                .await?;
            } else {
                let ins = format!(
                    "INSERT INTO {pivot} ({rfk}, {key}_id, {key}_type) \
                     VALUES ($1, $2, $3) ON CONFLICT DO NOTHING",
                    pivot = self.pivot,
                    rfk = self.related_fk,
                    key = self.key,
                );
                sqlx_pg::build_query(
                    &ins,
                    vec![id_val, self.owner_id.clone(), owner_type.clone()],
                )
                .execute(&pool)
                .await?;
            }
        }

        Ok(())
    }
}

// ── unit tests ────────────────────────────────────────────────────────────────

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

    struct Comment;
    impl Model for Comment {
        fn table_name() -> &'static str {
            "comments"
        }
        fn columns() -> &'static [&'static str] {
            &["id", "body"]
        }
    }

    struct Tag;
    impl Model for Tag {
        fn table_name() -> &'static str {
            "tags"
        }
        fn columns() -> &'static [&'static str] {
            &["id", "name"]
        }
    }

    #[allow(dead_code)]
    struct Post;
    impl Model for Post {
        fn table_name() -> &'static str {
            "posts"
        }
        fn columns() -> &'static [&'static str] {
            &["id", "title"]
        }
    }

    #[test]
    fn singular_regular() {
        assert_eq!(naive_singular("users"), "user");
        assert_eq!(naive_singular("posts"), "post");
    }

    #[test]
    fn singular_ies_ending() {
        assert_eq!(naive_singular("categories"), "category");
    }

    #[test]
    fn morph_to_stores_type_and_id() {
        let m = MorphTo::new("post", 42i64);
        assert_eq!(m.type_name(), "post");
        assert!(matches!(m.id(), SqlValue::Integer(42)));
    }

    #[test]
    fn morph_many_select_sql() {
        let q = MorphMany::<Comment>::new("post", 1i64, "commentable");
        let (sql, params) = q.select_sql();
        assert_eq!(
            sql,
            "SELECT * FROM comments WHERE commentable_type = $1 AND commentable_id = $2"
        );
        assert_eq!(params.len(), 2);
        assert!(matches!(&params[0], SqlValue::Text(s) if s == "post"));
    }

    #[test]
    fn morph_one_select_sql_has_limit() {
        let q = MorphOne::<Comment>::new("post", 1i64, "commentable");
        let (sql, params) = q.select_sql();
        assert!(sql.contains("LIMIT 1"), "sql={sql}");
        assert_eq!(params.len(), 2);
    }

    #[test]
    fn morph_to_many_select_sql() {
        let q = MorphToMany::<Tag>::new("post", 1i64, "taggables", "taggable", "tag_id");
        let (sql, params) = q.select_sql();
        assert!(sql.contains("SELECT tags.* FROM tags"), "sql={sql}");
        assert!(
            sql.contains("INNER JOIN taggables ON taggables.tag_id = tags.id"),
            "sql={sql}"
        );
        assert!(sql.contains("taggable_type = $1"), "sql={sql}");
        assert_eq!(params.len(), 2);
        assert!(matches!(&params[0], SqlValue::Text(s) if s == "post"));
    }
}