-
-
Notifications
You must be signed in to change notification settings - Fork 193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added PostgreSQL Interval value variant #709
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,7 @@ time = { version = "0.3", default-features = false, optional = true, features = | |
ipnetwork = { version = "0.20", default-features = false, optional = true } | ||
mac_address = { version = "1.1", default-features = false, optional = true } | ||
ordered-float = { version = "3.4", default-features = false, optional = true } | ||
sqlx = { git = "https://github.com/yasamoka/sqlx", branch = "pg-interval-hash", default-features = false, optional = true } | ||
|
||
[dev-dependencies] | ||
sea-query = { path = ".", features = ["tests-cfg"] } | ||
|
@@ -60,7 +61,7 @@ derive = ["sea-query-derive"] | |
attr = ["sea-query-attr"] | ||
hashable-value = ["derivative", "ordered-float"] | ||
postgres-array = [] | ||
postgres-interval = ["proc-macro2", "quote"] | ||
postgres-interval = ["proc-macro2", "quote", "sqlx/postgres"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this the preferred way of adding this feature dependency? |
||
thread-safe = [] | ||
with-chrono = ["chrono"] | ||
with-json = ["serde_json"] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ rust-version = "1.60" | |
|
||
[dependencies] | ||
sea-query = { version = "0.31", path = "..", default-features = false, features = ["thread-safe"] } | ||
sqlx = { version = "0.7", default-features = false, optional = true } | ||
sqlx = { git = "https://github.com/yasamoka/sqlx", branch = "pg-interval-hash", default-features = false, optional = true } | ||
serde_json = { version = "1", default-features = false, optional = true, features = ["std"] } | ||
chrono = { version = "0.4", default-features = false, optional = true, features = ["clock"] } | ||
rust_decimal = { version = "1", default-features = false, optional = true } | ||
|
@@ -42,6 +42,7 @@ with-time = ["sqlx?/time", "sea-query/with-time", "time"] | |
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"] | ||
postgres-interval = ["sqlx-postgres", "sqlx/chrono"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this the preferred way of adding this feature dependency? |
||
runtime-async-std-native-tls = ["sqlx?/runtime-async-std-native-tls"] | ||
runtime-async-std-rustls = ["sqlx?/runtime-async-std-rustls", ] | ||
runtime-actix-native-tls = ["sqlx?/runtime-tokio-native-tls"] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,6 +73,8 @@ impl sqlx::IntoArguments<'_, sqlx::mysql::MySql> for SqlxValues { | |
Value::ChronoDateTimeWithTimeZone(t) => { | ||
args.add(Value::ChronoDateTimeWithTimeZone(t).chrono_as_naive_utc_in_string()); | ||
} | ||
#[cfg(feature = "postgres-interval")] | ||
Value::Interval(_) => {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a better of doing this other than splitting the query builder or throwing an error? |
||
#[cfg(feature = "with-time")] | ||
Value::TimeDate(t) => { | ||
args.add(t.as_deref()); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,6 +73,8 @@ impl<'q> sqlx::IntoArguments<'q, sqlx::sqlite::Sqlite> for SqlxValues { | |
Value::ChronoDateTimeWithTimeZone(t) => { | ||
args.add(t.map(|t| *t)); | ||
} | ||
#[cfg(feature = "postgres-interval")] | ||
Value::Interval(_) => {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a better of doing this other than splitting the query builder or throwing an error? |
||
#[cfg(feature = "with-time")] | ||
Value::TimeDate(t) => { | ||
args.add(t.map(|t| *t)); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1002,6 +1002,8 @@ pub trait QueryBuilder: | |
Value::ChronoDateTimeLocal(None) => write!(s, "NULL").unwrap(), | ||
#[cfg(feature = "with-chrono")] | ||
Value::ChronoDateTimeWithTimeZone(None) => write!(s, "NULL").unwrap(), | ||
#[cfg(feature = "postgres-interval")] | ||
Value::Interval(None) => write!(s, "NULL").unwrap(), | ||
#[cfg(feature = "with-time")] | ||
Value::TimeDate(None) => write!(s, "NULL").unwrap(), | ||
#[cfg(feature = "with-time")] | ||
|
@@ -1060,6 +1062,34 @@ pub trait QueryBuilder: | |
Value::ChronoDateTimeWithTimeZone(Some(v)) => { | ||
write!(s, "'{}'", v.format("%Y-%m-%d %H:%M:%S %:z")).unwrap() | ||
} | ||
#[cfg(feature = "postgres-interval")] | ||
Value::Interval(Some(v)) => { | ||
let mut space = false; | ||
|
||
write!(s, "'").unwrap(); | ||
|
||
if v.months > 0 { | ||
write!(s, "{} MONTHS", v.days).unwrap(); | ||
space = true; | ||
} | ||
|
||
if v.days > 0 { | ||
if space { | ||
write!(s, " ").unwrap(); | ||
} | ||
write!(s, "{} DAYS", v.days).unwrap(); | ||
space = true; | ||
} | ||
|
||
if v.microseconds > 0 { | ||
if space { | ||
write!(s, " ").unwrap(); | ||
} | ||
write!(s, "{} MICROSECONDS", v.microseconds).unwrap(); | ||
} | ||
|
||
write!(s, "'::interval").unwrap(); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shall I make the units abbreviated or is this better for clarity when inspecting the built SQL statement? |
||
#[cfg(feature = "with-time")] | ||
Value::TimeDate(Some(v)) => { | ||
write!(s, "'{}'", v.format(time_format::FORMAT_DATE).unwrap()).unwrap() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,8 @@ use std::net::IpAddr; | |
use mac_address::MacAddress; | ||
|
||
use crate::{BlobSize, ColumnType, CommonSqlQueryBuilder, QueryBuilder}; | ||
#[cfg(feature = "postgres-interval")] | ||
use sqlx::postgres::types::PgInterval; | ||
|
||
/// [`Value`] types variant for Postgres array | ||
#[derive(Clone, Debug, Eq, PartialEq, Hash)] | ||
|
@@ -79,6 +81,9 @@ pub enum ArrayType { | |
#[cfg_attr(docsrs, doc(cfg(feature = "with-chrono")))] | ||
ChronoDateTimeWithTimeZone, | ||
|
||
#[cfg(feature = "postgres-interval")] | ||
Interval, | ||
|
||
#[cfg(feature = "with-time")] | ||
#[cfg_attr(docsrs, doc(cfg(feature = "with-time")))] | ||
TimeDate, | ||
|
@@ -202,6 +207,9 @@ pub enum Value { | |
#[cfg_attr(docsrs, doc(cfg(feature = "with-chrono")))] | ||
ChronoDateTimeWithTimeZone(Option<Box<DateTime<FixedOffset>>>), | ||
|
||
#[cfg(feature = "postgres-interval")] | ||
Interval(Option<Box<PgInterval>>), | ||
|
||
#[cfg(feature = "with-time")] | ||
#[cfg_attr(docsrs, doc(cfg(feature = "with-time")))] | ||
TimeDate(Option<Box<time::Date>>), | ||
|
@@ -540,6 +548,13 @@ mod with_chrono { | |
} | ||
} | ||
|
||
#[cfg(feature = "postgres-interval")] | ||
impl From<PgInterval> for Value { | ||
fn from(v: PgInterval) -> Self { | ||
Value::Interval(Some(Box::new(v))) | ||
} | ||
} | ||
|
||
impl Nullable for DateTime<Utc> { | ||
fn null() -> Value { | ||
Value::ChronoDateTimeUtc(None) | ||
|
@@ -594,6 +609,28 @@ mod with_chrono { | |
} | ||
} | ||
|
||
#[cfg(feature = "postgres-interval")] | ||
impl ValueType for PgInterval { | ||
fn try_from(v: Value) -> Result<Self, ValueTypeErr> { | ||
match v { | ||
Value::Interval(Some(x)) => Ok(*x), | ||
_ => Err(ValueTypeErr), | ||
} | ||
} | ||
|
||
fn type_name() -> String { | ||
stringify!(PgInterval).to_owned() | ||
} | ||
|
||
fn array_type() -> ArrayType { | ||
ArrayType::Interval | ||
} | ||
|
||
fn column_type() -> ColumnType { | ||
ColumnType::Interval(None, None) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do I pass to |
||
} | ||
} | ||
|
||
impl Nullable for DateTime<FixedOffset> { | ||
fn null() -> Value { | ||
Value::ChronoDateTimeWithTimeZone(None) | ||
|
@@ -805,6 +842,9 @@ pub mod with_array { | |
#[cfg(feature = "with-chrono")] | ||
impl<Tz> NotU8 for DateTime<Tz> where Tz: chrono::TimeZone {} | ||
|
||
#[cfg(feature = "postgres-interval")] | ||
impl NotU8 for PgInterval {} | ||
|
||
#[cfg(feature = "with-time")] | ||
impl NotU8 for time::Date {} | ||
|
||
|
@@ -1161,6 +1201,20 @@ impl Value { | |
} | ||
} | ||
|
||
#[cfg(feature = "postgres-interval")] | ||
impl Value { | ||
pub fn is_interval(&self) -> bool { | ||
matches!(self, Self::Interval(_)) | ||
} | ||
|
||
pub fn as_ref_interval(&self) -> Option<&PgInterval> { | ||
match self { | ||
Self::Interval(v) => box_to_opt_ref!(v), | ||
_ => panic!("not Value::Interval"), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "with-ipnetwork")] | ||
impl Value { | ||
pub fn is_ipnetwork(&self) -> bool { | ||
|
@@ -1423,6 +1477,8 @@ pub fn sea_value_to_json_value(value: &Value) -> Json { | |
Value::ChronoDateTimeUtc(_) => CommonSqlQueryBuilder.value_to_string(value).into(), | ||
#[cfg(feature = "with-chrono")] | ||
Value::ChronoDateTimeLocal(_) => CommonSqlQueryBuilder.value_to_string(value).into(), | ||
#[cfg(feature = "postgres-interval")] | ||
Value::Interval(_) => CommonSqlQueryBuilder.value_to_string(value).into(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this correct? |
||
#[cfg(feature = "with-time")] | ||
Value::TimeDate(_) => CommonSqlQueryBuilder.value_to_string(value).into(), | ||
#[cfg(feature = "with-time")] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was previously no dependency on SQLx. However,
PgInterval
is provided by SQLx. Is this fine?