1use std::io::prelude::*;
2
3use crate::deserialize::{self, FromSql, FromSqlRow};
4use crate::expression::AsExpression;
5use crate::pg::{Pg, PgValue};
6use crate::serialize::{self, IsNull, Output, ToSql};
7use crate::sql_types::Uuid;
8
9#[derive(const _: () =
{
use diesel;
impl<'__expr> diesel::expression::AsExpression<Uuid> for
&'__expr uuid::Uuid {
type Expression =
diesel::internal::derives::as_expression::Bound<Uuid, Self>;
fn as_expression(self)
->
<Self as
diesel::expression::AsExpression<Uuid>>::Expression {
diesel::internal::derives::as_expression::Bound::new(self)
}
}
#[diagnostic::do_not_recommend]
impl<'__expr>
diesel::expression::AsExpression<diesel::sql_types::Nullable<Uuid>>
for &'__expr uuid::Uuid {
type Expression =
diesel::internal::derives::as_expression::Bound<diesel::sql_types::Nullable<Uuid>,
Self>;
fn as_expression(self)
->
<Self as
diesel::expression::AsExpression<diesel::sql_types::Nullable<Uuid>>>::Expression {
diesel::internal::derives::as_expression::Bound::new(self)
}
}
#[diagnostic::do_not_recommend]
impl<'__expr, '__expr2> diesel::expression::AsExpression<Uuid> for
&'__expr2 &'__expr uuid::Uuid {
type Expression =
diesel::internal::derives::as_expression::Bound<Uuid, Self>;
fn as_expression(self)
->
<Self as
diesel::expression::AsExpression<Uuid>>::Expression {
diesel::internal::derives::as_expression::Bound::new(self)
}
}
#[diagnostic::do_not_recommend]
impl<'__expr, '__expr2>
diesel::expression::AsExpression<diesel::sql_types::Nullable<Uuid>>
for &'__expr2 &'__expr uuid::Uuid {
type Expression =
diesel::internal::derives::as_expression::Bound<diesel::sql_types::Nullable<Uuid>,
Self>;
fn as_expression(self)
->
<Self as
diesel::expression::AsExpression<diesel::sql_types::Nullable<Uuid>>>::Expression {
diesel::internal::derives::as_expression::Bound::new(self)
}
}
impl<__DB>
diesel::serialize::ToSql<diesel::sql_types::Nullable<Uuid>, __DB>
for uuid::Uuid where __DB: diesel::backend::Backend,
Self: diesel::serialize::ToSql<Uuid, __DB> {
fn to_sql<'__b>(&'__b self,
out: &mut diesel::serialize::Output<'__b, '_, __DB>)
-> diesel::serialize::Result {
diesel::serialize::ToSql::<Uuid, __DB>::to_sql(self, out)
}
}
impl diesel::expression::AsExpression<Uuid> for uuid::Uuid {
type Expression =
diesel::internal::derives::as_expression::Bound<Uuid, Self>;
fn as_expression(self)
->
<Self as
diesel::expression::AsExpression<Uuid>>::Expression {
diesel::internal::derives::as_expression::Bound::new(self)
}
}
impl diesel::expression::AsExpression<diesel::sql_types::Nullable<Uuid>>
for uuid::Uuid {
type Expression =
diesel::internal::derives::as_expression::Bound<diesel::sql_types::Nullable<Uuid>,
Self>;
fn as_expression(self)
->
<Self as
diesel::expression::AsExpression<diesel::sql_types::Nullable<Uuid>>>::Expression {
diesel::internal::derives::as_expression::Bound::new(self)
}
}
};AsExpression, const _: () =
{
use diesel;
impl<__DB, __ST> diesel::deserialize::Queryable<__ST, __DB> for
uuid::Uuid where __DB: diesel::backend::Backend,
__ST: diesel::sql_types::SingleValue,
Self: diesel::deserialize::FromSql<__ST, __DB> {
type Row = Self;
fn build(row: Self) -> diesel::deserialize::Result<Self> {
diesel::deserialize::Result::Ok(row)
}
}
};FromSqlRow)]
10#[diesel(foreign_derive)]
11#[diesel(sql_type = Uuid)]
12#[allow(dead_code)]
13struct UuidProxy(uuid::Uuid);
14
15#[cfg(all(feature = "postgres_backend", feature = "uuid"))]
16impl FromSql<Uuid, Pg> for uuid::Uuid {
17 fn from_sql(value: PgValue<'_>) -> deserialize::Result<Self> {
18 uuid::Uuid::from_slice(value.as_bytes()).map_err(Into::into)
19 }
20}
21
22#[cfg(all(feature = "postgres_backend", feature = "uuid"))]
23impl ToSql<Uuid, Pg> for uuid::Uuid {
24 fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
25 out.write_all(self.as_bytes())
26 .map(|_| IsNull::No)
27 .map_err(Into::into)
28 }
29}
30
31#[cfg(test)]
32mod tests {
33 use super::*;
34 use crate::test_helpers::format_error;
35
36 #[diesel_test_helper::test]
37 fn uuid_to_sql() {
38 use crate::query_builder::bind_collector::ByteWrapper;
39
40 let mut buffer = Vec::new();
41 let bytes = [
42 0xFF_u8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
43 0x31, 0x32,
44 ];
45
46 let test_uuid = uuid::Uuid::from_slice(&bytes).unwrap();
47 let mut bytes = Output::test(ByteWrapper(&mut buffer));
48 ToSql::<Uuid, Pg>::to_sql(&test_uuid, &mut bytes).unwrap();
49 assert_eq!(&buffer, test_uuid.as_bytes());
50 }
51
52 #[diesel_test_helper::test]
53 fn some_uuid_from_sql() {
54 let bytes = [
55 0xFF_u8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
56 0x31, 0x32,
57 ];
58 let input_uuid = uuid::Uuid::from_slice(&bytes).unwrap();
59 let output_uuid =
60 <uuid::Uuid as FromSql<Uuid, Pg>>::from_sql(PgValue::for_test(input_uuid.as_bytes()))
61 .unwrap();
62 assert_eq!(input_uuid, output_uuid);
63 }
64
65 #[diesel_test_helper::test]
66 fn bad_uuid_from_sql() {
67 let uuid = uuid::Uuid::from_sql(PgValue::for_test(b"boom"));
68 assert!(uuid.is_err());
69 let error_message = format_error(&*uuid.unwrap_err());
76 assert!(error_message.starts_with("invalid"));
77 assert!(error_message.contains("length"));
78 assert!(error_message.contains("expected 16"));
79 assert!(error_message.ends_with("found 4"));
80 }
81
82 #[diesel_test_helper::test]
83 fn no_uuid_from_sql() {
84 let uuid = uuid::Uuid::from_nullable_sql(None);
85 assert_eq!(
86 format_error(&*uuid.unwrap_err()),
87 "Unexpected null for non-null column"
88 );
89 }
90}