# Statiq-Macros
**Procedural macros for Statiq — compile-time SQL code generation**
[](https://crates.io/crates/statiq-macros)
[](https://docs.rs/statiq-macros)
This crate provides the `#[derive(SqlEntity)]` procedural macro used by [statiq](https://crates.io/crates/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`:
```toml
[dependencies]
statiq = "0.1"
```
If you need to use the macro directly (rare):
```toml
[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)]`:
```rust
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
| `#[sql_table(...)]` | `#[sql_table("Users", schema = "dbo")]` | Map struct to database table |
### Field-Level
| `#[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):
```rust
// 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](https://crates.io/crates/statiq)** — Main crate with CRUD operations, connection pooling, caching
- **Docs**: [docs.rs/statiq-macros](https://docs.rs/statiq-macros)
- **GitHub**: [github.com/yasinakmaz/Statiq](https://github.com/yasinakmaz/Statiq)
---
## 📄 License
Licensed under MIT OR Apache-2.0. See [LICENSE](../LICENSE-MIT) and [LICENSE-APACHE](../LICENSE-APACHE).