Statiq-Macros
Procedural macros for Statiq — compile-time SQL code generation
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 time — SELECT, 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:
[]
= "0.1"
If you need to use the macro directly (rare):
[]
= "0.1"
# You'll also need:
= "0.1"
= { = "1", = ["derive"] }
📖 Usage
The macro is used via #[derive(SqlEntity)]:
use SqlEntity;
🏷️ 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
- statiq — Main crate with CRUD operations, connection pooling, caching
- Docs: docs.rs/statiq-macros
- GitHub: github.com/yasinakmaz/Statiq
📄 License
Licensed under MIT OR Apache-2.0. See LICENSE and LICENSE-APACHE.