diesel/pg/types/
uuid.rs

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)
            }
        }
        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)
            }
        }
        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)
            }
        }
        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
35    #[diesel_test_helper::test]
36    fn uuid_to_sql() {
37        use crate::query_builder::bind_collector::ByteWrapper;
38
39        let mut buffer = Vec::new();
40        let bytes = [
41            0xFF_u8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
42            0x31, 0x32,
43        ];
44
45        let test_uuid = uuid::Uuid::from_slice(&bytes).unwrap();
46        let mut bytes = Output::test(ByteWrapper(&mut buffer));
47        ToSql::<Uuid, Pg>::to_sql(&test_uuid, &mut bytes).unwrap();
48        assert_eq!(&buffer, test_uuid.as_bytes());
49    }
50
51    #[diesel_test_helper::test]
52    fn some_uuid_from_sql() {
53        let bytes = [
54            0xFF_u8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
55            0x31, 0x32,
56        ];
57        let input_uuid = uuid::Uuid::from_slice(&bytes).unwrap();
58        let output_uuid =
59            <uuid::Uuid as FromSql<Uuid, Pg>>::from_sql(PgValue::for_test(input_uuid.as_bytes()))
60                .unwrap();
61        assert_eq!(input_uuid, output_uuid);
62    }
63
64    #[diesel_test_helper::test]
65    fn bad_uuid_from_sql() {
66        let uuid = uuid::Uuid::from_sql(PgValue::for_test(b"boom"));
67        assert!(uuid.is_err());
68        // The error message changes slightly between different
69        // uuid versions, so we just check on the relevant parts
70        // The exact error messages are either:
71        // "invalid bytes length: expected 16, found 4"
72        // or
73        // "invalid length: expected 16 bytes, found 4"
74        let error_message = uuid.unwrap_err().to_string();
75        assert!(error_message.starts_with("invalid"));
76        assert!(error_message.contains("length"));
77        assert!(error_message.contains("expected 16"));
78        assert!(error_message.ends_with("found 4"));
79    }
80
81    #[diesel_test_helper::test]
82    fn no_uuid_from_sql() {
83        let uuid = uuid::Uuid::from_nullable_sql(None);
84        assert_eq!(
85            uuid.unwrap_err().to_string(),
86            "Unexpected null for non-null column"
87        );
88    }
89}