raisfast 0.2.19

The last backend you'll ever need. Rust-powered headless CMS with built-in blog, ecommerce, wallet, payment and 4 plugin engines.
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
//! Unified database driver abstraction.
//!
//! A sealed trait [`DbDriver`] with exactly one compile-time implementation.
//! Adding a new database backend requires only:
//! 1. Add a zero-sized marker struct and `impl sealed::Sealed for X`
//! 2. `impl DbDriver for X` (~100 lines)
//! 3. Add the cfg-selected type alias `type Driver = X`
//! 4. Add pool type aliases in `pool.rs`
//!
//! Business code calls `Driver::ph(1)`, `Driver::has_column(...)`, etc.
//! The compiler resolves these to the single active impl — zero runtime cost.

use super::pool::Pool;

mod sealed {
    pub trait Sealed {}
}

pub struct Sqlite;
pub struct Postgres;
pub struct MySql;

impl sealed::Sealed for Sqlite {}
impl sealed::Sealed for Postgres {}
impl sealed::Sealed for MySql {}

#[cfg(feature = "db-sqlite")]
pub type Driver = Sqlite;
#[cfg(feature = "db-postgres")]
pub type Driver = Postgres;
#[cfg(feature = "db-mysql")]
pub type Driver = MySql;

/// Check if an identifier contains only safe characters (letters, digits, underscores).
#[must_use]
pub fn is_safe_identifier(name: &str) -> bool {
    !name.is_empty() && name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
}

/// Trim and validate an identifier. Returns `Some(trimmed)` if valid, `None` otherwise.
#[must_use]
pub fn sanitize_identifier(name: &str) -> Option<&str> {
    let trimmed = name.trim();
    if is_safe_identifier(trimmed) {
        Some(trimmed)
    } else {
        None
    }
}

/// All database-specific behavior in one sealed trait.
///
/// Call as `Driver::method(...)` — resolves to the compile-time selected backend.
pub trait DbDriver: sealed::Sealed {
    // ── DDL types ────────────────────────────────────────────────

    /// PRIMARY KEY column type (`INTEGER` / `BIGINT`).
    fn pk_type() -> &'static str;

    /// Foreign key column type (derived from `pk_type`).
    fn fk_type() -> &'static str;

    // ── SQL dialect (sync) ──────────────────────────────────────

    /// Positional placeholder (`?` or `$N`).
    fn ph(idx: usize) -> String;

    /// Current timestamp function.
    fn now_fn() -> &'static str;

    /// Expression for `N days ago`.
    fn ago_expr(days: i64) -> String;

    /// Truncate a datetime column to day granularity.
    fn date_trunc_day(col: &str) -> String;

    /// UPSERT conflict clause.
    fn upsert_clause(conflict_cols: &str, assignments: &str) -> String;

    /// Reference to the new/excluded value in an UPSERT.
    fn excluded_col(col: &str) -> String;

    /// `RETURNING *` clause (empty string on MySQL).
    fn returning_clause() -> &'static str;

    /// `RETURNING {col}` clause (empty string on MySQL).
    fn returning_col(col: &str) -> String;

    /// INSERT IGNORE (or equivalent) full statement.
    fn insert_ignore_sql(table: &str, columns: &str, placeholders: &str) -> String;

    // ── Metadata SQL builders (sync) ────────────────────────────

    /// SQL + column-name-index for listing columns with types.
    fn columns_sql(table: &str) -> (String, usize);

    /// SQL + column-name-index for listing column names only.
    fn column_names_sql(table: &str) -> (String, usize);

    /// Full rebuild-migration SQL (FK control + BEGIN/COMMIT wrapper).
    fn rebuild_wrapper_sql(table: &str, temp: &str, inner_ddl: &str, indexes: &[String]) -> String;

    // ── Metadata queries (async) ────────────────────────────────

    /// Check if a table has a specific column.
    fn has_column(
        pool: &Pool,
        table: &str,
        column: &str,
    ) -> impl std::future::Future<Output = bool> + Send;

