Skip to content

Commit

Permalink
Add option to use UUID v1.0 with postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
marti4d committed May 11, 2022
1 parent ddd17ac commit bce1f61
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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"]
Expand All @@ -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]]
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions sea-query-driver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
2 changes: 2 additions & 0 deletions src/driver/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Value>),
#[allow(unreachable_patterns)]
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
//!
Expand Down
15 changes: 15 additions & 0 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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() {
Expand Down
13 changes: 13 additions & 0 deletions tests/postgres/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down

0 comments on commit bce1f61

Please sign in to comment.