statiq_macros/lib.rs
1mod entity;
2mod params;
3
4use proc_macro::TokenStream;
5use syn::{parse_macro_input, DeriveInput};
6
7/// Derive macro that implements `statiq::entity::SqlEntity` for a struct.
8///
9/// Attributes:
10/// - `#[sql_table("TableName", schema = "dbo")]` — on the struct
11/// - `#[sql_primary_key(identity)]` — on one field
12/// - `#[sql_column("ColName")]` — override column name
13/// - `#[sql_ignore]` — exclude field from all SQL (not even SELECT)
14/// - `#[sql_computed]` — DB-computed column; SELECT only, excluded from INSERT/UPDATE/MERGE
15/// - `#[sql_default]` — server-side default; excluded from INSERT, included in SELECT/UPDATE
16///
17/// # Example
18/// ```ignore
19/// #[derive(SqlEntity)]
20/// #[sql_table("Users", schema = "dbo")]
21/// pub struct User {
22/// #[sql_primary_key(identity)]
23/// pub id: i32,
24/// #[sql_column("UserName")]
25/// pub name: String,
26/// pub active: bool,
27/// #[sql_computed]
28/// pub full_name: String, // DB-computed, never written
29/// #[sql_default]
30/// pub created_at: chrono::DateTime<chrono::Utc>, // GETDATE() default, not in INSERT
31/// }
32/// ```
33#[proc_macro_derive(SqlEntity, attributes(sql_table, sql_column, sql_primary_key, sql_ignore, sql_computed, sql_default))]
34pub fn derive_sql_entity(input: TokenStream) -> TokenStream {
35 let input = parse_macro_input!(input as DeriveInput);
36 entity::derive_sql_entity(input)
37 .unwrap_or_else(|e| e.to_compile_error())
38 .into()
39}