Skip to content
Open
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
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ repository = "https://github.com/sfackler/rust-postgres-range"

[features]
with-chrono-0_4 = ["chrono-04", "postgres-types/with-chrono-0_4"]
with-rust_decimal-1 = ["rust_decimal-1"]

[dependencies]
postgres-protocol = "0.6"
postgres-types = "0.2"
chrono-04 = { version = "0.4", package = "chrono", optional = true, default-features = false }
rust_decimal-1 = { version = "1.32.0", package = "rust_decimal", optional = true, default-features = false, features = ["db-tokio-postgres"] }

[dev-dependencies]
postgres = "0.19"
87 changes: 67 additions & 20 deletions src/impls.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::error::Error;
use postgres_types::{FromSql, IsNull, Kind, ToSql, Type};
use postgres_types::private::BytesMut;
use postgres_protocol::{self as protocol, types};
use postgres_types::private::BytesMut;
use postgres_types::{FromSql, IsNull, Kind, ToSql, Type};
use std::error::Error;

use crate::{BoundSided, BoundType, Normalizable, Range, RangeBound};

Expand Down Expand Up @@ -33,7 +33,10 @@ where
}
}

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>>
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>>
where
T: PartialOrd + Normalizable + FromSql<'a>,
S: BoundSided,
Expand Down Expand Up @@ -61,7 +64,11 @@ impl<T> ToSql for Range<T>
where
T: PartialOrd + Normalizable + ToSql,
{
fn to_sql(&self, ty: &Type, buf: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
fn to_sql(
&self,
ty: &Type,
buf: &mut BytesMut,
) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
let element_type = match *ty.kind() {
Kind::Range(ref ty) => ty,
_ => panic!("unexpected type {:?}", ty),
Expand Down Expand Up @@ -90,7 +97,11 @@ where
to_sql_checked!();
}

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>>
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>>
where
S: BoundSided,
T: ToSql,
Expand All @@ -115,10 +126,12 @@ where
mod test {
use std::fmt;

use postgres::{Client, NoTls};
use postgres::types::{FromSql, ToSql};
#[cfg(feature = "with-chrono-0_4")]
use chrono_04::{TimeZone, Utc, Duration};
use chrono_04::{Duration, TimeZone, Utc};
use postgres::types::{FromSql, ToSql};
use postgres::{Client, NoTls};
#[cfg(feature = "with-rust_decimal-1")]
use rust_decimal_1::Decimal;

macro_rules! test_range {
($name:expr, $t:ty, $low:expr, $low_str:expr, $high:expr, $high_str:expr) => ({
Expand All @@ -141,21 +154,33 @@ mod test {
})
}


fn test_type<T, S>(sql_type: &str, checks: &[(T, S)])
where for<'a>
T: Sync + PartialEq + FromSql<'a> + ToSql,
S: fmt::Display
where
for<'a> T: Sync + PartialEq + FromSql<'a> + ToSql,
S: fmt::Display,
{
let mut conn = Client::connect("postgres://postgres@localhost", NoTls).unwrap();
for &(ref val, ref repr) in checks {
let stmt = conn.prepare(&*format!("SELECT {}::{}", *repr, sql_type))
let stmt = conn
.prepare(&*format!("SELECT {}::{}", *repr, sql_type))
.unwrap();
let result = conn.query(&stmt, &[]).unwrap().iter().next().unwrap().get(0);
let result = conn
.query(&stmt, &[])
.unwrap()
.iter()
.next()
.unwrap()
.get(0);
assert!(val == &result);

let stmt = conn.prepare(&*format!("SELECT $1::{}", sql_type)).unwrap();
let result = conn.query(&stmt, &[val]).unwrap().iter().next().unwrap().get(0);
let result = conn
.query(&stmt, &[val])
.unwrap()
.iter()
.next()
.unwrap()
.get(0);
assert!(val == &result);
}
}
Expand All @@ -170,19 +195,41 @@ mod test {
test_range!("INT8RANGE", i64, 100i64, "100", 200i64, "200")
}

#[test]
#[cfg(feature = "with-rust_decimal-1")]
fn test_numrange_params() {
let low = Decimal::new(202, 2);
let high = Decimal::new(202, 1);
test_range!("NUMRANGE", Decimal, low, "2.02", high, "20.2");
}

#[test]
#[cfg(feature = "with-chrono-0_4")]
fn test_tsrange_params() {
let low = Utc.timestamp(0, 0);
let low = Utc.timestamp_opt(0, 0).unwrap();
let high = low + Duration::days(10);
test_range!("TSRANGE", NaiveDateTime, low.naive_utc(), "1970-01-01", high.naive_utc(), "1970-01-11");
test_range!(
"TSRANGE",
NaiveDateTime,
low.naive_utc(),
"1970-01-01",
high.naive_utc(),
"1970-01-11"
);
}

#[test]
#[cfg(feature = "with-chrono-0_4")]
fn test_tstzrange_params() {
let low = Utc.timestamp(0, 0);
let low = Utc.timestamp_opt(0, 0).unwrap();
let high = low + Duration::days(10);
test_range!("TSTZRANGE", DateTime<Utc>, low, "1970-01-01", high, "1970-01-11");
test_range!(
"TSTZRANGE",
DateTime<Utc>,
low,
"1970-01-01",
high,
"1970-01-11"
);
}
}
Loading