statiq-macros 0.2.3

Proc-macro crate for statiq — compile-time SQL generation for MSSQL entities via #[derive(SqlEntity)]
Documentation

Statiq-Macros

Procedural macros for Statiq — compile-time SQL code generation

Crates.io Docs.rs

This crate provides the #[derive(SqlEntity)] procedural macro used by statiq to generate compile-time SQL constants and metadata for MSSQL database operations.


🎯 Purpose

statiq-macros is automatically used when you add the statiq crate to your project. It:

Generates SQL at compile timeSELECT, INSERT, UPDATE, DELETE statements as &'static str
Extracts type information — Entity structure, column names, primary keys
Supports SQL overrides#[sql_column], #[sql_table] attributes
Zero runtime overhead — All work happens during compilation


📦 Installation

You don't need to add this manually. When you add statiq to Cargo.toml, it automatically includes statiq-macros:

[dependencies]

statiq = "0.1"

If you need to use the macro directly (rare):

[dependencies]

statiq-macros = "0.1"



# You'll also need:

statiq = "0.1"

serde = { version = "1", features = ["derive"] }


📖 Usage

The macro is used via #[derive(SqlEntity)]:

use statiq_macros::SqlEntity;

#[derive(SqlEntity, serde::Serialize, serde::Deserialize, Clone)]
#[sql_table("Users", schema = "dbo")]
pub struct User {
    #[sql_primary_key(identity)]
    pub id: i32,

    pub email: String,
    pub name: String,

    #[sql_default]
    pub created_at: chrono::DateTime<chrono::Utc>,

    #[sql_computed]
    pub full_name: Option<String>,

    #[sql_ignore]
    pub temp_field: bool,
}

🏷️ Supported Attributes

Struct-Level

Attribute Example Purpose
#[sql_table(...)] #[sql_table("Users", schema = "dbo")] Map struct to database table

Field-Level

Attribute Purpose Auto-Included Auto-Excluded
#[sql_primary_key] Regular primary key SELECT, UPDATE INSERT
#[sql_primary_key(identity)] IDENTITY column SELECT, UPDATE INSERT
#[sql_column("ColumnName")] Override SQL column name - -
#[sql_default] Database default value SELECT, UPDATE INSERT
#[sql_computed] Computed column SELECT INSERT, UPDATE
#[sql_ignore] Not mapped to database - All SQL operations

🔍 Generated SQL Example

For the User struct above, the macro generates (conceptually):

// SELECT
const SQL_SELECT: &str = "SELECT [id], [email], [name], [created_at], [full_name] FROM [dbo].[Users]";
const SQL_SELECT_BY_ID: &str = "SELECT ... WHERE [id] = @id";

// INSERT (excludes id, full_name)
const SQL_INSERT: &str = "INSERT INTO [dbo].[Users] ([email], [name], [created_at]) VALUES (@email, @name, @created_at)";

// UPDATE (excludes id, full_name)
const SQL_UPDATE: &str = "UPDATE [dbo].[Users] SET [email] = @email, [name] = @name, [created_at] = @created_at WHERE [id] = @id";

// DELETE
const SQL_DELETE: &str = "DELETE FROM [dbo].[Users] WHERE [id] = @id";

All as compile-time constants — no runtime string building!


🚀 Why Use statiq-macros?

Performance

  • Zero reflection overhead
  • Static dispatch, no vtables
  • SQL statements are compile-time constants

Type Safety

  • Result sets map directly to Rust structs
  • Column name changes caught at compile time
  • Invalid SQL caught before runtime

Developer Experience

  • Single source of truth (your Rust struct)
  • No manual SQL strings to maintain
  • IDE support for field names

🔗 See Also


📄 License

Licensed under MIT OR Apache-2.0. See LICENSE and LICENSE-APACHE.