Make a PostgresResult typedef

This commit is contained in:
Steven Fackler 2014-04-02 21:26:41 -07:00
parent ec6fd52301
commit 15f79973a4
2 changed files with 46 additions and 44 deletions

View File

@ -207,6 +207,9 @@ mod test;
static DEFAULT_PORT: Port = 5432; static DEFAULT_PORT: Port = 5432;
/// A typedef of the result returned by many methods.
pub type PostgresResult<T> = Result<T, PostgresError>;
/// Trait for types that can handle Postgres notice messages /// Trait for types that can handle Postgres notice messages
pub trait PostgresNoticeHandler { pub trait PostgresNoticeHandler {
/// Handle a Postgres notice message /// Handle a Postgres notice message
@ -412,8 +415,8 @@ impl Drop for InnerPostgresConnection {
} }
impl InnerPostgresConnection { impl InnerPostgresConnection {
fn connect(url: &str, ssl: &SslMode) fn connect(url: &str, ssl: &SslMode) -> Result<InnerPostgresConnection,
-> Result<InnerPostgresConnection, PostgresConnectError> { PostgresConnectError> {
let Url { let Url {
host, host,
port, port,
@ -511,8 +514,8 @@ impl InnerPostgresConnection {
} }
} }
fn handle_auth(&mut self, user: UserInfo) -> fn handle_auth(&mut self, user: UserInfo)
Result<(), PostgresConnectError> { -> Result<(), PostgresConnectError> {
match try_pg_conn!(self.read_message()) { match try_pg_conn!(self.read_message()) {
AuthenticationOk => return Ok(()), AuthenticationOk => return Ok(()),
AuthenticationCleartextPassword => { AuthenticationCleartextPassword => {
@ -565,7 +568,7 @@ impl InnerPostgresConnection {
} }
fn prepare<'a>(&mut self, query: &str, conn: &'a PostgresConnection) fn prepare<'a>(&mut self, query: &str, conn: &'a PostgresConnection)
-> Result<PostgresStatement<'a>, PostgresError> { -> PostgresResult<PostgresStatement<'a>> {
let stmt_name = format!("statement_{}", self.next_stmt_id); let stmt_name = format!("statement_{}", self.next_stmt_id);
self.next_stmt_id += 1; self.next_stmt_id += 1;
@ -626,7 +629,7 @@ impl InnerPostgresConnection {
} }
fn set_type_names<'a, I: Iterator<&'a mut PostgresType>>(&mut self, mut it: I) fn set_type_names<'a, I: Iterator<&'a mut PostgresType>>(&mut self, mut it: I)
-> Result<(), PostgresError> { -> PostgresResult<()> {
for ty in it { for ty in it {
match *ty { match *ty {
PgUnknownType { oid, ref mut name } => PgUnknownType { oid, ref mut name } =>
@ -637,7 +640,7 @@ impl InnerPostgresConnection {
Ok(()) Ok(())
} }
fn get_type_name(&mut self, oid: Oid) -> Result<~str, PostgresError> { fn get_type_name(&mut self, oid: Oid) -> PostgresResult<~str> {
match self.unknown_types.find(&oid) { match self.unknown_types.find(&oid) {
Some(name) => return Ok(name.clone()), Some(name) => return Ok(name.clone()),
None => {} None => {}
@ -653,7 +656,7 @@ impl InnerPostgresConnection {
self.desynchronized self.desynchronized
} }
fn wait_for_ready(&mut self) -> Result<(), PostgresError> { fn wait_for_ready(&mut self) -> PostgresResult<()> {
match try_pg!(self.read_message()) { match try_pg!(self.read_message()) {
ReadyForQuery { .. } => Ok(()), ReadyForQuery { .. } => Ok(()),
_ => unreachable!() _ => unreachable!()
@ -661,7 +664,7 @@ impl InnerPostgresConnection {
} }
fn quick_query(&mut self, query: &str) fn quick_query(&mut self, query: &str)
-> Result<Vec<Vec<Option<~str>>>, PostgresError> { -> PostgresResult<Vec<Vec<Option<~str>>>> {
check_desync!(self); check_desync!(self);
try_pg!(self.write_messages([Query { query: query }])); try_pg!(self.write_messages([Query { query: query }]));
@ -683,7 +686,7 @@ impl InnerPostgresConnection {
Ok(result) Ok(result)
} }
fn finish_inner(&mut self) -> Result<(), PostgresError> { fn finish_inner(&mut self) -> PostgresResult<()> {
check_desync!(self); check_desync!(self);
Ok(try_pg!(self.write_messages([Terminate]))) Ok(try_pg!(self.write_messages([Terminate])))
} }
@ -762,7 +765,7 @@ impl PostgresConnection {
/// Err(err) => fail!("Error preparing statement: {}", err) /// Err(err) => fail!("Error preparing statement: {}", err)
/// }; /// };
pub fn prepare<'a>(&'a self, query: &str) pub fn prepare<'a>(&'a self, query: &str)
-> Result<PostgresStatement<'a>, PostgresError> { -> PostgresResult<PostgresStatement<'a>> {
self.conn.borrow_mut().prepare(query, self) self.conn.borrow_mut().prepare(query, self)
} }
@ -793,7 +796,7 @@ impl PostgresConnection {
/// # } /// # }
/// ``` /// ```
pub fn transaction<'a>(&'a self) pub fn transaction<'a>(&'a self)
-> Result<PostgresTransaction<'a>, PostgresError> { -> PostgresResult<PostgresTransaction<'a>> {
check_desync!(self); check_desync!(self);
try!(self.quick_query("BEGIN")); try!(self.quick_query("BEGIN"));
Ok(PostgresTransaction { Ok(PostgresTransaction {
@ -811,7 +814,7 @@ impl PostgresConnection {
/// ///
/// On success, returns the number of rows modified or 0 if not applicable. /// On success, returns the number of rows modified or 0 if not applicable.
pub fn execute(&self, query: &str, params: &[&ToSql]) pub fn execute(&self, query: &str, params: &[&ToSql])
-> Result<uint, PostgresError> { -> PostgresResult<uint> {
self.prepare(query).and_then(|stmt| stmt.execute(params)) self.prepare(query).and_then(|stmt| stmt.execute(params))
} }
@ -837,18 +840,18 @@ impl PostgresConnection {
/// Functionally equivalent to the `Drop` implementation for /// Functionally equivalent to the `Drop` implementation for
/// `PostgresConnection` except that it returns any error encountered to /// `PostgresConnection` except that it returns any error encountered to
/// the caller. /// the caller.
pub fn finish(self) -> Result<(), PostgresError> { pub fn finish(self) -> PostgresResult<()> {
let mut conn = self.conn.borrow_mut(); let mut conn = self.conn.borrow_mut();
conn.finished = true; conn.finished = true;
conn.finish_inner() conn.finish_inner()
} }
fn quick_query(&self, query: &str) fn quick_query(&self, query: &str)
-> Result<Vec<Vec<Option<~str>>>, PostgresError> { -> PostgresResult<Vec<Vec<Option<~str>>>> {
self.conn.borrow_mut().quick_query(query) self.conn.borrow_mut().quick_query(query)
} }
fn wait_for_ready(&self) -> Result<(), PostgresError> { fn wait_for_ready(&self) -> PostgresResult<()> {
self.conn.borrow_mut().wait_for_ready() self.conn.borrow_mut().wait_for_ready()
} }
@ -893,7 +896,7 @@ impl<'conn> Drop for PostgresTransaction<'conn> {
} }
impl<'conn> PostgresTransaction<'conn> { impl<'conn> PostgresTransaction<'conn> {
fn finish_inner(&mut self) -> Result<(), PostgresError> { fn finish_inner(&mut self) -> PostgresResult<()> {
if task::failing() || !self.commit.get() { if task::failing() || !self.commit.get() {
if self.nested { if self.nested {
try!(self.conn.quick_query("ROLLBACK TO sp")); try!(self.conn.quick_query("ROLLBACK TO sp"));
@ -914,19 +917,19 @@ impl<'conn> PostgresTransaction<'conn> {
impl<'conn> PostgresTransaction<'conn> { impl<'conn> PostgresTransaction<'conn> {
/// Like `PostgresConnection::prepare`. /// Like `PostgresConnection::prepare`.
pub fn prepare<'a>(&'a self, query: &str) pub fn prepare<'a>(&'a self, query: &str)
-> Result<PostgresStatement<'a>, PostgresError> { -> PostgresResult<PostgresStatement<'a>> {
self.conn.prepare(query) self.conn.prepare(query)
} }
/// Like `PostgresConnection::execute`. /// Like `PostgresConnection::execute`.
pub fn execute(&self, query: &str, params: &[&ToSql]) pub fn execute(&self, query: &str, params: &[&ToSql])
-> Result<uint, PostgresError> { -> PostgresResult<uint> {
self.conn.execute(query, params) self.conn.execute(query, params)
} }
/// Like `PostgresConnection::transaction`. /// Like `PostgresConnection::transaction`.
pub fn transaction<'a>(&'a self) pub fn transaction<'a>(&'a self)
-> Result<PostgresTransaction<'a>, PostgresError> { -> PostgresResult<PostgresTransaction<'a>> {
check_desync!(self.conn); check_desync!(self.conn);
try!(self.conn.quick_query("SAVEPOINT sp")); try!(self.conn.quick_query("SAVEPOINT sp"));
Ok(PostgresTransaction { Ok(PostgresTransaction {
@ -966,7 +969,7 @@ impl<'conn> PostgresTransaction<'conn> {
/// ///
/// Functionally equivalent to the `Drop` implementation of /// Functionally equivalent to the `Drop` implementation of
/// `PostgresTransaction` except that it returns any error to the caller. /// `PostgresTransaction` except that it returns any error to the caller.
pub fn finish(mut self) -> Result<(), PostgresError> { pub fn finish(mut self) -> PostgresResult<()> {
self.finished = true; self.finished = true;
self.finish_inner() self.finish_inner()
} }
@ -981,8 +984,8 @@ impl<'conn> PostgresTransaction<'conn> {
stmt: &'stmt PostgresStatement, stmt: &'stmt PostgresStatement,
params: &[&ToSql], params: &[&ToSql],
row_limit: uint) row_limit: uint)
-> Result<PostgresLazyRows<'trans, 'stmt>, -> PostgresResult<PostgresLazyRows
PostgresError> { <'trans, 'stmt>> {
if self.conn as *PostgresConnection != stmt.conn as *PostgresConnection { if self.conn as *PostgresConnection != stmt.conn as *PostgresConnection {
return Err(PgWrongConnection); return Err(PgWrongConnection);
} }
@ -1020,7 +1023,7 @@ impl<'conn> Drop for PostgresStatement<'conn> {
} }
impl<'conn> PostgresStatement<'conn> { impl<'conn> PostgresStatement<'conn> {
fn finish_inner(&mut self) -> Result<(), PostgresError> { fn finish_inner(&mut self) -> PostgresResult<()> {
check_desync!(self.conn); check_desync!(self.conn);
try_pg!(self.conn.write_messages([ try_pg!(self.conn.write_messages([
Close { Close {
@ -1042,7 +1045,7 @@ impl<'conn> PostgresStatement<'conn> {
} }
fn inner_execute(&self, portal_name: &str, row_limit: uint, params: &[&ToSql]) fn inner_execute(&self, portal_name: &str, row_limit: uint, params: &[&ToSql])
-> Result<(), PostgresError> { -> PostgresResult<()> {
if self.param_types.len() != params.len() { if self.param_types.len() != params.len() {
return Err(PgWrongParamCount { return Err(PgWrongParamCount {
expected: self.param_types.len(), expected: self.param_types.len(),
@ -1086,7 +1089,7 @@ impl<'conn> PostgresStatement<'conn> {
} }
fn lazy_query<'a>(&'a self, row_limit: uint, params: &[&ToSql]) fn lazy_query<'a>(&'a self, row_limit: uint, params: &[&ToSql])
-> Result<PostgresRows<'a>, PostgresError> { -> PostgresResult<PostgresRows<'a>> {
let id = self.next_portal_id.get(); let id = self.next_portal_id.get();
self.next_portal_id.set(id + 1); self.next_portal_id.set(id + 1);
let portal_name = format!("{}_portal_{}", self.name, id); let portal_name = format!("{}_portal_{}", self.name, id);
@ -1133,7 +1136,7 @@ impl<'conn> PostgresStatement<'conn> {
/// Ok(count) => println!("{} row(s) updated", count), /// Ok(count) => println!("{} row(s) updated", count),
/// Err(err) => println!("Error executing query: {}", err) /// Err(err) => println!("Error executing query: {}", err)
/// } /// }
pub fn execute(&self, params: &[&ToSql]) -> Result<uint, PostgresError> { pub fn execute(&self, params: &[&ToSql]) -> PostgresResult<uint> {
check_desync!(self.conn); check_desync!(self.conn);
try!(self.inner_execute("", 0, params)); try!(self.inner_execute("", 0, params));
@ -1186,7 +1189,7 @@ impl<'conn> PostgresStatement<'conn> {
/// } /// }
/// ``` /// ```
pub fn query<'a>(&'a self, params: &[&ToSql]) pub fn query<'a>(&'a self, params: &[&ToSql])
-> Result<PostgresRows<'a>, PostgresError> { -> PostgresResult<PostgresRows<'a>> {
check_desync!(self.conn); check_desync!(self.conn);
self.lazy_query(0, params) self.lazy_query(0, params)
} }
@ -1195,7 +1198,7 @@ impl<'conn> PostgresStatement<'conn> {
/// ///
/// Functionally identical to the `Drop` implementation of the /// Functionally identical to the `Drop` implementation of the
/// `PostgresStatement` except that it returns any error to the caller. /// `PostgresStatement` except that it returns any error to the caller.
pub fn finish(mut self) -> Result<(), PostgresError> { pub fn finish(mut self) -> PostgresResult<()> {
self.finished.set(true); self.finished.set(true);
self.finish_inner() self.finish_inner()
} }
@ -1234,7 +1237,7 @@ impl<'stmt> Drop for PostgresRows<'stmt> {
} }
impl<'stmt> PostgresRows<'stmt> { impl<'stmt> PostgresRows<'stmt> {
fn finish_inner(&mut self) -> Result<(), PostgresError> { fn finish_inner(&mut self) -> PostgresResult<()> {
check_desync!(self.stmt.conn); check_desync!(self.stmt.conn);
try_pg!(self.stmt.conn.write_messages([ try_pg!(self.stmt.conn.write_messages([
Close { Close {
@ -1255,7 +1258,7 @@ impl<'stmt> PostgresRows<'stmt> {
Ok(()) Ok(())
} }
fn read_rows(&mut self) -> Result<(), PostgresError> { fn read_rows(&mut self) -> PostgresResult<()> {
loop { loop {
match try_pg!(self.stmt.conn.read_message()) { match try_pg!(self.stmt.conn.read_message()) {
EmptyQueryResponse | EmptyQueryResponse |
@ -1274,7 +1277,7 @@ impl<'stmt> PostgresRows<'stmt> {
self.stmt.conn.wait_for_ready() self.stmt.conn.wait_for_ready()
} }
fn execute(&mut self) -> Result<(), PostgresError> { fn execute(&mut self) -> PostgresResult<()> {
try_pg!(self.stmt.conn.write_messages([ try_pg!(self.stmt.conn.write_messages([
Execute { Execute {
portal: self.name, portal: self.name,
@ -1290,13 +1293,12 @@ impl<'stmt> PostgresRows<'stmt> {
/// ///
/// Functionally identical to the `Drop` implementation on `PostgresRows` /// Functionally identical to the `Drop` implementation on `PostgresRows`
/// except that it returns any error to the caller. /// except that it returns any error to the caller.
pub fn finish(mut self) -> Result<(), PostgresError> { pub fn finish(mut self) -> PostgresResult<()> {
self.finished = true; self.finished = true;
self.finish_inner() self.finish_inner()
} }
fn try_next(&mut self) -> Option<Result<PostgresRow<'stmt>, fn try_next(&mut self) -> Option<PostgresResult<PostgresRow<'stmt>>> {
PostgresError>> {
if self.data.is_empty() && self.more_rows { if self.data.is_empty() && self.more_rows {
match self.execute() { match self.execute() {
Ok(()) => {} Ok(()) => {}
@ -1344,8 +1346,7 @@ impl<'stmt> PostgresRow<'stmt> {
/// ///
/// Returns an `Error` value if the index does not reference a column or /// Returns an `Error` value if the index does not reference a column or
/// the return type is not compatible with the Postgres type. /// the return type is not compatible with the Postgres type.
pub fn get<I: RowIndex, T: FromSql>(&self, idx: I) pub fn get<I: RowIndex, T: FromSql>(&self, idx: I) -> PostgresResult<T> {
-> Result<T, PostgresError> {
let idx = match idx.idx(self.stmt) { let idx = match idx.idx(self.stmt) {
Some(idx) => idx, Some(idx) => idx,
None => return Err(PgInvalidColumn) None => return Err(PgInvalidColumn)
@ -1442,14 +1443,14 @@ pub struct PostgresLazyRows<'trans, 'stmt> {
impl<'trans, 'stmt> PostgresLazyRows<'trans, 'stmt> { impl<'trans, 'stmt> PostgresLazyRows<'trans, 'stmt> {
/// Like `PostgresRows::finish`. /// Like `PostgresRows::finish`.
pub fn finish(self) -> Result<(), PostgresError> { pub fn finish(self) -> PostgresResult<()> {
self.result.finish() self.result.finish()
} }
} }
impl<'trans, 'stmt> Iterator<Result<PostgresRow<'stmt>, PostgresError>> impl<'trans, 'stmt> Iterator<PostgresResult<PostgresRow<'stmt>>>
for PostgresLazyRows<'trans, 'stmt> { for PostgresLazyRows<'trans, 'stmt> {
fn next(&mut self) -> Option<Result<PostgresRow<'stmt>, PostgresError>> { fn next(&mut self) -> Option<PostgresResult<PostgresRow<'stmt>>> {
self.result.try_next() self.result.try_next()
} }

View File

@ -4,12 +4,13 @@ use std::cast;
use sync::{Arc, Mutex}; use sync::{Arc, Mutex};
use {PostgresNotifications, use {PostgresNotifications,
PostgresResult,
PostgresCancelData, PostgresCancelData,
PostgresConnection, PostgresConnection,
PostgresStatement, PostgresStatement,
PostgresTransaction, PostgresTransaction,
SslMode}; SslMode};
use error::{PostgresConnectError, PostgresError}; use error::PostgresConnectError;
use types::ToSql; use types::ToSql;
struct InnerConnectionPool { struct InnerConnectionPool {
@ -130,19 +131,19 @@ impl Drop for PooledPostgresConnection {
impl PooledPostgresConnection { impl PooledPostgresConnection {
/// Like `PostgresConnection::prepare`. /// Like `PostgresConnection::prepare`.
pub fn prepare<'a>(&'a self, query: &str) pub fn prepare<'a>(&'a self, query: &str)
-> Result<PostgresStatement<'a>, PostgresError> { -> PostgresResult<PostgresStatement<'a>> {
self.conn.get_ref().prepare(query) self.conn.get_ref().prepare(query)
} }
/// Like `PostgresConnection::execute`. /// Like `PostgresConnection::execute`.
pub fn execute(&self, query: &str, params: &[&ToSql]) pub fn execute(&self, query: &str, params: &[&ToSql])
-> Result<uint, PostgresError> { -> PostgresResult<uint> {
self.conn.get_ref().execute(query, params) self.conn.get_ref().execute(query, params)
} }
/// Like `PostgresConnection::transaction`. /// Like `PostgresConnection::transaction`.
pub fn transaction<'a>(&'a self) pub fn transaction<'a>(&'a self)
-> Result<PostgresTransaction<'a>, PostgresError> { -> PostgresResult<PostgresTransaction<'a>> {
self.conn.get_ref().transaction() self.conn.get_ref().transaction()
} }