sql-check

Crates.iosql-check
lib.rssql-check
version0.9.0-alpha.1
created_at2026-01-21 10:12:24.08466+00
updated_at2026-01-21 10:12:24.08466+00
descriptionCompile-time SQL validation crate extracted from SQLx
homepage
repositoryhttps://github.com/tamaro-skaljic/sql-check
max_upload_size
id2058787
size91,608
Tamaro Skaljic (tamaro-skaljic)

documentation

README

sql-check

Compile-time SQL validation extracted from SQLx.

This crate provides a check! macro that validates SQL queries at compile time by connecting to a database and ensuring the query is valid.

Features

  • Compile-time SQL validation: Catch SQL errors before your code runs
  • Feature-gated: Enable/disable validation with a feature flag
  • Multi-database support: PostgreSQL, MySQL, and SQLite
  • Zero runtime overhead: Validation happens only at compile time

Usage

Add sql-check to your Cargo.toml:

[dependencies]
sql-check = "0.9.0-alpha.1"

# Enable database support
sql-check = { version = "0.9.0-alpha.1", features = ["postgres"] }

Basic Example

use sql_check::check;

// This will be validated at compile time
let sql = check!("SELECT * FROM users WHERE id = 1");

// Use the SQL string with your database library
let result = database.execute(sql).await?;

Feature Flags

Core Features

  • check (default): Enables compile-time SQL validation
    • When enabled: Validates SQL by connecting to database
    • When disabled: Macro becomes a no-op and just returns the SQL string

Database Support

  • postgres: PostgreSQL support
  • mysql: MySQL support
  • sqlite: SQLite support

Runtime Support (choose one)

  • _rt-tokio: Use Tokio runtime (recommended)
  • _rt-async-std: Use async-std runtime
  • _rt-smol: Use smol runtime
  • _rt-async-global-executor: Use async-global-executor runtime

TLS Support (choose one)

  • _tls-rustls-ring-webpki: Use rustls with ring and webpki
  • _tls-rustls-aws-lc-rs: Use rustls with AWS-LC
  • _tls-rustls-ring-native-roots: Use rustls with ring and native roots
  • _tls-native-tls: Use native-tls

Example with Features

[dependencies]
sql-check = { 
    version = "0.9.0-alpha.1", 
    features = ["postgres", "_rt-tokio", "_tls-rustls-ring-webpki"] 
}

Environment Variables

The macro requires the following environment variables:

  • DATABASE_URL: Connection string for your database
    • PostgreSQL: postgres://user:pass@host/db
    • MySQL: mysql://user:pass@host/db
    • SQLite: sqlite://path/to/db.sqlite

Example

# Set DATABASE_URL before building
export DATABASE_URL="postgres://user:pass@localhost/mydb"
cargo build

Disabling Validation (CI/CD & Production)

For CI/CD pipelines, production builds, or when you don't have a database connection, disable the check feature:

[dependencies]
sql-check = { version = "0.9.0-alpha.1", default-features = false }

Or use cargo build flags:

cargo build --release --no-default-features

How It Works

  1. At Compile Time: The macro connects to your database
  2. Sends PREPARE: Database parses and validates the SQL
  3. Returns Metadata: Database returns column and parameter information
  4. Compilation Success: If valid, macro expands to the SQL string
  5. Compilation Error: If invalid, compilation fails with database error

Example Error

let sql = check!("SELECT invalid_column FROM nonexistent_table");

Compile Error:

error: SQL validation failed: relation "nonexistent_table" does not exist
  --> src/main.rs:5:15
   |
5  |     let sql = check!("SELECT invalid_column FROM nonexistent_table");
   |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Comparison with query! Macro

Feature sql-check check! SQLx query!
SQL Validation
Type Checking
Returns SQL String Typed Query
Use Case Pre-validation Full ORM

The check! macro is ideal when you:

  • Want SQL validation without SQLx's full macro system
  • Use a different database library (e.g., tokio-postgres, diesel)
  • Need just validation, not type generation

License

Licensed under either of

at your option.

Contributing

This crate is extracted from SQLx. Contributions are welcome!

Commit count: 8

cargo fmt