Eloquent
Eloquent is a fluent and type-safe query builder for Rust, designed to simplify SQL query construction. It allows you to build complex SQL queries using method chains that closely mirror SQL syntax, all while ensuring type safety and readability.
The library supports a wide range of SQL operations - WHERE
, JOIN
, IN
, NOT IN
, LIKE
, and more—along with nested conditions via closures and subqueries. With features for filtering, grouping, and sorting, Eloquent provides the flexibility needed for constructing powerful, maintainable queries without sacrificing clarity or control over the SQL being generated.
use Eloquent;
let result = 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!; // or .sql() for unformatted SQL
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