drizzle-sqlite 0.1.13

A type-safe SQL query builder 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
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
650
651
652
653
654
655
656
//! Attribute markers for `SQLiteTable` derive macro.
//!
//! These const markers are used within `#[column(...)]` and `#[SQLiteTable(...)]`
//! attributes. Import them from the prelude to get IDE hover documentation.
//!
//! # Example
//! ```rust
//! # let _ = r####"
//! # use drizzle::sqlite::prelude::*;
//!
//! #[SQLiteTable(
//!     name = "users",
//!     strict,
//!     unique(columns(email, tenant_id)),
//!     check(name = "users_score_check", expr = "score >= 0")
//! )]
//! struct User {
//!     #[column(primary, autoincrement)]
//!     id: i32,
//!     #[column(unique)]
//!     email: String,
//!     tenant_id: i32,
//!     score: i32,
//!     metadata: String,
//! }
//! # "####;
//! ```

/// Marker struct for column constraint attributes.
#[derive(Debug, Clone, Copy)]
pub struct ColumnMarker;

//------------------------------------------------------------------------------
// Primary Key Constraints
//------------------------------------------------------------------------------

/// Marks this column as the PRIMARY KEY.
///
/// ## Example
/// ```rust
/// # let _ = r####"
/// #[column(primary)]
/// id: i32,
/// # "####;
/// ```
///
/// See: <https://sqlite.org/lang_createtable.html#primkeyconst>
pub const PRIMARY: ColumnMarker = ColumnMarker;

/// Alias for [`PRIMARY`].
pub const PRIMARY_KEY: ColumnMarker = ColumnMarker;

/// Enables AUTOINCREMENT for INTEGER PRIMARY KEY columns.
///
/// ## Example
/// ```rust
/// # let _ = r####"
/// #[column(primary, autoincrement)]
/// id: i32,
/// # "####;
/// ```
///
/// See: <https://sqlite.org/autoinc.html>
pub const AUTOINCREMENT: ColumnMarker = ColumnMarker;

//------------------------------------------------------------------------------
// Uniqueness Constraints
//------------------------------------------------------------------------------

/// Adds a UNIQUE constraint to a column, table, or index.
///
/// ## Examples
/// ```rust
/// # let _ = r####"
/// #[column(unique)]
/// email: String,
///
/// #[SQLiteTable(unique(columns(email, tenant_id)))]
/// struct Users {
///     email: String,
///     tenant_id: i32,
/// }
///
/// #[SQLiteIndex(unique)]
/// struct UsersEmailIdx(Users::email);
/// # "####;
/// ```
///
/// See: <https://sqlite.org/lang_createtable.html#unique_constraints>
pub const UNIQUE: ColumnMarker = ColumnMarker;

//------------------------------------------------------------------------------
// Serialization Modes
//------------------------------------------------------------------------------

/// Enables JSON serialization with TEXT storage.
///
/// ## Example
/// ```rust
/// # let _ = r####"
/// #[column(json)]
/// metadata: UserMetadata,
/// # "####;
/// ```
///
/// Requires the `serde` feature. The field type must implement `Serialize` and `Deserialize`.
pub const JSON: ColumnMarker = ColumnMarker;

/// Enables JSON serialization with BLOB storage.
///
/// ## Example
/// ```rust
/// # let _ = r####"
/// #[column(jsonb)]
/// config: AppConfig,
/// # "####;
/// ```
///
/// Requires the `serde` feature. The field type must implement `Serialize` and `Deserialize`.
pub const JSONB: ColumnMarker = ColumnMarker;

/// Marks this column as storing an enum type.
///
/// ## Example
/// ```rust
/// # let _ = r####"
/// #[column(enum)]
/// role: Role,
///
/// #[column(integer, enum)]
/// status: Status,
/// # "####;
/// ```
///
/// The enum must derive `SQLiteEnum`.
pub const ENUM: ColumnMarker = ColumnMarker;

