Skip to content

Commit 80d4da6

Browse files
authored
Support jiff (#941)
<!-- Thank you for contributing to this project! If you need any help please feel free to contact us on Discord: https://discord.com/invite/uCPdDXzbdv Or, mention our core members by typing `@GitHub_Handle` on any issue / PR Add some test cases! It help reviewers to understand the behaviour and prevent it to be broken in the future. --> ## PR Info <!-- mention the related issue --> - Closes #851 <!-- is this PR depends on other PR? (if applicable) --> - Dependencies: - <!-- PR link --> <!-- any PR depends on this PR? (if applicable) --> - Dependents: - <!-- PR link --> ## New Features - [ ] <!-- what are the new features? --> ## Bug Fixes - [ ] <!-- if it fixes a bug, please provide a brief analysis of the original bug --> ## Breaking Changes - [ ] <!-- any change in behaviour or method signature? is it backward compatible? --> ## Changes - [ ] <!-- any other non-breaking changes to the codebase -->
1 parent 19b2783 commit 80d4da6

File tree

6 files changed

+319
-2
lines changed

6 files changed

+319
-2
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ rust_decimal = { version = "1", default-features = false, optional = true }
3838
bigdecimal = { version = "0.4", default-features = false, optional = true }
3939
uuid = { version = "1", default-features = false, optional = true }
4040
time = { version = "0.3.36", default-features = false, optional = true, features = ["macros", "formatting"] }
41+
jiff = { version = "0.2.15", default-features = false, optional = true, features = ["std", "perf-inline"] }
4142
ipnetwork = { version = "0.20", default-features = false, optional = true }
4243
mac_address = { version = "1.1", default-features = false, optional = true }
4344
ordered-float = { version = "4.6", default-features = false, optional = true }
@@ -67,6 +68,7 @@ with-rust_decimal = ["rust_decimal"]
6768
with-bigdecimal = ["bigdecimal"]
6869
with-uuid = ["uuid"]
6970
with-time = ["time"]
71+
jiff = ["dep:jiff"]
7072
with-ipnetwork = ["ipnetwork"]
7173
with-mac_address = ["mac_address"]
7274
tests-cfg = []
@@ -91,6 +93,7 @@ all-types = [
9193
"with-bigdecimal",
9294
"with-uuid",
9395
"with-time",
96+
"jiff",
9497
"with-ipnetwork",
9598
"with-mac_address",
9699
]

src/backend/query_builder.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,16 @@ pub trait QueryBuilder:
10811081
Value::TimeDateTime(None) => write!(s, "NULL").unwrap(),
10821082
#[cfg(feature = "with-time")]
10831083
Value::TimeDateTimeWithTimeZone(None) => write!(s, "NULL").unwrap(),
1084+
#[cfg(feature = "jiff")]
1085+
Value::JiffDate(None) => write!(s, "NULL").unwrap(),
1086+
#[cfg(feature = "jiff")]
1087+
Value::JiffTime(None) => write!(s, "NULL").unwrap(),
1088+
#[cfg(feature = "jiff")]
1089+
Value::JiffDateTime(None) => write!(s, "NULL").unwrap(),
1090+
#[cfg(feature = "jiff")]
1091+
Value::JiffTimestamp(None) => write!(s, "NULL").unwrap(),
1092+
#[cfg(feature = "jiff")]
1093+
Value::JiffZoned(None) => write!(s, "NULL").unwrap(),
10841094
#[cfg(feature = "with-rust_decimal")]
10851095
Value::Decimal(None) => write!(s, "NULL").unwrap(),
10861096
#[cfg(feature = "with-bigdecimal")]
@@ -1152,6 +1162,30 @@ pub trait QueryBuilder:
11521162
v.format(time_format::FORMAT_DATETIME_TZ).unwrap()
11531163
)
11541164
.unwrap(),
1165+
// Jiff date and time dosen't need format string
1166+
// The default behavior is what we want
1167+
#[cfg(feature = "jiff")]
1168+
Value::JiffDate(Some(v)) => write!(s, "'{v}'").unwrap(),
1169+
#[cfg(feature = "jiff")]
1170+
Value::JiffTime(Some(v)) => write!(s, "'{v}'").unwrap(),
1171+
// Both JiffDateTime and JiffTimestamp map to timestamp
1172+
#[cfg(feature = "jiff")]
1173+
Value::JiffDateTime(Some(v)) => {
1174+
use crate::with_jiff::JIFF_DATE_TIME_FMT_STR;
1175+
write!(s, "'{}'", v.strftime(JIFF_DATE_TIME_FMT_STR)).unwrap()
1176+
}
1177+
#[cfg(feature = "jiff")]
1178+
Value::JiffTimestamp(Some(v)) => {
1179+
use crate::with_jiff::JIFF_TIMESTAMP_FMT_STR;
1180+
write!(s, "'{}'", v.strftime(JIFF_TIMESTAMP_FMT_STR)).unwrap()
1181+
}
1182+
#[cfg(feature = "jiff")]
1183+
Value::JiffZoned(Some(v)) => {
1184+
// Zoned map to timestamp with timezone
1185+
1186+
use crate::with_jiff::JIFF_ZONE_FMT_STR;
1187+
write!(s, "'{}'", v.strftime(JIFF_ZONE_FMT_STR)).unwrap()
1188+
}
11551189
#[cfg(feature = "with-rust_decimal")]
11561190
Value::Decimal(Some(v)) => write!(s, "{v}").unwrap(),
11571191
#[cfg(feature = "with-bigdecimal")]

src/value.rs

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ use chrono::{DateTime, FixedOffset, Local, NaiveDate, NaiveDateTime, NaiveTime,
1313
#[cfg(feature = "with-time")]
1414
use time::{OffsetDateTime, PrimitiveDateTime};
1515

16+
#[cfg(feature = "jiff")]
17+
use jiff::{Timestamp, Zoned};
18+
1619
#[cfg(feature = "with-rust_decimal")]
1720
use rust_decimal::Decimal;
1821

@@ -62,6 +65,10 @@ pub mod time_format;
6265
#[cfg_attr(docsrs, doc(cfg(feature = "with-time")))]
6366
mod with_time;
6467

68+
#[cfg(feature = "jiff")]
69+
#[cfg_attr(docsrs, doc(cfg(feature = "jiff")))]
70+
pub(crate) mod with_jiff;
71+
6572
#[cfg(feature = "with-rust_decimal")]
6673
#[cfg_attr(docsrs, doc(cfg(feature = "with-rust_decimal")))]
6774
mod with_rust_decimal;
@@ -152,6 +159,26 @@ pub enum ArrayType {
152159
#[cfg_attr(docsrs, doc(cfg(feature = "with-time")))]
153160
TimeDateTimeWithTimeZone,
154161

162+
#[cfg(feature = "jiff")]
163+
#[cfg_attr(docsrs, doc(cfg(feature = "jiff")))]
164+
JiffDate,
165+
166+
#[cfg(feature = "jiff")]
167+
#[cfg_attr(docsrs, doc(cfg(feature = "jiff")))]
168+
JiffTime,
169+
170+
#[cfg(feature = "jiff")]
171+
#[cfg_attr(docsrs, doc(cfg(feature = "jiff")))]
172+
JiffDateTime,
173+
174+
#[cfg(feature = "jiff")]
175+
#[cfg_attr(docsrs, doc(cfg(feature = "jiff")))]
176+
JiffTimestamp,
177+
178+
#[cfg(feature = "jiff")]
179+
#[cfg_attr(docsrs, doc(cfg(feature = "jiff")))]
180+
JiffZoned,
181+
155182
#[cfg(feature = "with-uuid")]
156183
#[cfg_attr(docsrs, doc(cfg(feature = "with-uuid")))]
157184
Uuid,
@@ -243,6 +270,26 @@ pub enum Value {
243270
#[cfg_attr(docsrs, doc(cfg(feature = "with-time")))]
244271
TimeDateTimeWithTimeZone(Option<OffsetDateTime>),
245272

273+
#[cfg(feature = "jiff")]
274+
#[cfg_attr(docsrs, doc(cfg(feature = "jiff")))]
275+
JiffDate(Option<jiff::civil::Date>),
276+
277+
#[cfg(feature = "jiff")]
278+
#[cfg_attr(docsrs, doc(cfg(feature = "jiff")))]
279+
JiffTime(Option<jiff::civil::Time>),
280+
281+
#[cfg(feature = "jiff")]
282+
#[cfg_attr(docsrs, doc(cfg(feature = "jiff")))]
283+
JiffDateTime(Option<jiff::civil::DateTime>),
284+
285+
#[cfg(feature = "jiff")]
286+
#[cfg_attr(docsrs, doc(cfg(feature = "jiff")))]
287+
JiffTimestamp(Option<Timestamp>),
288+
289+
#[cfg(feature = "jiff")]
290+
#[cfg_attr(docsrs, doc(cfg(feature = "jiff")))]
291+
JiffZoned(Option<Zoned>),
292+
246293
#[cfg(feature = "with-uuid")]
247294
#[cfg_attr(docsrs, doc(cfg(feature = "with-uuid")))]
248295
Uuid(Option<Uuid>),
@@ -279,8 +326,8 @@ pub enum Value {
279326
pub const VALUE_SIZE: usize = check_value_size();
280327

281328
const fn check_value_size() -> usize {
282-
if std::mem::size_of::<Value>() > 32 {
283-
panic!("the size of Value shouldn't be greater than 32 bytes")
329+
if std::mem::size_of::<Value>() > 120 {
330+
panic!("the size of Value shouldn't be greater than 120 bytes")
284331
}
285332
std::mem::size_of::<Value>()
286333
}
@@ -374,6 +421,26 @@ impl Value {
374421
#[cfg_attr(docsrs, doc(cfg(feature = "with-time")))]
375422
Self::TimeDateTimeWithTimeZone(_) => Self::TimeDateTimeWithTimeZone(None),
376423

424+
#[cfg(feature = "jiff")]
425+
#[cfg_attr(docsrs, doc(cfg(feature = "jiff")))]
426+
Self::JiffDate(_) => Self::JiffDate(None),
427+
428+
#[cfg(feature = "jiff")]
429+
#[cfg_attr(docsrs, doc(cfg(feature = "jiff")))]
430+
Self::JiffTime(_) => Self::JiffTime(None),
431+
432+
#[cfg(feature = "jiff")]
433+
#[cfg_attr(docsrs, doc(cfg(feature = "jiff")))]
434+
Self::JiffDateTime(_) => Self::JiffDateTime(None),
435+
436+
#[cfg(feature = "jiff")]
437+
#[cfg_attr(docsrs, doc(cfg(feature = "jiff")))]
438+
Self::JiffTimestamp(_) => Self::JiffTimestamp(None),
439+
440+
#[cfg(feature = "jiff")]
441+
#[cfg_attr(docsrs, doc(cfg(feature = "jiff")))]
442+
Self::JiffZoned(_) => Self::JiffZoned(None),
443+
377444
#[cfg(feature = "with-uuid")]
378445
#[cfg_attr(docsrs, doc(cfg(feature = "with-uuid")))]
379446
Self::Uuid(_) => Self::Uuid(None),
@@ -478,6 +545,30 @@ impl Value {
478545
Self::TimeDateTimeWithTimeZone(Some(OffsetDateTime::UNIX_EPOCH))
479546
}
480547

548+
#[cfg(feature = "jiff")]
549+
#[cfg_attr(docsrs, doc(cfg(feature = "jiff")))]
550+
Self::JiffDate(_) => Self::JiffDate(Some(jiff::civil::date(1970, 1, 1))),
551+
552+
#[cfg(feature = "jiff")]
553+
#[cfg_attr(docsrs, doc(cfg(feature = "jiff")))]
554+
Self::JiffTime(_) => Self::JiffTime(Some(jiff::civil::time(0, 0, 0, 0))),
555+
556+
#[cfg(feature = "jiff")]
557+
#[cfg_attr(docsrs, doc(cfg(feature = "jiff")))]
558+
Self::JiffDateTime(_) => {
559+
Self::JiffDateTime(Some(jiff::civil::date(1970, 1, 1).at(0, 0, 0, 0)))
560+
}
561+
562+
#[cfg(feature = "jiff")]
563+
#[cfg_attr(docsrs, doc(cfg(feature = "jiff")))]
564+
Self::JiffTimestamp(_) => Self::JiffTimestamp(Some(Timestamp::UNIX_EPOCH)),
565+
566+
#[cfg(feature = "jiff")]
567+
#[cfg_attr(docsrs, doc(cfg(feature = "jiff")))]
568+
Self::JiffZoned(_) => Self::JiffZoned(Some(
569+
Timestamp::UNIX_EPOCH.to_zoned(jiff::tz::TimeZone::UTC),
570+
)),
571+
481572
#[cfg(feature = "with-uuid")]
482573
#[cfg_attr(docsrs, doc(cfg(feature = "with-uuid")))]
483574
Self::Uuid(_) => Self::Uuid(Some(Default::default())),

src/value/hashable_value.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ impl PartialEq for Value {
4848
#[cfg(feature = "with-time")]
4949
(Self::TimeDateTimeWithTimeZone(l), Self::TimeDateTimeWithTimeZone(r)) => l == r,
5050

51+
#[cfg(feature = "jiff")]
52+
(Self::JiffDate(l), Self::JiffDate(r)) => l == r,
53+
#[cfg(feature = "jiff")]
54+
(Self::JiffTime(l), Self::JiffTime(r)) => l == r,
55+
#[cfg(feature = "jiff")]
56+
(Self::JiffDateTime(l), Self::JiffDateTime(r)) => l == r,
57+
#[cfg(feature = "jiff")]
58+
(Self::JiffTimestamp(l), Self::JiffTimestamp(r)) => l == r,
59+
#[cfg(feature = "jiff")]
60+
(Self::JiffZoned(l), Self::JiffZoned(r)) => l == r,
61+
5162
#[cfg(feature = "with-uuid")]
5263
(Self::Uuid(l), Self::Uuid(r)) => l == r,
5364

@@ -122,6 +133,17 @@ impl Hash for Value {
122133
#[cfg(feature = "with-time")]
123134
Value::TimeDateTimeWithTimeZone(offset_date_time) => offset_date_time.hash(state),
124135

136+
#[cfg(feature = "jiff")]
137+
Value::JiffDate(date) => date.hash(state),
138+
#[cfg(feature = "jiff")]
139+
Value::JiffTime(time) => time.hash(state),
140+
#[cfg(feature = "jiff")]
141+
Value::JiffDateTime(datetime) => datetime.hash(state),
142+
#[cfg(feature = "jiff")]
143+
Value::JiffTimestamp(timestamp) => timestamp.hash(state),
144+
#[cfg(feature = "jiff")]
145+
Value::JiffZoned(zoned) => zoned.hash(state),
146+
125147
#[cfg(feature = "with-uuid")]
126148
Value::Uuid(uuid) => uuid.hash(state),
127149

0 commit comments

Comments
 (0)