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 error;
34pub mod ident;
35pub mod value;
36pub mod where_;
37
38#[cfg(any(
39 feature = "sqlx_mysql",
40 feature = "sqlx_sqlite",
41 feature = "sqlx_postgres"
42))]
43pub mod fetch;
44#[cfg(any(
45 feature = "sqlx_mysql",
46 feature = "sqlx_sqlite",
47 feature = "sqlx_postgres"
48))]
49pub mod sqlx_bind;
50
51pub use builder::{
52 AggFn, ConflictAction, Cte, Having, Join, JoinClause, JoinCond, JoinKind, Lock, LockStrength,
53 LockWait, Method, OnConflict, Order, QueryBuilder, SelectExpr,
54};
55pub use compile::{compile, try_compile};
56pub use dialect::{Dialect, MySql, Postgres, Sqlite, UpsertStyle};
57pub use error::{BuildError, Error};
58pub use value::{IntoBind, Value};
59pub use where_::{Conj, Predicate, WhereBuilder};
60
61#[cfg(any(
62 feature = "sqlx_mysql",
63 feature = "sqlx_sqlite",
64 feature = "sqlx_postgres"
65))]
66pub use sqlx_bind::{SqlxDialect, SqlxQuery, SqlxQueryAs};