    /// Check if a table exists.
    fn table_exists(pool: &Pool, table: &str) -> impl std::future::Future<Output = bool> + Send;

    /// List user tables excluding core tables.
    fn list_user_tables(
        pool: &Pool,
        excluded: &str,
    ) -> impl std::future::Future<Output = Vec<String>> + Send;

    /// Fetch `(column_name, sql_type)` pairs for a table.
    fn fetch_columns_with_types(
        pool: &Pool,
        table: &str,
    ) -> impl std::future::Future<Output = Result<Vec<(String, String)>, sqlx::Error>> + Send;
}

// ════════════════════════════════════════════════════════════════
// SQLite
// ════════════════════════════════════════════════════════════════

#[cfg(feature = "db-sqlite")]
impl DbDriver for Sqlite {
    fn pk_type() -> &'static str {
        "INTEGER PRIMARY KEY"
    }

    fn fk_type() -> &'static str {
        "INTEGER"
    }

    fn ph(idx: usize) -> String {
        let _ = idx;
        "?".to_string()
    }

    fn now_fn() -> &'static str {
        "strftime('%Y-%m-%dT%H:%M:%SZ', 'now')"
    }

    fn ago_expr(days: i64) -> String {
        format!("strftime('%Y-%m-%dT%H:%M:%SZ', 'now', '-{days} days')")
    }

    fn date_trunc_day(col: &str) -> String {
        format!("DATE({col})")
    }

    fn upsert_clause(conflict_cols: &str, assignments: &str) -> String {
        format!("ON CONFLICT({conflict_cols}) DO UPDATE SET {assignments}")
    }

    fn excluded_col(col: &str) -> String {
        format!("excluded.{col}")
    }

    fn returning_clause() -> &'static str {
        "RETURNING *"
    }

    fn returning_col(col: &str) -> String {
        format!("RETURNING {col}")
    }

    fn insert_ignore_sql(table: &str, columns: &str, placeholders: &str) -> String {
        assert!(is_safe_identifier(table), "unsafe table name: {table}");
        format!("INSERT OR IGNORE INTO {table} ({columns}) VALUES ({placeholders})")
    }

    fn columns_sql(table: &str) -> (String, usize) {
        (format!("PRAGMA table_info({table})"), 1)
    }

    fn column_names_sql(table: &str) -> (String, usize) {
        (format!("PRAGMA table_info({table})"), 1)
    }

    fn rebuild_wrapper_sql(table: &str, temp: &str, inner_ddl: &str, indexes: &[String]) -> String {
        let index_sql = indexes
            .iter()
            .map(|i| format!("{i};\n"))
            .collect::<String>();
        let mut sql = String::new();
        sql.push_str("PRAGMA foreign_keys = OFF;\n\n");
        sql.push_str("BEGIN TRANSACTION;\n\n");
        sql.push_str(inner_ddl);
        sql.push_str(&format!("INSERT INTO {temp} SELECT * FROM {table};\n\n"));
        sql.push_str(&format!("DROP TABLE {table};\n\n"));
        sql.push_str(&format!("ALTER TABLE {temp} RENAME TO {table};\n\n"));
        sql.push_str(&index_sql);
        sql.push_str("\nPRAGMA foreign_key_check;\n\n");
        sql.push_str("COMMIT;\n\n");
        sql.push_str("PRAGMA foreign_keys = ON;\n");
        sql
    }

    async fn has_column(pool: &Pool, table: &str, column: &str) -> bool {
        assert!(is_safe_identifier(table), "unsafe table: {table}");
        let sql = format!("PRAGMA table_info({table})");
        let rows: Vec<(i32, String, String, bool, Option<String>, bool)> = sqlx::query_as(&sql)
            .fetch_all(pool)
            .await
            .unwrap_or_default();
        rows.iter().any(|(_, name, _, _, _, _)| name == column)
    }

    async fn table_exists(pool: &Pool, table: &str) -> bool {
        assert!(is_safe_identifier(table), "unsafe table: {table}");
        let sql =
            format!("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='{table}'");
        sqlx::query_scalar::<_, i64>(&sql)
            .fetch_one(pool)
            .await
            .unwrap_or(0)
            > 0
    }

    async fn list_user_tables(pool: &Pool, excluded: &str) -> Vec<String> {
        let sql = format!(
            "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%%' AND name NOT IN ({excluded})"
        );
        let rows = sqlx::query_as::<_, (String,)>(&sql)
            .fetch_all(pool)
            .await
            .unwrap_or_default();
        rows.into_iter().map(|(n,)| n).collect()
    }

    async fn fetch_columns_with_types(
        pool: &Pool,
        table: &str,
    ) -> Result<Vec<(String, String)>, sqlx::Error> {
        use sqlx::Row;
        let sql = format!("PRAGMA table_info({table})");
        let rows = sqlx::query(&sql).fetch_all(pool).await?;
        let mut cols = Vec::new();
        for row in &rows {
            let name: String = row.try_get(1).unwrap_or_default();
            let typ: String = row.try_get(2).unwrap_or_default();
            if !name.is_empty() {
                cols.push((name, typ));
            }
        }
        Ok(cols)
    }
}

