6 releases (1 stable)

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

#2771 in Database interfaces

Download history 146/week @ 2024-02-12 9/week @ 2024-02-19 23/week @ 2024-02-26 5/week @ 2024-03-11 104/week @ 2024-04-01

109 downloads per month
Used in eloquent

MIT license

41KB
683 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)"
    );
}

lib.rs:

Eloquent Core

Eloquent Core is the core library for the Eloquent libary. It provides the core functionality for building SQL queries.

No runtime deps