Skip to content

Commit b694619

Browse files
committed
jiff-sqlx: add integration crate for SQLx
This PR adds a new `jiff-sqlx` crate. It defines wrapper types for `Timestamp`, `DateTime`, `Date`, `Time` and `Span`. For each wrapper type, the SQLx encoding traits are implemented. (Except, with `Span`, only the decoding trait is implemented.) This is similar to #141, but organizes things a bit differently. This also comes with SQLite support. MySQL support is missing since it seems, at present, to require exposing APIs in SQLx for a correct implementation. This initial implementation also omits `Zoned` entirely. I've left a comment in the source code explaining why. The quick summary is that, at least for PostgreSQL, I don't see a way to provide support for it without either silently losing data (the time zone) or just storing it as an RFC 9557 timestamp in a `TEXT` field. The downside of the latter is that it doesn't use PostgreSQL native datetime types. (Becuase we can't. Because PostgreSQL doesn't support storing anything other than civil time and timestamps with respect to its datetime types.) I do personally lean toward just using RFC 9557 as a `TEXT` type, but I'd like to collect real use cases first to make sure that's the right way to go. Ref #50, Closes #141 Ref launchbadge/sqlx#3487
1 parent 6544ed4 commit b694619

File tree

19 files changed

+1105
-2
lines changed

19 files changed

+1105
-2
lines changed

.github/workflows/ci.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,24 @@ jobs:
124124
# - name: Run integration tests
125125
# run: cargo test --test integration
126126

127+
# This job tests that examples outside the workspace build.
128+
#
129+
# These are outside the workspace because their dependency graphs are
130+
# absolutely ludicrous.
131+
examples:
132+
runs-on: ubuntu-latest
133+
steps:
134+
- name: Checkout repository
135+
uses: actions/checkout@v4
136+
- name: Install Rust
137+
uses: dtolnay/rust-toolchain@master
138+
with:
139+
toolchain: stable
140+
- name: Build sqlx-postgres example
141+
run: cargo run --manifest-path ./examples/sqlx-postgres/Cargo.toml
142+
- name: Run sqlx-sqlite example
143+
run: cargo run --manifest-path ./examples/sqlx-sqlite/Cargo.toml
144+
127145
# Generic testing for most cross targets. Some get special treatment in
128146
# other jobs.
129147
cross:

