Skip to content

Commit 334a49c

Browse files
committed
Bump rust-postgres version
1 parent 00580aa commit 334a49c

File tree

3 files changed

+90
-134
lines changed

3 files changed

+90
-134
lines changed

Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,9 @@ repository = "https://github.com/sfackler/rust-postgres-range"
88
documentation = "https://sfackler.github.io/doc/postgres_range"
99

1010
[dependencies]
11-
postgres = "0.6"
1211
time = "0.1"
12+
postgres = "0.7.1"
13+
byteorder = "0.2"
14+
15+
[dev-dependencies.postgres]
16+
features = ["time"]

src/impls/mod.rs

Lines changed: 48 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,23 @@
1-
use std::old_io::ByRefReader;
2-
use std::old_io::util::LimitReader;
1+
use std::io::prelude::*;
2+
use postgres::{self, Type, Kind, ToSql, FromSql};
3+
use postgres::types::IsNull;
4+
use byteorder::{ReadBytesExt, WriteBytesExt, BigEndian};
35

4-
use time::Timespec;
5-
use postgres::Type;
6-
use postgres::types::{RawFromSql, RawToSql};
7-
8-
use {postgres, Range, RangeBound, BoundType, BoundSided, Normalizable};
9-
10-
macro_rules! check_types {
11-
($actual:ident, $($expected:pat),+) => (
12-
match $actual {
13-
$(&$expected)|+ => {}
14-
actual => return Err(::postgres::Error::WrongType(actual.clone()))
15-
}
16-
)
17-
}
18-
19-
macro_rules! from_sql_impl {
20-
($t:ty, $($oid:pat),+) => {
21-
impl ::postgres::FromSql for Option<::Range<$t>> {
22-
fn from_sql(ty: &::postgres::Type, raw: Option<&[u8]>) -> ::postgres::Result<Self> {
23-
check_types!(ty, $($oid),+);
24-
25-
match raw {
26-
Some(mut raw) => ::postgres::types::RawFromSql::raw_from_sql(ty, &mut raw).map(Some),
27-
None => Ok(None),
28-
}
29-
}
30-
}
31-
32-
impl ::postgres::FromSql for ::Range<$t> {
33-
fn from_sql(ty: &::postgres::Type, raw: Option<&[u8]>) -> ::postgres::Result<Self> {
34-
let v: ::postgres::Result<Option<Self>> = ::postgres::FromSql::from_sql(ty, raw);
35-
match v {
36-
Ok(None) => Err(::postgres::Error::WasNull),
37-
Ok(Some(v)) => Ok(v),
38-
Err(err) => Err(err),
39-
}
40-
}
41-
}
42-
}
43-
}
44-
45-
macro_rules! to_sql_impl {
46-
($t:ty, $($oid:pat),+) => {
47-
impl ::postgres::ToSql for ::Range<$t> {
48-
fn to_sql(&self, ty: &::postgres::Type) -> ::postgres::Result<Option<Vec<u8>>> {
49-
check_types!(ty, $($oid),+);
50-
51-
let mut writer = vec![];
52-
try!(self.raw_to_sql(ty, &mut writer));
53-
Ok(Some(writer))
54-
}
55-
}
56-
57-
impl ::postgres::ToSql for Option<::Range<$t>> {
58-
fn to_sql(&self, ty: &::postgres::Type) -> ::postgres::Result<Option<Vec<u8>>> {
59-
check_types!(ty, $($oid),+);
60-
match *self {
61-
Some(ref arr) => arr.to_sql(ty),
62-
None => Ok(None)
63-
}
64-
}
65-
}
66-
}
67-
}
6+
use {Range, RangeBound, BoundType, BoundSided, Normalizable};
687

698
const RANGE_UPPER_UNBOUNDED: i8 = 0b0001_0000;
709
const RANGE_LOWER_UNBOUNDED: i8 = 0b0000_1000;
7110
const RANGE_UPPER_INCLUSIVE: i8 = 0b0000_0100;
7211
const RANGE_LOWER_INCLUSIVE: i8 = 0b0000_0010;
7312
const RANGE_EMPTY: i8 = 0b0000_0001;
7413

