Skip to content

Commit

Permalink
Supports time along with with chrono (SeaQL#267)
Browse files Browse the repository at this point in the history
* with-time

* fmt

* fix CI

* Revert CI changes

* Add `chrono` & `time` version of datetime `Value`

* cargo fmt

* Fixup

* Examples with `time` crate

* Fixup

Co-authored-by: Bastian Schubert <[email protected]>
  • Loading branch information
billy1624 and Bastian Schubert authored Mar 15, 2022
1 parent 519f458 commit 1cadf7e
Show file tree
Hide file tree
Showing 27 changed files with 913 additions and 184 deletions.
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.2", 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_2"]
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"]

[[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 @@ -50,7 +50,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
1 change: 1 addition & 0 deletions examples/cockroach/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ enum Character {
}

#[derive(Debug)]
#[allow(dead_code)]
struct CharacterStruct {
id: i64,
character: String,
Expand Down
5 changes: 3 additions & 2 deletions examples/cockroach_json/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ edition = "2021"

[dependencies]
chrono = "^0"
time = "^0.2"
postgres = "^0.19"
sea-query = { path = "../../", features = ["postgres", "postgres-chrono", "postgres-json", "postgres-uuid"] }
sea-query = { path = "../../", features = ["postgres", "postgres-chrono", "postgres-json", "postgres-uuid", "postgres-time"] }
# 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"] }
# sea-query = { version = "^0", features = ["postgres", "postgres-chrono", "postgres-json", "postgres-time"] }
serde_json = "^1"
59 changes: 51 additions & 8 deletions examples/cockroach_json/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use chrono::{NaiveDate, NaiveDateTime};
use postgres::{Client, NoTls, Row};
use sea_query::{ColumnDef, Iden, Order, PostgresDriver, PostgresQueryBuilder, Query, Table};
use time::{date, time, PrimitiveDateTime};

fn main() {
let mut client = Client::connect("postgresql://root:@localhost/query", NoTls).unwrap();
Expand Down Expand Up @@ -33,7 +34,7 @@ fn main() {
println!("Create table document: {:?}\n", result);

// Create
let document = DocumentStruct {
let document_chrono = DocumentStructChrono {
id: 1,
json_field: serde_json::json! {{
"a": 25.0,
Expand All @@ -45,12 +46,32 @@ fn main() {
}},
timestamp: NaiveDate::from_ymd(2020, 1, 1).and_hms(2, 2, 2),
};
let document_time = DocumentStructTime {
id: 2,
json_field: serde_json::json! {{
"a": 25.0,
"b": "whatever",
"c": {
"another": "object",
"bla": 1
}
}},
timestamp: date!(2020 - 01 - 01).with_time(time!(2:2:2)),
};
let (sql, values) = Query::insert()
.into_table(Document::Table)
.columns(vec![Document::JsonField, Document::Timestamp])
.values_panic(vec![
serde_json::to_value(document.json_field).unwrap().into(),
document.timestamp.into(),
serde_json::to_value(document_chrono.json_field)
.unwrap()
.into(),
document_chrono.timestamp.into(),
])
.values_panic(vec![
serde_json::to_value(document_time.json_field)
.unwrap()
.into(),
document_time.timestamp.into(),
])
.build(PostgresQueryBuilder);

Expand All @@ -68,8 +89,11 @@ fn main() {

let rows = client.query(sql.as_str(), &values.as_params()).unwrap();
println!("Select one from document:");
for row in rows.into_iter() {
let item = DocumentStruct::from(row);
for row in rows.iter() {
let item = DocumentStructChrono::from(row);
println!("{:?}", item);

let item = DocumentStructTime::from(row);
println!("{:?}", item);
}
println!();
Expand All @@ -84,14 +108,33 @@ enum Document {
}

#[derive(Debug)]
struct DocumentStruct {
#[allow(dead_code)]
struct DocumentStructChrono {
id: i64,
json_field: serde_json::Value,
timestamp: NaiveDateTime,
}

impl From<Row> for DocumentStruct {
fn from(row: Row) -> Self {
#[derive(Debug)]
#[allow(dead_code)]
struct DocumentStructTime {
id: i64,
json_field: serde_json::Value,
timestamp: PrimitiveDateTime,
}

impl From<&Row> for DocumentStructChrono {
fn from(row: &Row) -> Self {
Self {
id: row.get("id"),
json_field: row.get("json_field"),
timestamp: row.get("timestamp"),
}
}
}

impl From<&Row> for DocumentStructTime {
fn from(row: &Row) -> Self {
Self {
id: row.get("id"),
json_field: row.get("json_field"),
Expand Down
1 change: 1 addition & 0 deletions examples/postgres/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ enum Character {
}

#[derive(Debug)]
#[allow(dead_code)]
struct CharacterStruct {
id: i32,
character: String,
Expand Down
3 changes: 3 additions & 0 deletions examples/postgres_json/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2021"

[dependencies]
chrono = "^0"
time = "^0.2"
uuid = { version = "^0", features = ["serde", "v4"] }
serde_json = "^1"
rust_decimal = { version = "^1" }
Expand All @@ -16,6 +17,7 @@ sea-query = { path = "../../", features = [
"postgres-rust_decimal",
"postgres-uuid",
"postgres-array",
"postgres-time",
] }
# NOTE: if you are copying this example into your own project, use the following line instead:
# sea-query = { version = "^0", features = [
Expand All @@ -24,4 +26,5 @@ sea-query = { path = "../../", features = [
# "postgres-json",
# "postgres-rust_decimal",
# "postgres-uuid",
# "postgres-time",
# ] }
86 changes: 74 additions & 12 deletions examples/postgres_json/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use chrono::{DateTime, FixedOffset, NaiveDate, NaiveDateTime};
use postgres::{Client, NoTls, Row};
use rust_decimal::Decimal;
use sea_query::{ColumnDef, Iden, Order, PostgresDriver, PostgresQueryBuilder, Query, Table};
use time::{date, offset, time, OffsetDateTime, PrimitiveDateTime};
use uuid::Uuid;

fn main() {
Expand Down Expand Up @@ -39,7 +40,7 @@ fn main() {
println!("Create table document: {:?}\n", result);

// Create
let document = DocumentStruct {
let document_chrono = DocumentStructChrono {
id: 1,
uuid: Uuid::new_v4(),
json_field: serde_json::json! {{
Expand All @@ -56,6 +57,26 @@ fn main() {
decimal: Decimal::from_i128_with_scale(3141i128, 3),
array: vec![3, 4, 5, 6],
};
let document_time = DocumentStructTime {
id: 2,
uuid: Uuid::new_v4(),
json_field: serde_json::json! {{
"a": 25.0,
"b": "whatever",
"c": {
"another": "object",
"bla": 1
}
}},
timestamp: date!(2020 - 1 - 1).with_time(time!(2:2:2)),
timestamp_with_time_zone: date!(2020 - 01 - 01)
.with_time(time!(02:02:02))
.assume_utc()
.to_offset(offset!(+8)),
decimal: Decimal::from_i128_with_scale(3141i128, 3),
array: vec![3, 4, 5, 6],
};

let (sql, values) = Query::insert()
.into_table(Document::Table)
.columns(vec![
Expand All @@ -67,12 +88,24 @@ fn main() {
Document::Array,
])
.values_panic(vec![
document.uuid.into(),
serde_json::to_value(document.json_field).unwrap().into(),
document.timestamp.into(),
document.timestamp_with_time_zone.into(),
document.decimal.into(),
document.array.into(),
document_chrono.uuid.into(),
serde_json::to_value(document_chrono.json_field)
.unwrap()
.into(),
document_chrono.timestamp.into(),
document_chrono.timestamp_with_time_zone.into(),
document_chrono.decimal.into(),
document_chrono.array.into(),
])
.values_panic(vec![
document_time.uuid.into(),
serde_json::to_value(document_time.json_field)
.unwrap()
.into(),
document_time.timestamp.into(),
document_time.timestamp_with_time_zone.into(),
document_time.decimal.into(),
document_time.array.into(),
])
.build(PostgresQueryBuilder);

Expand All @@ -98,8 +131,11 @@ fn main() {

let rows = client.query(sql.as_str(), &values.as_params()).unwrap();
println!("Select one from document:");
for row in rows.into_iter() {
let item = DocumentStruct::from(row);
for row in rows.iter() {
let item = DocumentStructChrono::from(row);
println!("{:?}", item);

let item = DocumentStructTime::from(row);
println!("{:?}", item);
}
println!();
Expand All @@ -118,7 +154,8 @@ enum Document {
}

#[derive(Debug)]
struct DocumentStruct {
#[allow(dead_code)]
struct DocumentStructChrono {
id: i32,
uuid: Uuid,
json_field: serde_json::Value,
Expand All @@ -128,8 +165,33 @@ struct DocumentStruct {
array: Vec<i32>,
}

impl From<Row> for DocumentStruct {
fn from(row: Row) -> Self {
#[derive(Debug)]
#[allow(dead_code)]
struct DocumentStructTime {
id: i32,
uuid: Uuid,
json_field: serde_json::Value,
timestamp: PrimitiveDateTime,
timestamp_with_time_zone: OffsetDateTime,
decimal: Decimal,
array: Vec<i32>,
}

impl From<&Row> for DocumentStructChrono {
fn from(row: &Row) -> Self {
Self {
id: row.get("id"),
uuid: row.get("uuid"),
json_field: row.get("json_field"),
timestamp: row.get("timestamp"),
timestamp_with_time_zone: row.get("timestamp_with_time_zone"),
decimal: row.get("decimal"),
array: row.get("array"),
}
}
}
impl From<&Row> for DocumentStructTime {
fn from(row: &Row) -> Self {
Self {
id: row.get("id"),
uuid: row.get("uuid"),
Expand Down
4 changes: 4 additions & 0 deletions examples/rusqlite/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@ edition = "2021"

[dependencies]
chrono = { version = "^0" }
time = { version = "^0.2" }
serde_json = { version = "^1" }
uuid = { version = "^0", features = ["serde", "v4"] }
sea-query = { path = "../../", features = [
"rusqlite",
"with-chrono",
"with-json",
"with-uuid",
"with-time",
] }
# NOTE: if you are copying this example into your own project, use the following line instead:
# sea-query = { version = "^0", features = [
# "rusqlite",
# "with-chrono",
# "with-json",
# "with-uuid",
# "with-time",
# ] }

[dependencies.rusqlite]
Expand All @@ -28,4 +31,5 @@ features = [
"chrono",
"serde_json",
"uuid",
"time",
]
Loading

0 comments on commit 1cadf7e

Please sign in to comment.