//------------------------------------------------------------------------------
// Default Value Parameters
//------------------------------------------------------------------------------

/// Specifies a function to generate default values at runtime.
///
/// The function is called for each insert when no value is provided.
///
/// ## Example
/// ```rust
/// # let _ = r####"
/// #[column(default_fn = Uuid::new_v4)]
/// id: Uuid,
/// # "####;
/// ```
///
/// ## Difference from DEFAULT
/// - `default_fn`: Calls the function at runtime for each insert (e.g., UUID generation)
/// - `default`: Uses a fixed compile-time value
pub const DEFAULT_FN: ColumnMarker = ColumnMarker;

/// Specifies a fixed default value for new rows.
///
/// ## Example
/// ```rust
/// # let _ = r####"
/// #[column(default = 0)]
/// count: i32,
///
/// #[column(default = "guest")]
/// role: String,
/// # "####;
/// ```
///
/// For runtime-generated values (UUIDs, timestamps), use [`DEFAULT_FN`] instead.
pub const DEFAULT: ColumnMarker = ColumnMarker;

/// Specifies a raw SQL default expression for new rows.
///
/// ## Example
/// ```rust
/// # let _ = r####"
/// #[column(default_sql = "CURRENT_TIMESTAMP")]
/// created_at: String,
/// # "####;
/// ```
///
/// See: <https://sqlite.org/lang_createtable.html#the_default_clause>
pub const DEFAULT_SQL: ColumnMarker = ColumnMarker;

/// Marks this column as a generated column.
///
/// ## Examples
/// ```rust
/// # let _ = r####"
/// #[column(generated(stored, "length(name)"))]
/// stored_name_len: i32,
///
/// #[column(generated(virtual, "length(name)"))]
/// virtual_name_len: i32,
/// # "####;
/// ```
///
/// See: <https://sqlite.org/gencol.html>
pub const GENERATED: ColumnMarker = ColumnMarker;

/// Adds a CHECK constraint for a column or table.
///
/// ## Examples
/// ```rust
/// # let _ = r####"
/// #[column(check = "score >= 0")]
/// score: i32,
///
/// #[SQLiteTable(check(name = "score_range", expr = "score >= 0 AND score <= 100"))]
/// struct Scores {
///     score: i32,
/// }
/// # "####;
/// ```
///
/// See: <https://sqlite.org/lang_createtable.html#check_constraints>
pub const CHECK: ColumnMarker = ColumnMarker;

/// Establishes a foreign key reference to another table's column.
///
/// ## Example
/// ```rust
/// # let _ = r####"
/// #[column(references = User::id)]
/// user_id: i32,
/// # "####;
/// ```
///
/// See: <https://sqlite.org/foreignkeys.html>
pub const REFERENCES: ColumnMarker = ColumnMarker;

/// Specifies the ON DELETE action for foreign key references.
///
/// ## Example
/// ```rust
/// # let _ = r####"
/// #[column(references = User::id, on_delete = CASCADE)]
/// user_id: i32,
/// # "####;
/// ```
///
/// ## Supported Actions
/// - `CASCADE`: Delete rows that reference the deleted row
/// - `SET_NULL`: Set the column to NULL when referenced row is deleted
/// - `SET_DEFAULT`: Set the column to its default value
/// - `RESTRICT`: Prevent deletion if referenced
/// - `NO_ACTION`: Similar to RESTRICT (default)
///
/// See: <https://sqlite.org/foreignkeys.html#fk_actions>
pub const ON_DELETE: ColumnMarker = ColumnMarker;