75-
impl<T> RawFromSql for Range<T> where T: PartialOrd+Normalizable+RawFromSql {
76-
fn raw_from_sql<R: Reader>(ty: &Type, rdr: &mut R) -> postgres::Result<Range<T>> {
14+
impl<T> FromSql for Range<T> where T: PartialOrd+Normalizable+FromSql {
15+
fn from_sql<R: Read>(ty: &Type, rdr: &mut R) -> postgres::Result<Range<T>> {
16+
let element_type = match ty.kind() {
17+
&Kind::Range(ref ty) => ty,
18+
_ => panic!("unexpected type {:?}", ty)
19+
};
20+
7721
let t = try!(rdr.read_i8());
7822

7923
if t & RANGE_EMPTY != 0 {
@@ -82,38 +26,45 @@ impl<T> RawFromSql for Range<T> where T: PartialOrd+Normalizable+RawFromSql {
8226

8327
fn make_bound<S, T, R>(ty: &Type, rdr: &mut R, tag: i8, bound_flag: i8, inclusive_flag: i8)
8428
-> postgres::Result<Option<RangeBound<S, T>>>
85-
where S: BoundSided, T: PartialOrd+Normalizable+RawFromSql, R: Reader {
29+
where S: BoundSided, T: PartialOrd+Normalizable+FromSql, R: Read {
8630
match tag & bound_flag {
8731
0 => {
8832
let type_ = match tag & inclusive_flag {
8933
0 => BoundType::Exclusive,
9034
_ => BoundType::Inclusive,
9135
};
92-
let len = try!(rdr.read_be_i32()) as usize;
93-
let mut limit = LimitReader::new(rdr.by_ref(), len);
94-
let bound = try!(RawFromSql::raw_from_sql(ty, &mut limit));
36+
let len = try!(rdr.read_i32::<BigEndian>()) as u64;
37+
let mut limit = rdr.take(len);
38+
let bound = try!(FromSql::from_sql(ty, &mut limit));
9539
if limit.limit() != 0 {
96-
return Err(postgres::Error::BadData);
40+
return Err(postgres::Error::BadResponse);
9741
}
9842
Ok(Some(RangeBound::new(bound, type_)))
9943
}
10044
_ => Ok(None)
10145
}
10246
}
10347

104-
let element_type = ty.element_type().unwrap();
105-
let lower = try!(make_bound(&element_type, rdr, t, RANGE_LOWER_UNBOUNDED, RANGE_LOWER_INCLUSIVE));
106-
let upper = try!(make_bound(&element_type, rdr, t, RANGE_UPPER_UNBOUNDED, RANGE_UPPER_INCLUSIVE));
48+
let lower = try!(make_bound(element_type, rdr, t, RANGE_LOWER_UNBOUNDED, RANGE_LOWER_INCLUSIVE));
49+
let upper = try!(make_bound(element_type, rdr, t, RANGE_UPPER_UNBOUNDED, RANGE_UPPER_INCLUSIVE));
10750
Ok(Range::new(lower, upper))
10851
}
52+
53+
fn accepts(ty: &Type) -> bool {
54+
match ty.kind() {
55+
&Kind::Range(ref inner) => <T as FromSql>::accepts(inner),
56+
_ => false,
57+
}
58+
}
10959
}
11060

111-
from_sql_impl!(i32, Type::Int4Range);
112-
from_sql_impl!(i64, Type::Int8Range);
113-
from_sql_impl!(Timespec, Type::TsRange, Type::TstzRange);
61+
impl<T> ToSql for Range<T> where T: PartialOrd+Normalizable+ToSql {
62+
fn to_sql<W: ?Sized+Write>(&self, ty: &Type, mut buf: &mut W) -> postgres::Result<IsNull> {
63+
let element_type = match ty.kind() {
64+
&Kind::Range(ref ty) => ty,
65+
_ => panic!("unexpected type {:?}", ty)
66+
};
11467

115-
impl<T> RawToSql for Range<T> where T: PartialOrd+Normalizable+RawToSql {
116-
fn raw_to_sql<W: Writer>(&self, ty: &Type, buf: &mut W) -> postgres::Result<()> {
11768
let mut tag = 0;
11869
if self.is_empty() {
11970
tag |= RANGE_EMPTY;
@@ -132,28 +83,32 @@ impl<T> RawToSql for Range<T> where T: PartialOrd+Normalizable+RawToSql {
13283

13384
try!(buf.write_i8(tag));
13485

135-
fn write_value<S, T, W>(ty: &Type, buf: &mut W, v: Option<&RangeBound<S, T>>) -> postgres::Result<()>
136-
where S: BoundSided, T: RawToSql, W: Writer {
86+
fn write_value<S, T, W: ?Sized>(ty: &Type, mut buf: &mut W, v: Option<&RangeBound<S, T>>) -> postgres::Result<()>
87+
where S: BoundSided, T: ToSql, W: Write {
13788
if let Some(bound) = v {
13889
let mut inner_buf = vec![];
139-
try!(bound.value.raw_to_sql(ty, &mut inner_buf));
140-
try!(buf.write_be_u32(inner_buf.len() as u32));
90+
try!(bound.value.to_sql(ty, &mut inner_buf));
91+
try!(buf.write_u32::<BigEndian>(inner_buf.len() as u32));
14192
try!(buf.write_all(&*inner_buf));
14293
}
14394
Ok(())
14495
}
14596

146-
let element_type = ty.element_type().unwrap();
14797
try!(write_value(&element_type, buf, self.lower()));
14898
try!(write_value(&element_type, buf, self.upper()));
14999

150-
Ok(())
100+
Ok(IsNull::No)
151101
}
152-
}
153102

154-
to_sql_impl!(i32, Type::Int4Range);
155-
to_sql_impl!(i64, Type::Int8Range);
156-
to_sql_impl!(Timespec, Type::TsRange, Type::TstzRange);
103+
fn accepts(ty: &Type) -> bool {
104+
match ty.kind() {
105+
&Kind::Range(ref inner) => <T as ToSql>::accepts(inner),
106+
_ => false,
107+
}
108+
}
109+
110+
to_sql_checked!();
111+
}
157112

158113
#[cfg(test)]
159114
mod test {
@@ -187,11 +142,11 @@ mod test {
187142
let conn = Connection::connect("postgres://postgres@localhost", &SslMode::None).unwrap();
188143
for &(ref val, ref repr) in checks {
189144
let stmt = conn.prepare(&*format!("SELECT {}::{}", *repr, sql_type)).unwrap();
190-
let result = stmt.query(&[]).unwrap().next().unwrap().get(0);
145+
let result = stmt.query(&[]).unwrap().iter().next().unwrap().get(0);
191146
assert!(val == &result);
192147

193148
let stmt = conn.prepare(&*format!("SELECT $1::{}", sql_type)).unwrap();
194-
let result = stmt.query(&[val]).unwrap().next().unwrap().get(0);
149+
let result = stmt.query(&[val]).unwrap().iter().next().unwrap().get(0);
195150
assert!(val == &result);
196151
}
197152
}

src/lib.rs

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
//! Types dealing with ranges of values
22
#![doc(html_root_url="https://sfackler.github.io/rust-postgres-range/doc")]
3-
#![cfg_attr(test, feature(core))]
4-
#![feature(io)]
3+
#![feature(io, core)]
54

5+
#[macro_use(to_sql_checked)]
66
extern crate postgres;
77
extern crate time;
8+
extern crate byteorder;
89

910
use std::cmp::Ordering;
1011
use std::fmt;
@@ -19,10 +20,6 @@ use BoundSide::{Lower, Upper};
1920
use BoundType::{Inclusive, Exclusive};
2021
use InnerRange::{Empty, Normal};
2122

22-
mod postgres_range {
23-
pub use super::{RangeBound, Range, BoundType};
24-
}
25-
2623
/// The `range!` macro can make it easier to create ranges. It roughly mirrors
2724
/// traditional mathematic range syntax.
2825
///
@@ -59,55 +56,55 @@ mod postgres_range {
5956
/// }
6057
#[macro_export]
6158
macro_rules! range {
62-
(empty) => (::postgres_range::Range::empty());
63-
('(',; ')') => (::postgres_range::Range::new(None, None));
59+
(empty) => ($crate::Range::empty());
60+
('(',; ')') => ($crate::Range::new(None, None));
6461
('(', $h:expr; ')') => (
65-
::postgres_range::Range::new(None,
66-
Some(::postgres_range::RangeBound::new($h,
67-
::postgres_range::BoundType::Exclusive)))
62+
$crate::Range::new(None,
63+
Some($crate::RangeBound::new($h,
64+
$crate::BoundType::Exclusive)))
6865
);
6966
('(', $h:expr; ']') => (
70-
::postgres_range::Range::new(None,
71-
Some(::postgres_range::RangeBound::new($h,
72-
::postgres_range::BoundType::Inclusive)))
67+
$crate::Range::new(None,
68+
Some($crate::RangeBound::new($h,
69+
$crate::BoundType::Inclusive)))
7370
);
7471
('(' $l:expr,; ')') => (
75-
::postgres_range::Range::new(
76-
Some(::postgres_range::RangeBound::new($l,
77-
::postgres_range::BoundType::Exclusive)), None)
72+
$crate::Range::new(
73+
Some($crate::RangeBound::new($l,
74+
$crate::BoundType::Exclusive)), None)
7875
);
7976
('[' $l:expr,; ')') => (
80-
::postgres_range::Range::new(
81-
Some(::postgres_range::RangeBound::new($l,
82-
::postgres_range::BoundType::Inclusive)), None)
77+
$crate::Range::new(
78+
Some($crate::RangeBound::new($l,
79+
$crate::BoundType::Inclusive)), None)
8380
);
8481
('(' $l:expr, $h:expr; ')') => (
85-
::postgres_range::Range::new(
86-
Some(::postgres_range::RangeBound::new($l,
87-
::postgres_range::BoundType::Exclusive)),
88-
Some(::postgres_range::RangeBound::new($h,
89-
::postgres_range::BoundType::Exclusive)))
82+
$crate::Range::new(
83+
Some($crate::RangeBound::new($l,
84+
$crate::BoundType::Exclusive)),
85+
Some($crate::RangeBound::new($h,
86+
$crate::BoundType::Exclusive)))
9087
);
9188
('(' $l:expr, $h:expr; ']') => (
92-
::postgres_range::Range::new(
93-
Some(::postgres_range::RangeBound::new($l,
94-
::postgres_range::BoundType::Exclusive)),
95-
Some(::postgres_range::RangeBound::new($h,
96-
::postgres_range::BoundType::Inclusive)))
89+
$crate::Range::new(
90+
Some($crate::RangeBound::new($l,
91+
$crate::BoundType::Exclusive)),
92+
Some($crate::RangeBound::new($h,
93+
$crate::BoundType::Inclusive)))
9794
);
9895
('[' $l:expr, $h:expr; ')') => (
99-
::postgres_range::Range::new(
100-
Some(::postgres_range::RangeBound::new($l,
101-
::postgres_range::BoundType::Inclusive)),
102-
Some(::postgres_range::RangeBound::new($h,
103-
::postgres_range::BoundType::Exclusive)))
96+
$crate::Range::new(
97+
Some($crate::RangeBound::new($l,
98+
$crate::BoundType::Inclusive)),
99+
Some($crate::RangeBound::new($h,
100+
$crate::BoundType::Exclusive)))
104101
);
105102
('[' $l:expr, $h:expr; ']') => (
106-
::postgres_range::Range::new(
107-
Some(::postgres_range::RangeBound::new($l,
108-
::postgres_range::BoundType::Inclusive)),
109-
Some(::postgres_range::RangeBound::new($h,
110-
::postgres_range::BoundType::Inclusive)))
103+
$crate::Range::new(
104+
Some($crate::RangeBound::new($l,
105+
$crate::BoundType::Inclusive)),
106+
Some($crate::RangeBound::new($h,
107+
$crate::BoundType::Inclusive)))
111108
)
112109
}
113110

0 commit comments

Comments
 (0)