Skip to content

Introduction

chain-builder is a typed, dialect-aware SQL query builder for Rust. One builder API, generic over a Dialect marker type, targets PostgreSQL, MySQL, and SQLite — mixing dialects is a compile error, not a runtime surprise.

Two safety properties hold everywhere in the API, by construction:

  • Values are always bound parameters. Anything you pass as a value (where_eq("status", "active"), insert(...), limit(...)) becomes a placeholder ($1 on Postgres, ? on MySQL/SQLite) plus a typed bind — it is never interpolated into the SQL string.
  • Identifiers are always escaped. Table, column, and database names are quoted with the dialect's quote character ("users" on Postgres/SQLite, `users` on MySQL), with embedded quote characters doubled.

The only places these guarantees do not apply are the explicitly named *_raw escape hatches — see the Security Model for the complete inventory.

A taste

rust
let (sql, binds) = QueryBuilder::<Postgres>::table("users")
    .select(["id"])
    .where_eq("status", "active")
    .to_sql();
// SELECT "id" FROM "users" WHERE "status" = $1

Swap Postgres for MySql and the same chain renders backticks and ? placeholders. See Getting Started for installation and a full walkthrough.

Dialect feature matrix

FeaturePostgreSQLMySQLSQLite
Placeholder style$N (numbered)? (positional)? (positional)
Identifier quote char"`"
Upsert styleON CONFLICT (...) DO NOTHING / DO UPDATE SET …INSERT IGNORE / ON DUPLICATE KEY UPDATE …ON CONFLICT (...) DO NOTHING / DO UPDATE SET …
RETURNING✅ supported❌ not supported (clause is a no-op)✅ supported
DISTINCT ON✅ supported❌ build error❌ build error
Row locking (FOR UPDATE / FOR SHARE)✅ supported✅ supported❌ silent no-op (SQLite locks the whole database)
Native ILIKE✅ native operatorlowered to LOWER(col) LIKE LOWER(?)lowered to LOWER(col) LIKE LOWER(?)

The Dialect Differences page expands each row with prose notes and examples.

Where to find things

This book is the long-form guide: query-building chapters, a reference section (binds, errors, sqlx execution, dialects), a cookbook of realistic recipes, and deep dives into the security model and compiler internals.