Skip to content

Commit 95f901a

Browse files
committed
Update to postgres 0.17, refactor chrono support, drop time support
- Updates integration and tests to postgres 0.17 - Moves chrono crate support to "with-chrono-0_4" to match postgres crate - Drops time crate support since it was dropped in postgres crate
1 parent 3e66a77 commit 95f901a

File tree

4 files changed

+60
-77
lines changed

4 files changed

+60
-77
lines changed

Cargo.toml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@ repository = "https://github.com/sfackler/rust-postgres-range"
88
documentation = "https://sfackler.github.io/rust-postgres-range/doc/v0.9.0/postgres_range"
99

1010
[features]
11-
with-time = ["time", "postgres-shared/with-time"]
12-
with-chrono = ["chrono", "postgres-shared/with-chrono"]
11+
with-chrono-0_4 = ["chrono-04", "postgres-types/with-chrono-0_4"]
1312

1413
[dependencies]
15-
time = { version = "0.1", optional = true }
16-
postgres-protocol = "0.3"
17-
chrono = { version = "0.4.0", optional = true }
18-
postgres-shared = "0.4.0"
14+
postgres-protocol = "0.5"
15+
postgres-types = "0.1"
16+
chrono-04 = { version = "0.4", package = "chrono", optional = true }
1917

2018
[dev-dependencies]
21-
postgres = { version = "0.15", features = ["with-time"] }
19+
postgres = { version = "0.17" }

src/chrono_04.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use chrono_04::{DateTime, NaiveDateTime, TimeZone};
2+
3+
use crate::{Normalizable, RangeBound, BoundSided};
4+
5+
impl<T> Normalizable for DateTime<T>
6+
where T: TimeZone {
7+
fn normalize<S>(bound: RangeBound<S, DateTime<T>>) -> RangeBound<S, DateTime<T>>
8+
where
9+
S: BoundSided,
10+
{
11+
bound
12+
}
13+
}
14+
15+
impl Normalizable for NaiveDateTime
16+
{
17+
fn normalize<S>(bound: RangeBound<S, NaiveDateTime>) -> RangeBound<S, NaiveDateTime>
18+
where
19+
S: BoundSided,
20+
{
21+
bound
22+
}
23+
}

src/impls.rs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
use std::error::Error;
2-
use postgres_shared::types::{FromSql, IsNull, Kind, ToSql, Type};
2+
use postgres_types::{FromSql, IsNull, Kind, ToSql, Type};
3+
use postgres_types::private::BytesMut;
34
use postgres_protocol::{self as protocol, types};
45

56
use {BoundSided, BoundType, Normalizable, Range, RangeBound};
67