/// Specifies the ON UPDATE action for foreign key references.
///
/// ## Example
/// ```rust
/// # let _ = r####"
/// #[column(references = User::id, on_update = CASCADE)]
/// user_id: i32,
/// # "####;
/// ```
///
/// ## Supported Actions
/// - `CASCADE`: Update referencing rows when referenced row is updated
/// - `SET_NULL`: Set the column to NULL when referenced row is updated
/// - `SET_DEFAULT`: Set the column to its default value
/// - `RESTRICT`: Prevent update if referenced
/// - `NO_ACTION`: Similar to RESTRICT (default)
///
/// See: <https://sqlite.org/foreignkeys.html#fk_actions>
pub const ON_UPDATE: ColumnMarker = ColumnMarker;

//------------------------------------------------------------------------------
// Referential Action Values
//------------------------------------------------------------------------------

/// Type alias for referential action markers (uses `ColumnMarker` for macro compatibility).
pub type ReferentialAction = ColumnMarker;

/// CASCADE action: Propagate the delete/update to referencing rows.
///
/// ## Example
/// ```rust
/// # let _ = r####"
/// #[column(references = User::id, on_delete = CASCADE)]
/// user_id: i32,
/// # "####;
/// ```
///
/// See: <https://sqlite.org/foreignkeys.html#fk_actions>
pub const CASCADE: ColumnMarker = ColumnMarker;

/// SET NULL action: Set referencing columns to NULL.
///
/// ## Example
/// ```rust
/// # let _ = r####"
/// #[column(references = User::id, on_delete = SET_NULL)]
/// user_id: Option<i32>,
/// # "####;
/// ```
///
/// See: <https://sqlite.org/foreignkeys.html#fk_actions>
pub const SET_NULL: ColumnMarker = ColumnMarker;

/// SET DEFAULT action: Set referencing columns to their default values.
///
/// ## Example
/// ```rust
/// # let _ = r####"
/// #[column(references = User::id, on_delete = SET_DEFAULT, default = 0)]
/// user_id: i32,
/// # "####;
/// ```
///
/// See: <https://sqlite.org/foreignkeys.html#fk_actions>
pub const SET_DEFAULT: ColumnMarker = ColumnMarker;

/// RESTRICT action: Prevent delete/update if referenced.
///
/// ## Example
/// ```rust
/// # let _ = r####"
/// #[column(references = User::id, on_delete = RESTRICT)]
/// user_id: i32,
/// # "####;
/// ```
///
/// See: <https://sqlite.org/foreignkeys.html#fk_actions>
pub const RESTRICT: ColumnMarker = ColumnMarker;

/// NO ACTION action: Similar to RESTRICT (default behavior).
///
/// ## Example
/// ```rust
/// # let _ = r####"
/// #[column(references = User::id, on_delete = NO_ACTION)]
/// user_id: i32,
/// # "####;
/// ```
///
/// See: <https://sqlite.org/foreignkeys.html#fk_actions>
pub const NO_ACTION: ColumnMarker = ColumnMarker;

//------------------------------------------------------------------------------
// Collation Markers
//------------------------------------------------------------------------------

/// Specifies a collation sequence for a text column.
///
/// ## Example
/// ```rust
/// # let _ = r####"
/// #[column(COLLATE = NOCASE)]
/// name: String,
///
/// // String form for custom registered collations:
/// #[column(COLLATE = "my_collation")]
/// label: String,
/// # "####;
/// ```
///
/// See: <https://sqlite.org/datatype3.html#collation>
pub const COLLATE: ColumnMarker = ColumnMarker;

/// BINARY collation: bytewise comparison of operands. The default for `BLOB`
/// columns and any column without an explicit collation.
pub const BINARY: ColumnMarker = ColumnMarker;

/// NOCASE collation: ASCII case-insensitive comparison. Useful for text
/// columns that need case-insensitive equality / sorting.
pub const NOCASE: ColumnMarker = ColumnMarker;

/// RTRIM collation: like `BINARY` but trailing spaces are ignored when
/// comparing.
pub const RTRIM: ColumnMarker = ColumnMarker;

//------------------------------------------------------------------------------
// Name Marker (shared by column and table attributes)
//------------------------------------------------------------------------------

