Skip to content
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

upgrade time dependency version to 0.3 #341

Closed
wants to merge 7 commits into from
Closed
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
51 changes: 23 additions & 28 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,20 @@ jobs:
toolchain: stable
components: clippy
override: true


- uses: actions-rs/cargo@v1
with:
command: clippy
args: >
--all-targets
--features backend-mysql,backend-postgres,backend-sqlite,derive,attr,postgres,postgres-chrono,postgres-json,postgres-rust_decimal,postgres-bigdecimal,postgres-uuid,postgres-array,postgres-interval,postgres-time-0_2,rusqlite,sqlx-mysql,sqlx-postgres,sqlx-sqlite,thread-safe,with-chrono,with-json,with-rust_decimal,with-bigdecimal,with-uuid,with-time-0_2

- uses: actions-rs/cargo@v1
with:
command: clippy
args: >
--all-targets
--all
--features backend-mysql,backend-postgres,backend-sqlite,derive,attr,postgres,postgres-chrono,postgres-json,postgres-rust_decimal,postgres-bigdecimal,postgres-uuid,postgres-array,postgres-interval,postgres-time-0_3,rusqlite,sqlx-mysql,sqlx-postgres,sqlx-sqlite,thread-safe,with-chrono,with-json,with-rust_decimal,with-bigdecimal,with-uuid,with-time-0_3

build:
name: Build
Expand All @@ -44,38 +51,21 @@ jobs:

- uses: actions-rs/cargo@v1
with:
command: build
args: --no-default-features
command: install
args: cargo-hack

# By default, when using --each-feature, cargo hack will run with --all-features which is not possible
# considering with-time-0_2 and with-time-0_3 are exclusive. Excluding with-time-0_2 solves this but we
# need to check it afterward.
- uses: actions-rs/cargo@v1
with:
command: build
args: --all-features
command: hack
args: build --each-feature --exclude-features with-time-0_2

- uses: actions-rs/cargo@v1
with:
command: build
args: --features=thread-safe

- uses: actions-rs/cargo@v1
with:
command: build
args: --features=with-chrono

- uses: actions-rs/cargo@v1
with:
command: build
args: --features=with-json

- uses: actions-rs/cargo@v1
with:
command: build
args: --features=with-rust_decimal

- uses: actions-rs/cargo@v1
with:
command: build
args: --features=with-uuid
args: --features with-time-0_3

test:
name: Unit Test
Expand All @@ -92,7 +82,12 @@ jobs:
- uses: actions-rs/cargo@v1
with:
command: test
args: --all-features
args: --features backend-mysql,backend-postgres,backend-sqlite,derive,attr,postgres,postgres-chrono,postgres-json,postgres-rust_decimal,postgres-bigdecimal,postgres-uuid,postgres-array,postgres-interval,postgres-time-0_2,rusqlite,sqlx-mysql,sqlx-postgres,sqlx-sqlite,thread-safe,with-chrono,with-json,with-rust_decimal,with-bigdecimal,with-uuid,with-time-0_2

- uses: actions-rs/cargo@v1
with:
command: test
args: --features backend-mysql,backend-postgres,backend-sqlite,derive,attr,postgres,postgres-chrono,postgres-json,postgres-rust_decimal,postgres-bigdecimal,postgres-uuid,postgres-array,postgres-interval,postgres-time-0_3,rusqlite,sqlx-mysql,sqlx-postgres,sqlx-sqlite,thread-safe,with-chrono,with-json,with-rust_decimal,with-bigdecimal,with-uuid,with-time-0_3

derive-test:
name: Derive Tests
Expand Down
12 changes: 9 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ bigdecimal = { version = "^0.2", optional = true }
uuid = { version = "^0", optional = true }
proc-macro2 = { version = "1", optional = true }
quote = { version = "^1", optional = true }
time = { version = "^0.2", optional = true }
time-0_2 = { package = "time", version = "^0.2", optional = true }
# time-0_3 cannot be used due to a macro referencing `time`
time = { package = "time", version = "^0.3", optional = true, features = ["formatting", "macros", "parsing"] }

[dev-dependencies]
criterion = { version = "0.3", features = ["html_reports"] }
Expand All @@ -70,7 +72,9 @@ postgres-bigdecimal = ["with-bigdecimal"]
postgres-uuid = ["with-uuid", "postgres-types/with-uuid-0_8"]
postgres-array = ["postgres-types/array-impls", "sea-query-driver?/postgres-array"]
postgres-interval = ["proc-macro2", "quote"]
postgres-time = ["with-time", "postgres-types/with-time-0_2"]
postgres-time-0_2 = ["with-time-0_2", "postgres-types/with-time-0_2"]
postgres-time-0_3 = ["with-time-0_3", "postgres-types/with-time-0_3"]
postgres-time = ["postgres-time-0_3"]
rusqlite = ["sea-query-driver/rusqlite"]
sqlx-mysql = ["sea-query-driver/sqlx-mysql"]
sqlx-postgres = ["sea-query-driver/sqlx-postgres"]
Expand All @@ -81,7 +85,9 @@ with-json = ["serde_json", "sea-query-driver?/with-json"]
with-rust_decimal = ["rust_decimal", "sea-query-driver?/with-rust_decimal"]
with-bigdecimal = ["bigdecimal", "sea-query-driver?/with-bigdecimal"]
with-uuid = ["uuid", "sea-query-driver?/with-uuid"]
with-time = ["time", "sea-query-driver?/with-time"]
with-time-0_2 = ["time-0_2", "sea-query-driver?/with-time"]
with-time-0_3 = ["time", "sea-query-driver?/with-time"]
with-time = ["with-time-0_3"]

[[test]]
name = "test-derive"
Expand Down
2 changes: 1 addition & 1 deletion examples/cockroach_json/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
chrono = "^0"
time = "^0.2"
postgres = "^0.19"
sea-query = { path = "../../", features = ["postgres", "postgres-chrono", "postgres-json", "postgres-uuid", "postgres-time"] }
sea-query = { path = "../../", features = ["postgres", "postgres-chrono", "postgres-json", "postgres-uuid", "postgres-time-0_2"] }
# NOTE: if you are copying this example into your own project, use the following line instead:
# sea-query = { version = "^0", features = ["postgres", "postgres-chrono", "postgres-json", "postgres-time"] }
serde_json = "^1"
40 changes: 28 additions & 12 deletions src/backend/query_builder.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[cfg(any(feature = "with-time-0_3", feature = "with-time-0_2"))]
use crate::deps::time::{FORMAT_DATE, FORMAT_DATETIME, FORMAT_DATETIME_TZ, FORMAT_TIME};
use crate::*;
use std::ops::Deref;

Expand Down Expand Up @@ -1068,13 +1070,13 @@ pub trait QueryBuilder: QuotedBuilder {
Value::ChronoDateTimeLocal(None) => write!(s, "NULL").unwrap(),
#[cfg(feature = "with-chrono")]
Value::ChronoDateTimeWithTimeZone(None) => write!(s, "NULL").unwrap(),
#[cfg(feature = "with-time")]
#[cfg(any(feature = "with-time-0_3", feature = "with-time-0_2"))]
Value::TimeDate(None) => write!(s, "NULL").unwrap(),
#[cfg(feature = "with-time")]
#[cfg(any(feature = "with-time-0_3", feature = "with-time-0_2"))]
Value::TimeTime(None) => write!(s, "NULL").unwrap(),
#[cfg(feature = "with-time")]
#[cfg(any(feature = "with-time-0_3", feature = "with-time-0_2"))]
Value::TimeDateTime(None) => write!(s, "NULL").unwrap(),
#[cfg(feature = "with-time")]
#[cfg(any(feature = "with-time-0_3", feature = "with-time-0_2"))]
Value::TimeDateTimeWithTimeZone(None) => write!(s, "NULL").unwrap(),
#[cfg(feature = "with-rust_decimal")]
Value::Decimal(None) => write!(s, "NULL").unwrap(),
Expand Down Expand Up @@ -1130,17 +1132,31 @@ pub trait QueryBuilder: QuotedBuilder {
Value::ChronoDateTimeWithTimeZone(Some(v)) => {
write!(s, "\'{}\'", v.format("%Y-%m-%d %H:%M:%S %:z")).unwrap()
}
#[cfg(feature = "with-time")]
Value::TimeDate(Some(v)) => write!(s, "\'{}\'", v.format("%Y-%m-%d")).unwrap(),
#[cfg(feature = "with-time")]
Value::TimeTime(Some(v)) => write!(s, "\'{}\'", v.format("%H:%M:%S")).unwrap(),
#[cfg(feature = "with-time")]
#[cfg(feature = "with-time-0_3")]
Value::TimeDate(Some(v)) => {
write!(s, "\'{}\'", v.format(FORMAT_TIME).unwrap()).unwrap()
}
#[cfg(feature = "with-time-0_3")]
Value::TimeTime(Some(v)) => {
write!(s, "\'{}\'", v.format(FORMAT_DATE).unwrap()).unwrap()
}
#[cfg(feature = "with-time-0_3")]
Value::TimeDateTime(Some(v)) => {
write!(s, "\'{}\'", v.format("%Y-%m-%d %H:%M:%S")).unwrap()
write!(s, "\'{}\'", v.format(FORMAT_DATETIME).unwrap()).unwrap()
}
#[cfg(feature = "with-time-0_3")]
Value::TimeDateTimeWithTimeZone(Some(v)) => {
write!(s, "\'{}\'", v.format(FORMAT_DATETIME_TZ).unwrap()).unwrap()
}
#[cfg(feature = "with-time")]
#[cfg(feature = "with-time-0_2")]
Value::TimeDate(Some(v)) => write!(s, "\'{}\'", v.format(FORMAT_TIME)).unwrap(),
#[cfg(feature = "with-time-0_2")]
Value::TimeTime(Some(v)) => write!(s, "\'{}\'", v.format(FORMAT_DATE)).unwrap(),
#[cfg(feature = "with-time-0_2")]
Value::TimeDateTime(Some(v)) => write!(s, "\'{}\'", v.format(FORMAT_DATETIME)).unwrap(),
#[cfg(feature = "with-time-0_2")]
Value::TimeDateTimeWithTimeZone(Some(v)) => {
write!(s, "\'{}\'", v.format("%Y-%m-%d %H:%M:%S %z")).unwrap()
write!(s, "\'{}\'", v.format(FORMAT_DATETIME_TZ)).unwrap()
}
#[cfg(feature = "with-rust_decimal")]
Value::Decimal(Some(v)) => write!(s, "{}", v).unwrap(),
Expand Down
102 changes: 102 additions & 0 deletions src/deps.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#[cfg(all(feature = "with-time-0_3", feature = "with-time-0_2"))]
compile_error!("features `with-time-0_3` and `with-time-0_2` are mutually exclusive");

#[cfg(feature = "with-time-0_3")]
pub mod time {
pub use time::{Date, OffsetDateTime, PrimitiveDateTime, Time, UtcOffset};

use time::format_description::FormatItem;
use time::macros::format_description;

pub static FORMAT_TIME: &[FormatItem<'static>] = format_description!("[year]-[month]-[day]");
pub static FORMAT_DATE: &[FormatItem<'static>] =
format_description!("[hour]:[minute]:[second]");
pub static FORMAT_DATETIME: &[FormatItem<'static>] =
format_description!("[year]-[month]-[day] [hour]:[minute]:[second]");
pub static FORMAT_DATETIME_TZ: &[FormatItem<'static>] = format_description!(
"[year]-[month]-[day] [hour]:[minute]:[second] [offset_hour sign:mandatory][offset_minute]"
);

pub fn offset(hours: i8) -> UtcOffset {
UtcOffset::from_hms(hours, 0, 0).unwrap()
}

pub fn time(hour: u8, minute: u8, second: u8) -> Time {
Time::from_hms(hour, minute, second).unwrap()
}

pub fn date(year: i32, month: u8, day: u8) -> time::Date {
let month = time::Month::try_from(month).unwrap();
Date::from_calendar_date(year, month, day).unwrap()
}

pub fn datetime(
year: i32,
month: u8,
day: u8,
hour: u8,
minute: u8,
second: u8,
) -> PrimitiveDateTime {
date(year, month, day).with_time(time(hour, minute, second))
}

pub fn datetimetz(
year: i32,
month: u8,
day: u8,
hour: u8,
minute: u8,
second: u8,
offset_hours: i8,
) -> OffsetDateTime {
datetime(year, month, day, hour, minute, second).assume_offset(offset(offset_hours))
}
}

#[cfg(feature = "with-time-0_2")]
pub mod time {
pub use time_0_2::{Date, OffsetDateTime, PrimitiveDateTime, Time, UtcOffset};

pub static FORMAT_TIME: &str = "%Y-%m-%d";
pub static FORMAT_DATE: &str = "%H:%M:%S";
pub static FORMAT_DATETIME: &str = "%Y-%m-%d %H:%M:%S";
pub static FORMAT_DATETIME_TZ: &str = "%Y-%m-%d %H:%M:%S %z";

pub fn offset(hours: i8) -> UtcOffset {
UtcOffset::hours(hours)
}

pub fn time(hour: u8, minute: u8, second: u8) -> Time {
Time::try_from_hms(hour, minute, second).unwrap()
}

pub fn date(year: i32, month: u8, day: u8) -> Date {
Date::try_from_ymd(year, month, day).unwrap()
}

pub fn datetime(
year: i32,
month: u8,
day: u8,
hour: u8,
minute: u8,
second: u8,
) -> PrimitiveDateTime {
date(year, month, day).with_time(time(hour, minute, second))
}

pub fn datetimetz(
year: i32,
month: u8,
day: u8,
hour: u8,
minute: u8,
second: u8,
offset_hours: i8,
) -> OffsetDateTime {
datetime(year, month, day, hour, minute, second)
.assume_utc()
.to_offset(offset(offset_hours))
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,7 @@
)]

pub mod backend;
mod deps;
pub mod driver;
pub mod error;
pub mod expr;
Expand Down
3 changes: 3 additions & 0 deletions src/tests_cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ pub use std::fmt::Write as FmtWrite;
#[cfg(feature = "with-json")]
pub use serde_json::json;

#[cfg(any(feature = "with-time-0_3", feature = "with-time-0_2"))]
pub use crate::deps::time;

use crate::Iden;

/// Representation of a database table named `Character`.
Expand Down
Loading