7-
impl<T> FromSql for Range<T>
8+
impl<'a, T> FromSql<'a> for Range<T>
89
where
9-
T: PartialOrd + Normalizable + FromSql,
10+
T: PartialOrd + Normalizable + FromSql<'a>,
1011
{
11-
fn from_sql(ty: &Type, raw: &[u8]) -> Result<Range<T>, Box<Error + Sync + Send>> {
12+
fn from_sql(ty: &Type, raw: &'a [u8]) -> Result<Range<T>, Box<dyn Error + Sync + Send>> {
1213
let element_type = match ty.kind() {
1314
&Kind::Range(ref ty) => ty,
1415
_ => panic!("unexpected type {:?}", ty),
@@ -32,9 +33,9 @@ where
3233
}
3334
}
3435

35-
fn bound_from_sql<T, S>(bound: types::RangeBound<Option<&[u8]>>, ty: &Type) -> Result<Option<RangeBound<S, T>>, Box<Error + Sync + Send>>
36+
fn bound_from_sql<'a, T, S>(bound: types::RangeBound<Option<&'a [u8]>>, ty: &Type) -> Result<Option<RangeBound<S, T>>, Box<dyn Error + Sync + Send>>
3637
where
37-
T: PartialOrd + Normalizable + FromSql,
38+
T: PartialOrd + Normalizable + FromSql<'a>,
3839
S: BoundSided,
3940
{
4041
match bound {
@@ -60,7 +61,7 @@ impl<T> ToSql for Range<T>
6061
where
6162
T: PartialOrd + Normalizable + ToSql,
6263
{
63-
fn to_sql(&self, ty: &Type, buf: &mut Vec<u8>) -> Result<IsNull, Box<Error + Sync + Send>> {
64+
fn to_sql(&self, ty: &Type, buf: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
6465
let element_type = match ty.kind() {
6566
&Kind::Range(ref ty) => ty,
6667
_ => panic!("unexpected type {:?}", ty),
@@ -89,7 +90,7 @@ where
8990
to_sql_checked!();
9091
}
9192

92-
fn bound_to_sql<S, T>(bound: Option<&RangeBound<S, T>>, ty: &Type, buf: &mut Vec<u8>) -> Result<types::RangeBound<protocol::IsNull>, Box<Error + Sync + Send>>
93+
fn bound_to_sql<S, T>(bound: Option<&RangeBound<S, T>>, ty: &Type, buf: &mut BytesMut) -> Result<types::RangeBound<protocol::IsNull>, Box<dyn Error + Sync + Send>>
9394
where
9495
S: BoundSided,
9596
T: ToSql,
@@ -114,10 +115,10 @@ where
114115
mod test {
115116
use std::fmt;
116117

117-
use postgres::{Connection, TlsMode};
118+
use postgres::{Client, NoTls};
118119
use postgres::types::{FromSql, ToSql};
119-
#[cfg(feature = "with-time")]
120-
use time::{self, Timespec};
120+
#[cfg(feature = "with-chrono-0_4")]
121+
use chrono_04::{TimeZone, Utc, Duration};
121122

122123
macro_rules! test_range {
123124
($name:expr, $t:ty, $low:expr, $low_str:expr, $high:expr, $high_str:expr) => ({
@@ -140,16 +141,21 @@ mod test {
140141
})
141142
}
142143

143-
fn test_type<T: PartialEq + FromSql + ToSql, S: fmt::Display>(sql_type: &str, checks: &[(T, S)]) {
144-
let conn = Connection::connect("postgres://postgres@localhost", TlsMode::None).unwrap();
144+
145+
fn test_type<T, S>(sql_type: &str, checks: &[(T, S)])
146+
where for<'a>
147+
T: Sync + PartialEq + FromSql<'a> + ToSql,
148+
S: fmt::Display
149+
{
150+
let mut conn = Client::connect("postgres://postgres@localhost", NoTls).unwrap();
145151
for &(ref val, ref repr) in checks {
146152
let stmt = conn.prepare(&*format!("SELECT {}::{}", *repr, sql_type))
147153
.unwrap();
148-
let result = stmt.query(&[]).unwrap().iter().next().unwrap().get(0);
154+
let result = conn.query(&stmt, &[]).unwrap().iter().next().unwrap().get(0);
149155
assert!(val == &result);
150156

151157
let stmt = conn.prepare(&*format!("SELECT $1::{}", sql_type)).unwrap();
152-
let result = stmt.query(&[val]).unwrap().iter().next().unwrap().get(0);
158+
let result = conn.query(&stmt, &[val]).unwrap().iter().next().unwrap().get(0);
153159
assert!(val == &result);
154160
}
155161
}
@@ -164,25 +170,19 @@ mod test {
164170
test_range!("INT8RANGE", i64, 100i64, "100", 200i64, "200")
165171
}
166172

167-
#[cfg(feature = "with-time")]
168-
fn test_timespec_range_params(sql_type: &str) {
169-
fn t(time: &str) -> Timespec {
170-
time::strptime(time, "%Y-%m-%d").unwrap().to_timespec()
171-
}
172-
let low = "1970-01-01";
173-
let high = "1980-01-01";
174-
test_range!(sql_type, Timespec, t(low), low, t(high), high);
175-
}
176-
177173
#[test]
178-
#[cfg(feature = "with-time")]
174+
#[cfg(feature = "with-chrono-0_4")]
179175
fn test_tsrange_params() {
180-
test_timespec_range_params("TSRANGE");
176+
let low = Utc.timestamp(0, 0);
177+
let high = low + Duration::days(10);
178+
test_range!("TSRANGE", NaiveDateTime, low.naive_utc(), "1970-01-01", high.naive_utc(), "1970-01-11");
181179
}
182180

183181
#[test]
184-
#[cfg(feature = "with-time")]
182+
#[cfg(feature = "with-chrono-0_4")]
185183
fn test_tstzrange_params() {
186-
test_timespec_range_params("TSTZRANGE");
184+
let low = Utc.timestamp(0, 0);
185+
let high = low + Duration::days(10);
186+
test_range!("TSTZRANGE", DateTime<Utc>, low, "1970-01-01", high, "1970-01-11");
187187
}
188188
}

src/lib.rs

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,19 @@
33

44
extern crate postgres_protocol;
55
#[macro_use(to_sql_checked)]
6-
extern crate postgres_shared;
6+
extern crate postgres_types;
77

8-
#[cfg(feature = "with-time")]
9-
extern crate time;
10-
#[cfg(feature = "with-chrono")]
11-
extern crate chrono;
8+
#[cfg(feature = "with-chrono-0_4")]
9+
mod chrono_04;
1210

1311
#[cfg(test)]
1412
extern crate postgres;
1513

16-
#[cfg(feature = "with-chrono")]
17-
use chrono::{DateTime, NaiveDateTime, TimeZone};
1814
use std::cmp::Ordering;
1915
use std::fmt;
2016
use std::i32;
2117
use std::i64;
2218
use std::marker::PhantomData;
23-
#[cfg(feature = "with-time")]
24-
use time::Timespec;
2519

2620
use BoundSide::{Lower, Upper};
2721
use BoundType::{Exclusive, Inclusive};
@@ -155,38 +149,6 @@ macro_rules! bounded_normalizable {
155149
bounded_normalizable!(i32);
156150
bounded_normalizable!(i64);
157151

158-
#[cfg(feature = "with-time")]
159-
impl Normalizable for Timespec {
160-
fn normalize<S>(bound: RangeBound<S, Timespec>) -> RangeBound<S, Timespec>
161-
where
162-
S: BoundSided,
163-
{
164-
bound
165-
}
166-
}
167-
168-
#[cfg(feature = "with-chrono")]
169-
impl<T> Normalizable for DateTime<T>
170-
where T: TimeZone {
171-
fn normalize<S>(bound: RangeBound<S, DateTime<T>>) -> RangeBound<S, DateTime<T>>
172-
where
173-
S: BoundSided,
174-
{
175-
bound
176-
}
177-
}
178-
179-
#[cfg(feature = "with-chrono")]
180-
impl Normalizable for NaiveDateTime
181-
{
182-
fn normalize<S>(bound: RangeBound<S, NaiveDateTime>) -> RangeBound<S, NaiveDateTime>
183-
where
184-
S: BoundSided,
185-
{
186-
bound
187-
}
188-
}
189-
190152
/// The possible sides of a bound.
191153
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
192154
pub enum BoundSide {

0 commit comments

Comments
 (0)