diff --git a/examples/sqlx_postgres/Cargo.toml b/examples/sqlx_postgres/Cargo.toml index ebe4dc408..29658ca83 100644 --- a/examples/sqlx_postgres/Cargo.toml +++ b/examples/sqlx_postgres/Cargo.toml @@ -11,6 +11,7 @@ publish = false [dependencies] chrono = { version = "0.4", default-features = false, features = ["clock"] } time = { version = "0.3.36", features = ["macros"] } +jiff = { version = "0.2.15", features = ["std"] } uuid = { version = "1", features = ["serde", "v4"] } serde_json = "1" rust_decimal = { version = "1" } @@ -28,6 +29,7 @@ sea-query-binder = { path = "../../sea-query-binder", features = [ "with-bigdecimal", "with-uuid", "with-time", + "with-jiff", "with-ipnetwork", "with-mac_address", "runtime-async-std-native-tls", diff --git a/examples/sqlx_postgres/src/main.rs b/examples/sqlx_postgres/src/main.rs index 3d30e631a..fdfbe9790 100644 --- a/examples/sqlx_postgres/src/main.rs +++ b/examples/sqlx_postgres/src/main.rs @@ -26,9 +26,16 @@ async fn main() { // Schema + let sql = Table::drop() + .table(Character::Table) + .if_exists() + .build(PostgresQueryBuilder); + + let result = sqlx::query(&sql).execute(&mut *pool).await; + println!("Drop table character: {result:?}\n"); + let sql = Table::create() .table(Character::Table) - .if_not_exists() .col( ColumnDef::new(Character::Id) .integer() @@ -78,11 +85,12 @@ async fn main() { .unwrap() .with_scale(3) .into(), - NaiveDate::from_ymd_opt(2020, 8, 20) - .unwrap() - .and_hms_opt(0, 0, 0) - .unwrap() - .into(), + // NaiveDate::from_ymd_opt(2020, 8, 20) + // .unwrap() + // .and_hms_opt(0, 0, 0) + // .unwrap() + // .into(), + jiff::civil::date(2020, 8, 20).at(0, 0, 0, 0).into(), IpNetwork::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8) .unwrap() .into(), diff --git a/sea-query-binder/Cargo.toml b/sea-query-binder/Cargo.toml index 2084b2170..1c48a694f 100644 --- a/sea-query-binder/Cargo.toml +++ b/sea-query-binder/Cargo.toml @@ -25,14 +25,16 @@ rust_decimal = { version = "1", default-features = false, optional = true } bigdecimal = { version = "0.4", default-features = false, optional = true } uuid = { version = "1", default-features = false, optional = true } time = { version = "0.3.36", default-features = false, optional = true, features = ["macros", "formatting"] } +jiff = { version = "0.2.15", default-features = false, optional = true, features = ["std", "perf-inline"] } +jiff-sqlx = { version = "0.1", optional = true } ipnetwork = { version = "0.20", default-features = false, optional = true } mac_address = { version = "1.1", default-features = false, optional = true } pgvector = { version = "~0.4", default-features = false, optional = true } [features] sqlx-mysql = ["sqlx/mysql"] -sqlx-postgres = ["sqlx/postgres"] -sqlx-sqlite = ["sqlx/sqlite"] +sqlx-postgres = ["sqlx/postgres", "jiff-sqlx?/postgres"] +sqlx-sqlite = ["sqlx/sqlite", "jiff-sqlx?/sqlite"] sqlx-any = ["sqlx/any"] with-chrono = ["sqlx?/chrono", "sea-query/with-chrono", "chrono"] with-json = ["sqlx?/json", "sea-query/with-json", "serde_json"] @@ -40,6 +42,7 @@ with-rust_decimal = ["sqlx?/rust_decimal", "sea-query/with-rust_decimal", "rust_ with-bigdecimal = ["sqlx?/bigdecimal", "sea-query/with-bigdecimal", "bigdecimal"] with-uuid = ["sqlx?/uuid", "sea-query/with-uuid", "uuid"] with-time = ["sqlx?/time", "sea-query/with-time", "time"] +with-jiff = ["sea-query/with-jiff", "jiff", "jiff-sqlx"] with-ipnetwork = ["sqlx?/ipnetwork", "sea-query/with-ipnetwork", "ipnetwork"] with-mac_address = ["sqlx?/mac_address", "sea-query/with-mac_address", "mac_address"] postgres-array = ["sea-query/postgres-array"] diff --git a/sea-query-binder/src/sqlx_postgres.rs b/sea-query-binder/src/sqlx_postgres.rs index 6402de556..46e91fac8 100644 --- a/sea-query-binder/src/sqlx_postgres.rs +++ b/sea-query-binder/src/sqlx_postgres.rs @@ -13,7 +13,7 @@ use serde_json::Value as Json; #[cfg(feature = "with-uuid")] use uuid::Uuid; -use sea_query::{ArrayType, Value}; +use sea_query::{ArrayType, Value, ValueType}; use crate::SqlxValues; @@ -105,6 +105,26 @@ impl sqlx::IntoArguments<'_, sqlx::postgres::Postgres> for SqlxValues { Value::TimeDateTimeWithTimeZone(t) => { let _ = args.add(t); } + #[cfg(feature = "with-jiff")] + Value::JiffDate(j) => { + let _ = args.add(j.map(|j| jiff_sqlx::ToSqlx::to_sqlx(j))); + } + #[cfg(feature = "with-jiff")] + Value::JiffTime(j) => { + let _ = args.add(j.map(|j| jiff_sqlx::ToSqlx::to_sqlx(j))); + } + #[cfg(feature = "with-jiff")] + Value::JiffDateTime(j) => { + let _ = args.add(j.map(|j| jiff_sqlx::ToSqlx::to_sqlx(j))); + } + #[cfg(feature = "with-jiff")] + Value::JiffTimestamp(j) => { + let _ = args.add(j.map(|j| jiff_sqlx::ToSqlx::to_sqlx(j))); + } + #[cfg(feature = "with-jiff")] + Value::JiffZoned(_) => { + unimplemented!("no support by jiff-sqlx"); + } #[cfg(feature = "with-uuid")] Value::Uuid(uuid) => { let _ = args.add(uuid); @@ -277,6 +297,75 @@ impl sqlx::IntoArguments<'_, sqlx::postgres::Postgres> for SqlxValues { ); let _ = args.add(value); } + #[cfg(feature = "with-jiff")] + ArrayType::JiffDate => { + let value = match v { + Some(j) => Some( + j.into_iter() + .map(|j| { + jiff_sqlx::ToSqlx::to_sqlx( + ::try_from(j).unwrap(), + ) + }) + .collect::>(), + ), + None => None, + }; + let _ = args.add(value); + } + #[cfg(feature = "with-jiff")] + ArrayType::JiffTime => { + let value = match v { + Some(j) => Some( + j.into_iter() + .map(|j| { + jiff_sqlx::ToSqlx::to_sqlx( + ::try_from(j).unwrap(), + ) + }) + .collect::>(), + ), + None => None, + }; + let _ = args.add(value); + } + #[cfg(feature = "with-jiff")] + ArrayType::JiffDateTime => { + let value = match v { + Some(j) => Some( + j.into_iter() + .map(|j| { + jiff_sqlx::ToSqlx::to_sqlx( + ::try_from(j) + .unwrap(), + ) + }) + .collect::>(), + ), + None => None, + }; + let _ = args.add(value); + } + #[cfg(feature = "with-jiff")] + ArrayType::JiffTimestamp => { + let value = match v { + Some(j) => Some( + j.into_iter() + .map(|j| { + jiff_sqlx::ToSqlx::to_sqlx( + ::try_from(j).unwrap(), + ) + }) + .collect::>(), + ), + None => None, + }; + let _ = args.add(value); + } + #[cfg(feature = "with-jiff")] + ArrayType::JiffZoned => { + unimplemented!("no support by jiff-sqlx"); + } #[cfg(feature = "with-uuid")] ArrayType::Uuid => { let value: Option> = Value::Array(ty, v) diff --git a/sea-query-binder/src/sqlx_sqlite.rs b/sea-query-binder/src/sqlx_sqlite.rs index 888e55778..48b855381 100644 --- a/sea-query-binder/src/sqlx_sqlite.rs +++ b/sea-query-binder/src/sqlx_sqlite.rs @@ -90,6 +90,26 @@ impl<'q> sqlx::IntoArguments<'q, sqlx::sqlite::Sqlite> for SqlxValues { Value::TimeDateTimeWithTimeZone(t) => { let _ = args.add(t); } + #[cfg(feature = "with-jiff")] + Value::JiffDate(j) => { + let _ = args.add(j.map(|j| jiff_sqlx::ToSqlx::to_sqlx(j))); + } + #[cfg(feature = "with-jiff")] + Value::JiffTime(j) => { + let _ = args.add(j.map(|j| jiff_sqlx::ToSqlx::to_sqlx(j))); + } + #[cfg(feature = "with-jiff")] + Value::JiffDateTime(j) => { + let _ = args.add(j.map(|j| jiff_sqlx::ToSqlx::to_sqlx(j))); + } + #[cfg(feature = "with-jiff")] + Value::JiffTimestamp(j) => { + let _ = args.add(j.map(|j| jiff_sqlx::ToSqlx::to_sqlx(j))); + } + #[cfg(feature = "with-jiff")] + Value::JiffZoned(_) => { + unimplemented!("no support by jiff-sqlx"); + } #[cfg(feature = "with-uuid")] Value::Uuid(uuid) => { let _ = args.add(uuid); diff --git a/src/value.rs b/src/value.rs index 4ebc7e052..87a40f39c 100644 --- a/src/value.rs +++ b/src/value.rs @@ -14,7 +14,7 @@ use chrono::{DateTime, FixedOffset, Local, NaiveDate, NaiveDateTime, NaiveTime, use time::{OffsetDateTime, PrimitiveDateTime}; #[cfg(feature = "with-jiff")] -use jiff::{Timestamp, Zoned}; +use jiff::{Timestamp as JiffTimestamp, Zoned as JiffZoned}; #[cfg(feature = "with-rust_decimal")] use rust_decimal::Decimal; @@ -284,11 +284,11 @@ pub enum Value { #[cfg(feature = "with-jiff")] #[cfg_attr(docsrs, doc(cfg(feature = "with-jiff")))] - JiffTimestamp(Option), + JiffTimestamp(Option), #[cfg(feature = "with-jiff")] #[cfg_attr(docsrs, doc(cfg(feature = "with-jiff")))] - JiffZoned(Option), + JiffZoned(Option), #[cfg(feature = "with-uuid")] #[cfg_attr(docsrs, doc(cfg(feature = "with-uuid")))] @@ -561,12 +561,12 @@ impl Value { #[cfg(feature = "with-jiff")] #[cfg_attr(docsrs, doc(cfg(feature = "with-jiff")))] - Self::JiffTimestamp(_) => Self::JiffTimestamp(Some(Timestamp::UNIX_EPOCH)), + Self::JiffTimestamp(_) => Self::JiffTimestamp(Some(JiffTimestamp::UNIX_EPOCH)), #[cfg(feature = "with-jiff")] #[cfg_attr(docsrs, doc(cfg(feature = "with-jiff")))] Self::JiffZoned(_) => Self::JiffZoned(Some( - Timestamp::UNIX_EPOCH.to_zoned(jiff::tz::TimeZone::UTC), + JiffTimestamp::UNIX_EPOCH.to_zoned(jiff::tz::TimeZone::UTC), )), #[cfg(feature = "with-uuid")]