sqlw 0.1.0

Compile-time SQL query building with schema-safe field references and automatic parameter binding
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
//! End-to-end CRUD usecases demonstrating the idiomatic library usage pattern:
//!
//!   schema! → associated methods → query_qmark! → QueryExecutor
//!
//! Schema types own their CRUD logic as associated methods, accept
//! backend-agnostic `&impl QueryExecutor`, and return domain newtypes.
//! All schema fields use newtypes — no raw primitives on the struct.
//! `IntoValue` derive emits both `From<T> for Value` and `From<&T> for Value`,
//! so owned and borrowed values work directly in `{placeholder}` bindings.

use sqlw::{
    FromRow, IntoValue, Query, QueryExecutor, TryFromValueRef, query_qmark as query, schema,
};
use sqlw_backend::turso::TursoExecutor;

type StdResult<T = ()> = Result<T, Box<dyn std::error::Error>>;

#[derive(Debug)]
pub struct Paginated<T> {
    pub next: Option<ID>,
    pub items: Vec<T>,
}

impl<T> Paginated<T> {
    pub fn new(next: Option<ID>, items: Vec<T>) -> Self {
        Self { next, items }
    }
}

// ---------------------------------------------------------------------------
// Domain newtypes — these are used both as schema field types (so FromRow
// extracts them directly) and as parameters in query methods (so `{val}`
// works directly without unwrapping).
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, TryFromValueRef, IntoValue)]
pub struct ID(pub i64);

#[derive(Debug, Clone, PartialEq, Eq, Default, TryFromValueRef, IntoValue)]
pub struct ConversationTitle(pub String);

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, TryFromValueRef, IntoValue)]
pub struct Timestamp(pub i64);

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Limit(pub usize);

#[derive(Debug, Clone, Copy, PartialEq, Eq, TryFromValueRef, IntoValue)]
pub enum Role {
    #[value = "user"]
    User,
    #[value = "assistant"]
    Assistant,
}

impl Default for Role {
    fn default() -> Self {
        Role::User
    }
}

// ---------------------------------------------------------------------------
// Manual FromRow structs for non-schema queries (computed columns, aliases).
// ---------------------------------------------------------------------------

#[derive(Debug, PartialEq, FromRow)]
pub struct ConversationTitleView {
    id: i64,
    title: String,
    title_length: usize,
}

#[derive(Debug, PartialEq, sqlw::FromRow)]
pub struct MessageCount {
    #[field = "id"]
    conv_id: i64,
    #[field = "title"]
    conv_title: ConversationTitle,
    #[field = "message_count"]
    count: i64,
}

// ---------------------------------------------------------------------------
// Schema definitions — field types are domain newtypes, so the auto-generated
// FromRow extracts them directly and query constants carry type info.
// ---------------------------------------------------------------------------

schema! {
    /// Chat conversations containing messages
    Conversation "conversation" {
        /// Unique identifier
        ID: ID "id",
        /// Human-readable conversation title
        TITLE: ConversationTitle "title",
        /// Creation timestamp (epoch seconds)
        CREATED_AT: Timestamp "created_at",
        /// Last update timestamp (epoch seconds)
        UPDATED_AT: Timestamp "updated_at",
    }
}

impl Conversation {
    pub async fn create(
        db: &impl QueryExecutor,
        id: ID,
        title: ConversationTitle,
        now: Timestamp,
    ) -> StdResult<ID> {
        let query = query! {
            INSERT INTO Conversation::TABLE
                (Conversation::ID, Conversation::TITLE,
                 Conversation::CREATED_AT, Conversation::UPDATED_AT)
            VALUES ({id}, {title}, {now}, {now})
        };

        db.query_void(query).await?;
        Ok(id)
    }

    pub async fn load(db: &impl QueryExecutor, id: &ID) -> StdResult<Option<Conversation>> {
        let query = query! {
            SELECT Conversation::ID,
                   Conversation::TITLE,
                   Conversation::CREATED_AT,
                   Conversation::UPDATED_AT
            FROM Conversation::TABLE
            WHERE Conversation::ID = {id}
        };
        Ok(db.query_one::<Conversation>(query).await?)
    }

    pub async fn paginated(
        db: &impl QueryExecutor,
        after_id: Option<ID>,
        limit: Limit,
    ) -> StdResult<Paginated<Conversation>> {
        let query = query! {
            SELECT Conversation::ID,
                   Conversation::TITLE,
                   Conversation::CREATED_AT,
                   Conversation::UPDATED_AT
            FROM Conversation::TABLE
            WHERE {after_id} IS NULL OR Conversation::ID > {after_id}
            ORDER BY Conversation::ID ASC
            LIMIT {limit.0}
        };

        let conversations = db.query_list::<Conversation>(query).await?;
        let next = conversations.last().map(|c| ID(c.id.0));

        Ok(Paginated::new(next, conversations))
    }