// ════════════════════════════════════════════════════════════════
// PostgreSQL
// ════════════════════════════════════════════════════════════════

#[cfg(feature = "db-postgres")]
impl DbDriver for Postgres {
    fn pk_type() -> &'static str {
        "BIGINT PRIMARY KEY"
    }

    fn fk_type() -> &'static str {
        "BIGINT"
    }

    fn ph(idx: usize) -> String {
        format!("${idx}")
    }

    fn now_fn() -> &'static str {
        "NOW()"
    }

    fn ago_expr(days: i64) -> String {
        format!("NOW() - INTERVAL '{days} days'")
    }

    fn date_trunc_day(col: &str) -> String {
        format!("DATE_TRUNC('day', {col}::timestamp)")
    }

    fn upsert_clause(conflict_cols: &str, assignments: &str) -> String {
        format!("ON CONFLICT({conflict_cols}) DO UPDATE SET {assignments}")
    }

    fn excluded_col(col: &str) -> String {
        format!("excluded.{col}")
    }

    fn returning_clause() -> &'static str {
        "RETURNING *"
    }

    fn returning_col(col: &str) -> String {
        format!("RETURNING {col}")
    }

    fn insert_ignore_sql(table: &str, columns: &str, placeholders: &str) -> String {
        assert!(is_safe_identifier(table), "unsafe table name: {table}");
        format!("INSERT INTO {table} ({columns}) VALUES ({placeholders}) ON CONFLICT DO NOTHING")
    }

    fn columns_sql(table: &str) -> (String, usize) {
        (
            format!(
                "SELECT column_name, data_type FROM information_schema.columns WHERE table_name = '{table}'"
            ),
            0,
        )
    }

    fn column_names_sql(table: &str) -> (String, usize) {
        (
            format!(
                "SELECT column_name FROM information_schema.columns WHERE table_name = '{table}'"
            ),
            0,
        )
    }

    fn rebuild_wrapper_sql(table: &str, temp: &str, inner_ddl: &str, indexes: &[String]) -> String {
        let index_sql = indexes
            .iter()
            .map(|i| format!("{i};\n"))
            .collect::<String>();
        let mut sql = String::new();
        sql.push_str("BEGIN;\n\n");
        sql.push_str(inner_ddl);
        sql.push_str(&format!("INSERT INTO {temp} SELECT * FROM {table};\n\n"));
        sql.push_str(&format!("DROP TABLE {table};\n\n"));
        sql.push_str(&format!("ALTER TABLE {temp} RENAME TO {table};\n\n"));
        sql.push_str(&index_sql);
        sql.push_str("\nCOMMIT;\n");
        sql
    }

    async fn has_column(pool: &Pool, table: &str, column: &str) -> bool {
        assert!(is_safe_identifier(table), "unsafe table: {table}");
        sqlx::query_scalar(
            "SELECT 1 FROM information_schema.columns WHERE table_name = $1 AND column_name = $2",
        )
        .bind(table)
        .bind(column)
        .fetch_optional(pool)
        .await
        .unwrap_or(None)
        .is_some()
    }

    async fn table_exists(pool: &Pool, table: &str) -> bool {
        assert!(is_safe_identifier(table), "unsafe table: {table}");
        let sql = format!(
            "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'public' AND table_name = '{table}'"
        );
        sqlx::query_scalar::<_, i64>(&sql)
            .fetch_one(pool)
            .await
            .unwrap_or(0)
            > 0
    }

    async fn list_user_tables(pool: &Pool, excluded: &str) -> Vec<String> {
        let sql = format!(
            "SELECT tablename FROM pg_tables WHERE schemaname = 'public' AND tablename NOT IN ({excluded})"
        );
        let rows = sqlx::query_as::<_, (String,)>(&sql)
            .fetch_all(pool)
            .await
            .unwrap_or_default();
        rows.into_iter().map(|(n,)| n).collect()
    }

    async fn fetch_columns_with_types(
        pool: &Pool,
        table: &str,
    ) -> Result<Vec<(String, String)>, sqlx::Error> {
        use sqlx::Row;
        let sql = format!(
            "SELECT column_name, data_type FROM information_schema.columns WHERE table_name = '{table}'"
        );
        let rows = sqlx::query(&sql).fetch_all(pool).await?;
        let mut cols = Vec::new();
        for row in &rows {
            let name: String = row.try_get(0).unwrap_or_default();
            let typ: String = row.try_get(1).unwrap_or_default();
            if !name.is_empty() {
                cols.push((name, typ));
            }
        }
        Ok(cols)
    }
}

