From ffb020bcdd5004fdcba501950e6a87bc82c806ed Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Mon, 4 Dec 2023 14:27:43 +0100 Subject: [PATCH] Also add diesel derives for CollectionId --- src/fetch/collection_id.rs | 90 ++++++++++++++++++++++++++++++++++++++ src/fetch/object_id.rs | 2 +- 2 files changed, 91 insertions(+), 1 deletion(-) diff --git a/src/fetch/collection_id.rs b/src/fetch/collection_id.rs index ae17ca0..4297ca3 100644 --- a/src/fetch/collection_id.rs +++ b/src/fetch/collection_id.rs @@ -102,3 +102,93 @@ where self.0.eq(&other.0) && self.1 == other.1 } } + +#[cfg(feature = "diesel")] +const _IMPL_DIESEL_NEW_TYPE_FOR_COLLECTION_ID: () = { + use diesel::{ + backend::Backend, + deserialize::{FromSql, FromStaticSqlRow}, + expression::AsExpression, + internal::derives::as_expression::Bound, + pg::Pg, + query_builder::QueryId, + serialize, + serialize::{Output, ToSql}, + sql_types::{HasSqlType, SingleValue, Text}, + Expression, + Queryable, + }; + + // TODO: this impl only works for Postgres db because of to_string() call which requires reborrow + impl ToSql for CollectionId + where + Kind: Collection, + for<'de2> ::Kind: Deserialize<'de2>, + String: ToSql, + { + fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result { + let v = self.0.to_string(); + >::to_sql(&v, &mut out.reborrow()) + } + } + impl<'expr, Kind, ST> AsExpression for &'expr CollectionId + where + Kind: Collection, + for<'de2> ::Kind: Deserialize<'de2>, + Bound: Expression, + ST: SingleValue, + { + type Expression = Bound; + fn as_expression(self) -> Self::Expression { + Bound::new(self.0.as_str()) + } + } + impl AsExpression for CollectionId + where + Kind: Collection, + for<'de2> ::Kind: Deserialize<'de2>, + Bound: Expression, + ST: SingleValue, + { + type Expression = Bound; + fn as_expression(self) -> Self::Expression { + // TODO: deprecated in favor of `.into()` but that fails to compile + Bound::new(self.0.into_string()) + } + } + impl FromSql for CollectionId + where + Kind: Collection + Send + 'static, + for<'de2> ::Kind: Deserialize<'de2>, + String: FromSql, + DB: Backend, + DB: HasSqlType, + { + fn from_sql( + raw: DB::RawValue<'_>, + ) -> Result> { + let string: String = FromSql::::from_sql(raw)?; + Ok(CollectionId::parse(&string)?) + } + } + impl Queryable for CollectionId + where + Kind: Collection + Send + 'static, + for<'de2> ::Kind: Deserialize<'de2>, + String: FromStaticSqlRow, + DB: Backend, + DB: HasSqlType, + { + type Row = String; + fn build(row: Self::Row) -> diesel::deserialize::Result { + Ok(CollectionId::parse(&row)?) + } + } + impl QueryId for CollectionId + where + Kind: Collection + 'static, + for<'de2> ::Kind: Deserialize<'de2>, + { + type QueryId = Self; + } +}; diff --git a/src/fetch/object_id.rs b/src/fetch/object_id.rs index ac8efda..209960b 100644 --- a/src/fetch/object_id.rs +++ b/src/fetch/object_id.rs @@ -237,7 +237,7 @@ where } } -//#[cfg(feature = "diesel")] +#[cfg(feature = "diesel")] const _IMPL_DIESEL_NEW_TYPE_FOR_OBJECT_ID: () = { use diesel::{ backend::Backend,