Skip to main content

drizzle_sqlite/
attrs.rs

1//! Attribute markers for `SQLiteTable` derive macro.
2//!
3//! These const markers are used within `#[column(...)]` and `#[SQLiteTable(...)]`
4//! attributes. Import them from the prelude to get IDE hover documentation.
5//!
6//! # Example
7//! ```rust
8//! # let _ = r####"
9//! # use drizzle::sqlite::prelude::*;
10//!
11//! #[SQLiteTable(
12//!     name = "users",
13//!     strict,
14//!     unique(columns(email, tenant_id)),
15//!     check(name = "users_score_check", expr = "score >= 0")
16//! )]
17//! struct User {
18//!     #[column(primary, autoincrement)]
19//!     id: i32,
20//!     #[column(unique)]
21//!     email: String,
22//!     tenant_id: i32,
23//!     score: i32,
24//!     metadata: String,
25//! }
26//! # "####;
27//! ```
28
29/// Marker struct for column constraint attributes.
30#[derive(Debug, Clone, Copy)]
31pub struct ColumnMarker;
32
33//------------------------------------------------------------------------------
34// Primary Key Constraints
35//------------------------------------------------------------------------------
36
37/// Marks this column as the PRIMARY KEY.
38///
39/// ## Example
40/// ```rust
41/// # let _ = r####"
42/// #[column(primary)]
43/// id: i32,
44/// # "####;
45/// ```
46///
47/// See: <https://sqlite.org/lang_createtable.html#primkeyconst>
48pub const PRIMARY: ColumnMarker = ColumnMarker;
49
50/// Alias for [`PRIMARY`].
51pub const PRIMARY_KEY: ColumnMarker = ColumnMarker;
52
53/// Enables AUTOINCREMENT for INTEGER PRIMARY KEY columns.
54///
55/// ## Example
56/// ```rust
57/// # let _ = r####"
58/// #[column(primary, autoincrement)]
59/// id: i32,
60/// # "####;
61/// ```
62///
63/// See: <https://sqlite.org/autoinc.html>
64pub const AUTOINCREMENT: ColumnMarker = ColumnMarker;
65
66//------------------------------------------------------------------------------
67// Uniqueness Constraints
68//------------------------------------------------------------------------------
69
70/// Adds a UNIQUE constraint to a column, table, or index.
71///
72/// ## Examples
73/// ```rust
74/// # let _ = r####"
75/// #[column(unique)]
76/// email: String,
77///
78/// #[SQLiteTable(unique(columns(email, tenant_id)))]
79/// struct Users {
80///     email: String,
81///     tenant_id: i32,
82/// }
83///
84/// #[SQLiteIndex(unique)]
85/// struct UsersEmailIdx(Users::email);
86/// # "####;
87/// ```
88///
89/// See: <https://sqlite.org/lang_createtable.html#unique_constraints>
90pub const UNIQUE: ColumnMarker = ColumnMarker;
91
92//------------------------------------------------------------------------------
93// Serialization Modes
94//------------------------------------------------------------------------------
95
96/// Enables JSON serialization with TEXT storage.
97///
98/// ## Example
99/// ```rust
100/// # let _ = r####"
101/// #[column(json)]
102/// metadata: UserMetadata,
103/// # "####;
104/// ```
105///
106/// Requires the `serde` feature. The field type must implement `Serialize` and `Deserialize`.
107pub const JSON: ColumnMarker = ColumnMarker;
108
109/// Enables JSON serialization with BLOB storage.
110///
111/// ## Example
112/// ```rust
113/// # let _ = r####"
114/// #[column(jsonb)]
115/// config: AppConfig,
116/// # "####;
117/// ```
118///
119/// Requires the `serde` feature. The field type must implement `Serialize` and `Deserialize`.
120pub const JSONB: ColumnMarker = ColumnMarker;
121
122/// Marks this column as storing an enum type.
123///
124/// ## Example
125/// ```rust
126/// # let _ = r####"
127/// #[column(enum)]
128/// role: Role,
129///
130/// #[column(integer, enum)]
131/// status: Status,
132/// # "####;
133/// ```
134///
135/// The enum must derive `SQLiteEnum`.
136pub const ENUM: ColumnMarker = ColumnMarker;
137
138//------------------------------------------------------------------------------
139// Default Value Parameters
140//------------------------------------------------------------------------------
141
142/// Specifies a function to generate default values at runtime.
143///
144/// The function is called for each insert when no value is provided.
145///
146/// ## Example
147/// ```rust
148/// # let _ = r####"
149/// #[column(default_fn = Uuid::new_v4)]
150/// id: Uuid,
151/// # "####;
152/// ```
153///
154/// ## Difference from DEFAULT
155/// - `default_fn`: Calls the function at runtime for each insert (e.g., UUID generation)
156/// - `default`: Uses a fixed compile-time value
157pub const DEFAULT_FN: ColumnMarker = ColumnMarker;
158
159/// Specifies a fixed default value for new rows.
160///
161/// ## Example
162/// ```rust
163/// # let _ = r####"
164/// #[column(default = 0)]
165/// count: i32,
166///
167/// #[column(default = "guest")]
168/// role: String,
169/// # "####;
170/// ```
171///
172/// For runtime-generated values (UUIDs, timestamps), use [`DEFAULT_FN`] instead.
173pub const DEFAULT: ColumnMarker = ColumnMarker;
174
175/// Specifies a raw SQL default expression for new rows.
176///
177/// ## Example
178/// ```rust
179/// # let _ = r####"
180/// #[column(default_sql = "CURRENT_TIMESTAMP")]
181/// created_at: String,
182/// # "####;
183/// ```
184///
185/// See: <https://sqlite.org/lang_createtable.html#the_default_clause>
186pub const DEFAULT_SQL: ColumnMarker = ColumnMarker;
187
188/// Marks this column as a generated column.
189///
190/// ## Examples
191/// ```rust
192/// # let _ = r####"
193/// #[column(generated(stored, "length(name)"))]
194/// stored_name_len: i32,
195///
196/// #[column(generated(virtual, "length(name)"))]
197/// virtual_name_len: i32,
198/// # "####;
199/// ```
200///
201/// See: <https://sqlite.org/gencol.html>
202pub const GENERATED: ColumnMarker = ColumnMarker;
203
204/// Adds a CHECK constraint for a column or table.
205///
206/// ## Examples
207/// ```rust
208/// # let _ = r####"
209/// #[column(check = "score >= 0")]
210/// score: i32,
211///
212/// #[SQLiteTable(check(name = "score_range", expr = "score >= 0 AND score <= 100"))]
213/// struct Scores {
214///     score: i32,
215/// }
216/// # "####;
217/// ```
218///
219/// See: <https://sqlite.org/lang_createtable.html#check_constraints>
220pub const CHECK: ColumnMarker = ColumnMarker;
221
222/// Establishes a foreign key reference to another table's column.
223///
224/// ## Example
225/// ```rust
226/// # let _ = r####"
227/// #[column(references = User::id)]
228/// user_id: i32,
229/// # "####;
230/// ```
231///
232/// See: <https://sqlite.org/foreignkeys.html>
233pub const REFERENCES: ColumnMarker = ColumnMarker;
234
235/// Specifies the ON DELETE action for foreign key references.
236///
237/// ## Example
238/// ```rust
239/// # let _ = r####"
240/// #[column(references = User::id, on_delete = CASCADE)]
241/// user_id: i32,
242/// # "####;
243/// ```
244///
245/// ## Supported Actions
246/// - `CASCADE`: Delete rows that reference the deleted row
247/// - `SET_NULL`: Set the column to NULL when referenced row is deleted
248/// - `SET_DEFAULT`: Set the column to its default value
249/// - `RESTRICT`: Prevent deletion if referenced
250/// - `NO_ACTION`: Similar to RESTRICT (default)
251///
252/// See: <https://sqlite.org/foreignkeys.html#fk_actions>
253pub const ON_DELETE: ColumnMarker = ColumnMarker;
254
255/// Specifies the ON UPDATE action for foreign key references.
256///
257/// ## Example
258/// ```rust
259/// # let _ = r####"
260/// #[column(references = User::id, on_update = CASCADE)]
261/// user_id: i32,
262/// # "####;
263/// ```
264///
265/// ## Supported Actions
266/// - `CASCADE`: Update referencing rows when referenced row is updated
267/// - `SET_NULL`: Set the column to NULL when referenced row is updated
268/// - `SET_DEFAULT`: Set the column to its default value
269/// - `RESTRICT`: Prevent update if referenced
270/// - `NO_ACTION`: Similar to RESTRICT (default)
271///
272/// See: <https://sqlite.org/foreignkeys.html#fk_actions>
273pub const ON_UPDATE: ColumnMarker = ColumnMarker;
274
275//------------------------------------------------------------------------------
276// Referential Action Values
277//------------------------------------------------------------------------------
278
279/// Type alias for referential action markers (uses `ColumnMarker` for macro compatibility).
280pub type ReferentialAction = ColumnMarker;
281
282/// CASCADE action: Propagate the delete/update to referencing rows.
283///
284/// ## Example
285/// ```rust
286/// # let _ = r####"
287/// #[column(references = User::id, on_delete = CASCADE)]
288/// user_id: i32,
289/// # "####;
290/// ```
291///
292/// See: <https://sqlite.org/foreignkeys.html#fk_actions>
293pub const CASCADE: ColumnMarker = ColumnMarker;
294
295/// SET NULL action: Set referencing columns to NULL.
296///
297/// ## Example
298/// ```rust
299/// # let _ = r####"
300/// #[column(references = User::id, on_delete = SET_NULL)]
301/// user_id: Option<i32>,
302/// # "####;
303/// ```
304///
305/// See: <https://sqlite.org/foreignkeys.html#fk_actions>
306pub const SET_NULL: ColumnMarker = ColumnMarker;
307
308/// SET DEFAULT action: Set referencing columns to their default values.
309///
310/// ## Example
311/// ```rust
312/// # let _ = r####"
313/// #[column(references = User::id, on_delete = SET_DEFAULT, default = 0)]
314/// user_id: i32,
315/// # "####;
316/// ```
317///
318/// See: <https://sqlite.org/foreignkeys.html#fk_actions>
319pub const SET_DEFAULT: ColumnMarker = ColumnMarker;
320
321/// RESTRICT action: Prevent delete/update if referenced.
322///
323/// ## Example
324/// ```rust
325/// # let _ = r####"
326/// #[column(references = User::id, on_delete = RESTRICT)]
327/// user_id: i32,
328/// # "####;
329/// ```
330///
331/// See: <https://sqlite.org/foreignkeys.html#fk_actions>
332pub const RESTRICT: ColumnMarker = ColumnMarker;
333
334/// NO ACTION action: Similar to RESTRICT (default behavior).
335///
336/// ## Example
337/// ```rust
338/// # let _ = r####"
339/// #[column(references = User::id, on_delete = NO_ACTION)]
340/// user_id: i32,
341/// # "####;
342/// ```
343///
344/// See: <https://sqlite.org/foreignkeys.html#fk_actions>
345pub const NO_ACTION: ColumnMarker = ColumnMarker;
346
347//------------------------------------------------------------------------------
348// Collation Markers
349//------------------------------------------------------------------------------
350
351/// Specifies a collation sequence for a text column.
352///
353/// ## Example
354/// ```rust
355/// # let _ = r####"
356/// #[column(COLLATE = NOCASE)]
357/// name: String,
358///
359/// // String form for custom registered collations:
360/// #[column(COLLATE = "my_collation")]
361/// label: String,
362/// # "####;
363/// ```
364///
365/// See: <https://sqlite.org/datatype3.html#collation>
366pub const COLLATE: ColumnMarker = ColumnMarker;
367
368/// BINARY collation: bytewise comparison of operands. The default for `BLOB`
369/// columns and any column without an explicit collation.
370pub const BINARY: ColumnMarker = ColumnMarker;
371
372/// NOCASE collation: ASCII case-insensitive comparison. Useful for text
373/// columns that need case-insensitive equality / sorting.
374pub const NOCASE: ColumnMarker = ColumnMarker;
375
376/// RTRIM collation: like `BINARY` but trailing spaces are ignored when
377/// comparing.
378pub const RTRIM: ColumnMarker = ColumnMarker;
379
380//------------------------------------------------------------------------------
381// Name Marker (shared by column and table attributes)
382//------------------------------------------------------------------------------
383
384/// Marker struct for the NAME attribute.
385#[derive(Debug, Clone, Copy)]
386pub struct NameMarker;
387
388/// Specifies a custom name in the database.
389///
390/// By default, table, view, and column names are automatically converted to `snake_case`
391/// from the Rust struct/field name. Use NAME to override this behavior.
392///
393/// ## Column Example
394/// ```rust
395/// # let _ = r####"
396/// // Field `createdAt` becomes `created_at` by default
397/// created_at: DateTime<Utc>,
398///
399/// // Override with custom name:
400/// #[column(name = "creation_timestamp")]
401/// created_at: DateTime<Utc>,
402/// # "####;
403/// ```
404///
405/// ## Table Example
406/// ```rust
407/// # let _ = r####"
408/// // Struct `UserAccount` becomes table `user_account` by default
409/// struct UserAccount { ... }
410///
411/// // Override with custom name:
412/// #[SQLiteTable(name = "user_accounts")]
413/// struct UserAccount { ... }
414/// # "####;
415/// ```
416///
417/// ## View Example
418/// ```rust
419/// # let _ = r####"
420/// #[SQLiteView(NAME = "active_users")]
421/// struct ActiveUsers { ... }
422/// # "####;
423/// ```
424pub const NAME: NameMarker = NameMarker;
425
426//------------------------------------------------------------------------------
427// View Attribute Markers
428//------------------------------------------------------------------------------
429
430/// Marker struct for view attributes.
431#[derive(Debug, Clone, Copy)]
432pub struct ViewMarker;
433
434/// Specifies a view definition SQL string or expression.
435///
436/// ## Examples
437/// ```rust
438/// # let _ = r####"
439/// #[SQLiteView(DEFINITION = "SELECT id, email FROM users")]
440/// struct UserEmails { id: i32, email: String }
441/// # "####;
442/// ```
443///
444/// ```rust
445/// # let _ = r####"
446/// #[SQLiteView(
447///     DEFINITION = {
448///         let builder = drizzle::sqlite::QueryBuilder::new::<Schema>();
449///         let Schema { user } = Schema::new();
450///         builder.select((user.id, user.email)).from(user)
451///     }
452/// )]
453/// struct UserEmails { id: i32, email: String }
454/// # "####;
455/// ```
456pub const DEFINITION: ViewMarker = ViewMarker;
457
458/// Marks the view as existing (skip creation).
459///
460/// ## Example
461/// ```rust
462/// # let _ = r####"
463/// #[SQLiteView(EXISTING)]
464/// struct ExistingView { ... }
465/// # "####;
466/// ```
467pub const EXISTING: ViewMarker = ViewMarker;
468
469//------------------------------------------------------------------------------
470// Table Attribute Markers
471//------------------------------------------------------------------------------
472
473/// Marker struct for table-level attributes.
474#[derive(Debug, Clone, Copy)]
475pub struct TableMarker;
476
477/// Adds a table-level composite foreign key constraint.
478///
479/// ## Example
480/// ```rust
481/// # let _ = r####"
482/// #[SQLiteTable(foreign_key(
483///     columns(tenant_id, user_id),
484///     references(Users, tenant_id, id),
485///     on_delete = "CASCADE"
486/// ))]
487/// struct Posts {
488///     tenant_id: i32,
489///     user_id: i32,
490/// }
491/// # "####;
492/// ```
493///
494/// See: <https://sqlite.org/foreignkeys.html#fk_composite>
495pub const FOREIGN_KEY: TableMarker = TableMarker;
496
497/// Enables STRICT mode for the table.
498///
499/// ## Example
500/// ```rust
501/// # let _ = r####"
502/// #[SQLiteTable(strict)]
503/// struct Users {
504///     #[column(primary)]
505///     id: i32,
506///     name: String,
507/// }
508/// # "####;
509/// ```
510///
511/// # `SQLite` Behavior
512/// - Enforces that values match declared column types exactly
513/// - `INTEGER` columns only accept integers
514/// - `TEXT` columns only accept text
515/// - `REAL` columns only accept floating-point numbers
516/// - `BLOB` columns only accept blobs
517/// - `ANY` type allows any value (only in STRICT tables)
518///
519/// See: <https://sqlite.org/stricttables.html>
520pub const STRICT: TableMarker = TableMarker;
521
522/// Enables WITHOUT ROWID optimization for the table.
523///
524/// ## Example
525/// ```rust
526/// # let _ = r####"
527/// #[SQLiteTable(without_rowid)]
528/// struct KeyValue {
529///     #[column(primary)]
530///     key: String,
531///     value: String,
532/// }
533/// # "####;
534/// ```
535///
536/// Requires an explicit PRIMARY KEY.
537///
538/// See: <https://sqlite.org/withoutrowid.html>
539pub const WITHOUT_ROWID: TableMarker = TableMarker;
540
541//------------------------------------------------------------------------------
542// Column Type Markers
543//------------------------------------------------------------------------------
544
545/// Marker struct for column type attributes.
546#[derive(Debug, Clone, Copy)]
547pub struct TypeMarker;
548
549/// Specifies an INTEGER column type.
550///
551/// ## Example
552/// ```rust
553/// # let _ = r####"
554/// #[column(integer, primary)]
555/// id: i32,
556/// # "####;
557/// ```
558///
559/// INTEGER columns store signed integers up to 8 bytes (64-bit).
560/// `SQLite` uses a variable-length encoding, so small values use less space.
561///
562/// See: <https://sqlite.org/datatype3.html#storage_classes_and_datatypes>
563pub const INTEGER: TypeMarker = TypeMarker;
564
565/// Specifies a TEXT column type.
566///
567/// ## Example
568/// ```rust
569/// # let _ = r####"
570/// #[column(text)]
571/// name: String,
572/// # "####;
573/// ```
574///
575/// TEXT columns store variable-length UTF-8 character strings with no size limit.
576///
577/// See: <https://sqlite.org/datatype3.html#storage_classes_and_datatypes>
578pub const TEXT: TypeMarker = TypeMarker;
579
580/// Specifies a BLOB column type.
581///
582/// ## Example
583/// ```rust
584/// # let _ = r####"
585/// #[column(blob)]
586/// data: Vec<u8>,
587/// # "####;
588/// ```
589///
590/// BLOB columns store binary data exactly as input.
591///
592/// See: <https://sqlite.org/datatype3.html#storage_classes_and_datatypes>
593pub const BLOB: TypeMarker = TypeMarker;
594
595/// Specifies a REAL column type.
596///
597/// ## Example
598/// ```rust
599/// # let _ = r####"
600/// #[column(real)]
601/// price: f64,
602/// # "####;
603/// ```
604///
605/// REAL columns store 8-byte IEEE floating point numbers.
606///
607/// See: <https://sqlite.org/datatype3.html#storage_classes_and_datatypes>
608pub const REAL: TypeMarker = TypeMarker;
609
610/// Specifies a NUMERIC column type.
611///
612/// ## Example
613/// ```rust
614/// # let _ = r####"
615/// #[column(numeric)]
616/// amount: f64,
617/// # "####;
618/// ```
619///
620/// NUMERIC columns store values as INTEGER, REAL, or TEXT depending on the value.
621///
622/// See: <https://sqlite.org/datatype3.html#type_affinity>
623pub const NUMERIC: TypeMarker = TypeMarker;
624
625/// Specifies an ANY column type (STRICT tables only).
626///
627/// ## Example
628/// ```rust
629/// # let _ = r####"
630/// #[SQLiteTable(strict)]
631/// struct Data {
632///     #[column(any)]
633///     value: serde_json::Value,
634/// }
635/// # "####;
636/// ```
637///
638/// ANY allows any type of data. Only valid in STRICT tables.
639///
640/// See: <https://sqlite.org/stricttables.html>
641pub const ANY: TypeMarker = TypeMarker;
642
643/// Specifies a BOOLEAN column (stored as INTEGER 0/1).
644///
645/// ## Example
646/// ```rust
647/// # let _ = r####"
648/// #[column(boolean)]
649/// active: bool,
650/// # "####;
651/// ```
652///
653/// `SQLite` has no native BOOLEAN. Values are stored as INTEGER (0 for false, 1 for true).
654///
655/// See: <https://sqlite.org/datatype3.html#boolean_datatype>
656pub const BOOLEAN: TypeMarker = TypeMarker;