mirror of
https://github.com/launchbadge/sqlx.git
synced 2025-10-02 23:35:20 +00:00
Add Simple format for Uuid for MySQL & SQLite. (#2469)
* Add Simple format for Uuid for MySQL & SQLite. Copy pasted the implementation for `Hyphenated` and adapt. * Add uuid Simple docs for SQLite
This commit is contained in:
parent
003878698e
commit
cbf8dd37e9
@ -63,6 +63,7 @@
|
|||||||
//! |---------------------------------------|------------------------------------------------------|
|
//! |---------------------------------------|------------------------------------------------------|
|
||||||
//! | `uuid::Uuid` | BYTE(16), VARCHAR, CHAR, TEXT |
|
//! | `uuid::Uuid` | BYTE(16), VARCHAR, CHAR, TEXT |
|
||||||
//! | `uuid::fmt::Hyphenated` | CHAR(36) |
|
//! | `uuid::fmt::Hyphenated` | CHAR(36) |
|
||||||
|
//! | `uuid::fmt::Simple` | CHAR(32) |
|
||||||
//!
|
//!
|
||||||
//! ### [`json`](https://crates.io/crates/serde_json)
|
//! ### [`json`](https://crates.io/crates/serde_json)
|
||||||
//!
|
//!
|
||||||
|
@ -1,4 +1,7 @@
|
|||||||
use uuid::{fmt::Hyphenated, Uuid};
|
use uuid::{
|
||||||
|
fmt::{Hyphenated, Simple},
|
||||||
|
Uuid,
|
||||||
|
};
|
||||||
|
|
||||||
use crate::decode::Decode;
|
use crate::decode::Decode;
|
||||||
use crate::encode::{Encode, IsNull};
|
use crate::encode::{Encode, IsNull};
|
||||||
@ -64,3 +67,33 @@ impl Decode<'_, MySql> for Hyphenated {
|
|||||||
.map(|u| u.hyphenated())
|
.map(|u| u.hyphenated())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Type<MySql> for Simple {
|
||||||
|
fn type_info() -> MySqlTypeInfo {
|
||||||
|
<&str as Type<MySql>>::type_info()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn compatible(ty: &MySqlTypeInfo) -> bool {
|
||||||
|
<&str as Type<MySql>>::compatible(ty)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Encode<'_, MySql> for Simple {
|
||||||
|
fn encode_by_ref(&self, buf: &mut Vec<u8>) -> IsNull {
|
||||||
|
buf.put_str_lenenc(&self.to_string());
|
||||||
|
|
||||||
|
IsNull::No
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Decode<'_, MySql> for Simple {
|
||||||
|
fn decode(value: MySqlValueRef<'_>) -> Result<Self, BoxDynError> {
|
||||||
|
// delegate to the &str type to decode from MySQL
|
||||||
|
let text = <&str as Decode<MySql>>::decode(value)?;
|
||||||
|
|
||||||
|
// parse a UUID from the text
|
||||||
|
Uuid::parse_str(text)
|
||||||
|
.map_err(Into::into)
|
||||||
|
.map(|u| u.simple())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -62,6 +62,7 @@
|
|||||||
//! |---------------------------------------|------------------------------------------------------|
|
//! |---------------------------------------|------------------------------------------------------|
|
||||||
//! | `uuid::Uuid` | BLOB, TEXT |
|
//! | `uuid::Uuid` | BLOB, TEXT |
|
||||||
//! | `uuid::fmt::Hyphenated` | TEXT |
|
//! | `uuid::fmt::Hyphenated` | TEXT |
|
||||||
|
//! | `uuid::fmt::Simple` | TEXT |
|
||||||
//!
|
//!
|
||||||
//! ### [`json`](https://crates.io/crates/serde_json)
|
//! ### [`json`](https://crates.io/crates/serde_json)
|
||||||
//!
|
//!
|
||||||
|
@ -5,7 +5,10 @@ use crate::type_info::DataType;
|
|||||||
use crate::types::Type;
|
use crate::types::Type;
|
||||||
use crate::{Sqlite, SqliteArgumentValue, SqliteTypeInfo, SqliteValueRef};
|
use crate::{Sqlite, SqliteArgumentValue, SqliteTypeInfo, SqliteValueRef};
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use uuid::{fmt::Hyphenated, Uuid};
|
use uuid::{
|
||||||
|
fmt::{Hyphenated, Simple},
|
||||||
|
Uuid,
|
||||||
|
};
|
||||||
|
|
||||||
impl Type<Sqlite> for Uuid {
|
impl Type<Sqlite> for Uuid {
|
||||||
fn type_info() -> SqliteTypeInfo {
|
fn type_info() -> SqliteTypeInfo {
|
||||||
@ -56,3 +59,26 @@ impl Decode<'_, Sqlite> for Hyphenated {
|
|||||||
Ok(uuid?.hyphenated())
|
Ok(uuid?.hyphenated())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Type<Sqlite> for Simple {
|
||||||
|
fn type_info() -> SqliteTypeInfo {
|
||||||
|
SqliteTypeInfo(DataType::Text)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'q> Encode<'q, Sqlite> for Simple {
|
||||||
|
fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue<'q>>) -> IsNull {
|
||||||
|
args.push(SqliteArgumentValue::Text(Cow::Owned(self.to_string())));
|
||||||
|
|
||||||
|
IsNull::No
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Decode<'_, Sqlite> for Simple {
|
||||||
|
fn decode(value: SqliteValueRef<'_>) -> Result<Self, BoxDynError> {
|
||||||
|
let uuid: Result<Uuid, BoxDynError> =
|
||||||
|
Uuid::parse_str(&value.text().map(ToOwned::to_owned)?).map_err(Into::into);
|
||||||
|
|
||||||
|
Ok(uuid?.simple())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -58,6 +58,14 @@ test_type!(uuid_hyphenated<sqlx::types::uuid::fmt::Hyphenated>(MySql,
|
|||||||
== sqlx::types::Uuid::parse_str("00000000-0000-0000-0000-000000000000").unwrap().hyphenated()
|
== sqlx::types::Uuid::parse_str("00000000-0000-0000-0000-000000000000").unwrap().hyphenated()
|
||||||
));
|
));
|
||||||
|
|
||||||
|
#[cfg(feature = "uuid")]
|
||||||
|
test_type!(uuid_simple<sqlx::types::uuid::fmt::Simple>(MySql,
|
||||||
|
"'b731678f636f4135bc6f19440c13bd19'"
|
||||||
|
== sqlx::types::Uuid::parse_str("b731678f636f4135bc6f19440c13bd19").unwrap().simple(),
|
||||||
|
"'00000000000000000000000000000000'"
|
||||||
|
== sqlx::types::Uuid::parse_str("00000000000000000000000000000000").unwrap().simple()
|
||||||
|
));
|
||||||
|
|
||||||
#[cfg(feature = "chrono")]
|
#[cfg(feature = "chrono")]
|
||||||
mod chrono {
|
mod chrono {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
@ -196,3 +196,11 @@ test_type!(uuid_hyphenated<sqlx::types::uuid::fmt::Hyphenated>(Sqlite,
|
|||||||
"'00000000-0000-0000-0000-000000000000'"
|
"'00000000-0000-0000-0000-000000000000'"
|
||||||
== sqlx::types::Uuid::parse_str("00000000-0000-0000-0000-000000000000").unwrap().hyphenated()
|
== sqlx::types::Uuid::parse_str("00000000-0000-0000-0000-000000000000").unwrap().hyphenated()
|
||||||
));
|
));
|
||||||
|
|
||||||
|
#[cfg(feature = "uuid")]
|
||||||
|
test_type!(uuid_simple<sqlx::types::uuid::fmt::Simple>(Sqlite,
|
||||||
|
"'b731678f636f4135bc6f19440c13bd19'"
|
||||||
|
== sqlx::types::Uuid::parse_str("b731678f636f4135bc6f19440c13bd19").unwrap().simple(),
|
||||||
|
"'00000000000000000000000000000000'"
|
||||||
|
== sqlx::types::Uuid::parse_str("00000000000000000000000000000000").unwrap().simple()
|
||||||
|
));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user