Skip to content

Commit

Permalink
Removed AsDbType::DbType
Browse files Browse the repository at this point in the history
  • Loading branch information
gammelalf committed Sep 10, 2024
1 parent ea65956 commit a5237e7
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 46 deletions.
2 changes: 0 additions & 2 deletions src/fields/types/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ impl<T: Serialize + DeserializeOwned + 'static> FieldType for Json<T> {
}
impl<T: Serialize + DeserializeOwned + 'static> AsDbType for Json<T> {
type Primitive = Vec<u8>;
type DbType = Binary;
}

new_converting_decoder!(
Expand Down Expand Up @@ -120,7 +119,6 @@ impl<T: Serialize + DeserializeOwned + 'static> FieldType for Option<Json<T>> {
}
impl<T: Serialize + DeserializeOwned + 'static> AsDbType for Option<Json<T>> {
type Primitive = Option<Vec<u8>>;
type DbType = Binary;
}

// From
Expand Down
3 changes: 0 additions & 3 deletions src/fields/types/max_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use crate::impl_FieldEq;
use crate::internal::field::as_db_type::AsDbType;
use crate::internal::field::decoder::FieldDecoder;
use crate::internal::field::{Field, FieldProxy};
use crate::internal::hmr;
use crate::internal::hmr::annotations::{Annotations, MaxLength};
use crate::internal::query_context::QueryContext;
use crate::internal::relation_path::Path;
Expand Down Expand Up @@ -160,15 +159,13 @@ where
Impl: LenImpl + Default + 'static,
{
type Primitive = String;
type DbType = hmr::db_type::VarChar;
}

impl<const MAX_LEN: usize, Impl> AsDbType for Option<MaxStr<MAX_LEN, Impl, String>>
where
Impl: LenImpl + Default + 'static,
{
type Primitive = String;
type DbType = hmr::db_type::VarChar;
}

impl<const MAX_LEN: usize, Impl> FieldType for MaxStr<MAX_LEN, Impl, String>
Expand Down
2 changes: 0 additions & 2 deletions src/fields/types/msgpack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ impl<T: Serialize + DeserializeOwned + 'static> FieldType for MsgPack<T> {
}
impl<T: Serialize + DeserializeOwned + 'static> AsDbType for MsgPack<T> {
type Primitive = Vec<u8>;
type DbType = Binary;
}

new_converting_decoder!(
Expand Down Expand Up @@ -116,7 +115,6 @@ impl<T: Serialize + DeserializeOwned + 'static> FieldType for Option<MsgPack<T>>
}
impl<T: Serialize + DeserializeOwned + 'static> AsDbType for Option<MsgPack<T>> {
type Primitive = Option<Vec<u8>>;
type DbType = Binary;
}

// From
Expand Down
15 changes: 5 additions & 10 deletions src/fields/types/url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use crate::fields::utils::check::string_check;
use crate::fields::utils::get_annotations::{forward_annotations, set_null_annotations};
use crate::fields::utils::get_names::single_column_name;
use crate::internal::field::as_db_type::AsDbType;
use crate::internal::hmr;
use crate::{impl_FieldEq, new_converting_decoder, Error};

impl_FieldEq!(impl<'rhs> FieldEq<'rhs, &'rhs Url> for Url {|url: &'rhs Url| Value::String(Cow::Borrowed(url.as_str()))});
Expand Down Expand Up @@ -39,8 +38,6 @@ impl FieldType for Url {
}
impl AsDbType for Url {
type Primitive = String;

type DbType = hmr::db_type::VarChar;
}
new_converting_decoder!(
pub UrlDecoder,
Expand All @@ -55,15 +52,14 @@ impl FieldType for Option<Url> {
const NULL: FieldColumns<Self, NullType> = [NullType::String];

fn into_values(self) -> FieldColumns<Self, Value<'static>> {
self.map(<Url>::into_values).unwrap_or([Value::Null(
<<Url as AsDbType>::DbType as hmr::db_type::DbType>::NULL_TYPE,
)])
self.map(<Url>::into_values)
.unwrap_or(Self::NULL.map(Value::Null))
}

fn as_values(&self) -> FieldColumns<Self, Value<'_>> {
self.as_ref().map(<Url>::as_values).unwrap_or([Value::Null(
<<Url as AsDbType>::DbType as hmr::db_type::DbType>::NULL_TYPE,
)])
self.as_ref()
.map(<Url>::as_values)
.unwrap_or(Self::NULL.map(Value::Null))
}