    pub async fn update_title(
        db: &impl QueryExecutor,
        id: &ID,
        title: &ConversationTitle,
        now: Timestamp,
    ) -> StdResult<()> {
        let query = query! {
            UPDATE Conversation::TABLE
            SET Conversation::TITLE = {title},
                Conversation::UPDATED_AT = {now}
            WHERE Conversation::ID = {id}
        };

        db.query_void(query).await?;
        Ok(())
    }

    pub async fn delete(db: &impl QueryExecutor, id: &ID) -> StdResult<()> {
        let query = query! {
            DELETE FROM Conversation::TABLE
            WHERE Conversation::ID = {id}
        };
        db.query_void(query).await?;
        Ok(())
    }

    pub fn create_table() -> Query {
        query! {
            CREATE TABLE IF NOT EXISTS Conversation::TABLE (
                Conversation::ID INTEGER PRIMARY KEY,
                Conversation::TITLE TEXT NOT NULL,
                Conversation::CREATED_AT INTEGER NOT NULL,
                Conversation::UPDATED_AT INTEGER NOT NULL
            )
        }
    }
}

schema! {
    /// Messages within a conversation
    Message "message" {
        /// Unique identifier
        ID: ID "id",
        /// Owning conversation
        CONVERSATION_ID: ID "conversation_id",
        /// Message role: "user" or "assistant"
        ROLE: Role "role",
        /// Message body
        CONTENT: String "content",
        /// Creation timestamp (epoch seconds)
        CREATED_AT: Timestamp "created_at",
    }
}

impl Message {
    pub async fn create(
        db: &impl QueryExecutor,
        id: ID,
        conversation_id: ID,
        role: Role,
        content: &str,
        now: Timestamp,
    ) -> StdResult<ID> {
        let query = query! {
            INSERT INTO Message::TABLE
                (Message::ID, Message::CONVERSATION_ID,
                 Message::ROLE, Message::CONTENT,
                 Message::CREATED_AT)
            VALUES ({id}, {conversation_id}, {role}, {content}, {now})
        };

        db.query_void(query).await?;
        Ok(id)
    }

    pub async fn list_for_conversation(
        db: &impl QueryExecutor,
        conversation_id: &ID,
    ) -> StdResult<Vec<Message>> {
        let query = query! {
            SELECT Message::ID,
                   Message::CONVERSATION_ID,
                   Message::ROLE,
                   Message::CONTENT,
                   Message::CREATED_AT
            FROM Message::TABLE
            WHERE Message::CONVERSATION_ID = {conversation_id}
            ORDER BY Message::ID ASC
        };

        Ok(db.query_list::<Message>(query).await?)
    }

    pub async fn delete(db: &impl QueryExecutor, id: &ID) -> StdResult<()> {
        let query = query! {
            DELETE FROM Message::TABLE
            WHERE Message::ID = {id}
        };
        db.query_void(query).await?;
        Ok(())
    }

    pub fn create_table() -> Query {
        query! {
            CREATE TABLE IF NOT EXISTS Message::TABLE (
                Message::ID INTEGER PRIMARY KEY,
                Message::CONVERSATION_ID INTEGER NOT NULL,
                Message::ROLE TEXT NOT NULL,
                Message::CONTENT TEXT NOT NULL,
                Message::CREATED_AT INTEGER NOT NULL,
                FOREIGN KEY (Message::CONVERSATION_ID) REFERENCES Conversation::TABLE(Conversation::ID)
            )
        }
    }

    pub fn create_indexes() -> Query {
        query! {
            CREATE INDEX IF NOT EXISTS idx_message_conversation
                ON Message::TABLE(Message::CONVERSATION_ID);
            CREATE INDEX IF NOT EXISTS idx_message_created
                ON Message::TABLE(Message::CREATED_AT);
        }
    }
}

// --------------------------------------------------------------------------
// Test helpers
// ---------------------------------------------------------------------------

async fn create_executor() -> TursoExecutor {
    let connector = || async {
        let db = ::turso::Builder::new_local(":memory:").build().await?;
        db.connect()
    };
    TursoExecutor::new(connector).await.unwrap()
}

