Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Support Range<NaiveDate> for daterange#18

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
the-kenny wants to merge6 commits intorust-postgres:master
base:master
Choose a base branch
Loading
fromthe-kenny:date-ranges
Open
Show file tree
Hide file tree
Changes fromall 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
6 changes: 4 additions & 2 deletionssrc/chrono_04.rs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
use chrono_04::{DateTime, NaiveDateTime, TimeZone};
use chrono_04::{DateTime, NaiveDateTime, TimeZone, NaiveDate};

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

Expand All@@ -20,4 +20,6 @@ impl Normalizable for NaiveDateTime
{
bound
}
}
}

bounded_normalizable!(NaiveDate, ::chrono_04::Duration::days(1));
28 changes: 20 additions & 8 deletionssrc/impls.rs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -118,7 +118,7 @@ mod test {
use postgres::{Client, NoTls};
use postgres::types::{FromSql, ToSql};
#[cfg(feature = "with-chrono-0_4")]
use chrono_04::{TimeZone, Utc, Duration};
use chrono_04::{NaiveDate, NaiveTime, NaiveDateTime,TimeZone, Utc, Duration};

macro_rules! test_range {
($name:expr, $t:ty, $low:expr, $low_str:expr, $high:expr, $high_str:expr) => ({
Expand All@@ -144,19 +144,19 @@ mod test {

fn test_type<T, S>(sql_type: &str, checks: &[(T, S)])
where for<'a>
T: Sync + PartialEq + FromSql<'a> + ToSql,
T: Sync + PartialEq + FromSql<'a> + ToSql + fmt::Debug,
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))
.unwrap();
let result = conn.query(&stmt, &[]).unwrap().iter().next().unwrap().get(0);
assert!(val ==&result);
assert_eq!(val,&result, "'SELECT {repr}::{sql_type}'");

let stmt = conn.prepare(&*format!("SELECT $1::{}", sql_type)).unwrap();
let result = conn.query(&stmt, &[val]).unwrap().iter().next().unwrap().get(0);
assert!(val ==&result);
assert_eq!(val,&result, "'SELECT $1::{sql_type}'");
}
}

Expand All@@ -173,16 +173,28 @@ mod test {
#[test]
#[cfg(feature = "with-chrono-0_4")]
fn test_tsrange_params() {
let low = Utc.timestamp(0, 0);
let d = NaiveDate::from_ymd_opt(2015, 6, 3).unwrap();
let t = NaiveTime::from_hms_milli_opt(12, 34, 56, 789).unwrap();

let low = NaiveDateTime::new(d, t);
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, "2015-06-03T12:34:56.789", high, "2015-06-13T12:34:56.789");
}

#[test]
#[cfg(feature = "with-chrono-0_4")]
fn test_tstzrange_params() {
let low = Utc.timestamp(0, 0);
let low = Utc.with_ymd_and_hms(2014, 7, 8, 9, 10, 11).unwrap();
let high = low + Duration::days(10);
test_range!("TSTZRANGE", DateTime<_>, low, "2014-07-08T09:10:11Z", high, "2014-07-18T09:10:11Z");
}


#[test]
#[cfg(feature = "with-chrono-0_4")]
fn test_daterange_params() {
let low = NaiveDate::from_ymd_opt(2015, 6, 4).unwrap();
let high = low + Duration::days(10);
test_range!("TSTZRANGE",DateTime<Utc>, low, "1970-01-01", high, "1970-01-11");
test_range!("DATERANGE",NaiveDate, low, "2015-06-04", high, "2015-06-14");
}
}
22 changes: 12 additions & 10 deletionssrc/lib.rs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,9 +5,6 @@
#[macro_use(to_sql_checked)]
extern crate postgres_types;

#[cfg(feature = "with-chrono-0_4")]
mod chrono_04;

use std::cmp::Ordering;
use std::fmt;
use std::i32;
Expand DownExpand Up@@ -106,8 +103,6 @@ macro_rules! range {
)
}

mod impls;

/// A trait that normalizes a range bound for a type
pub trait Normalizable: Sized {
/// Given a range bound, returns the normalized version of that bound. For
Expand All@@ -124,17 +119,19 @@ pub trait Normalizable: Sized {
}

macro_rules! bounded_normalizable {
($t:ident) => (
($t:ident, $delta:expr) => (
impl Normalizable for $t {
fn normalize<S>(bound: RangeBound<S, $t>) -> RangeBound<S, $t> where S: BoundSided {
use $crate::{BoundSide::*, BoundType::*};

match (<S as BoundSided>::side(), bound.type_) {
(Upper, Inclusive) => {
assert!(bound.value != $t::MAX);
RangeBound::new(bound.value +1, Exclusive)
RangeBound::new(bound.value +($delta), Exclusive)
}
(Lower, Exclusive) => {
assert!(bound.value != $t::MAX);
RangeBound::new(bound.value +1, Inclusive)
RangeBound::new(bound.value +($delta), Inclusive)
}
_ => bound
}
Expand All@@ -143,8 +140,13 @@ macro_rules! bounded_normalizable {
)
}

bounded_normalizable!(i32);
bounded_normalizable!(i64);
bounded_normalizable!(i32, 1);
bounded_normalizable!(i64, 1);

mod impls;

#[cfg(feature = "with-chrono-0_4")]
mod chrono_04;

/// The possible sides of a bound.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp