Skip to content

tjardoo/eloquent-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

56 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

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

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Languages