From caa3c3f20b3461991edf1b286b6aa86f35d90d2e Mon Sep 17 00:00:00 2001 From: gammelalf Date: Tue, 5 Sep 2023 19:53:30 +0200 Subject: [PATCH] Implemented FieldEq and FieldOrd for chrono and options --- src/fields/traits/cmp.rs | 76 ++++++++++++++++++++++++-------------- src/fields/types/chrono.rs | 25 ++++++++++++- src/fields/types/uuid.rs | 6 ++- 3 files changed, 78 insertions(+), 29 deletions(-) diff --git a/src/fields/traits/cmp.rs b/src/fields/traits/cmp.rs index 066159e..c0b3a40 100644 --- a/src/fields/traits/cmp.rs +++ b/src/fields/traits/cmp.rs @@ -13,6 +13,8 @@ use std::borrow::Cow; +use rorm_db::sql::value::NullType; + use super::FieldType; use crate::conditions::{Binary, BinaryOperator, Column, Condition, Value}; use crate::internal::field::access::FieldAccess; @@ -142,16 +144,36 @@ impl_FieldEq!(impl<'rhs> FieldEq<'rhs, i32> for i32 { Value::I32 }); impl_FieldEq!(impl<'rhs> FieldEq<'rhs, i64> for i64 { Value::I64 }); impl_FieldEq!(impl<'rhs> FieldEq<'rhs, f32> for f32 { Value::F32 }); impl_FieldEq!(impl<'rhs> FieldEq<'rhs, f64> for f64 { Value::F64 }); +impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Option> for Option { |option: Self| option.map(Value::Bool).unwrap_or(Value::Null(NullType::Bool)) }); +impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Option> for Option { |option: Self| option.map(Value::I16).unwrap_or(Value::Null(NullType::I16)) }); +impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Option> for Option { |option: Self| option.map(Value::I32).unwrap_or(Value::Null(NullType::I32)) }); +impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Option> for Option { |option: Self| option.map(Value::I64).unwrap_or(Value::Null(NullType::I64)) }); +impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Option> for Option { |option: Self| option.map(Value::F32).unwrap_or(Value::Null(NullType::F32)) }); +impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Option> for Option { |option: Self| option.map(Value::F64).unwrap_or(Value::Null(NullType::F64)) }); + +impl_FieldEq!(impl<'rhs> FieldEq<'rhs, &'rhs str> for String { conv_string }); +impl_FieldEq!(impl<'rhs> FieldEq<'rhs, &'rhs String> for String { conv_string }); +impl_FieldEq!(impl<'rhs> FieldEq<'rhs, String> for String { conv_string }); +impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Cow<'rhs, str>> for String { conv_string }); +impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Option<&'rhs str>> for Option { |option: Option<_>| option.map(conv_string).unwrap_or(Value::Null(NullType::String)) }); +impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Option<&'rhs String>> for Option { |option: Option<_>| option.map(conv_string).unwrap_or(Value::Null(NullType::String)) }); +impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Option> for Option { |option: Option<_>| option.map(conv_string).unwrap_or(Value::Null(NullType::String)) }); +impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Option>> for Option { |option: Option<_>| option.map(conv_string).unwrap_or(Value::Null(NullType::String)) }); +fn conv_string<'a>(value: impl Into>) -> Value<'a> { + Value::String(value.into()) +} -impl_FieldEq!(impl<'rhs> FieldEq<'rhs, &'rhs str> for String { |s| Value::String(Cow::Borrowed(s)) }); -impl_FieldEq!(impl<'rhs> FieldEq<'rhs, &'rhs String> for String { |s| Value::String(Cow::Borrowed(s)) }); -impl_FieldEq!(impl<'rhs> FieldEq<'rhs, String> for String { |s| Value::String(Cow::Owned(s)) }); -impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Cow<'rhs, str>> for String { Value::String }); - -impl_FieldEq!(impl<'rhs> FieldEq<'rhs, &'rhs [u8]> for Vec { |b| Value::Binary(Cow::Borrowed(b)) }); -impl_FieldEq!(impl<'rhs> FieldEq<'rhs, &'rhs Vec> for Vec { |b| Value::Binary(Cow::Borrowed(b)) }); -impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Vec> for Vec { |b| Value::Binary(Cow::Owned(b)) }); -impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Cow<'rhs, [u8]>> for Vec { Value::Binary }); +impl_FieldEq!(impl<'rhs> FieldEq<'rhs, &'rhs [u8]> for Vec { conv_bytes }); +impl_FieldEq!(impl<'rhs> FieldEq<'rhs, &'rhs Vec> for Vec { conv_bytes }); +impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Vec> for Vec { conv_bytes }); +impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Cow<'rhs, [u8]>> for Vec { conv_bytes }); +impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Option<&'rhs [u8]>> for Option> { |option: Option<_>| option.map(conv_bytes).unwrap_or(Value::Null(NullType::Binary)) }); +impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Option<&'rhs Vec>> for Option> { |option: Option<_>| option.map(conv_bytes).unwrap_or(Value::Null(NullType::Binary)) }); +impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Option>> for Option> { |option: Option<_>| option.map(conv_bytes).unwrap_or(Value::Null(NullType::Binary)) }); +impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Option>> for Option> { |option: Option<_>| option.map(conv_bytes).unwrap_or(Value::Null(NullType::Binary)) }); +fn conv_bytes<'a>(value: impl Into>) -> Value<'a> { + Value::Binary(value.into()) +} // Impl FieldEq iff FieldEq impl<'rhs, F, P, T> FieldEq<'rhs, FieldProxy> for T @@ -193,41 +215,41 @@ where macro_rules! impl_FieldOrd { ($lhs:ty, $rhs:ty, $into_value:expr) => { impl<'rhs> $crate::fields::traits::cmp::FieldOrd<'rhs, $rhs> for $lhs { - type LtCond = Binary, Value<'rhs>>; + type LtCond = $crate::conditions::Binary<$crate::conditions::Column, $crate::conditions::Value<'rhs>>; fn field_less_than(access: A, value: $rhs) -> Self::LtCond { - Binary { - operator: BinaryOperator::Less, - fst_arg: Column(access), + $crate::conditions::Binary { + operator: $crate::conditions::BinaryOperator::Less, + fst_arg: $crate::conditions::Column(access), #[allow(clippy::redundant_closure_call)] // clean way to pass code to a macro snd_arg: $into_value(value), } } - type LeCond = Binary, Value<'rhs>>; + type LeCond = $crate::conditions::Binary<$crate::conditions::Column, $crate::conditions::Value<'rhs>>; fn field_less_equals(access: A, value: $rhs) -> Self::LeCond { - Binary { - operator: BinaryOperator::LessOrEquals, - fst_arg: Column(access), + $crate::conditions::Binary { + operator: $crate::conditions::BinaryOperator::LessOrEquals, + fst_arg: $crate::conditions::Column(access), #[allow(clippy::redundant_closure_call)] // clean way to pass code to a macro snd_arg: $into_value(value), } } - type GtCond = Binary, Value<'rhs>>; - fn field_greater_than(access: A, value: $rhs) -> Self::GtCond { - Binary { - operator: BinaryOperator::Greater, - fst_arg: Column(access), + type GtCond = $crate::conditions::Binary<$crate::conditions::Column, $crate::conditions::Value<'rhs>>; + fn field_greater_than(access: A, value: $rhs) -> Self::GtCond { + $crate::conditions::Binary { + operator: $crate::conditions::BinaryOperator::Greater, + fst_arg: $crate::conditions::Column(access), #[allow(clippy::redundant_closure_call)] // clean way to pass code to a macro snd_arg: $into_value(value), } } - type GeCond = Binary, Value<'rhs>>; - fn field_greater_equals(access: A, value: $rhs) -> Self::GeCond { - Binary { - operator: BinaryOperator::GreaterOrEquals, - fst_arg: Column(access), + type GeCond = $crate::conditions::Binary<$crate::conditions::Column, $crate::conditions::Value<'rhs>>; + fn field_greater_equals(access: A, value: $rhs) -> Self::GeCond { + $crate::conditions::Binary { + operator: $crate::conditions::BinaryOperator::GreaterOrEquals, + fst_arg: $crate::conditions::Column(access), #[allow(clippy::redundant_closure_call)] // clean way to pass code to a macro snd_arg: $into_value(value), } diff --git a/src/fields/types/chrono.rs b/src/fields/types/chrono.rs index b965137..704f034 100644 --- a/src/fields/types/chrono.rs +++ b/src/fields/types/chrono.rs @@ -1,6 +1,7 @@ use std::marker::PhantomData; use chrono::{DateTime, FixedOffset, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Utc}; +use rorm_db::sql::value::NullType; use rorm_db::Error::DecodeError; use rorm_db::{Error, Row}; use rorm_declaration::imr; @@ -16,12 +17,34 @@ use crate::internal::hmr::{db_type, Source}; use crate::internal::query_context::QueryContext; use crate::internal::relation_path::Path; use crate::model::ConstNew; -use crate::{const_concat, impl_AsDbType, new_converting_decoder, sealed}; +use crate::{ + const_concat, impl_AsDbType, impl_FieldEq, impl_FieldOrd, new_converting_decoder, sealed, +}; impl_AsDbType!(NaiveTime, db_type::Time, Value::ChronoNaiveTime); impl_AsDbType!(NaiveDate, db_type::Date, Value::ChronoNaiveDate); impl_AsDbType!(NaiveDateTime, db_type::DateTime, Value::ChronoNaiveDateTime); impl_AsDbType!(DateTime, db_type::DateTime, Value::ChronoDateTime); +impl_FieldEq!(impl<'rhs> FieldEq<'rhs, NaiveTime> for NaiveTime { Value::ChronoNaiveTime }); +impl_FieldEq!(impl<'rhs> FieldEq<'rhs, NaiveDate> for NaiveDate { Value::ChronoNaiveDate }); +impl_FieldEq!(impl<'rhs> FieldEq<'rhs, NaiveDateTime> for NaiveDateTime { Value::ChronoNaiveDateTime }); +impl_FieldEq!(impl<'rhs> FieldEq<'rhs, DateTime> for DateTime { Value::ChronoDateTime }); +impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Option> for Option { |option: Self| option.map(Value::ChronoNaiveTime).unwrap_or(Value::Null(NullType::ChronoNaiveTime)) }); +impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Option> for Option { |option: Self| option.map(Value::ChronoNaiveDate).unwrap_or(Value::Null(NullType::ChronoNaiveDate)) }); +impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Option> for Option { |option: Self| option.map(Value::ChronoNaiveDateTime).unwrap_or(Value::Null(NullType::ChronoNaiveDateTime)) }); +impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Option>> for Option> { |option: Self| option.map(Value::ChronoDateTime).unwrap_or(Value::Null(NullType::ChronoDateTime)) }); +impl_FieldOrd!(NaiveTime, NaiveTime, Value::ChronoNaiveTime); +impl_FieldOrd!(NaiveDate, NaiveDate, Value::ChronoNaiveDate); +impl_FieldOrd!(NaiveDateTime, NaiveDateTime, Value::ChronoNaiveDateTime); +impl_FieldOrd!(DateTime, DateTime, Value::ChronoDateTime); +#[rustfmt::skip] +impl_FieldOrd!(Option, Option, |option: Self| option.map(Value::ChronoNaiveTime).unwrap_or(Value::Null(NullType::ChronoNaiveTime))); +#[rustfmt::skip] +impl_FieldOrd!(Option, Option, |option: Self| option.map(Value::ChronoNaiveDate).unwrap_or(Value::Null(NullType::ChronoNaiveDate))); +#[rustfmt::skip] +impl_FieldOrd!(Option, Option, |option: Self| option.map(Value::ChronoNaiveDateTime).unwrap_or(Value::Null(NullType::ChronoNaiveDateTime))); +#[rustfmt::skip] +impl_FieldOrd!(Option>, Option>, |option: Self| option.map(Value::ChronoDateTime).unwrap_or(Value::Null(NullType::ChronoDateTime))); /// Implementation detail of [`DateTime`] const _: () = { diff --git a/src/fields/types/uuid.rs b/src/fields/types/uuid.rs index 4e17bfd..6c67868 100644 --- a/src/fields/types/uuid.rs +++ b/src/fields/types/uuid.rs @@ -1,6 +1,10 @@ +use rorm_db::sql::value::NullType; +use uuid::Uuid; + use crate::conditions::Value; use crate::internal::hmr::db_type; use crate::{impl_AsDbType, impl_FieldEq}; impl_AsDbType!(uuid::Uuid, db_type::Binary, Value::Uuid); -impl_FieldEq!(impl<'rhs> FieldEq<'rhs, uuid::Uuid> for uuid::Uuid { Value::Uuid }); +impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Uuid> for Uuid { Value::Uuid }); +impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Option> for Option { |option: Option<_>| option.map(Value::Uuid).unwrap_or(Value::Null(NullType::Uuid)) });