| 1 |
use std::io::prelude::*; |
| 2 |
|
| 3 |
use crate::deserialize::{self, FromSql, FromSqlRow}; |
| 4 |
use crate::expression::AsExpression; |
| 5 |
use crate::pg::{Pg, PgValue}; |
| 6 |
use crate::serialize::{self, IsNull, Output, ToSql}; |
| 7 |
use crate::sql_types::Uuid; |
| 8 |
|
| 9 |
#[derive(AsExpression, FromSqlRow)] |
| 10 |
#[diesel(foreign_derive)] |
| 11 |
#[diesel(sql_type = Uuid)] |
| 12 |
#[allow(dead_code)] |
| 13 |
struct UuidProxy(uuid::Uuid); |
| 14 |
|
| 15 |
#[cfg(all(feature = "postgres_backend", feature = "uuid"))] |
| 16 |
impl 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"))] |
| 23 |
impl 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)] |
| 32 |
mod 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 |
} |