// ════════════════════════════════════════════════════════════════
// MySQL
// ════════════════════════════════════════════════════════════════

#[cfg(feature = "db-mysql")]
impl DbDriver for MySql {
    fn pk_type() -> &'static str {
        "BIGINT PRIMARY KEY"
    }

    fn fk_type() -> &'static str {
        "BIGINT"
    }

    fn ph(idx: usize) -> String {
        let _ = idx;
        "?".to_string()
    }

    fn now_fn() -> &'static str {
        "NOW()"
    }

    fn ago_expr(days: i64) -> String {
        format!("DATE_SUB(NOW(), INTERVAL {days} DAY)")
    }

    fn date_trunc_day(col: &str) -> String {
        format!("DATE({col})")
    }

    fn upsert_clause(conflict_cols: &str, assignments: &str) -> String {
        let _ = conflict_cols;
        format!("ON DUPLICATE KEY UPDATE {assignments}")
    }

    fn excluded_col(col: &str) -> String {
        format!("VALUES({col})")
    }

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

    fn returning_col(col: &str) -> String {
        let _ = col;
        String::new()
    }

    fn insert_ignore_sql(table: &str, columns: &str, placeholders: &str) -> String {
        assert!(is_safe_identifier(table), "unsafe table name: {table}");
        format!("INSERT IGNORE INTO {table} ({columns}) VALUES ({placeholders})")
    }

    fn columns_sql(table: &str) -> (String, usize) {
        (
            format!(
                "SELECT column_name, data_type FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name = '{table}'"
            ),
            0,
        )
    }

    fn column_names_sql(table: &str) -> (String, usize) {
        (
            format!(
                "SELECT column_name FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name = '{table}'"
            ),
            0,
        )
    }

    fn rebuild_wrapper_sql(table: &str, temp: &str, inner_ddl: &str, indexes: &[String]) -> String {
        let index_sql = indexes
            .iter()
            .map(|i| format!("{i};\n"))
            .collect::<String>();
        let mut sql = String::new();
        sql.push_str("SET FOREIGN_KEY_CHECKS = 0;\n\n");
        sql.push_str(inner_ddl);
        sql.push_str(&format!("INSERT INTO {temp} SELECT * FROM {table};\n\n"));
        sql.push_str(&format!("DROP TABLE {table};\n\n"));
        sql.push_str(&format!("ALTER TABLE {temp} RENAME TO {table};\n\n"));
        sql.push_str(&index_sql);
        sql.push_str("\nSET FOREIGN_KEY_CHECKS = 1;\n");
        sql
    }

    async fn has_column(pool: &Pool, table: &str, column: &str) -> bool {
        assert!(is_safe_identifier(table), "unsafe table: {table}");
        sqlx::query_scalar(
            "SELECT 1 FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name = ? AND column_name = ?",
        )
        .bind(table)
        .bind(column)
        .fetch_optional(pool)
        .await
        .unwrap_or(None)
        .is_some()
    }

    async fn table_exists(pool: &Pool, table: &str) -> bool {
        assert!(is_safe_identifier(table), "unsafe table: {table}");
        let sql = format!(
            "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = '{table}'"
        );
        sqlx::query_scalar::<_, i64>(&sql)
            .fetch_one(pool)
            .await
            .unwrap_or(0)
            > 0
    }

    async fn list_user_tables(pool: &Pool, excluded: &str) -> Vec<String> {
        let sql = format!(
            "SELECT table_name FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name NOT IN ({excluded})"
        );
        let rows = sqlx::query_as::<_, (String,)>(&sql)
            .fetch_all(pool)
            .await
            .unwrap_or_default();
        rows.into_iter().map(|(n,)| n).collect()
    }

    async fn fetch_columns_with_types(
        pool: &Pool,
        table: &str,
    ) -> Result<Vec<(String, String)>, sqlx::Error> {
        use sqlx::Row;
        let sql = format!(
            "SELECT column_name, data_type FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name = '{table}'"
        );
        let rows = sqlx::query(&sql).fetch_all(pool).await?;
        let mut cols = Vec::new();
        for row in &rows {
            let name: String = row.try_get(0).unwrap_or_default();
            let typ: String = row.try_get(1).unwrap_or_default();
            if !name.is_empty() {
                cols.push((name, typ));
            }
        }
        Ok(cols)
    }
}

