Eloquent
Eloquent is a fluent, type-safe query builder for Rust, designed to make SQL query construction intuitive. It provides an expressive API, allowing developers to craft complex SQL queries through method chaining.
Features
- Fluent API for building SQL queries.
- Type-safe query construction with method chaining.
- Support for:
SELECT,JOIN,WHERE,GROUP BY,HAVING, etc.- Conditional queries with
AND,OR,NOT,LIKE,IN,NOT IN,IS NULL, etc. - Aggregation functions:
AVG,SUM,MIN,MAX, andCOUNT. - Date functions:
DATE,TIME,YEAR,MONTH,DAY, etc. - Function aliases and raw expressions.
- CRUD operations:
INSERT,UPDATE, andDELETE. - Subqueries and nested conditions using closures.
- Cursor-based pagination support via
paginate(). - SQL query generation as raw
sql()or formatted outputpretty_sql(). - Query validation and error handling (can be skipped with
skip_validation()). - Use question or dollar placeholders for prepared statements.
Use your IDE to explore the available methods, or refer to the docs.rs/eloquent - QueryBuilder.
Installation
To use Eloquent, add the following to your Cargo.toml:
[]
= "2.0"
Usage
Simple query example
This example demonstrates a basic SQL query using Eloquent's fluent API.
use Eloquent;
let query = query
.table
.select
.where_not_null
.where_like
.limit;
println!;
SELECT
name,
email
FROM
users
WHERE
verified_at IS NOT NULL
AND email LIKE '%@gmail.com'
LIMIT
100
Complex query example
This example demonstrates a more complex SQL query using Eloquent's fluent API.
use Eloquent;
let query = query
.table
.select
.select_avg
.select_as
.join
.r#where
.where_not_in
.where_not_null
.where_closure
.group_by
.having_gt
.order_by_asc
.limit;
println!;
SELECT
origin_airport,
AVG(startup_time_in_minutes) AS startup_time_in_minutes_avg,
airports.city AS destination_city
FROM
flights
JOIN airports ON flights.destination_airport = airports.iata_code
WHERE
origin_airport = 'AMS'
AND flight_number NOT IN ('KL123', 'KL456')
AND gate_number IS NOT NULL
AND (
flight_duration >= 120
OR airports.city LIKE '%NY%'
)
GROUP BY
origin_airport,
airports.city
HAVING
startup_time_in_minutes_avg > 120
ORDER BY
startup_time_in_minutes_avg ASC
LIMIT
20
Subquery example
This example demonstrates a subquery using Eloquent's fluent API.
use Eloquent;
let subquery = subquery
.table
.select
.select_avg
.group_by
.order_by_desc
.limit;
let query = query
.table
.select
.r#where
.pretty_sql?;
SELECT
event_name,
event_date
FROM
EVENTS
WHERE
event_id = (
SELECT
event_id,
AVG(price) AS price_avg
FROM
tickets
GROUP BY
event_id
ORDER BY
price_avg DESC
LIMIT
1
)
Pagination example
This example demonstrates cursor-based pagination using Eloquent's paginate() method.
use Eloquent;
let last_id = None; // initial query
let query = query
.table
.select
.
.sql?;
SELECT flight_number FROM departures LIMIT 25
use Eloquent;
let last_id = Some; // last id from previous query
let query = query
.table
.select
.paginate
.sql?;
SELECT flight_number FROM departures WHERE id > 40 LIMIT 25
Prepared statements example
This example demonstrates using placeholders for prepared statements.
You can use the feature bind-placeholder-questionmark to control the use of $ vs ? as placeholders.
use ;
let result = new
.table
.insert
.sql?;
INSERT INTO flights (name) VALUES ($4)