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 the validated SQL as a &'static str literal.

§Syntax

let sql = query!("SELECT id, name FROM users WHERE id = ?");

§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