🔱 A dynamic query builder for MySQL, Postgres and SQLite
[](https://crates.io/crates/sea-query)
[](https://docs.rs/sea-query)
[](https://github.com/SeaQL/sea-query/actions/workflows/rust.yml)
## SeaQuery
SeaQuery is a query builder to help you construct dynamic SQL queries in Rust.
You can construct expressions, queries and schema as abstract syntax trees using an ergonomic API.
We support MySQL, Postgres and SQLite behind a common interface that aligns their behaviour where appropriate.
MS SQL Server Support is available under [SeaORM X](https://www.sea-ql.org/SeaORM-X/).
SeaQuery is written in 100% safe Rust. All workspace crates has `#![forbid(unsafe_code)]`.
SeaQuery is the foundation of [SeaORM](https://github.com/SeaQL/sea-orm), an async & dynamic ORM for Rust.
We provide integration for [SQLx](https://crates.io/crates/sqlx),
[postgres](https://crates.io/crates/postgres) and [rusqlite](https://crates.io/crates/rusqlite).
See [examples](https://github.com/SeaQL/sea-query/blob/master/examples) for usage.
[](https://github.com/SeaQL/sea-query/stargazers/)
If you like what we do, consider starring, commenting, sharing and contributing!
[](https://discord.com/invite/uCPdDXzbdv)
Join our Discord server to chat with others in the SeaQL community!
## Install
```toml
# Cargo.toml
[dependencies]
sea-query = "1.0"
```
SeaQuery is very lightweight, all dependencies are optional.
### Feature flags
Macro: `derive`
SQL engine: `backend-mysql`, `backend-postgres`, `backend-sqlite`
Type support: `with-chrono`, `with-time`, `with-json`, `with-rust_decimal`, `with-bigdecimal`, `with-uuid`,
`with-ipnetwork`, `with-mac_address`, `postgres-array`, `postgres-interval`, `postgres-vector`
## Usage
Table of Content
1. Basics
1. [Iden](#iden)
1. [Expression](#expression)
1. [Condition](#condition)
1. [Statement Builders](#statement-builders)
1. Query Statement
1. [Query Select](#query-select)
1. [Query Insert](#query-insert)
1. [Query Update](#query-update)
1. [Query Delete](#query-delete)
1. Advanced
1. [Aggregate Functions](#aggregate-functions)
1. [Casting](#casting)
1. [Custom Function](#custom-function)
1. Schema Statement
1. [Table Create](#table-create)
1. [Table Alter](#table-alter)
1. [Table Drop](#table-drop)
1. [Table Rename](#table-rename)
1. [Table Truncate](#table-truncate)
1. [Foreign Key Create](#foreign-key-create)
1. [Foreign Key Drop](#foreign-key-drop)
1. [Index Create](#index-create)
1. [Index Drop](#index-drop)
## Motivation
Why would you want to use a dynamic query builder?
### 1. Parameter bindings
One of the headaches when using raw SQL is parameter binding. With SeaQuery you can inject parameters
right alongside the expression, and the $N sequencing will be handled for you. No more "off by one" errors!
```rust
assert_eq!(
Query::select()
.expr(Expr::col("size_w").add(1).mul(2))
.from("glyph")
.and_where(Expr::col("image").like("A"))
.and_where(Expr::col("id").is_in([3, 4, 5]))
.build(PostgresQueryBuilder),
(
r#"SELECT ("size_w" + $1) * $2 FROM "glyph" WHERE "image" LIKE $3 AND "id" IN ($4, $5, $6)"#
.to_owned(),
Values(vec![
1.into(),
2.into(),
"A".to_owned().into(),
3.into(),
4.into(),
5.into(),
])
)
);
```
If you need an "escape hatch" to construct complex queries, you can use custom expressions,
and still have the benefit of sequentially-binded parameters.
```rust
assert_eq!(
Query::select()
.columns(["size_w", "size_h"])
.from("character")
.and_where(Expr::col("id").eq(1)) // this is $1
// custom expressions only need to define local parameter sequence.
// its global sequence will be re-written.
// here, we flip the order of $2 & $1 to make it look tricker!
.and_where(Expr::cust_with_values(r#""size_w" = $2 * $1"#, [3, 2]))
.and_where(Expr::col("size_h").gt(4)) // this is $N?
.build(PostgresQueryBuilder),
(
r#"SELECT "size_w", "size_h" FROM "character" WHERE "id" = $1 AND ("size_w" = $2 * $3) AND "size_h" > $4"#
.to_owned(),
Values(vec![1.into(), 2.into(), 3.into(), 4.into()])
)
);
```
### 2. Dynamic query
You can construct the query at runtime based on user inputs with a fluent interface,
so you don't have to append `WHERE` or `AND` conditionally.
```rust
fn query(a: Option