Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions sea-query-sqlx/src/sqlx_any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ impl<'q> sqlx::IntoArguments<'q, sqlx::any::Any> for SqlxValues {
let _ = args.add(Value::TimeDateTime(t).time_as_naive_utc_in_string());
}
#[cfg(feature = "with-time")]
Value::TimeDateTimeUtc(t) => {
let _ = args.add(Value::TimeDateTimeUtc(t).time_as_naive_utc_in_string());
}
#[cfg(feature = "with-time")]
Value::TimeDateTimeWithTimeZone(t) => {
let _ =
args.add(Value::TimeDateTimeWithTimeZone(t).time_as_naive_utc_in_string());
Expand Down
4 changes: 4 additions & 0 deletions sea-query-sqlx/src/sqlx_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ impl sqlx::IntoArguments<'_, sqlx::mysql::MySql> for SqlxValues {
let _ = args.add(t);
}
#[cfg(feature = "with-time")]
Value::TimeDateTimeUtc(t) => {
let _ = args.add(t.map(sqlx::types::time::OffsetDateTime::from));
}
#[cfg(feature = "with-time")]
Value::TimeDateTimeWithTimeZone(t) => {
let _ = args.add(t);
}
Expand Down
13 changes: 13 additions & 0 deletions sea-query-sqlx/src/sqlx_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ impl sqlx::IntoArguments<'_, sqlx::postgres::Postgres> for SqlxValues {
let _ = args.add(t);
}
#[cfg(feature = "with-time")]
Value::TimeDateTimeUtc(t) => {
let _ = args.add(t.map(sqlx::types::time::OffsetDateTime::from));
}
#[cfg(feature = "with-time")]
Value::TimeDateTimeWithTimeZone(t) => {
let _ = args.add(t);
}
Expand Down Expand Up @@ -273,6 +277,15 @@ impl sqlx::IntoArguments<'_, sqlx::postgres::Postgres> for SqlxValues {
let _ = args.add(value);
}
#[cfg(feature = "with-time")]
ArrayType::TimeDateTimeUtc => {
let value: Option<Vec<time::UtcDateTime>> = Value::Array(ty, v)
.expect("This Value::Array should consist of Value::TimeDateTimeUtc");
// temprorary, until sqlx supports UtcDateTime
let value: Option<Vec<time::OffsetDateTime>> = value
.map(|vec| vec.into_iter().map(time::OffsetDateTime::from).collect());
let _ = args.add(value);
}
#[cfg(feature = "with-time")]
ArrayType::TimeDateTimeWithTimeZone => {
let value: Option<Vec<time::OffsetDateTime>> = Value::Array(ty, v).expect(
"This Value::Array should consist of Value::TimeDateTimeWithTimeZone",
Expand Down
5 changes: 4 additions & 1 deletion sea-query-sqlx/src/sqlx_sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ impl<'q> sqlx::IntoArguments<'q, sqlx::sqlite::Sqlite> for SqlxValues {
let _ = args.add(t);
}
#[cfg(feature = "with-time")]
Value::TimeDateTimeUtc(t) => {
let _ = args.add(t.map(sqlx::types::time::OffsetDateTime::from));
}
#[cfg(feature = "with-time")]
Value::TimeDateTimeWithTimeZone(t) => {
let _ = args.add(t);
}
Expand All @@ -100,7 +104,6 @@ impl<'q> sqlx::IntoArguments<'q, sqlx::sqlite::Sqlite> for SqlxValues {
}
#[cfg(feature = "with-bigdecimal")]
Value::BigDecimal(big_decimal) => {
use sea_query::prelude::bigdecimal::ToPrimitive;
let _ = args.add(big_decimal.map(|d| d.to_string()));
}
#[cfg(feature = "with-json")]
Expand Down
8 changes: 8 additions & 0 deletions src/backend/query_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1199,6 +1199,8 @@ pub trait QueryBuilder:
#[cfg(feature = "with-time")]
Value::TimeDateTime(None) => buf.write_str("NULL")?,
#[cfg(feature = "with-time")]
Value::TimeDateTimeUtc(None) => buf.write_str("NULL")?,
#[cfg(feature = "with-time")]
Value::TimeDateTimeWithTimeZone(None) => buf.write_str("NULL")?,
#[cfg(feature = "with-jiff")]
Value::JiffDate(None) => buf.write_str("NULL")?,
Expand Down Expand Up @@ -1315,6 +1317,12 @@ pub trait QueryBuilder:
buf.write_str("'")?;
}
#[cfg(feature = "with-time")]
Value::TimeDateTimeUtc(Some(v)) => {
buf.write_str("'")?;
buf.write_str(&v.format(time_format::FORMAT_DATETIME_TZ).unwrap())?;
buf.write_str("'")?;
}
#[cfg(feature = "with-time")]
Value::TimeDateTimeWithTimeZone(Some(v)) => {
buf.write_str("'")?;
buf.write_str(&v.format(time_format::FORMAT_DATETIME_TZ).unwrap())?;
Expand Down
16 changes: 16 additions & 0 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ pub enum ArrayType {
#[cfg_attr(docsrs, doc(cfg(feature = "with-time")))]
TimeDateTime,

#[cfg(feature = "with-time")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-time")))]
TimeDateTimeUtc,

#[cfg(feature = "with-time")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-time")))]
TimeDateTimeWithTimeZone,
Expand Down Expand Up @@ -246,6 +250,10 @@ pub enum Value {
#[cfg_attr(docsrs, doc(cfg(feature = "with-time")))]
TimeDateTime(Option<PrimitiveDateTime>),

#[cfg(feature = "with-time")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-time")))]
TimeDateTimeUtc(Option<time::UtcDateTime>),

#[cfg(feature = "with-time")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-time")))]
TimeDateTimeWithTimeZone(Option<OffsetDateTime>),
Expand Down Expand Up @@ -409,6 +417,10 @@ impl Value {
#[cfg_attr(docsrs, doc(cfg(feature = "with-time")))]
Self::TimeDateTime(_) => Self::TimeDateTime(None),

#[cfg(feature = "with-time")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-time")))]
Self::TimeDateTimeUtc(_) => Self::TimeDateTimeUtc(None),

#[cfg(feature = "with-time")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-time")))]
Self::TimeDateTimeWithTimeZone(_) => Self::TimeDateTimeWithTimeZone(None),
Expand Down Expand Up @@ -535,6 +547,10 @@ impl Value {
#[cfg_attr(docsrs, doc(cfg(feature = "with-time")))]
Self::TimeDateTime(_) => Self::TimeDateTime(Some(PrimitiveDateTime::MIN)),

#[cfg(feature = "with-time")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-time")))]
Self::TimeDateTimeUtc(_) => Self::TimeDateTimeUtc(Some(time::UtcDateTime::MIN)),

#[cfg(feature = "with-time")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-time")))]
Self::TimeDateTimeWithTimeZone(_) => {
Expand Down
2 changes: 2 additions & 0 deletions src/value/hashable_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ impl Hash for Value {
#[cfg(feature = "with-time")]
Value::TimeDateTime(primitive_date_time) => primitive_date_time.hash(state),
#[cfg(feature = "with-time")]
Value::TimeDateTimeUtc(utc_date_time) => utc_date_time.hash(state),
#[cfg(feature = "with-time")]
Value::TimeDateTimeWithTimeZone(offset_date_time) => offset_date_time.hash(state),

#[cfg(feature = "with-jiff")]
Expand Down
2 changes: 1 addition & 1 deletion src/value/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub use serde_json::{self, Value as Json};
pub use chrono::{self, DateTime, FixedOffset, Local, NaiveDate, NaiveDateTime, NaiveTime, Utc};

#[cfg(feature = "with-time")]
pub use time::{self, OffsetDateTime, PrimitiveDateTime};
pub use time::{self, OffsetDateTime, PrimitiveDateTime, UtcDateTime};

#[cfg(feature = "with-jiff")]
pub use jiff::{self, Timestamp, Zoned};
Expand Down
3 changes: 3 additions & 0 deletions src/value/with_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ impl NotU8 for PrimitiveDateTime {}
#[cfg(feature = "with-time")]
impl NotU8 for OffsetDateTime {}

#[cfg(feature = "with-time")]
impl NotU8 for UtcDateTime {}

#[cfg(feature = "with-rust_decimal")]
impl NotU8 for Decimal {}

Expand Down
2 changes: 2 additions & 0 deletions src/value/with_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ pub fn sea_value_to_json_value(value: &Value) -> Json {
#[cfg(feature = "with-time")]
Value::TimeDateTime(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
#[cfg(feature = "with-time")]
Value::TimeDateTimeUtc(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
#[cfg(feature = "with-time")]
Value::TimeDateTimeWithTimeZone(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
#[cfg(feature = "with-jiff")]
Value::JiffDate(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
Expand Down
59 changes: 55 additions & 4 deletions src/value/with_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,27 @@ impl DateLikeValue for time::Date {}
impl TimeLikeValue for time::Time {}
impl DateTimeLikeValue for time::PrimitiveDateTime {}
impl DateTimeLikeValue for time::OffsetDateTime {}
impl DateTimeLikeValue for time::UtcDateTime {}

impl DateLikeValueNullable for Option<time::Date> {}
impl TimeLikeValueNullable for Option<time::Time> {}
impl DateTimeLikeValueNullable for Option<time::PrimitiveDateTime> {}
impl DateTimeLikeValueNullable for Option<time::OffsetDateTime> {}
impl DateTimeLikeValueNullable for Option<time::UtcDateTime> {}

impl From<OffsetDateTime> for Value {
fn from(v: OffsetDateTime) -> Value {
impl From<time::OffsetDateTime> for Value {
fn from(v: time::OffsetDateTime) -> Value {
Value::TimeDateTimeWithTimeZone(Some(v))
}
}

impl Nullable for OffsetDateTime {
impl Nullable for time::OffsetDateTime {
fn null() -> Value {
Value::TimeDateTimeWithTimeZone(None)
}
}

impl ValueType for OffsetDateTime {
impl ValueType for time::OffsetDateTime {
fn try_from(v: Value) -> Result<Self, ValueTypeErr> {
match v {
Value::TimeDateTimeWithTimeZone(Some(x)) => Ok(x),
Expand All @@ -47,6 +49,39 @@ impl ValueType for OffsetDateTime {
}
}

impl From<time::UtcDateTime> for Value {
fn from(v: time::UtcDateTime) -> Value {
Value::TimeDateTimeUtc(Some(v))
}
}

impl Nullable for time::UtcDateTime {
fn null() -> Value {
Value::TimeDateTimeUtc(None)
}
}

impl ValueType for time::UtcDateTime {
fn try_from(v: Value) -> Result<Self, ValueTypeErr> {
match v {
Value::TimeDateTimeUtc(Some(x)) => Ok(x),
_ => Err(ValueTypeErr),
}
}

fn type_name() -> String {
stringify!(UtcDateTime).to_owned()
}

fn array_type() -> ArrayType {
ArrayType::TimeDateTimeUtc
}

fn column_type() -> ColumnType {
ColumnType::TimestampWithTimeZone
}
}

impl Value {
pub fn is_time_date(&self) -> bool {
matches!(self, Self::TimeDate(_))
Expand Down Expand Up @@ -86,6 +121,19 @@ impl Value {
}
}

impl Value {
pub fn is_time_date_time_utc(&self) -> bool {
matches!(self, Self::TimeDateTimeUtc(_))
}

pub fn as_ref_time_date_time_utc(&self) -> Option<&time::UtcDateTime> {
match self {
Self::TimeDateTimeUtc(v) => v.as_ref(),
_ => panic!("not Value::TimeDateTimeUtc"),
}
}
}

impl Value {
pub fn is_time_date_time_with_time_zone(&self) -> bool {
matches!(self, Self::TimeDateTimeWithTimeZone(_))
Expand All @@ -111,6 +159,9 @@ impl Value {
Self::TimeDateTime(v) => v
.as_ref()
.and_then(|v| v.format(time_format::FORMAT_DATETIME).ok()),
Self::TimeDateTimeUtc(v) => v.as_ref().and_then(|v| {
v.format(time_format::FORMAT_DATETIME_TZ).ok()
}),
Self::TimeDateTimeWithTimeZone(v) => v.as_ref().and_then(|v| {
v.to_offset(time::macros::offset!(UTC))
.format(time_format::FORMAT_DATETIME_TZ)
Expand Down
Loading