Skip to main content

Crate sz_orm_macros

Crate sz_orm_macros 

Source
Expand description

SZ-ORM Procedural Macros - compile-time SQL validation & derive macros

Provides:

  • sql_string! macro that validates SQL string literals at compile time. Errors like SELECT * FORM users or '; DROP TABLE are caught before the binary is built.
  • #[derive(Schema)] — auto-generate table structure info from a struct.
  • #[derive(Builder)] — auto-generate builder pattern code for a struct.

§Usage

use sz_orm_macros::sql_string;

// Basic usage
let sql = sql_string!("SELECT * FROM users WHERE id = 1"); // ✅ compiles

// With parameter count check
let sql = sql_string!("SELECT * FROM users WHERE id = ?");
                     params: 1);                          // ✅ compiles

// ❌ compile error: missing FROM
let sql = sql_string!("SELECT * users WHERE id = 1");

// ❌ compile error: SQL injection detected
let sql = sql_string!("SELECT * FROM users WHERE name = 'x' OR '1'='1'");

// ❌ compile error: parameter count mismatch
let sql = sql_string!("SELECT * FROM users WHERE id = ?");
                     params: 2);

§Derive macros

use sz_orm_macros::{Schema, Builder};

#[derive(Schema)]
#[table(name = "users")]
struct User {
    #[column(primary_key)]
    id: i64,
    name: String,
}

#[derive(Builder)]
struct Order {
    id: i64,
    total: f64,
}

Macros§

migrate
migrate! 宏 — 编译时创建迁移并验证 SQL 语法
query
Compile-time SQL validation with optional real DB verification.
query_as
Compile-time SQL schema generator.
schema
sql_string
Compile-time SQL validation macro.
typed_query
Diesel-style strongly-typed AST macro (coexists with sql_string! / query!).

Derive Macros§

Builder
Derive macro: auto-generates builder pattern code.
ColumnEnum
Derive macro: auto-generates a <StructName>Column column-name enum from struct fields (P2-2).
Entity
Derive macro: auto-generates an sz_orm_core::Model trait impl.
FromQueryResult
Derive macro: auto-generates an sz_orm_core::FromQueryResult trait impl.
FromRow
Derive macro: auto-generates an sz_orm_core::queryable::FromRow trait impl.
GraphQLModel
Derive macro: auto-generates an sz_orm_graphql::schema_gen::GraphQLModelInfo impl.
Relation
Derive macro: auto-generates an sz_orm_core::model::ModelExt trait impl, populating the relations() map and eliminating hand-written relation boilerplate.
RelationTrait
#[derive(RelationTrait)] — auto-generates a RelationTrait impl (P-F-2, v2.1.0)
Schema
Derive macro: auto-generates table structure info from a Rust struct.
SqlType
Derive macro: auto-generates an sz_orm_core::FromQueryResult trait impl and a to_value() method for a Rust enum.