Skip to content

Commit

Permalink
Refactor bindings logic (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
tjardoo authored Feb 13, 2024
1 parent fc97945 commit e38c49b
Show file tree
Hide file tree
Showing 26 changed files with 1,766 additions and 1,350 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/eloquent-core-test.yml
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: test
name: Eloquent Test

on:
push:
Expand All @@ -10,7 +10,7 @@ env:
CARGO_TERM_COLOR: always

jobs:
build:
build-and-test:

runs-on: ubuntu-latest

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
/Cargo.lock
.vscode
8 changes: 3 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "eloquent"
version = "0.2.1"
version = "1.0.0"
edition = "2021"
license = "MIT"
description = "A Rust library for building queries in an eloquent way."
Expand All @@ -9,9 +9,7 @@ documentation = "https://docs.rs/eloquent"
keywords = ["database", "query"]

[workspace]
members = [
"eloquent_core",
]
members = ["eloquent_core"]

[dependencies]
eloquent_core = { path = "eloquent_core", version = "0.2" }
eloquent_core = { path = "eloquent_core", version = "1.0" }
121 changes: 38 additions & 83 deletions README.md
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`))"
);
}
```
82 changes: 82 additions & 0 deletions docs/available-methods.md
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()``
4 changes: 2 additions & 2 deletions eloquent_core/Cargo.toml
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]
Loading

0 comments on commit e38c49b

Please sign in to comment.