From bce1f61ba65dd496dffa9698cdc5f9b0e40a1e45 Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Wed, 11 May 2022 13:57:44 -0400 Subject: [PATCH] Add option to use UUID v1.0 with postgres --- Cargo.toml | 3 +++ README.md | 4 ++-- sea-query-driver/Cargo.toml | 1 + src/driver/postgres.rs | 2 ++ src/lib.rs | 4 ++-- src/value.rs | 15 +++++++++++++++ tests/postgres/query.rs | 13 +++++++++++++ 7 files changed, 38 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index dac7393b7..07e1143df 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,6 +47,7 @@ postgres-types = { version = "^0", optional = true } rust_decimal = { version = "^1", optional = true } bigdecimal = { version = "^0.2", optional = true } uuid = { version = "^0", optional = true } +uuid-1 = { version = "^1", package = "uuid", optional = true } proc-macro2 = { version = "1", optional = true } quote = { version = "^1", optional = true } time = { version = "^0.2", optional = true } @@ -68,6 +69,7 @@ postgres-json = ["with-json", "postgres-types/with-serde_json-1"] postgres-rust_decimal = ["with-rust_decimal", "rust_decimal/db-postgres"] postgres-bigdecimal = ["with-bigdecimal"] postgres-uuid = ["with-uuid", "postgres-types/with-uuid-0_8"] +postgres-uuid-1 = ["with-uuid-1", "postgres-types/with-uuid-1"] postgres-array = ["postgres-types/array-impls", "sea-query-driver/postgres-array"] postgres-interval = ["proc-macro2", "quote"] postgres-time = ["with-time", "postgres-types/with-time-0_2"] @@ -82,6 +84,7 @@ with-json = ["serde_json", "sea-query-driver/with-json"] with-rust_decimal = ["rust_decimal", "sea-query-driver/with-rust_decimal"] with-bigdecimal = ["bigdecimal", "sea-query-driver/with-bigdecimal"] with-uuid = ["uuid", "uuid-enabled", "sea-query-driver/with-uuid"] +with-uuid-1 = ["uuid-1", "uuid-enabled", "sea-query-driver/with-uuid-1"] with-time = ["time", "sea-query-driver/with-time"] [[test]] diff --git a/README.md b/README.md index 4ca826685..609e673b5 100644 --- a/README.md +++ b/README.md @@ -51,13 +51,13 @@ Async support: `thread-safe` (use `Arc` inplace of `Rc`) SQL dialect: `backend-mysql`, `backend-postgres`, `backend-sqlite` Type support: `with-chrono`, `with-time`, `with-json`, `with-rust_decimal`, `with-bigdecimal`, `with-uuid`, -`postgres-array` +`with-uuid-1`, `postgres-array` Driver support: `sqlx-mysql`, `sqlx-postgres`, `sqlx-sqlite`, `postgres`, `postgres-*`, `rusqlite` Postgres support: `postgres`, `postgres-chrono`, `postgres-json`, `postgres-rust_decimal`, -`postgres-bigdecimal`, `postgres-uuid`, `postgres-array`, `postgres-interval` +`postgres-bigdecimal`, `postgres-uuid`, `postgres-uuid-1`, `postgres-array`, `postgres-interval` ## Usage diff --git a/sea-query-driver/Cargo.toml b/sea-query-driver/Cargo.toml index 99369f544..61dbb619f 100644 --- a/sea-query-driver/Cargo.toml +++ b/sea-query-driver/Cargo.toml @@ -29,6 +29,7 @@ with-json = [] with-rust_decimal = [] with-bigdecimal = [] with-uuid = ["uuid-enabled"] +with-uuid-1 = ["uuid-enabled"] with-time = [] with-ipnetwork = [] with-mac_address = [] diff --git a/src/driver/postgres.rs b/src/driver/postgres.rs index f0af751c7..5360e7baa 100644 --- a/src/driver/postgres.rs +++ b/src/driver/postgres.rs @@ -73,6 +73,8 @@ impl ToSql for Value { Value::BigDecimal(_) => unimplemented!("Not supported"), #[cfg(feature = "postgres-uuid")] Value::Uuid(v) => box_to_sql!(v, uuid::Uuid), + #[cfg(feature = "postgres-uuid-1")] + Value::Uuid(v) => box_to_sql!(v, uuid_1::Uuid), #[cfg(feature = "postgres-array")] Value::Array(v) => box_to_sql!(v, Vec), #[allow(unreachable_patterns)] diff --git a/src/lib.rs b/src/lib.rs index 062376c9f..ab151ebd9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -54,13 +54,13 @@ //! SQL dialect: `backend-mysql`, `backend-postgres`, `backend-sqlite` //! //! Type support: `with-chrono`, `with-time`, `with-json`, `with-rust_decimal`, `with-bigdecimal`, `with-uuid`, -//! `postgres-array` +//! `with-uuid-1`, `postgres-array` //! //! Driver support: `sqlx-mysql`, `sqlx-postgres`, `sqlx-sqlite`, //! `postgres`, `postgres-*`, `rusqlite` //! //! Postgres support: `postgres`, `postgres-chrono`, `postgres-json`, `postgres-rust_decimal`, -//! `postgres-bigdecimal`, `postgres-uuid`, `postgres-array`, `postgres-interval` +//! `postgres-bigdecimal`, `postgres-uuid`, `postgres-uuid-1`, `postgres-array`, `postgres-interval` //! //! ## Usage //! diff --git a/src/value.rs b/src/value.rs index b933859ce..2387b2216 100644 --- a/src/value.rs +++ b/src/value.rs @@ -18,9 +18,15 @@ use rust_decimal::Decimal; #[cfg(feature = "with-bigdecimal")] use bigdecimal::BigDecimal; +#[cfg(all(feature = "with-uuid", feature = "with-uuid-1"))] +compile_error!("You cannot enable `with-uuid` and `with-uuid-1` together"); + #[cfg(feature = "with-uuid")] use uuid::Uuid; +#[cfg(feature = "with-uuid-1")] +use uuid_1::Uuid; + #[cfg(feature = "with-ipnetwork")] use ipnetwork::{Ipv4Network, Ipv6Network}; @@ -1674,6 +1680,15 @@ mod tests { assert_eq!(out, uuid); } + #[test] + #[cfg(feature = "with-uuid-1")] + fn test_uuid_value() { + let uuid = uuid_1::Uuid::parse_str("936DA01F9ABD4d9d80C702AF85C822A8").unwrap(); + let value: Value = uuid.into(); + let out: uuid_1::Uuid = value.unwrap(); + assert_eq!(out, uuid); + } + #[test] #[cfg(feature = "with-rust_decimal")] fn test_decimal_value() { diff --git a/tests/postgres/query.rs b/tests/postgres/query.rs index f821dbf99..25f096035 100644 --- a/tests/postgres/query.rs +++ b/tests/postgres/query.rs @@ -1115,6 +1115,19 @@ fn insert_5() { ); } +#[test] +#[cfg(feature = "with-uuid-1")] +fn insert_5_uuid_1() { + assert_eq!( + Query::insert() + .into_table(Glyph::Table) + .columns([Glyph::Image]) + .values_panic(vec![uuid_1::Uuid::nil().into()]) + .to_string(PostgresQueryBuilder), + "INSERT INTO \"glyph\" (\"image\") VALUES ('00000000-0000-0000-0000-000000000000')" + ); +} + #[test] fn insert_from_select() { assert_eq!(