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