use crate::types::{Boolean, Float, Integer, SqlType, Text};
use crate::value::SqlValue;
use core::marker::PhantomData;
pub trait Expr {
type SqlType;
fn write_to(&self, sql: &mut String, params: &mut Vec<SqlValue>);
}
impl Expr for i32 {
type SqlType = Integer;
fn write_to(&self, sql: &mut String, params: &mut Vec<SqlValue>) {
sql.push('?');
params.push(SqlValue::Integer(i64::from(*self)));
}
}
impl Expr for i64 {
type SqlType = Integer;
fn write_to(&self, sql: &mut String, params: &mut Vec<SqlValue>) {
sql.push('?');
params.push(SqlValue::Integer(*self));
}
}
impl Expr for bool {
type SqlType = Boolean;
fn write_to(&self, sql: &mut String, params: &mut Vec<SqlValue>) {
sql.push('?');
params.push(SqlValue::Boolean(*self));
}
}
impl Expr for f32 {
type SqlType = Float;
fn write_to(&self, sql: &mut String, params: &mut Vec<SqlValue>) {
sql.push('?');
params.push(SqlValue::Float(f64::from(*self)));
}
}
impl Expr for f64 {
type SqlType = Float;
fn write_to(&self, sql: &mut String, params: &mut Vec<SqlValue>) {
sql.push('?');
params.push(SqlValue::Float(*self));
}
}
impl Expr for String {
type SqlType = Text;
fn write_to(&self, sql: &mut String, params: &mut Vec<SqlValue>) {
sql.push('?');
params.push(SqlValue::Text(self.clone()));
}
}
impl Expr for &str {
type SqlType = Text;
fn write_to(&self, sql: &mut String, params: &mut Vec<SqlValue>) {
sql.push('?');
params.push(SqlValue::Text((*self).to_string()));
}
}
#[derive(Debug, Clone)]
pub struct Lit<T> {
value: SqlValue,
_ty: PhantomData<T>,
}
impl<T: SqlType> Lit<T> {
pub fn from_raw(value: SqlValue) -> Self {
Self {
value,
_ty: PhantomData,
}
}
}
impl Lit<Integer> {
pub fn int(value: i64) -> Self {
Self::from_raw(SqlValue::Integer(value))
}
}
impl Lit<Text> {
pub fn text(value: impl Into<String>) -> Self {
Self::from_raw(SqlValue::Text(value.into()))
}
}
impl Lit<Boolean> {
pub fn boolean(value: bool) -> Self {
Self::from_raw(SqlValue::Boolean(value))
}
}
impl Lit<Float> {
pub fn float(value: f64) -> Self {
Self::from_raw(SqlValue::Float(value))
}
}
impl<T: SqlType> Expr for Lit<T> {
type SqlType = T;
fn write_to(&self, sql: &mut String, params: &mut Vec<SqlValue>) {
sql.push('?');
params.push(self.value.clone());
}
}
macro_rules! define_binary_compare {
($(#[$attr:meta])* $name:ident, $sep:literal) => {
$(#[$attr])*
#[derive(Debug, Clone, Copy)]
pub struct $name<L, R> {
pub(crate) left: L,
pub(crate) right: R,
}
impl<L, R> Expr for $name<L, R>
where
L: Expr,
R: Expr<SqlType = L::SqlType>,
{
type SqlType = Boolean;
fn write_to(&self, sql: &mut String, params: &mut Vec<SqlValue>) {
sql.push('(');
self.left.write_to(sql, params);
sql.push_str($sep);
self.right.write_to(sql, params);
sql.push(')');
}
}
};
}
define_binary_compare!(
Eq, " = "
);
define_binary_compare!(
NotEq, " != "
);
define_binary_compare!(
Lt, " < "
);
define_binary_compare!(
LtEq, " <= "
);
define_binary_compare!(
Gt, " > "
);
define_binary_compare!(
GtEq, " >= "
);
#[derive(Debug, Clone, Copy)]
pub struct And<L, R> {
pub(crate) left: L,
pub(crate) right: R,
}
impl<L, R> Expr for And<L, R>
where
L: Expr<SqlType = Boolean>,
R: Expr<SqlType = Boolean>,
{
type SqlType = Boolean;
fn write_to(&self, sql: &mut String, params: &mut Vec<SqlValue>) {
sql.push('(');
self.left.write_to(sql, params);
sql.push_str(" AND ");
self.right.write_to(sql, params);
sql.push(')');
}
}
#[derive(Debug, Clone, Copy)]
pub struct Or<L, R> {
pub(crate) left: L,
pub(crate) right: R,
}
impl<L, R> Expr for Or<L, R>
where
L: Expr<SqlType = Boolean>,
R: Expr<SqlType = Boolean>,
{
type SqlType = Boolean;
fn write_to(&self, sql: &mut String, params: &mut Vec<SqlValue>) {
sql.push('(');
self.left.write_to(sql, params);
sql.push_str(" OR ");
self.right.write_to(sql, params);
sql.push(')');
}
}
#[derive(Debug, Clone, Copy)]
pub struct Not<E> {
pub(crate) inner: E,
}
impl<E: Expr<SqlType = Boolean>> Expr for Not<E> {
type SqlType = Boolean;
fn write_to(&self, sql: &mut String, params: &mut Vec<SqlValue>) {
sql.push_str("NOT (");
self.inner.write_to(sql, params);
sql.push(')');
}
}
pub fn not<E: Expr<SqlType = Boolean>>(expr: E) -> Not<E> {
Not { inner: expr }
}
#[derive(Debug, Clone, Copy)]
pub struct Like<L, R> {
pub(crate) left: L,
pub(crate) right: R,
}
impl<L, R> Expr for Like<L, R>
where
L: Expr<SqlType = Text>,
R: Expr<SqlType = Text>,
{
type SqlType = Boolean;
fn write_to(&self, sql: &mut String, params: &mut Vec<SqlValue>) {
sql.push('(');
self.left.write_to(sql, params);
sql.push_str(" LIKE ");
self.right.write_to(sql, params);
sql.push(')');
}
}
#[derive(Debug, Clone, Copy)]
pub struct IsNull<E> {
pub(crate) inner: E,
}
impl<E: Expr> Expr for IsNull<E> {
type SqlType = Boolean;
fn write_to(&self, sql: &mut String, params: &mut Vec<SqlValue>) {
sql.push('(');
self.inner.write_to(sql, params);
sql.push_str(" IS NULL)");
}
}