From 3b7dddc028d356ae0bf1355ff55406b9b8b7772d Mon Sep 17 00:00:00 2001 From: Tjardo Date: Thu, 8 Feb 2024 16:01:07 +0100 Subject: [PATCH] WIP --- eloquent_core/src/lib.rs | 22 ++++++++++++++++++++ src/lib.rs | 44 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/eloquent_core/src/lib.rs b/eloquent_core/src/lib.rs index fb97a07..7a1452a 100644 --- a/eloquent_core/src/lib.rs +++ b/eloquent_core/src/lib.rs @@ -46,6 +46,8 @@ pub enum Operator { GreaterThanOrEqual, Like, NotLike, + In, + NotIn, } #[derive(Debug, Clone, PartialEq)] @@ -54,6 +56,13 @@ pub enum Variable { Int(u32), Bool(bool), Null, + Array(Vec), +} + +#[derive(Debug, Clone, PartialEq)] +pub enum ArrayVariable { + String(String), + Int(u32), } pub enum Direction { @@ -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"), } } } @@ -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::>() + .join(", ") + ), } } } diff --git a/src/lib.rs b/src/lib.rs index 98720d5..92b0ab7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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() { @@ -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`)" + ); + } }