diff --git a/sqlx-mysql/src/types/mod.rs b/sqlx-mysql/src/types/mod.rs index 6c2466ae..a7feb7b3 100644 --- a/sqlx-mysql/src/types/mod.rs +++ b/sqlx-mysql/src/types/mod.rs @@ -63,6 +63,7 @@ //! |---------------------------------------|------------------------------------------------------| //! | `uuid::Uuid` | BYTE(16), VARCHAR, CHAR, TEXT | //! | `uuid::fmt::Hyphenated` | CHAR(36) | +//! | `uuid::fmt::Simple` | CHAR(32) | //! //! ### [`json`](https://crates.io/crates/serde_json) //! diff --git a/sqlx-mysql/src/types/uuid.rs b/sqlx-mysql/src/types/uuid.rs index 4c351e7b..c18fdb78 100644 --- a/sqlx-mysql/src/types/uuid.rs +++ b/sqlx-mysql/src/types/uuid.rs @@ -1,4 +1,7 @@ -use uuid::{fmt::Hyphenated, Uuid}; +use uuid::{ + fmt::{Hyphenated, Simple}, + Uuid, +}; use crate::decode::Decode; use crate::encode::{Encode, IsNull}; @@ -64,3 +67,33 @@ impl Decode<'_, MySql> for Hyphenated { .map(|u| u.hyphenated()) } } + +impl Type for Simple { + fn type_info() -> MySqlTypeInfo { + <&str as Type>::type_info() + } + + fn compatible(ty: &MySqlTypeInfo) -> bool { + <&str as Type>::compatible(ty) + } +} + +impl Encode<'_, MySql> for Simple { + fn encode_by_ref(&self, buf: &mut Vec) -> IsNull { + buf.put_str_lenenc(&self.to_string()); + + IsNull::No + } +} + +impl Decode<'_, MySql> for Simple { + fn decode(value: MySqlValueRef<'_>) -> Result { + // delegate to the &str type to decode from MySQL + let text = <&str as Decode>::decode(value)?; + + // parse a UUID from the text + Uuid::parse_str(text) + .map_err(Into::into) + .map(|u| u.simple()) + } +} diff --git a/sqlx-sqlite/src/types/mod.rs b/sqlx-sqlite/src/types/mod.rs index 7e44127e..907a1de0 100644 --- a/sqlx-sqlite/src/types/mod.rs +++ b/sqlx-sqlite/src/types/mod.rs @@ -62,6 +62,7 @@ //! |---------------------------------------|------------------------------------------------------| //! | `uuid::Uuid` | BLOB, TEXT | //! | `uuid::fmt::Hyphenated` | TEXT | +//! | `uuid::fmt::Simple` | TEXT | //! //! ### [`json`](https://crates.io/crates/serde_json) //! diff --git a/sqlx-sqlite/src/types/uuid.rs b/sqlx-sqlite/src/types/uuid.rs index 56c7e0f0..9a11f960 100644 --- a/sqlx-sqlite/src/types/uuid.rs +++ b/sqlx-sqlite/src/types/uuid.rs @@ -5,7 +5,10 @@ use crate::type_info::DataType; use crate::types::Type; use crate::{Sqlite, SqliteArgumentValue, SqliteTypeInfo, SqliteValueRef}; use std::borrow::Cow; -use uuid::{fmt::Hyphenated, Uuid}; +use uuid::{ + fmt::{Hyphenated, Simple}, + Uuid, +}; impl Type for Uuid { fn type_info() -> SqliteTypeInfo { @@ -56,3 +59,26 @@ impl Decode<'_, Sqlite> for Hyphenated { Ok(uuid?.hyphenated()) } } + +impl Type for Simple { + fn type_info() -> SqliteTypeInfo { + SqliteTypeInfo(DataType::Text) + } +} + +impl<'q> Encode<'q, Sqlite> for Simple { + fn encode_by_ref(&self, args: &mut Vec>) -> IsNull { + args.push(SqliteArgumentValue::Text(Cow::Owned(self.to_string()))); + + IsNull::No + } +} + +impl Decode<'_, Sqlite> for Simple { + fn decode(value: SqliteValueRef<'_>) -> Result { + let uuid: Result = + Uuid::parse_str(&value.text().map(ToOwned::to_owned)?).map_err(Into::into); + + Ok(uuid?.simple()) + } +} diff --git a/tests/mysql/types.rs b/tests/mysql/types.rs index 4078b733..da931959 100644 --- a/tests/mysql/types.rs +++ b/tests/mysql/types.rs @@ -58,6 +58,14 @@ test_type!(uuid_hyphenated(MySql, == sqlx::types::Uuid::parse_str("00000000-0000-0000-0000-000000000000").unwrap().hyphenated() )); +#[cfg(feature = "uuid")] +test_type!(uuid_simple(MySql, + "'b731678f636f4135bc6f19440c13bd19'" + == sqlx::types::Uuid::parse_str("b731678f636f4135bc6f19440c13bd19").unwrap().simple(), + "'00000000000000000000000000000000'" + == sqlx::types::Uuid::parse_str("00000000000000000000000000000000").unwrap().simple() +)); + #[cfg(feature = "chrono")] mod chrono { use super::*; diff --git a/tests/sqlite/types.rs b/tests/sqlite/types.rs index f3db59c5..307e1409 100644 --- a/tests/sqlite/types.rs +++ b/tests/sqlite/types.rs @@ -196,3 +196,11 @@ test_type!(uuid_hyphenated(Sqlite, "'00000000-0000-0000-0000-000000000000'" == sqlx::types::Uuid::parse_str("00000000-0000-0000-0000-000000000000").unwrap().hyphenated() )); + +#[cfg(feature = "uuid")] +test_type!(uuid_simple(Sqlite, + "'b731678f636f4135bc6f19440c13bd19'" + == sqlx::types::Uuid::parse_str("b731678f636f4135bc6f19440c13bd19").unwrap().simple(), + "'00000000000000000000000000000000'" + == sqlx::types::Uuid::parse_str("00000000000000000000000000000000").unwrap().simple() +));