.vim/coc-settings.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"rust-analyzer.linkedProjects": [
33
"bench/Cargo.toml",
4+
"examples/sqlx-postgres/Cargo.toml",
5+
"examples/sqlx-sqlite/Cargo.toml",
46
"Cargo.toml"
57
]
68
}

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ include = [
3232
members = [
3333
"crates/jiff-cli",
3434
"crates/jiff-icu",
35+
"crates/jiff-sqlx",
3536
"crates/jiff-tzdb",
3637
"crates/jiff-tzdb-platform",
3738
"examples/*",
3839
]
40+
exclude = ["examples/sqlx-postgres", "examples/sqlx-sqlite"]
3941

4042
# Features are documented in the "Crate features" section of the crate docs:
4143
# https://docs.rs/jiff/*/#crate-features

crates/jiff-icu/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ license = "Unlicense OR MIT"
66
homepage = "https://github.com/BurntSushi/jiff/tree/master/crates/jiff-icu"
77
repository = "https://github.com/BurntSushi/jiff"
88
documentation = "https://docs.rs/jiff-icu"
9-
description = "The entire Time Zone Database embedded into your binary."
9+
description = "Conversion routines for Jiff and ICU4X."
1010
categories = ["date-and-time"]
11-
keywords = ["date", "time", "temporal", "zone", "iana"]
11+
keywords = ["date", "time", "temporal", "zone", "icu"]
1212
workspace = "../.."
1313
edition = "2021"
1414
rust-version = "1.70"

crates/jiff-sqlx/COPYING

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This project is dual-licensed under the Unlicense and MIT licenses.
2+
3+
You may use this code under the terms of either license.

crates/jiff-sqlx/Cargo.toml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[package]
2+
name = "jiff-sqlx"
3+
version = "0.0.1" #:version
4+
authors = ["Andrew Gallant <[email protected]>"]
5+
license = "Unlicense OR MIT"
6+
homepage = "https://github.com/BurntSushi/jiff/tree/master/crates/jiff-sqlx"
7+
repository = "https://github.com/BurntSushi/jiff"
8+
documentation = "https://docs.rs/jiff-sqlx"
9+
description = "Integration for Jiff in sqlx."
10+
categories = ["date-and-time"]
11+
keywords = ["date", "time", "jiff", "sqlx", "zone"]
12+
workspace = "../.."
13+
edition = "2021"
14+
rust-version = "1.70"
15+
include = ["/*.rs", "/*.dat", "COPYING", "LICENSE-MIT", "UNLICENSE"]
16+
17+
[lib]
18+
name = "jiff_sqlx"
19+
bench = false
20+
path = "src/lib.rs"
21+
22+
[features]
23+
default = []
24+
postgres = ["dep:sqlx-postgres"]
25+
sqlite = ["dep:sqlx-sqlite"]
26+
27+
[dependencies]
28+
jiff = { path = "../..", default-features = false }
29+
sqlx-core = { version = "0.8.0", default-features = false }
30+
sqlx-postgres = { version = "0.8.0", default-features = false, optional = true }
31+
sqlx-sqlite = { version = "0.8.0", default-features = false, optional = true }
32+
33+
[dev-dependencies]
34+
jiff = { path = "../..", default-features = true }

crates/jiff-sqlx/LICENSE-MIT

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Andrew Gallant
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

crates/jiff-sqlx/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
jiff-icu
2+
========
3+
WIP
4+
5+
### Documentation
6+
7+
https://docs.rs/jiff-icu

crates/jiff-sqlx/UNLICENSE

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
This is free and unencumbered software released into the public domain.
2+
3+
Anyone is free to copy, modify, publish, use, compile, sell, or
4+
distribute this software, either in source code form or as a compiled
5+
binary, for any purpose, commercial or non-commercial, and by any
6+
means.
7+
8+
In jurisdictions that recognize copyright laws, the author or authors
9+
of this software dedicate any and all copyright interest in the
10+
software to the public domain. We make this dedication for the benefit
11+
of the public at large and to the detriment of our heirs and
12+
successors. We intend this dedication to be an overt act of
13+
relinquishment in perpetuity of all present and future rights to this
14+
software under copyright law.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22+
OTHER DEALINGS IN THE SOFTWARE.
23+
24+
For more information, please refer to <http://unlicense.org/>

crates/jiff-sqlx/src/lib.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*!
2+
This crate provides integration points for [Jiff](jiff) and [SQLx][sqlx].
3+
4+
Examples can be found in the
5+
[examples directory of the Jiff repository][examples].
6+
7+
# Organization
8+
9+
This crates defines several types that wrap corresponding types in Jiff. Each
10+
wrapper type provides implementations of traits found in SQLx. For most types,
11+
these are the [`sqlx_core::types::Type`], [`sqlx_core::decode::Decode`] and
12+
[`sqlx_core::encode::Encode`] traits.
13+
14+
The intended workflow is to use these wrapper types within your wire types for
15+
encoding and decoding data from databases such as PostgreSQL. The wrapper types
16+
own the logic for encoding and decoding the data in database specific formats.
17+
18+
In order to the minimize the annoyance of wrapper types, the following
19+
conveniences are afforded:
20+
21+
* A [`ToSqlx`] trait is provided. Several Jiff types implement this trait. The
22+
trait provides easy conversion to the corresponding wrapper type in this crate.
23+
* A concrete `to_jiff` method is provided on each wrapper type. For example,
24+
[`Timestamp::to_jiff`]. This method is the reverse of `ToSqlx`. This converts
25+
from the wrapper type to the corresponding Jiff type.
26+
* There are `From` trait implementations from the wrapper type to the
27+
corresponding Jiff type, and vice versa.
28+
29+
# Database support
30+
31+
At present, both PostgreSQL and SQLite are supported.
32+
33+
Ideally, MySQL support would be present too, but it
34+
[appears impossible until SQLx exposes some APIs][sqlx-mysql-bunk].
35+
36+
# Future
37+
38+
This crate exists because there are generally only three ways to implement
39+
the necessary traits in SQLx:
40+
41+
1. Make Jiff depend on SQLx, and implement the corresponding traits where
42+
Jiff's types are defined.
43+
2. Make SQLx depend on Jiff, and implement the corresponding traits where the
44+
traits are defined.
45+
3. Make a crate like this one with types that wrap Jiff's types, and implements
46+
the corresponding traits for the wrapper types.
47+
48+
This was done because it seems inappropriate for a "lower level" crate like
49+
Jiff to depend on SQLx. And while it might be appropriate for SQLx to optionally
50+
depend on Jiff (like it does for [`chrono`] or [`time`]), at time of writing,
51+
Jiff is still early in its life. It's totally reasonable to wait for it to
52+
mature. Plus, the thought of three different datetime integrations is,
53+
admittedly, tough to stomach.
54+
55+
In the future, it may be prudent for this crate to be upstreamed into SQLx
56+
itself.
57+
58+
[sqlx]: https://docs.rs/sqlx/0.8
59+
[examples]: https://github.com/BurntSushi/jiff/tree/master/examples/uptime
60+
[`chrono`]: https://docs.rs/chrono
61+
[`time`]: https://docs.rs/time
62+
[sqlx-mysql-bunk]: https://github.com/launchbadge/sqlx/issues/3487#issuecomment-2641843693
63+
*/
64+
65+
#![deny(missing_docs)]
66+
67+
pub use self::wrappers::{Date, DateTime, Span, Time, Timestamp, ToSqlx};
68+
69+
#[cfg(feature = "postgres")]
70+
mod postgres;
71+
#[cfg(feature = "sqlite")]
72+
mod sqlite;
73+
mod wrappers;

0 commit comments

Comments
 (0)