-
-
Notifications
You must be signed in to change notification settings - Fork 194
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 all 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 |
---|---|---|
|
@@ -8,6 +8,8 @@ use ipnetwork::IpNetwork; | |
use mac_address::MacAddress; | ||
#[cfg(feature = "with-rust_decimal")] | ||
use rust_decimal::Decimal; | ||
#[cfg(feature = "postgres-interval")] | ||
use sea_query::types::PgInterval; | ||
#[cfg(feature = "with-json")] | ||
use serde_json::Value as Json; | ||
#[cfg(feature = "with-uuid")] | ||
|
@@ -105,6 +107,10 @@ impl sqlx::IntoArguments<'_, sqlx::postgres::Postgres> for SqlxValues { | |
Value::TimeDateTimeWithTimeZone(t) => { | ||
args.add(t.as_deref()); | ||
} | ||
#[cfg(feature = "postgres-interval")] | ||
Value::Interval(t) => { | ||
args.add(t.as_deref()); | ||
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. How would I implement |
||
} | ||
#[cfg(feature = "with-uuid")] | ||
Value::Uuid(uuid) => { | ||
args.add(uuid.as_deref()); | ||
|
@@ -277,6 +283,12 @@ impl sqlx::IntoArguments<'_, sqlx::postgres::Postgres> for SqlxValues { | |
); | ||
args.add(value); | ||
} | ||
#[cfg(feature = "postgres-interval")] | ||
ArrayType::Interval => { | ||
let value: Option<Vec<PgInterval>> = Value::Array(ty, v) | ||
.expect("This Value::Array should consist of Value::Interval"); | ||
args.add(value); | ||
} | ||
#[cfg(feature = "with-uuid")] | ||
ArrayType::Uuid => { | ||
let value: Option<Vec<Uuid>> = Value::Array(ty, v) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1010,6 +1010,8 @@ pub trait QueryBuilder: | |
Value::TimeDateTime(None) => write!(s, "NULL").unwrap(), | ||
#[cfg(feature = "with-time")] | ||
Value::TimeDateTimeWithTimeZone(None) => write!(s, "NULL").unwrap(), | ||
#[cfg(feature = "postgres-interval")] | ||
Value::Interval(None) => write!(s, "NULL").unwrap(), | ||
#[cfg(feature = "with-rust_decimal")] | ||
Value::Decimal(None) => write!(s, "NULL").unwrap(), | ||
#[cfg(feature = "with-bigdecimal")] | ||
|
@@ -1079,6 +1081,34 @@ pub trait QueryBuilder: | |
v.format(time_format::FORMAT_DATETIME_TZ).unwrap() | ||
) | ||
.unwrap(), | ||
#[cfg(feature = "postgres-interval")] | ||
Value::Interval(Some(v)) => { | ||
let mut space = false; | ||
|
||
write!(s, "'").unwrap(); | ||
|
||
if v.months > 0 { | ||
write!(s, "{} MONTHS", v.months).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-rust_decimal")] | ||
Value::Decimal(Some(v)) => write!(s, "{v}").unwrap(), | ||
#[cfg(feature = "with-bigdecimal")] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -95,6 +95,9 @@ pub enum ArrayType { | |
#[cfg_attr(docsrs, doc(cfg(feature = "with-time")))] | ||
TimeDateTimeWithTimeZone, | ||
|
||
#[cfg(feature = "postgres-interval")] | ||
Interval, | ||
|
||
#[cfg(feature = "with-uuid")] | ||
#[cfg_attr(docsrs, doc(cfg(feature = "with-uuid")))] | ||
Uuid, | ||
|
@@ -218,6 +221,9 @@ pub enum Value { | |
#[cfg_attr(docsrs, doc(cfg(feature = "with-time")))] | ||
TimeDateTimeWithTimeZone(Option<Box<OffsetDateTime>>), | ||
|
||
#[cfg(feature = "postgres-interval")] | ||
Interval(Option<Box<PgIntervalValue>>), | ||
|
||
#[cfg(feature = "with-uuid")] | ||
#[cfg_attr(docsrs, doc(cfg(feature = "with-uuid")))] | ||
Uuid(Option<Box<Uuid>>), | ||
|
@@ -502,6 +508,14 @@ impl ValueType for Cow<'_, str> { | |
type_to_box_value!(Vec<u8>, Bytes, Binary(BlobSize::Blob(None))); | ||
type_to_box_value!(String, String, String(None)); | ||
|
||
#[cfg(feature = "postgres-interval")] | ||
#[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)] | ||
pub struct PgIntervalValue { | ||
pub months: i32, | ||
pub days: i32, | ||
pub microseconds: i64, | ||
} | ||
|
||
#[cfg(feature = "with-json")] | ||
#[cfg_attr(docsrs, doc(cfg(feature = "with-json")))] | ||
mod with_json { | ||
|
@@ -681,6 +695,38 @@ mod with_time { | |
} | ||
} | ||
|
||
#[cfg(feature = "postgres-interval")] | ||
mod with_postgres_interval { | ||
use super::*; | ||
|
||
impl From<PgIntervalValue> for Value { | ||
fn from(v: PgIntervalValue) -> Self { | ||
Value::Interval(Some(Box::new(v))) | ||
} | ||
} | ||
|
||
impl ValueType for PgIntervalValue { | ||
fn try_from(v: Value) -> Result<Self, ValueTypeErr> { | ||
match v { | ||
Value::Interval(Some(x)) => Ok(*x), | ||
_ => Err(ValueTypeErr), | ||
} | ||
} | ||
|
||
fn type_name() -> String { | ||
stringify!(PgIntervalValue).to_owned() | ||
} | ||
|
||
fn array_type() -> ArrayType { | ||
ArrayType::Interval | ||
} | ||
|
||
fn column_type() -> ColumnType { | ||
ColumnType::Interval(None, None) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "with-rust_decimal")] | ||
#[cfg_attr(docsrs, doc(cfg(feature = "with-rust_decimal")))] | ||
mod with_rust_decimal { | ||
|
@@ -817,6 +863,9 @@ pub mod with_array { | |
#[cfg(feature = "with-time")] | ||
impl NotU8 for OffsetDateTime {} | ||
|
||
#[cfg(feature = "postgres-interval")] | ||
impl NotU8 for PgIntervalValue {} | ||
|
||
#[cfg(feature = "with-rust_decimal")] | ||
impl NotU8 for Decimal {} | ||
|
||
|
@@ -1095,6 +1144,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<&PgIntervalValue> { | ||
match self { | ||
Self::Interval(v) => box_to_opt_ref!(v), | ||
_ => panic!("not Value::Interval"), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "with-rust_decimal")] | ||
impl Value { | ||
pub fn is_decimal(&self) -> bool { | ||
|
@@ -1431,6 +1494,8 @@ pub fn sea_value_to_json_value(value: &Value) -> Json { | |
Value::TimeDateTime(_) => CommonSqlQueryBuilder.value_to_string(value).into(), | ||
#[cfg(feature = "with-time")] | ||
Value::TimeDateTimeWithTimeZone(_) => CommonSqlQueryBuilder.value_to_string(value).into(), | ||
#[cfg(feature = "postgres-interval")] | ||
Value::Interval(_) => CommonSqlQueryBuilder.value_to_string(value).into(), | ||
#[cfg(feature = "with-rust_decimal")] | ||
Value::Decimal(Some(v)) => { | ||
use rust_decimal::prelude::ToPrimitive; | ||
|
@@ -1855,6 +1920,116 @@ mod tests { | |
); | ||
} | ||
|
||
#[test] | ||
#[cfg(feature = "postgres-interval")] | ||
fn test_pginterval_value() { | ||
let interval = PgIntervalValue { | ||
months: 1, | ||
days: 2, | ||
microseconds: 300, | ||
}; | ||
let value: Value = interval.into(); | ||
let out: PgIntervalValue = value.unwrap(); | ||
assert_eq!(out, interval); | ||
} | ||
|
||
#[test] | ||
#[cfg(feature = "postgres-interval")] | ||
fn test_pginterval_query() { | ||
use crate::*; | ||
|
||
const VALUES: [(PgIntervalValue, &str); 10] = [ | ||
( | ||
PgIntervalValue { | ||
months: 0, | ||
days: 0, | ||
microseconds: 1, | ||
}, | ||
"1 MICROSECONDS", | ||
), | ||
( | ||
PgIntervalValue { | ||
months: 0, | ||
days: 0, | ||
microseconds: 100, | ||
}, | ||
"100 MICROSECONDS", | ||
), | ||
( | ||
PgIntervalValue { | ||
months: 0, | ||
days: 1, | ||
microseconds: 0, | ||
}, | ||
"1 DAYS", | ||
), | ||
( | ||
PgIntervalValue { | ||
months: 0, | ||
days: 2, | ||
microseconds: 0, | ||
}, | ||
"2 DAYS", | ||
), | ||
( | ||
PgIntervalValue { | ||
months: 0, | ||
days: 2, | ||
microseconds: 100, | ||
}, | ||
"2 DAYS 100 MICROSECONDS", | ||
), | ||
( | ||
PgIntervalValue { | ||
months: 1, | ||
days: 0, | ||
microseconds: 0, | ||
}, | ||
"1 MONTHS", | ||
), | ||
( | ||
PgIntervalValue { | ||
months: 2, | ||
days: 0, | ||
microseconds: 0, | ||
}, | ||
"2 MONTHS", | ||
), | ||
( | ||
PgIntervalValue { | ||
months: 2, | ||
days: 0, | ||
microseconds: 100, | ||
}, | ||
"2 MONTHS 100 MICROSECONDS", | ||
), | ||
( | ||
PgIntervalValue { | ||
months: 2, | ||
days: 2, | ||
microseconds: 0, | ||
}, | ||
"2 MONTHS 2 DAYS", | ||
), | ||
( | ||
PgIntervalValue { | ||
months: 2, | ||
days: 2, | ||
microseconds: 100, | ||
}, | ||
"2 MONTHS 2 DAYS 100 MICROSECONDS", | ||
), | ||
]; | ||
|
||
for (interval, formatted) in VALUES { | ||
let query = Query::select().expr(interval).to_owned(); | ||
assert_eq!( | ||
query.to_string(PostgresQueryBuilder), | ||
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 shall we do about |
||
format!("SELECT '{formatted}'::interval") | ||
); | ||
} | ||
} | ||
|
||
#[test] | ||
#[cfg(feature = "with-uuid")] | ||
fn test_uuid_value() { | ||
|
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.
Is this the preferred way of adding this feature dependency?