// ════════════════════════════════════════════════════════════════
// Tests
// ════════════════════════════════════════════════════════════════

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

    #[test]
    fn safe_identifier_accepts_valid() {
        assert!(is_safe_identifier("users"));
        assert!(is_safe_identifier("created_at"));
        assert!(is_safe_identifier("_meta"));
        assert!(is_safe_identifier("col1"));
    }

    #[test]
    fn safe_identifier_rejects_invalid() {
        assert!(!is_safe_identifier(""));
        assert!(!is_safe_identifier("DROP TABLE"));
        assert!(!is_safe_identifier("id; DROP TABLE users--"));
        assert!(!is_safe_identifier("col name"));
        assert!(!is_safe_identifier("a'b"));
        assert!(!is_safe_identifier("1;DROP"));
        assert!(!is_safe_identifier(" posts "));
        assert!(!is_safe_identifier("posts "));
    }

    #[test]
    fn sanitize_identifier_trims_whitespace() {
        assert_eq!(sanitize_identifier("  posts  "), Some("posts"));
        assert_eq!(sanitize_identifier("users"), Some("users"));
        assert_eq!(sanitize_identifier("\t col1 \n"), Some("col1"));
        assert_eq!(sanitize_identifier("  "), None);
        assert_eq!(sanitize_identifier(" drop table "), None);
    }

    #[test]
    fn ph_generates_correct_placeholder() {
        let p = Driver::ph(1);
        #[cfg(feature = "db-sqlite")]
        assert_eq!(p, "?");
        #[cfg(feature = "db-postgres")]
        assert_eq!(p, "$1");
        #[cfg(feature = "db-mysql")]
        assert_eq!(p, "?");
    }

    #[test]
    fn pk_type_matches_backend() {
        let pk = Driver::pk_type();
        #[cfg(feature = "db-sqlite")]
        assert!(pk.starts_with("INTEGER"));
        #[cfg(any(feature = "db-postgres", feature = "db-mysql"))]
        assert!(pk.starts_with("BIGINT"));
    }
}