-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
1,766 additions
and
1,350 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
name: Eloquent Core Test | ||
|
||
on: | ||
push: | ||
branches: [ "master" ] | ||
pull_request: | ||
branches: [ "master" ] | ||
|
||
env: | ||
CARGO_TERM_COLOR: always | ||
|
||
jobs: | ||
build-and-test: | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Build | ||
run: cd eloquent_core && cargo build --verbose | ||
- name: Run tests | ||
run: cd eloquent_core && cargo test --verbose |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/target | ||
/Cargo.lock | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,51 @@ | ||
# Eloquent | ||
|
||
[![tests](https://github.com/tjardoo/eloquent-rs/workflows/test/badge.svg?event=push)](https://github.com/tjardoo/eloquent-rs/actions) | ||
[![crate.io](https://img.shields.io/crates/v/eloquent.svg)](https://crates.io/crates/eloquent) | ||
[![docs](https://docs.rs/eloquent/badge.svg)](https://docs.rs/eloquent) | ||
> [!WARNING] | ||
> | ||
> This package is developed for learning purposes and is not intended for production use. | ||
A Rust library for building queries in an eloquent way. | ||
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. | ||
|
||
- [Usage](#usage) | ||
- Examples | ||
- [Select query](#select-query) | ||
- [Insert query](#insert-query) | ||
- [Update query](#update-query) | ||
- [Delete query](#delete-query) | ||
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](./docs/available-methods.md) for more details. | ||
|
||
## Usage | ||
|
||
```ini | ||
[dependencies] | ||
eloquent = "0.2" | ||
``` | ||
|
||
### Select Query | ||
|
||
```rust | ||
use eloquent_core::{Direction, GenericVar}; | ||
|
||
let query = Eloquent::query() | ||
.table("flights") | ||
.select("id") | ||
.select("flight_number") | ||
.r#where("destination", GenericVar::Str("SIN")) | ||
.to_sql() | ||
.unwrap(); | ||
|
||
assert_eq!(query, "SELECT `id`, `flight_number` FROM flights WHERE `destination` = \"SIN\";"); | ||
``` | ||
|
||
### Insert Query | ||
|
||
```rust | ||
use eloquent_core::{Direction, GenericVar, Clause}; | ||
|
||
let query = Eloquent::query() | ||
.insert("flights", vec![ | ||
Clause { | ||
column: "id".to_string(), | ||
value: GenericVar::Int(1), | ||
}, | ||
Clause { | ||
column: "flight_code".to_string(), | ||
value: GenericVar::Str("KL0803"), | ||
}, | ||
]) | ||
.to_sql() | ||
.unwrap(); | ||
|
||
assert_eq!(query, "INSERT INTO flights (`id`, `flight_code`) VALUES (1, \"KL0803\");"); | ||
eloquent = "1.0" | ||
``` | ||
|
||
### Update Query | ||
|
||
```rust | ||
use eloquent_core::{Direction, GenericVar, Clause}; | ||
|
||
let query = Eloquent::query() | ||
.update("flights", vec![ | ||
Clause { | ||
column: "flight_code".to_string(), | ||
value: GenericVar::Str("KL0803"), | ||
}, | ||
Clause { | ||
column: "destination".to_string(), | ||
value: GenericVar::Str("Bangkok"), | ||
}, | ||
]) | ||
.r#where("id", GenericVar::Int(1)) | ||
.to_sql() | ||
.unwrap(); | ||
|
||
assert_eq!(query, "INSERT INTO flights (`id`, `flight_code`) VALUES (1, \"KL0803\") WHERE `id` = 1;"); | ||
``` | ||
|
||
### Delete Query | ||
|
||
```rust | ||
use eloquent_core::{Direction, GenericVar}; | ||
|
||
let query = Eloquent::query() | ||
.delete("flights") | ||
.r#where("id", GenericVar::Int(1)) | ||
.to_sql() | ||
.unwrap(); | ||
|
||
assert_eq!(query, "DELETE FROM flights WHERE `id` = 1;"); | ||
use eloquent_core::{Eloquent, Operator, Variable}; | ||
|
||
fn select_test_query_1() { | ||
let query = Eloquent::table("users") | ||
.r#where("created_at", Operator::GreaterThanOrEqual, Variable::String("2024-01-01".to_string())) | ||
.where_null("deleted_at") | ||
.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( | ||
"status", | ||
Operator::In, | ||
Variable::Array(vec![ | ||
ArrayVariable::String("pending".to_string()), | ||
ArrayVariable::String("active".to_string()), | ||
]), | ||
); | ||
}) | ||
.to_sql(); | ||
|
||
assert_eq!( | ||
query, | ||
"SELECT * FROM users WHERE created_at >= `2024-01-01` AND deleted_at IS NULL AND (age >= 18 AND age < 25) OR (age >= 30 AND status IN (`pending`, `active`))" | ||
); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# Available Methods | ||
|
||
- [Table](#table) | ||
- [Select](#select) | ||
- [Insert](#insert) | ||
- [Update](#update) | ||
- [Delete](#delete) | ||
- [Where](#where) | ||
- [Join](#join) | ||
- [Group By](#group-by) | ||
- [Having](#having) | ||
- [Order By](#order-by) | ||
- [Limit & Offset](#limit-offset) | ||
- [To SQL](#to-sql) | ||
|
||
## Table | ||
|
||
- ``table("users")`` | ||
|
||
## Select | ||
|
||
- ``select("id")`` | ||
- ``select(vec!["id", "name"])`` | ||
- ``select_count("id", "total_users")`` | ||
- ``select_max("id", "max_id")`` | ||
- ``select_min("id", "min_id")`` | ||
- ``select_avg("id", "avg_id")`` | ||
- ``select_sum("id", "sum_id")`` | ||
|
||
## Insert | ||
|
||
- ``insert("name", Variable::new("John Doe"))`` | ||
- ``insert_many(vec![("first_name", Variable::new("John")), ("last_name", Variable::new("Doe"))])`` | ||
|
||
## Update | ||
|
||
- ``update("name", Variable::new("John Doe"))`` | ||
- ``update_many(vec![("first_name", Variable::new("John")), ("last_name", Variable::new("Doe"))])`` | ||
|
||
## Delete | ||
|
||
- ``delete()`` | ||
|
||
## Where | ||
|
||
- ``r#where()`` | ||
- ``or_where()`` | ||
- ``where_not()`` | ||
- ``where_null()`` | ||
- ``where_not_null()`` | ||
- ``or_where_null()`` | ||
- ``or_where_not_null()`` | ||
- ``where_closure(|closure|)`` | ||
- ``or_where_closure(|closure|)`` | ||
|
||
## Join | ||
|
||
- ``join("addresses", "users.id", "addresses.user_id")`` | ||
- ``left_join("addresses", "users.id", "addresses.user_id")`` | ||
- ``right_join("addresses", "users.id", "addresses.user_id")`` | ||
- ``full_join("addresses", "users.id", "addresses.user_id")`` | ||
|
||
## Group By | ||
|
||
- ``group_by("country_id")`` | ||
|
||
## Having | ||
|
||
- ``having("created_at", Operator::GreaterThanOrEqual, Variable::String("2024-01-01".to_string()))`` | ||
|
||
## Order By | ||
|
||
- ``order_by("id", Direction::Desc)`` | ||
|
||
## Limit & Offset {#limit-offset} | ||
|
||
- ``limit(100)`` | ||
- ``offset(100)`` | ||
|
||
## To SQL | ||
|
||
``to_sql()`` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
[package] | ||
name = "eloquent_core" | ||
version = "0.2.0" | ||
version = "1.0.0" | ||
edition = "2021" | ||
license = "MIT" | ||
repository = "https://github.com/tjardoo/eloquent-rs" | ||
description = "Core query builder expressions and tools." | ||
description = "Core query builder component for Eloquent." | ||
|
||
[dependencies] |
Oops, something went wrong.