Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tentatively moved uuid type and implemented blob-based variant for SQLite #4521

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions diesel/src/pg/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ pub(in crate::pg) mod pg_lsn;
mod primitives;
mod ranges;
mod record;
#[cfg(feature = "uuid")]
mod uuid;

/// PostgreSQL specific SQL types
///
Expand Down Expand Up @@ -313,25 +311,6 @@ pub mod sql_types {
#[cfg(feature = "postgres_backend")]
pub type BigSerial = crate::sql_types::BigInt;

/// The [`UUID`] SQL type. This type can only be used with `feature = "uuid"`
///
/// ### [`ToSql`] impls
///
/// - [`uuid::Uuid`][Uuid]
///
/// ### [`FromSql`] impls
///
/// - [`uuid::Uuid`][Uuid]
///
/// [`ToSql`]: crate::serialize::ToSql
/// [`FromSql`]: crate::deserialize::FromSql
/// [Uuid]: https://docs.rs/uuid/*/uuid/struct.Uuid.html
/// [`UUID`]: https://www.postgresql.org/docs/current/datatype-uuid.html
#[cfg(feature = "postgres_backend")]
#[derive(Debug, Clone, Copy, Default, QueryId, SqlType)]
#[diesel(postgres_type(oid = 2950, array_oid = 2951))]
pub struct Uuid;

/// Alias for `Binary`, to ensure `diesel print-schema` works
pub type Bytea = crate::sql_types::Binary;

Expand Down
21 changes: 21 additions & 0 deletions diesel/src/sql_types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,27 @@ pub type Decimal = Numeric;
#[diesel(mysql_type(name = "String"))]
pub struct Text;

#[cfg(feature = "uuid")]
/// The [`UUID`] SQL type. This type can only be used with `feature = "uuid"`
///
/// ### [`ToSql`] impls
///
/// - [`uuid::Uuid`][Uuid]
///
/// ### [`FromSql`] impls
///
/// - [`uuid::Uuid`][Uuid]
///
/// [`ToSql`]: crate::serialize::ToSql
/// [`FromSql`]: crate::deserialize::FromSql
/// [Uuid]: https://docs.rs/uuid/*/uuid/struct.Uuid.html
/// [`UUID`]: https://www.postgresql.org/docs/current/datatype-uuid.html
#[derive(Debug, Clone, Copy, Default, QueryId, SqlType)]
#[diesel(postgres_type(oid = 2950, array_oid = 2951))]
#[diesel(sqlite_type(name = "Binary"))]
#[diesel(mysql_type(name = "Blob"))]
pub struct Uuid;

/// The SQL `VARCHAR` type
///
/// This type is generally interchangeable with `TEXT`, so Diesel has this as an
Expand Down
5 changes: 5 additions & 0 deletions diesel/src/type_impls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ mod json;
mod option;
mod primitives;
pub(crate) mod tuples;
#[cfg(all(
feature = "uuid",
any(feature = "postgres_backend", feature = "sqlite")
))]
mod uuid;
45 changes: 31 additions & 14 deletions diesel/src/pg/types/uuid.rs → diesel/src/type_impls/uuid.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::io::prelude::*;
extern crate uuid;

use crate::deserialize::{self, FromSql, FromSqlRow};
use crate::expression::AsExpression;
use crate::pg::{Pg, PgValue};
use crate::serialize::{self, IsNull, Output, ToSql};
use crate::sql_types::Uuid;

Expand All @@ -12,24 +11,41 @@ use crate::sql_types::Uuid;
#[allow(dead_code)]
struct UuidProxy(uuid::Uuid);

#[cfg(all(feature = "postgres_backend", feature = "uuid"))]
impl FromSql<Uuid, Pg> for uuid::Uuid {
fn from_sql(value: PgValue<'_>) -> deserialize::Result<Self> {
#[cfg(feature = "postgres_backend")]
impl FromSql<Uuid, crate::pg::Pg> for uuid::Uuid {
fn from_sql(value: crate::pg::PgValue<'_>) -> deserialize::Result<Self> {
uuid::Uuid::from_slice(value.as_bytes()).map_err(Into::into)
}
}

#[cfg(all(feature = "postgres_backend", feature = "uuid"))]
impl ToSql<Uuid, Pg> for uuid::Uuid {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
#[cfg(feature = "postgres_backend")]
impl ToSql<Uuid, crate::pg::Pg> for uuid::Uuid {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, crate::pg::Pg>) -> serialize::Result {
use std::io::Write;
out.write_all(self.as_bytes())
.map(|_| IsNull::No)
.map_err(Into::into)
}
}

#[cfg(feature = "sqlite")]
impl ToSql<Uuid, crate::sqlite::Sqlite> for uuid::Uuid {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, crate::sqlite::Sqlite>) -> serialize::Result {
out.set_value(self.as_bytes().as_slice());
Ok(IsNull::No)
}
}

#[cfg(feature = "sqlite")]
impl FromSql<Uuid, crate::sqlite::Sqlite> for uuid::Uuid {
fn from_sql(mut value: crate::sqlite::SqliteValue<'_, '_, '_>) -> deserialize::Result<Self> {
uuid::Uuid::from_slice(value.read_blob()).map_err(Into::into)
}
}

#[cfg(test)]
mod tests {
#[cfg(feature = "postgres_backend")]
mod postgres_tests {
use super::*;

#[diesel_test_helper::test]
Expand All @@ -44,7 +60,7 @@ mod tests {

let test_uuid = uuid::Uuid::from_slice(&bytes).unwrap();
let mut bytes = Output::test(ByteWrapper(&mut buffer));
ToSql::<Uuid, Pg>::to_sql(&test_uuid, &mut bytes).unwrap();
ToSql::<Uuid, crate::pg::Pg>::to_sql(&test_uuid, &mut bytes).unwrap();
assert_eq!(&buffer, test_uuid.as_bytes());
}

Expand All @@ -55,15 +71,16 @@ mod tests {
0x31, 0x32,
];
let input_uuid = uuid::Uuid::from_slice(&bytes).unwrap();
let output_uuid =
<uuid::Uuid as FromSql<Uuid, Pg>>::from_sql(PgValue::for_test(input_uuid.as_bytes()))
.unwrap();
let output_uuid = <uuid::Uuid as FromSql<Uuid, crate::pg::Pg>>::from_sql(
crate::pg::PgValue::for_test(input_uuid.as_bytes()),
)
.unwrap();
assert_eq!(input_uuid, output_uuid);
}

#[diesel_test_helper::test]
fn bad_uuid_from_sql() {
let uuid = uuid::Uuid::from_sql(PgValue::for_test(b"boom"));
let uuid = uuid::Uuid::from_sql(crate::pg::PgValue::for_test(b"boom"));
assert!(uuid.is_err());
// The error message changes slightly between different
// uuid versions, so we just check on the relevant parts
Expand Down
Loading