Skip to content

Commit 42ce9bb

Browse files
committed
Update to postgres 0.15
1 parent d634304 commit 42ce9bb

File tree

2 files changed

+13
-46
lines changed

2 files changed

+13
-46
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ path = "tests/test.rs"
1919

2020
[dependencies]
2121
r2d2 = "0.7"
22-
postgres = "0.14"
22+
postgres = "0.15"
23+
postgres-shared = "0.4"

src/lib.rs

Lines changed: 11 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
#![warn(missing_docs)]
44
pub extern crate r2d2;
55
pub extern crate postgres;
6+
extern crate postgres_shared;
67

7-
use std::error;
8-
use std::error::Error as _StdError;
9-
use std::fmt;
8+
use postgres::{Connection, Error, Result};
109
use postgres::params::{ConnectParams, IntoConnectParams};
1110
use postgres::tls::TlsHandshake;
1211

@@ -21,37 +20,6 @@ pub enum TlsMode {
2120
Require(Box<TlsHandshake + Sync + Send>),
2221
}
2322

24-
/// A unified enum of errors returned by postgres::Connection
25-
#[derive(Debug)]
26-
pub enum Error {
27-
/// A postgres::error::ConnectError
28-
Connect(postgres::error::ConnectError),
29-
/// An postgres::error::Error
30-
Other(postgres::error::Error),
31-
}
32-
33-
impl fmt::Display for Error {
34-
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
35-
write!(fmt, "{}: {}", self.description(), self.cause().unwrap())
36-
}
37-
}
38-
39-
impl error::Error for Error {
40-
fn description(&self) -> &str {
41-
match *self {
42-
Error::Connect(_) => "Error opening a connection",
43-
Error::Other(_) => "Error communicating with server",
44-
}
45-
}
46-
47-
fn cause(&self) -> Option<&error::Error> {
48-
match *self {
49-
Error::Connect(ref err) => Some(err as &error::Error),
50-
Error::Other(ref err) => Some(err as &error::Error),
51-
}
52-
}
53-
}
54-
5523
/// An `r2d2::ManageConnection` for `postgres::Connection`s.
5624
///
5725
/// ## Example
@@ -92,13 +60,11 @@ impl PostgresConnectionManager {
9260
/// types.
9361
pub fn new<T>(params: T,
9462
ssl_mode: TlsMode)
95-
-> Result<PostgresConnectionManager, postgres::error::ConnectError>
63+
-> Result<PostgresConnectionManager>
9664
where T: IntoConnectParams
9765
{
98-
let params = match params.into_connect_params() {
99-
Ok(params) => params,
100-
Err(err) => return Err(postgres::error::ConnectError::ConnectParams(err)),
101-
};
66+
// fixme we shouldn't be using this private constructor :(
67+
let params = params.into_connect_params().map_err(postgres_shared::error::connect)?;
10268

10369
Ok(PostgresConnectionManager {
10470
params: params,
@@ -108,23 +74,23 @@ impl PostgresConnectionManager {
10874
}
10975

11076
impl r2d2::ManageConnection for PostgresConnectionManager {
111-
type Connection = postgres::Connection;
77+
type Connection = Connection;
11278
type Error = Error;
11379

114-
fn connect(&self) -> Result<postgres::Connection, Error> {
80+
fn connect(&self) -> Result<postgres::Connection> {
11581
let mode = match self.ssl_mode {
11682
TlsMode::None => postgres::TlsMode::None,
11783
TlsMode::Prefer(ref n) => postgres::TlsMode::Prefer(&**n),
11884
TlsMode::Require(ref n) => postgres::TlsMode::Require(&**n),
11985
};
120-
postgres::Connection::connect(self.params.clone(), mode).map_err(Error::Connect)
86+
postgres::Connection::connect(self.params.clone(), mode)
12187
}
12288

123-
fn is_valid(&self, conn: &mut postgres::Connection) -> Result<(), Error> {
124-
conn.batch_execute("").map_err(Error::Other)
89+
fn is_valid(&self, conn: &mut Connection) -> Result<()> {
90+
conn.batch_execute("")
12591
}
12692

127-
fn has_broken(&self, conn: &mut postgres::Connection) -> bool {
93+
fn has_broken(&self, conn: &mut Connection) -> bool {
12894
conn.is_desynchronized()
12995
}
13096
}

0 commit comments

Comments
 (0)