Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
tjardoo committed Feb 8, 2024
1 parent 52538f8 commit 3b7dddc
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
22 changes: 22 additions & 0 deletions eloquent_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ pub enum Operator {
GreaterThanOrEqual,
Like,
NotLike,
In,
NotIn,
}

#[derive(Debug, Clone, PartialEq)]
Expand All @@ -54,6 +56,13 @@ pub enum Variable {
Int(u32),
Bool(bool),
Null,
Array(Vec<ArrayVariable>),
}

#[derive(Debug, Clone, PartialEq)]
pub enum ArrayVariable {
String(String),
Int(u32),
}

pub enum Direction {
Expand Down Expand Up @@ -125,6 +134,8 @@ impl Display for Operator {
Operator::GreaterThanOrEqual => write!(f, ">="),
Operator::Like => write!(f, "LIKE"),
Operator::NotLike => write!(f, "NOT LIKE"),
Operator::In => write!(f, "IN"),
Operator::NotIn => write!(f, "NOT IN"),
}
}
}
Expand All @@ -137,6 +148,17 @@ impl Display for Variable {
Variable::Bool(true) => write!(f, "{}", true),
Variable::Bool(false) => write!(f, "{}", false),
Variable::Null => write!(f, "IS NULL"),
Variable::Array(a) => write!(
f,
"({})",
a.iter()
.map(|v| match v {
ArrayVariable::String(s) => format!("`{}`", s),
ArrayVariable::Int(i) => format!("{}", i),
})
.collect::<Vec<String>>()
.join(", ")
),
}
}
}
Expand Down
44 changes: 43 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
#[cfg(test)]
mod tests {
use eloquent_core::{Clause, Direction, Eloquent, Operator, Variable};
use std::vec;

use eloquent_core::{ArrayVariable, Clause, Direction, Eloquent, Operator, Variable};

#[test]
fn select_test_query_1() {
Expand Down Expand Up @@ -322,4 +324,44 @@ mod tests {

assert_eq!(query, "SELECT * FROM users WHERE email IS NOT NULL");
}

#[test]
fn select_where_in_query() {
let query = Eloquent::query()
.table("users")
.r#where(
"country_id",
Operator::In,
Variable::Array(vec![
ArrayVariable::String("NL".to_string()),
ArrayVariable::String("DE".to_string()),
]),
)
.to_sql();

assert_eq!(
query,
"SELECT * FROM users WHERE country_id IN (`NL`, `DE`)"
);
}

#[test]
fn select_where_not_in_query() {
let query = Eloquent::query()
.table("users")
.r#where(
"continent_id",
Operator::NotIn,
Variable::Array(vec![
ArrayVariable::String("AF".to_string()),
ArrayVariable::String("SA".to_string()),
]),
)
.to_sql();

assert_eq!(
query,
"SELECT * FROM users WHERE continent_id NOT IN (`AF`, `SA`)"
);
}
}

0 comments on commit 3b7dddc

Please sign in to comment.