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

Add "with-time" feature #256

Closed
wants to merge 3 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 30 additions & 5 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
toolchain: stable
components: clippy
override: true

- uses: actions-rs/cargo@v1
with:
command: clippy
Expand Down Expand Up @@ -50,7 +50,12 @@ jobs:
- uses: actions-rs/cargo@v1
with:
command: build
args: --all-features
args: cargo b --features "backend-mysql backend-postgres backend-sqlite bigdecimal bytes chrono default derive postgres postgres-array postgres-bigdecimal postgres-chrono postgres-interval postgres-json postgres-rust_decimal postgres-types postgres-uuid proc-macro2 quote rusqlite rust_decimal sea-query-derive serde_json sqlx-mysql sqlx-postgres sqlx-sqlite thread-safe uuid with-bigdecimal with-chrono with-json with-rust_decimal with-uuid"

- uses: actions-rs/cargo@v1
with:
command: build
args: cargo b --features "backend-mysql backend-postgres backend-sqlite bigdecimal bytes default derive postgres postgres-array postgres-bigdecimal postgres-interval postgres-json postgres-rust_decimal postgres-time postgres-types postgres-uuid proc-macro2 quote rusqlite rust_decimal sea-query-derive serde_json sqlx-mysql sqlx-postgres sqlx-sqlite thread-safe time uuid with-bigdecimal with-json with-rust_decimal with-time with-uuid"

- uses: actions-rs/cargo@v1
with:
Expand All @@ -62,6 +67,11 @@ jobs:
command: build
args: --features=with-chrono

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

- uses: actions-rs/cargo@v1
with:
command: build
Expand Down Expand Up @@ -92,7 +102,12 @@ jobs:
- uses: actions-rs/cargo@v1
with:
command: test
args: --all-features
args: cargo b --features "backend-mysql backend-postgres backend-sqlite bigdecimal bytes chrono default derive postgres postgres-array postgres-bigdecimal postgres-chrono postgres-interval postgres-json postgres-rust_decimal postgres-types postgres-uuid proc-macro2 quote rusqlite rust_decimal sea-query-derive serde_json sqlx-mysql sqlx-postgres sqlx-sqlite thread-safe uuid with-bigdecimal with-chrono with-json with-rust_decimal with-uuid"

- uses: actions-rs/cargo@v1
with:
command: test
args: cargo b --features "backend-mysql backend-postgres backend-sqlite bigdecimal bytes default derive postgres postgres-array postgres-bigdecimal postgres-interval postgres-json postgres-rust_decimal postgres-time postgres-types postgres-uuid proc-macro2 quote rusqlite rust_decimal sea-query-derive serde_json sqlx-mysql sqlx-postgres sqlx-sqlite thread-safe time uuid with-bigdecimal with-json with-rust_decimal with-time with-uuid"

derive-test:
name: Derive Tests
Expand All @@ -111,12 +126,22 @@ jobs:
- uses: actions-rs/cargo@v1
with:
command: build
args: --package sea-query-derive --all-features
args: --package sea-query-derive cargo b --features "backend-mysql backend-postgres backend-sqlite bigdecimal bytes chrono default derive postgres postgres-array postgres-bigdecimal postgres-chrono postgres-interval postgres-json postgres-rust_decimal postgres-types postgres-uuid proc-macro2 quote rusqlite rust_decimal sea-query-derive serde_json sqlx-mysql sqlx-postgres sqlx-sqlite thread-safe uuid with-bigdecimal with-chrono with-json with-rust_decimal with-uuid"

- uses: actions-rs/cargo@v1
with:
command: build
args: --package sea-query-derive cargo b --features "backend-mysql backend-postgres backend-sqlite bigdecimal bytes default derive postgres postgres-array postgres-bigdecimal postgres-interval postgres-json postgres-rust_decimal postgres-time postgres-types postgres-uuid proc-macro2 quote rusqlite rust_decimal sea-query-derive serde_json sqlx-mysql sqlx-postgres sqlx-sqlite thread-safe time uuid with-bigdecimal with-json with-rust_decimal with-time with-uuid"

- uses: actions-rs/cargo@v1
with:
command: test
args: --package sea-query-derive cargo b --features "backend-mysql backend-postgres backend-sqlite bigdecimal bytes chrono default derive postgres postgres-array postgres-bigdecimal postgres-chrono postgres-interval postgres-json postgres-rust_decimal postgres-types postgres-uuid proc-macro2 quote rusqlite rust_decimal sea-query-derive serde_json sqlx-mysql sqlx-postgres sqlx-sqlite thread-safe uuid with-bigdecimal with-chrono with-json with-rust_decimal with-uuid"

- uses: actions-rs/cargo@v1
with:
command: test
args: --package sea-query-derive --all-features
args: --package sea-query-derive cargo b --features "backend-mysql backend-postgres backend-sqlite bigdecimal bytes default derive postgres postgres-array postgres-bigdecimal postgres-interval postgres-json postgres-rust_decimal postgres-time postgres-types postgres-uuid proc-macro2 quote rusqlite rust_decimal sea-query-derive serde_json sqlx-mysql sqlx-postgres sqlx-sqlite thread-safe time uuid with-bigdecimal with-json with-rust_decimal with-time with-uuid"

sqlite:
name: SQLite
Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ 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.3", optional = true }

[dev-dependencies]
criterion = { version = "0.3", features = ["html_reports"] }
Expand All @@ -64,6 +65,7 @@ postgres-bigdecimal = ["with-bigdecimal"]
postgres-uuid = ["with-uuid", "postgres-types/with-uuid-0_8"]
postgres-array = ["postgres-types/array-impls"]
postgres-interval = ["proc-macro2", "quote"]
postgres-time = ["with-time", "postgres-types/with-time-0_3"]
rusqlite = []
sqlx-mysql = []
sqlx-postgres = []
Expand All @@ -74,6 +76,7 @@ with-json = ["serde_json"]
with-rust_decimal = ["rust_decimal"]
with-bigdecimal = ["bigdecimal"]
with-uuid = ["uuid"]
with-time = ["time", "time/macros", "time/formatting", "time/parsing"]

[[test]]
name = "test-derive"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Async support: `thread-safe` (use `Arc` inplace of `Rc`)

SQL dialect: `backend-mysql`, `backend-postgres`, `backend-sqlite`

Type support: `with-chrono`, `with-json`, `with-rust_decimal`, `with-bigdecimal`, `with-uuid`,
Type support: `with-chrono`, `with-time`, `with-json`, `with-rust_decimal`, `with-bigdecimal`, `with-uuid`,
`postgres-array`

Driver support: `sqlx-mysql`, `sqlx-postgres`, `sqlx-sqlite`,
Expand Down
46 changes: 46 additions & 0 deletions src/backend/query_builder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::*;
use std::ops::Deref;

#[cfg(feature = "with-time")]
use time::macros::format_description;

