Skip to main content

query

Macro query 

Source
query!() { /* proc-macro */ }
Expand description

Compile-time SQL validation with optional real DB verification.

Behavior:

  • Always runs the same syntax validation as sql_string!.
  • When the db-verify cargo feature is enabled AND the SZ_ORM_QUERY_VERIFY=1 environment variable is set at compile time, connects to the database pointed to by DATABASE_URL and runs EXPLAIN (MySQL/PostgreSQL) or EXPLAIN QUERY PLAN (SQLite) to verify the SQL is valid against the actual schema (column names, table names, joins, etc.).
  • Otherwise, falls back to syntax-only validation.

Emits a [sz_orm_core::queryable::Query] object wrapping the validated SQL.

§Syntax

use sz_orm_core::queryable::Query;
let q = query!("SELECT id, name FROM users WHERE id = ?");
let rows = q.fetch_all(&mut conn).await?;

§Verification setup

export DATABASE_URL="mysql://user:pass@host:3306/db"
export SZ_ORM_QUERY_VERIFY=1
cargo build --features sz-orm-macros/db-verify