use crate::error::{Error, Result};
use crate::method::backed_enum;
use crate::value::{IntoValue, IntoValues, Value};
backed_enum! {
pub enum Kind: "Utopia\\Query\\Builder\\Case\\Kind" {
Comparison = "comparison",
Null = "null",
NotNull = "notNull",
In = "in",
Raw = "raw",
}
}
backed_enum! {
pub enum Operator: "Utopia\\Query\\Builder\\Case\\Operator" {
Equal = "equal",
NotEqual = "notEqual",
LessThan = "lessThan",
LessThanEqual = "lessThanEqual",
GreaterThan = "greaterThan",
GreaterThanEqual = "greaterThanEqual",
}
}
impl Operator {
pub fn sql_operator(self) -> &'static str {
match self {
Operator::Equal => "=",
Operator::NotEqual => "!=",
Operator::LessThan => "<",
Operator::LessThanEqual => "<=",
Operator::GreaterThan => ">",
Operator::GreaterThanEqual => ">=",
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct WhenClause {
pub kind: Kind,
pub column: Option<String>,
pub operator: Option<Operator>,
pub value: Value,
pub then: Value,
pub values: Vec<Value>,
pub raw_condition: Option<String>,
pub raw_bindings: Vec<Value>,
}
impl WhenClause {
#[allow(clippy::too_many_arguments)]
pub fn new(
kind: Kind,
column: Option<String>,
operator: Option<Operator>,
value: Value,
then: Value,
values: Vec<Value>,
raw_condition: Option<String>,
raw_bindings: Vec<Value>,
) -> Self {
WhenClause { kind, column, operator, value, then, values, raw_condition, raw_bindings }
}
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct CaseExpression {
whens: Vec<WhenClause>,
has_else: bool,
else_value: Value,
alias: String,
}
impl CaseExpression {
pub fn new() -> Self {
CaseExpression::default()
}
pub fn when(
&mut self,
column: impl Into<String>,
operator: Operator,
value: impl IntoValue,
then: impl IntoValue,
) -> &mut Self {
self.whens.push(WhenClause::new(
Kind::Comparison,
Some(column.into()),
Some(operator),
value.into_value(),
then.into_value(),
Vec::new(),
None,
Vec::new(),
));
self
}
pub fn when_null(&mut self, column: impl Into<String>, then: impl IntoValue) -> &mut Self {
self.whens.push(WhenClause::new(
Kind::Null,
Some(column.into()),
None,
Value::Null,
then.into_value(),
Vec::new(),
None,
Vec::new(),
));
self
}
pub fn when_not_null(&mut self, column: impl Into<String>, then: impl IntoValue) -> &mut Self {
self.whens.push(WhenClause::new(
Kind::NotNull,
Some(column.into()),
None,
Value::Null,
then.into_value(),
Vec::new(),
None,
Vec::new(),
));
self
}
pub fn when_in(
&mut self,
column: impl Into<String>,
values: impl IntoValues,
then: impl IntoValue,
) -> Result<&mut Self> {
let values: Vec<Value> = values.into_values().values().cloned().collect();
if values.is_empty() {
return Err(Error::Validation("whenIn() requires at least one value.".into()));
}
self.whens.push(WhenClause::new(
Kind::In,
Some(column.into()),
None,
Value::Null,
then.into_value(),
values,
None,
Vec::new(),
));
Ok(self)
}
pub fn when_raw(
&mut self,
condition: impl Into<String>,
then: impl IntoValue,
bindings: impl IntoValues,
) -> &mut Self {
self.whens.push(WhenClause::new(
Kind::Raw,
None,
None,
Value::Null,
then.into_value(),
Vec::new(),
Some(condition.into()),
bindings.into_values().values().cloned().collect(),
));
self
}
pub fn else_(&mut self, value: impl IntoValue) -> &mut Self {
self.has_else = true;
self.else_value = value.into_value();
self
}
pub fn alias(&mut self, alias: impl Into<String>) -> &mut Self {
self.alias = alias.into();
self
}
pub fn whens(&self) -> &[WhenClause] {
&self.whens
}
pub fn has_else(&self) -> bool {
self.has_else
}
pub fn else_value(&self) -> &Value {
&self.else_value
}
pub fn get_alias(&self) -> &str {
&self.alias
}
}