pub trait QueryBuilder: QuotedBuilder {
/// The type of placeholder the builder uses for values, and whether it is numbered.
fn placeholder(&self) -> (&str, bool) {
Expand Down Expand Up @@ -946,6 +949,18 @@ pub trait QueryBuilder: QuotedBuilder {
Value::DateTimeLocal(None) => write!(s, "NULL").unwrap(),
#[cfg(feature = "with-chrono")]
Value::DateTimeWithTimeZone(None) => write!(s, "NULL").unwrap(),
#[cfg(feature = "with-time")]
Value::Date(None) => write!(s, "NULL").unwrap(),
#[cfg(feature = "with-time")]
Value::Time(None) => write!(s, "NULL").unwrap(),
#[cfg(feature = "with-time")]
Value::DateTime(None) => write!(s, "NULL").unwrap(),
#[cfg(feature = "with-time")]
Value::DateTimeUtc(None) => write!(s, "NULL").unwrap(),
#[cfg(feature = "with-time")]
Value::DateTimeLocal(None) => write!(s, "NULL").unwrap(),
#[cfg(feature = "with-time")]
Value::DateTimeWithTimeZone(None) => write!(s, "NULL").unwrap(),
#[cfg(feature = "with-rust_decimal")]
Value::Decimal(None) => write!(s, "NULL").unwrap(),
#[cfg(feature = "with-bigdecimal")]
Expand Down Expand Up @@ -994,6 +1009,37 @@ pub trait QueryBuilder: QuotedBuilder {
Value::DateTimeWithTimeZone(Some(v)) => {
write!(s, "\'{}\'", v.format("%Y-%m-%d %H:%M:%S %:z").to_string()).unwrap()
}

#[cfg(feature = "with-time")]
Value::Date(Some(v)) => {
let format = format_description!("[year]-[month]-[day]");
write!(s, "\'{}\'", v.format(&format).unwrap()).unwrap()
}
#[cfg(feature = "with-time")]
Value::Time(Some(v)) => {
let format = format_description!("[hour]:[minute]:[second]");
write!(s, "\'{}\'", v.format(&format).unwrap()).unwrap()
}
#[cfg(feature = "with-time")]
Value::DateTime(Some(v)) => {
let format = format_description!("[year]-[month]-[day] [hour]:[minute]:[second]");
write!(s, "\'{}\'", v.format(&format).unwrap()).unwrap()
}
#[cfg(feature = "with-time")]
Value::DateTimeUtc(Some(v)) => {
let format = format_description!("[year]-[month]-[day] [hour]:[minute]:[second] [offset_hour sign:mandatory]:[offset_minute]");
write!(s, "\'{}\'", v.format(&format).unwrap()).unwrap()
}
#[cfg(feature = "with-time")]
Value::DateTimeLocal(Some(v)) => {
let format = format_description!("[year]-[month]-[day] [hour]:[minute]:[second] [offset_hour sign:mandatory]:[offset_minute]");
write!(s, "\'{}\'", v.format(&format).unwrap()).unwrap()
}
#[cfg(feature = "with-time")]
Value::DateTimeWithTimeZone(Some(v)) => {
let format = format_description!("[year]-[month]-[day] [hour]:[minute]:[second] [offset_hour sign:mandatory]:[offset_minute]");
write!(s, "\'{}\'", v.format(&format).unwrap()).unwrap()
}
#[cfg(feature = "with-rust_decimal")]
Value::Decimal(Some(v)) => write!(s, "{}", v).unwrap(),
#[cfg(feature = "with-bigdecimal")]
Expand Down
8 changes: 6 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
//!
//! SQL dialect: `backend-mysql`, `backend-postgres`, `backend-sqlite`
//!
//! Type support: `with-chrono`, `with-json`, `with-rust_decimal`, `with-bigdecimal`, `with-uuid`,
//! Type support: `with-chrono`, `with-time`, `with-json`, `with-rust_decimal`, `with-bigdecimal`, `with-uuid`,
//! `postgres-array`
//!
//! Driver support: `sqlx-mysql`, `sqlx-postgres`, `sqlx-sqlite`,
Expand Down Expand Up @@ -708,6 +708,10 @@
#![doc(
html_logo_url = "https://raw.githubusercontent.com/SeaQL/sea-query/master/docs/SeaQL icon dark.png"
)]

#[cfg(all(feature = "with-chrono", feature = "with-time"))]
compile_error!("feature \"with-chrono\" and feature \"with-time\" cannot be enabled at the same time");

pub mod backend;
pub mod driver;
pub mod error;
Expand Down Expand Up @@ -745,4 +749,4 @@ pub use types::*;
pub use value::*;

#[cfg(feature = "derive")]
pub use sea_query_derive::Iden;
pub use sea_query_derive::Iden;