async fn setup(db: &TursoExecutor) {
    db.query_void(Conversation::create_table()).await.unwrap();
    db.query_void(Message::create_table()).await.unwrap();
    db.query_void(Message::create_indexes()).await.unwrap();
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[tokio::test]
async fn test_conversation_create_and_load() {
    let db = create_executor().await;
    setup(&db).await;

    let id = ID(1);
    let title = ConversationTitle("Hello World".into());
    let now = Timestamp(1_736_880_000);

    Conversation::create(&db, id, title.clone(), now)
        .await
        .unwrap();

    let loaded = Conversation::load(&db, &id).await.unwrap().unwrap();
    assert_eq!(loaded.id, id);
    assert_eq!(loaded.title, title);
    assert_eq!(loaded.created_at, now);
    assert_eq!(loaded.updated_at, now);
}

#[tokio::test]
async fn test_conversation_not_found() {
    let db = create_executor().await;
    setup(&db).await;

    let loaded = Conversation::load(&db, &ID(999)).await.unwrap();
    assert!(loaded.is_none());
}

#[tokio::test]
async fn test_conversation_update_title() {
    let db = create_executor().await;
    setup(&db).await;

    let id = ID(1);
    Conversation::create(&db, id, ConversationTitle("Old".into()), Timestamp(100))
        .await
        .unwrap();

    Conversation::update_title(
        &db,
        &id,
        &ConversationTitle("Updated".into()),
        Timestamp(200),
    )
    .await
    .unwrap();

    let loaded = Conversation::load(&db, &id).await.unwrap().unwrap();
    assert_eq!(loaded.title, ConversationTitle("Updated".into()));
    assert_eq!(loaded.updated_at, Timestamp(200));
    assert_eq!(loaded.created_at, Timestamp(100));
}

#[tokio::test]
async fn test_conversation_paginated() {
    let db = create_executor().await;
    setup(&db).await;

    for i in 1..=5_i64 {
        Conversation::create(
            &db,
            ID(i),
            ConversationTitle(format!("Chat {i}")),
            Timestamp(i),
        )
        .await
        .unwrap();
    }

    let page1 = Conversation::paginated(&db, None, Limit(2)).await.unwrap();
    assert_eq!(page1.items.len(), 2);
    assert_eq!(page1.items[0].id, ID(1));
    assert_eq!(page1.items[1].id, ID(2));
    assert_eq!(page1.next, Some(ID(2)));

    let page2 = Conversation::paginated(&db, Some(ID(2)), Limit(2))
        .await
        .unwrap();
    assert_eq!(page2.items.len(), 2);
    assert_eq!(page2.items[0].id, ID(3));
    assert_eq!(page2.items[1].id, ID(4));
    assert_eq!(page2.next, Some(ID(4)));

    let page3 = Conversation::paginated(&db, Some(ID(4)), Limit(2))
        .await
        .unwrap();
    assert_eq!(page3.items.len(), 1);
    assert_eq!(page3.items[0].id, ID(5));
    assert_eq!(page3.next, Some(ID(5)));

    let page4 = Conversation::paginated(&db, Some(ID(5)), Limit(2))
        .await
        .unwrap();
    assert_eq!(page4.items.len(), 0);
    assert_eq!(page4.next, None);
}

#[tokio::test]
async fn test_message_crud_within_conversation() {
    let db = create_executor().await;
    setup(&db).await;

    let conv_id = ID(10);
    let now = Timestamp(1_736_900_000);

    Conversation::create(&db, conv_id, ConversationTitle("SQL Chat".into()), now)
        .await
        .unwrap();

    let msg1 = Message::create(&db, ID(1), conv_id, Role::User, "What is sqlw?", now)
        .await
        .unwrap();
    let msg2 = Message::create(
        &db,
        ID(2),
        conv_id,
        Role::Assistant,
        "A compile-time SQL query builder.",
        now,
    )
    .await
    .unwrap();

    assert_eq!(msg1, ID(1));
    assert_eq!(msg2, ID(2));

    let messages = Message::list_for_conversation(&db, &conv_id).await.unwrap();
    assert_eq!(messages.len(), 2);
    assert_eq!(messages[0].role, Role::User);
    assert_eq!(messages[1].role, Role::Assistant);

    Message::delete(&db, &ID(1)).await.unwrap();

    let remaining = Message::list_for_conversation(&db, &conv_id).await.unwrap();
    assert_eq!(remaining.len(), 1);
    assert_eq!(remaining[0].id, ID(2));
}

#[tokio::test]
async fn test_conversation_delete_cascades() {
    let db = create_executor().await;
    setup(&db).await;

    let conv_id = ID(20);
    let now = Timestamp(1_737_000_000);

    Conversation::create(&db, conv_id, ConversationTitle("To Delete".into()), now)
        .await
        .unwrap();

    Message::create(&db, ID(1), conv_id, Role::User, "Hello!", now)
        .await
        .unwrap();

    Conversation::delete(&db, &conv_id).await.unwrap();

    let loaded = Conversation::load(&db, &conv_id).await.unwrap();
    assert!(loaded.is_none());
}

#[tokio::test]
async fn test_batch_create_conversation_and_messages() {
    let db = create_executor().await;
    setup(&db).await;

    fn create_conv() -> Query {
        query! {
            INSERT INTO Conversation::TABLE
                (Conversation::ID, Conversation::TITLE,
                 Conversation::CREATED_AT, Conversation::UPDATED_AT)
            VALUES ({ID(1)}, {ConversationTitle("Batch Chat".into())}, {Timestamp(1000)}, {Timestamp(1000)})
        }
    }

    fn add_msg1() -> Query {
        query! {
            INSERT INTO Message::TABLE
                (Message::ID, Message::CONVERSATION_ID,
                 Message::ROLE, Message::CONTENT,
                 Message::CREATED_AT)
            VALUES ({ID(1)}, {ID(1)}, {Role::User}, {"First"}, {Timestamp(1001)})
        }
    }

    fn add_msg2() -> Query {
        query! {
            INSERT INTO Message::TABLE
                (Message::ID, Message::CONVERSATION_ID,
                 Message::ROLE, Message::CONTENT,
                 Message::CREATED_AT)
            VALUES ({ID(2)}, {ID(1)}, {Role::Assistant}, {"Second"}, {Timestamp(1002)})
        }
    }

    let scripts: &[fn() -> Query] = &[create_conv, add_msg1, add_msg2];
    db.batch(scripts).await.unwrap();

    let conv = Conversation::load(&db, &ID(1)).await.unwrap().unwrap();
    assert_eq!(conv.title, ConversationTitle("Batch Chat".into()));

    let msgs = Message::list_for_conversation(&db, &ID(1)).await.unwrap();
    assert_eq!(msgs.len(), 2);
    assert_eq!(msgs[0].content, "First");
    assert_eq!(msgs[1].content, "Second");
}

#[tokio::test]
async fn test_computed_column_with_derived_fromrow() {
    let db = create_executor().await;
    setup(&db).await;

    Conversation::create(
        &db,
        ID(200),
        ConversationTitle("Compute field".into()),
        Timestamp(100),
    )
    .await
    .unwrap();

    let query = query! {
        SELECT Conversation::ID, Conversation::TITLE, LENGTH(Conversation::TITLE) AS title_length
        FROM Conversation::TABLE
        WHERE Conversation::ID = {ID(200)}
    };

    let view = db
        .query_one::<ConversationTitleView>(query)
        .await
        .unwrap()
        .expect("expected a result");

    assert_eq!(view.id, 200);
    assert_eq!(view.title, "Compute field");
    assert_eq!(view.title_length, 13);
}

#[tokio::test]
async fn test_derived_from_row_with_field_attrs() {
    let db = create_executor().await;
    setup(&db).await;

    Conversation::create(
        &db,
        ID(300),
        ConversationTitle("Derived".into()),
        Timestamp(200),
    )
    .await
    .unwrap();

    Message::create(&db, ID(10), ID(300), Role::User, "A", Timestamp(201))
        .await
        .unwrap();
    Message::create(&db, ID(11), ID(300), Role::Assistant, "B", Timestamp(202))
        .await
        .unwrap();

    let query = query! {
        SELECT
            Conversation::ID,
            Conversation::TITLE,
            (SELECT COUNT(*) FROM Message::TABLE
             WHERE Message::CONVERSATION_ID = {ID(300)}) AS message_count
        FROM Conversation::TABLE
        WHERE Conversation::ID = {ID(300)}
    };

    let result = db
        .query_one::<MessageCount>(query)
        .await
        .unwrap()
        .expect("expected a result");

    assert_eq!(result.conv_id, 300);
    assert_eq!(result.conv_title, ConversationTitle("Derived".into()));
    assert_eq!(result.count, 2);
}

#[tokio::test]
async fn test_typed_binding_through_where_clause() {
    let db = create_executor().await;
    setup(&db).await;

    Conversation::create(
        &db,
        ID(100),
        ConversationTitle("Typed Binding".into()),
        Timestamp(999),
    )
    .await
    .unwrap();

    let query = query! {
        SELECT Conversation::ID,
               Conversation::TITLE,
               Conversation::CREATED_AT,
               Conversation::UPDATED_AT
        FROM Conversation::TABLE
        WHERE Conversation::ID = {ID(100)}
    };

    let conv = db.query_one::<Conversation>(query).await.unwrap().unwrap();
    assert_eq!(conv.id, ID(100));
    assert_eq!(conv.title, ConversationTitle("Typed Binding".into()));
}