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