/// Marker struct for the NAME attribute.
#[derive(Debug, Clone, Copy)]
pub struct NameMarker;

/// Specifies a custom name in the database.
///
/// By default, table, view, and column names are automatically converted to `snake_case`
/// from the Rust struct/field name. Use NAME to override this behavior.
///
/// ## Column Example
/// ```rust
/// # let _ = r####"
/// // Field `createdAt` becomes `created_at` by default
/// created_at: DateTime<Utc>,
///
/// // Override with custom name:
/// #[column(name = "creation_timestamp")]
/// created_at: DateTime<Utc>,
/// # "####;
/// ```
///
/// ## Table Example
/// ```rust
/// # let _ = r####"
/// // Struct `UserAccount` becomes table `user_account` by default
/// struct UserAccount { ... }
///
/// // Override with custom name:
/// #[SQLiteTable(name = "user_accounts")]
/// struct UserAccount { ... }
/// # "####;
/// ```
///
/// ## View Example
/// ```rust
/// # let _ = r####"
/// #[SQLiteView(NAME = "active_users")]
/// struct ActiveUsers { ... }
/// # "####;
/// ```
pub const NAME: NameMarker = NameMarker;

//------------------------------------------------------------------------------
// View Attribute Markers
//------------------------------------------------------------------------------

/// Marker struct for view attributes.
#[derive(Debug, Clone, Copy)]
pub struct ViewMarker;

/// Specifies a view definition SQL string or expression.
///
/// ## Examples
/// ```rust
/// # let _ = r####"
/// #[SQLiteView(DEFINITION = "SELECT id, email FROM users")]
/// struct UserEmails { id: i32, email: String }
/// # "####;
/// ```
///
/// ```rust
/// # let _ = r####"
/// #[SQLiteView(
///     DEFINITION = {
///         let builder = drizzle::sqlite::QueryBuilder::new::<Schema>();
///         let Schema { user } = Schema::new();
///         builder.select((user.id, user.email)).from(user)
///     }
/// )]
/// struct UserEmails { id: i32, email: String }
/// # "####;
/// ```
pub const DEFINITION: ViewMarker = ViewMarker;

/// Marks the view as existing (skip creation).
///
/// ## Example
/// ```rust
/// # let _ = r####"
/// #[SQLiteView(EXISTING)]
/// struct ExistingView { ... }
/// # "####;
/// ```
pub const EXISTING: ViewMarker = ViewMarker;

//------------------------------------------------------------------------------
// Table Attribute Markers
//------------------------------------------------------------------------------

/// Marker struct for table-level attributes.
#[derive(Debug, Clone, Copy)]
pub struct TableMarker;

/// Adds a table-level composite foreign key constraint.
///
/// ## Example
/// ```rust
/// # let _ = r####"
/// #[SQLiteTable(foreign_key(
///     columns(tenant_id, user_id),
///     references(Users, tenant_id, id),
///     on_delete = "CASCADE"
/// ))]
/// struct Posts {
///     tenant_id: i32,
///     user_id: i32,
/// }
/// # "####;
/// ```
///
/// See: <https://sqlite.org/foreignkeys.html#fk_composite>
pub const FOREIGN_KEY: TableMarker = TableMarker;

/// Enables STRICT mode for the table.
///
/// ## Example
/// ```rust
/// # let _ = r####"
/// #[SQLiteTable(strict)]
/// struct Users {
///     #[column(primary)]
///     id: i32,
///     name: String,
/// }
/// # "####;
/// ```
///
/// # `SQLite` Behavior
/// - Enforces that values match declared column types exactly
/// - `INTEGER` columns only accept integers
/// - `TEXT` columns only accept text
/// - `REAL` columns only accept floating-point numbers
/// - `BLOB` columns only accept blobs
/// - `ANY` type allows any value (only in STRICT tables)
///
/// See: <https://sqlite.org/stricttables.html>
pub const STRICT: TableMarker = TableMarker;

