#sql-query #query-builder #query #sql-database #sql

eloquent

Eloquent is a SQL query builder to easily build complex queries in Rust

8 releases (1 stable)

1.0.0 Feb 14, 2024
0.2.1 Apr 20, 2023
0.1.4 Mar 24, 2023
0.1.3 Jan 25, 2023

#1748 in Database interfaces

Download history 147/week @ 2024-02-14 22/week @ 2024-02-21 13/week @ 2024-02-28 2/week @ 2024-03-13 26/week @ 2024-03-27 25/week @ 2024-04-03

53 downloads per month

MIT license

48KB
682 lines

Eloquent

[!WARNING]

This package is developed for learning purposes and is not intended for production use.

Eloquent is a SQL query builder to easily build complex SQL queries in Rust. It is inspired by Laravel's Query Builder and is designed to be simple and easy to use. This is not an ORM, in contrast to Laravel's Eloquent ORM. This libary is designed to be used with any SQL database and does not have any database specific features.

The query builder supports select, insert, update, delete, where, join, group_by, having, order_by, limit, offset and to_sql methods and support where clause closures.

See Available Methods for more details.

Usage

[dependencies]
eloquent = "1.0"
use eloquent_core::{Eloquent, Operator, Variable};

fn example_query() {
    let query = Eloquent::table("orders")
        .select("orders.customer_id")
        .select_as("customers.name", "customer_name")
        .select_count("orders.id", "total_orders")
        .join("customers", "orders.customer_id", "customers.id")
        .r#where(
            "orders.order_date",
            Operator::GreaterThanOrEqual,
            Variable::String("2024-01-01".to_string()),
        )
        .r#where(
            "customers.country_id",
            Operator::In,
            Variable::Array(vec![
                ArrayVariable::String("NL".to_string()),
                ArrayVariable::String("DE".to_string()),
            ]),
        )
        .where_not_null("shipped_at")
        .group_by(vec!["orders.customer_id", "customers.name"])
        .having("total_orders", Operator::GreaterThan, Variable::Int(5))
        .order_by("total_orders", Direction::Desc)
        .order_by("customer_name", Direction::Asc)
        .limit(10)
        .offset(0)
        .to_sql();

    assert_eq!(
        query,
        "SELECT orders.customer_id, customers.name AS customer_name, COUNT(orders.id) AS total_orders FROM orders JOIN customers ON orders.customer_id = customers.id WHERE orders.order_date >= `2024-01-01` AND customers.country_id IN (`NL`, `DE`) AND shipped_at IS NOT NULL GROUP BY orders.customer_id, customers.name HAVING total_orders > 5 ORDER BY total_orders DESC, customer_name ASC LIMIT 10 OFFSET 0"
    );
}
use eloquent_core::{Eloquent, Operator, Variable};

fn example_query() {
    let query = Eloquent::table("users")
        .where_closure(|closure| {
            closure
                .r#where("age", Operator::GreaterThanOrEqual, Variable::Int(18))
                .r#where("age", Operator::LessThan, Variable::Int(25));
        })
        .or_where_closure(|closure| {
            closure
                .r#where("age", Operator::GreaterThanOrEqual, Variable::Int(30))
                .r#where("age", Operator::LessThan, Variable::Int(35));
        })
        .to_sql();
    assert_eq!(
        query,
        "SELECT * FROM users WHERE (age >= 18 AND age < 25) OR (age >= 30 AND age < 35)"
    );
}

Dependencies