Skip to main content

chain_builder/
lib.rs

1//! # Chain Builder
2//!
3//! A typed, dialect-aware SQL query builder for Rust.
4//!
5//! Generic over a [`Dialect`] (PostgreSQL / MySQL / SQLite), with typed bind
6//! parameters via [`IntoBind`], dialect-correct placeholders (`$N` for Postgres,
7//! `?` for MySQL/SQLite), automatic identifier escaping, and an optional `sqlx`
8//! handoff for execution.
9//!
10//! # Example
11//! ```rust
12//! use chain_builder::{QueryBuilder, Postgres, Order};
13//!
14//! let (sql, binds) = QueryBuilder::<Postgres>::table("users")
15//!     .db("mydb")                       // multi-tenant: one connection, many DBs
16//!     .select(["id", "name"])
17//!     .where_eq("status", "active")
18//!     .where_in("role", ["admin", "staff"])
19//!     .order_by("name", Order::Desc)
20//!     .paginate(2, 20)
21//!     .to_sql();
22//!
23//! assert!(sql.starts_with(r#"SELECT "id", "name" FROM "mydb"."users""#));
24//! assert_eq!(binds.len(), 5); // active, admin, staff, limit, offset
25//! ```
26//!
27//! With a dialect's `sqlx_*` feature enabled, [`QueryBuilder::to_sqlx_query`] and
28//! the `fetch_*` helpers turn a builder into a ready-to-execute `sqlx` query.
29
30pub mod builder;
31pub mod compile;
32pub mod dialect;
33pub mod ident;
34pub mod value;
35pub mod where_;
36
37#[cfg(any(
38    feature = "sqlx_mysql",
39    feature = "sqlx_sqlite",
40    feature = "sqlx_postgres"
41))]
42pub mod fetch;
43#[cfg(any(
44    feature = "sqlx_mysql",
45    feature = "sqlx_sqlite",
46    feature = "sqlx_postgres"
47))]
48pub mod sqlx_bind;
49
50pub use builder::{
51    AggFn, ConflictAction, Cte, Having, Join, JoinClause, JoinCond, JoinKind, Lock, LockStrength,
52    LockWait, Method, OnConflict, Order, QueryBuilder, SelectExpr,
53};
54pub use compile::compile;
55pub use dialect::{Dialect, MySql, Postgres, Sqlite, UpsertStyle};
56pub use value::{IntoBind, Value};
57pub use where_::{Conj, Predicate, WhereBuilder};
58
59#[cfg(any(
60    feature = "sqlx_mysql",
61    feature = "sqlx_sqlite",
62    feature = "sqlx_postgres"
63))]
64pub use sqlx_bind::SqlxDialect;