/// Enables WITHOUT ROWID optimization for the table.
///
/// ## Example
/// ```rust
/// # let _ = r####"
/// #[SQLiteTable(without_rowid)]
/// struct KeyValue {
///     #[column(primary)]
///     key: String,
///     value: String,
/// }
/// # "####;
/// ```
///
/// Requires an explicit PRIMARY KEY.
///
/// See: <https://sqlite.org/withoutrowid.html>
pub const WITHOUT_ROWID: TableMarker = TableMarker;

//------------------------------------------------------------------------------
// Column Type Markers
//------------------------------------------------------------------------------

/// Marker struct for column type attributes.
#[derive(Debug, Clone, Copy)]
pub struct TypeMarker;

/// Specifies an INTEGER column type.
///
/// ## Example
/// ```rust
/// # let _ = r####"
/// #[column(integer, primary)]
/// id: i32,
/// # "####;
/// ```
///
/// INTEGER columns store signed integers up to 8 bytes (64-bit).
/// `SQLite` uses a variable-length encoding, so small values use less space.
///
/// See: <https://sqlite.org/datatype3.html#storage_classes_and_datatypes>
pub const INTEGER: TypeMarker = TypeMarker;

/// Specifies a TEXT column type.
///
/// ## Example
/// ```rust
/// # let _ = r####"
/// #[column(text)]
/// name: String,
/// # "####;
/// ```
///
/// TEXT columns store variable-length UTF-8 character strings with no size limit.
///
/// See: <https://sqlite.org/datatype3.html#storage_classes_and_datatypes>
pub const TEXT: TypeMarker = TypeMarker;

/// Specifies a BLOB column type.
///
/// ## Example
/// ```rust
/// # let _ = r####"
/// #[column(blob)]
/// data: Vec<u8>,
/// # "####;
/// ```
///
/// BLOB columns store binary data exactly as input.
///
/// See: <https://sqlite.org/datatype3.html#storage_classes_and_datatypes>
pub const BLOB: TypeMarker = TypeMarker;

/// Specifies a REAL column type.
///
/// ## Example
/// ```rust
/// # let _ = r####"
/// #[column(real)]
/// price: f64,
/// # "####;
/// ```
///
/// REAL columns store 8-byte IEEE floating point numbers.
///
/// See: <https://sqlite.org/datatype3.html#storage_classes_and_datatypes>
pub const REAL: TypeMarker = TypeMarker;

/// Specifies a NUMERIC column type.
///
/// ## Example
/// ```rust
/// # let _ = r####"
/// #[column(numeric)]
/// amount: f64,
/// # "####;
/// ```
///
/// NUMERIC columns store values as INTEGER, REAL, or TEXT depending on the value.
///
/// See: <https://sqlite.org/datatype3.html#type_affinity>
pub const NUMERIC: TypeMarker = TypeMarker;

/// Specifies an ANY column type (STRICT tables only).
///
/// ## Example
/// ```rust
/// # let _ = r####"
/// #[SQLiteTable(strict)]
/// struct Data {
///     #[column(any)]
///     value: serde_json::Value,
/// }
/// # "####;
/// ```
///
/// ANY allows any type of data. Only valid in STRICT tables.
///
/// See: <https://sqlite.org/stricttables.html>
pub const ANY: TypeMarker = TypeMarker;

/// Specifies a BOOLEAN column (stored as INTEGER 0/1).
///
/// ## Example
/// ```rust
/// # let _ = r####"
/// #[column(boolean)]
/// active: bool,
/// # "####;
/// ```
///
/// `SQLite` has no native BOOLEAN. Values are stored as INTEGER (0 for false, 1 for true).
///
/// See: <https://sqlite.org/datatype3.html#boolean_datatype>
pub const BOOLEAN: TypeMarker = TypeMarker;