type Decoder = OptionUrlDecoder;
Expand All @@ -76,7 +72,6 @@ impl FieldType for Option<Url> {
}
impl AsDbType for Option<Url> {
type Primitive = Option<<Url as AsDbType>::Primitive>;
type DbType = <Url as AsDbType>::DbType;
}
new_converting_decoder!(
pub OptionUrlDecoder,
Expand Down
58 changes: 39 additions & 19 deletions src/internal/field/as_db_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@
use rorm_db::row::DecodeOwned;

use crate::internal::field::FieldType;
use crate::internal::hmr::db_type::DbType;

/// This trait maps rust types to database types
///
/// I.e. it specifies which datatypes are allowed on model's fields.
pub trait AsDbType: FieldType + Sized {
/// A type which can be retrieved from the db and then converted into Self.
type Primitive: DecodeOwned;

/// The database type as defined in the Intermediate Model Representation
type DbType: DbType;
}

/// Provides the "default" implementation of [`AsDbType`] and [`FieldType`] of kind `AsDbType`.
Expand All @@ -29,21 +25,29 @@ pub trait AsDbType: FieldType + Sized {
#[allow(non_snake_case)] // makes it clearer that a trait and which trait is meant
#[macro_export]
macro_rules! impl_AsDbType {
(Option<$type:ty>, $decoder:ty) => {
(Option<$type:ty>, $db_type:ty, $decoder:ty) => {
impl $crate::fields::traits::FieldType for Option<$type> {
type Columns = $crate::fields::traits::Array<1>;

const NULL: $crate::fields::traits::FieldColumns<Self, $crate::db::sql::value::NullType> = <$type as $crate::fields::traits::FieldType>::NULL;
const NULL: $crate::fields::traits::FieldColumns<
Self,
$crate::db::sql::value::NullType,
> = <$type as $crate::fields::traits::FieldType>::NULL;

fn into_values(self) -> $crate::fields::traits::FieldColumns<Self, $crate::conditions::Value<'static>> {
fn into_values(
self,
) -> $crate::fields::traits::FieldColumns<Self, $crate::conditions::Value<'static>>
{
self.map(<$type>::into_values)
.unwrap_or([Value::Null(<<$type as $crate::internal::field::as_db_type::AsDbType>::DbType as $crate::internal::hmr::db_type::DbType>::NULL_TYPE)])
.unwrap_or(Self::NULL.map(Value::Null))
}

fn as_values(&self) -> $crate::fields::traits::FieldColumns<Self, $crate::conditions::Value<'_>> {
fn as_values(
&self,
) -> $crate::fields::traits::FieldColumns<Self, $crate::conditions::Value<'_>> {
self.as_ref()
.map(<$type>::as_values)
.unwrap_or([Value::Null(<<$type as $crate::internal::field::as_db_type::AsDbType>::DbType as $crate::internal::hmr::db_type::DbType>::NULL_TYPE)])
.unwrap_or(Self::NULL.map(Value::Null))
}

type Decoder = $decoder;
Expand All @@ -56,29 +60,43 @@ macro_rules! impl_AsDbType {
}

impl $crate::internal::field::as_db_type::AsDbType for Option<$type> {
type Primitive = Option<<$type as $crate::internal::field::as_db_type::AsDbType>::Primitive>;
type DbType = <$type as $crate::internal::field::as_db_type::AsDbType>::DbType;
type Primitive =
Option<<$type as $crate::internal::field::as_db_type::AsDbType>::Primitive>;
}
};
($type:ty, $db_type:ty, $into_value:expr) => {
impl_AsDbType!($type, $db_type, $into_value, |&value| $into_value(value));
};
($type:ty, $db_type:ty, $into_value:expr, $as_value:expr) => {
impl_AsDbType!($type, $db_type, $into_value, $as_value, $crate::fields::utils::check::shared_linter_check<1>);
impl_AsDbType!(
$type,
$db_type,
$into_value,
$as_value,
$crate::fields::utils::check::shared_linter_check<1>
);
};
($type:ty, $db_type:ty, $into_value:expr, $as_value:expr, $Check:ty) => {
impl $crate::fields::traits::FieldType for $type {
type Columns = $crate::fields::traits::Array<1>;

const NULL: $crate::fields::traits::FieldColumns<Self, $crate::db::sql::value::NullType> = [<$db_type as $crate::internal::hmr::db_type::DbType>::NULL_TYPE];
const NULL: $crate::fields::traits::FieldColumns<
Self,
$crate::db::sql::value::NullType,
> = [<$db_type as $crate::internal::hmr::db_type::DbType>::NULL_TYPE];

#[inline(always)]
fn as_values(&self) -> $crate::fields::traits::FieldColumns<Self, $crate::conditions::Value<'_>> {
fn as_values(
&self,
) -> $crate::fields::traits::FieldColumns<Self, $crate::conditions::Value<'_>> {
#[allow(clippy::redundant_closure_call)] // clean way to pass code to a macro
[$as_value(self)]
}

fn into_values(self) -> $crate::fields::traits::FieldColumns<Self, $crate::conditions::Value<'static>> {
fn into_values(
self,
) -> $crate::fields::traits::FieldColumns<Self, $crate::conditions::Value<'static>>
{
#[allow(clippy::redundant_closure_call)] // clean way to pass code to a macro
[$into_value(self)]
}
Expand All @@ -94,10 +112,12 @@ macro_rules! impl_AsDbType {

impl $crate::internal::field::as_db_type::AsDbType for $type {
type Primitive = Self;

type DbType = $db_type;
}

impl_AsDbType!(Option<$type>, $crate::crud::decoder::DirectDecoder<Self>);
impl_AsDbType!(
Option<$type>,
$db_type,
$crate::crud::decoder::DirectDecoder<Self>
);
};
}
12 changes: 2 additions & 10 deletions src/internal/field/foreign_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use crate::internal::field::decoder::FieldDecoder;
use crate::internal::field::{Field, FieldProxy, FieldType, SingleColumnField};
use crate::internal::hmr;
use crate::internal::hmr::annotations::Annotations;
use crate::internal::hmr::db_type::DbType;
use crate::internal::hmr::Source;
use crate::internal::query_context::QueryContext;
use crate::internal::relation_path::Path;
Expand Down Expand Up @@ -73,17 +72,13 @@ where

fn into_values(self) -> FieldColumns<Self, Value<'static>> {
self.map(ForeignModelByField::into_values)
.unwrap_or([Value::Null(
<<Option<FF::Type> as AsDbType>::DbType>::NULL_TYPE,
)])
.unwrap_or(Self::NULL.map(Value::Null))
}

fn as_values(&self) -> FieldColumns<Self, Value<'_>> {
self.as_ref()
.map(ForeignModelByField::as_values)
.unwrap_or([Value::Null(
<<Option<FF::Type> as AsDbType>::DbType>::NULL_TYPE,
)])
.unwrap_or(Self::NULL.map(Value::Null))
}

type Decoder = OptionForeignModelByFieldDecoder<FF>;
Expand All @@ -99,7 +94,6 @@ where
pub trait ForeignModelTrait {
sealed!(trait);

type DbType: DbType;
type RelatedField: SingleColumnField;
const IS_OPTION: bool;
fn as_key(&self) -> Option<&<Self::RelatedField as Field>::Type>;
Expand All @@ -113,7 +107,6 @@ where
{
sealed!(impl);

type DbType = <FF::Type as AsDbType>::DbType;
type RelatedField = FF;
const IS_OPTION: bool = false;
fn as_key(&self) -> Option<&<Self::RelatedField as Field>::Type> {
Expand All @@ -133,7 +126,6 @@ where
{
sealed!(impl);

type DbType = <FF::Type as AsDbType>::DbType;
type RelatedField = FF;
const IS_OPTION: bool = true;

Expand Down

0 comments on commit a5237e7

Please sign in to comment.