PostgresResult -> Result
This commit is contained in:
parent
b939526d7b
commit
cea15783a5
@ -3,11 +3,12 @@
|
||||
use std::collections::HashMap;
|
||||
use std::io;
|
||||
use std::fmt;
|
||||
use std::result;
|
||||
|
||||
use openssl::ssl::error;
|
||||
use phf;
|
||||
|
||||
use PostgresResult;
|
||||
use Result;
|
||||
use types::PostgresType;
|
||||
|
||||
macro_rules! make_errors(
|
||||
@ -477,7 +478,7 @@ pub struct PostgresDbError {
|
||||
|
||||
impl PostgresDbError {
|
||||
#[doc(hidden)]
|
||||
pub fn new_raw(fields: Vec<(u8, String)>) -> Result<PostgresDbError, ()> {
|
||||
pub fn new_raw(fields: Vec<(u8, String)>) -> result::Result<PostgresDbError, ()> {
|
||||
let mut map: HashMap<_, _> = fields.into_iter().collect();
|
||||
Ok(PostgresDbError {
|
||||
severity: try!(map.pop(&b'S').ok_or(())),
|
||||
@ -508,7 +509,7 @@ impl PostgresDbError {
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub fn new_connect<T>(fields: Vec<(u8, String)>) -> Result<T, PostgresConnectError> {
|
||||
pub fn new_connect<T>(fields: Vec<(u8, String)>) -> result::Result<T, PostgresConnectError> {
|
||||
match PostgresDbError::new_raw(fields) {
|
||||
Ok(err) => Err(PgConnectDbError(err)),
|
||||
Err(()) => Err(PgConnectBadResponse),
|
||||
@ -516,7 +517,7 @@ impl PostgresDbError {
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub fn new<T>(fields: Vec<(u8, String)>) -> PostgresResult<T> {
|
||||
pub fn new<T>(fields: Vec<(u8, String)>) -> Result<T> {
|
||||
match PostgresDbError::new_raw(fields) {
|
||||
Ok(err) => Err(PgDbError(err)),
|
||||
Err(()) => Err(PgBadData),
|
||||
|
134
src/lib.rs
134
src/lib.rs
@ -75,6 +75,7 @@ use std::io::{BufferedStream, IoResult, MemWriter};
|
||||
use std::io::net::ip::Port;
|
||||
use std::mem;
|
||||
use std::fmt;
|
||||
use std::result;
|
||||
|
||||
use error::{InvalidUrl,
|
||||
MissingPassword,
|
||||
@ -150,7 +151,7 @@ pub mod types;
|
||||
const CANARY: u32 = 0xdeadbeef;
|
||||
|
||||
/// A typedef of the result returned by many methods.
|
||||
pub type PostgresResult<T> = Result<T, PostgresError>;
|
||||
pub type Result<T> = result::Result<T, PostgresError>;
|
||||
|
||||
/// Specifies the target server to connect to.
|
||||
#[deriving(Clone)]
|
||||
@ -194,17 +195,17 @@ pub struct PostgresConnectParams {
|
||||
/// `PostgresConnectParams`.
|
||||
pub trait IntoConnectParams {
|
||||
/// Converts the value of `self` into a `PostgresConnectParams`.
|
||||
fn into_connect_params(self) -> Result<PostgresConnectParams, PostgresConnectError>;
|
||||
fn into_connect_params(self) -> result::Result<PostgresConnectParams, PostgresConnectError>;
|
||||
}
|
||||
|
||||
impl IntoConnectParams for PostgresConnectParams {
|
||||
fn into_connect_params(self) -> Result<PostgresConnectParams, PostgresConnectError> {
|
||||
fn into_connect_params(self) -> result::Result<PostgresConnectParams, PostgresConnectError> {
|
||||
Ok(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> IntoConnectParams for &'a str {
|
||||
fn into_connect_params(self) -> Result<PostgresConnectParams, PostgresConnectError> {
|
||||
fn into_connect_params(self) -> result::Result<PostgresConnectParams, PostgresConnectError> {
|
||||
match Url::parse(self) {
|
||||
Ok(url) => url.into_connect_params(),
|
||||
Err(err) => return Err(InvalidUrl(err)),
|
||||
@ -213,7 +214,7 @@ impl<'a> IntoConnectParams for &'a str {
|
||||
}
|
||||
|
||||
impl IntoConnectParams for Url {
|
||||
fn into_connect_params(self) -> Result<PostgresConnectParams, PostgresConnectError> {
|
||||
fn into_connect_params(self) -> result::Result<PostgresConnectParams, PostgresConnectError> {
|
||||
let Url {
|
||||
host,
|
||||
port,
|
||||
@ -331,7 +332,7 @@ pub struct PostgresCancelData {
|
||||
/// postgres::cancel_query(url, &NoSsl, cancel_data);
|
||||
/// ```
|
||||
pub fn cancel_query<T>(params: T, ssl: &SslMode, data: PostgresCancelData)
|
||||
-> Result<(), PostgresConnectError> where T: IntoConnectParams {
|
||||
-> result::Result<(), PostgresConnectError> where T: IntoConnectParams {
|
||||
let params = try!(params.into_connect_params());
|
||||
let mut socket = try!(io::initialize_stream(¶ms, ssl));
|
||||
|
||||
@ -368,7 +369,7 @@ impl Drop for InnerPostgresConnection {
|
||||
|
||||
impl InnerPostgresConnection {
|
||||
fn connect<T>(params: T, ssl: &SslMode)
|
||||
-> Result<InnerPostgresConnection, PostgresConnectError>
|
||||
-> result::Result<InnerPostgresConnection, PostgresConnectError>
|
||||
where T: IntoConnectParams {
|
||||
let params = try!(params.into_connect_params());
|
||||
let stream = try!(io::initialize_stream(¶ms, ssl));
|
||||
@ -461,7 +462,7 @@ impl InnerPostgresConnection {
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_auth(&mut self, user: PostgresUserInfo) -> Result<(), PostgresConnectError> {
|
||||
fn handle_auth(&mut self, user: PostgresUserInfo) -> result::Result<(), PostgresConnectError> {
|
||||
match try_pg_conn!(self.read_message()) {
|
||||
AuthenticationOk => return Ok(()),
|
||||
AuthenticationCleartextPassword => {
|
||||
@ -512,7 +513,7 @@ impl InnerPostgresConnection {
|
||||
}
|
||||
|
||||
fn raw_prepare(&mut self, query: &str)
|
||||
-> PostgresResult<(String, Vec<PostgresType>, Vec<ResultDescription>)> {
|
||||
-> Result<(String, Vec<PostgresType>, Vec<ResultDescription>)> {
|
||||
let stmt_name = format!("s{}", self.next_stmt_id);
|
||||
self.next_stmt_id += 1;
|
||||
|
||||
@ -567,7 +568,7 @@ impl InnerPostgresConnection {
|
||||
}
|
||||
|
||||
fn prepare<'a>(&mut self, query: &str, conn: &'a PostgresConnection)
|
||||
-> PostgresResult<PostgresStatement<'a>> {
|
||||
-> Result<PostgresStatement<'a>> {
|
||||
let (stmt_name, param_types, result_desc) = try!(self.raw_prepare(query));
|
||||
Ok(PostgresStatement {
|
||||
conn: conn,
|
||||
@ -580,7 +581,7 @@ impl InnerPostgresConnection {
|
||||
}
|
||||
|
||||
fn prepare_copy_in<'a>(&mut self, table: &str, rows: &[&str], conn: &'a PostgresConnection)
|
||||
-> PostgresResult<PostgresCopyInStatement<'a>> {
|
||||
-> Result<PostgresCopyInStatement<'a>> {
|
||||
let mut query = MemWriter::new();
|
||||
let _ = write!(query, "SELECT ");
|
||||
let _ = util::comma_join(&mut query, rows.iter().map(|&e| e));
|
||||
@ -606,7 +607,7 @@ impl InnerPostgresConnection {
|
||||
})
|
||||
}
|
||||
|
||||
fn close_statement(&mut self, stmt_name: &str) -> PostgresResult<()> {
|
||||
fn close_statement(&mut self, stmt_name: &str) -> Result<()> {
|
||||
try_pg!(self.write_messages([
|
||||
Close {
|
||||
variant: b'S',
|
||||
@ -626,7 +627,7 @@ impl InnerPostgresConnection {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn set_type_names<'a, I>(&mut self, mut it: I) -> PostgresResult<()>
|
||||
fn set_type_names<'a, I>(&mut self, mut it: I) -> Result<()>
|
||||
where I: Iterator<&'a mut PostgresType> {
|
||||
for ty in it {
|
||||
if let &PgUnknownType { oid, ref mut name } = ty {
|
||||
@ -637,7 +638,7 @@ impl InnerPostgresConnection {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_type_name(&mut self, oid: Oid) -> PostgresResult<String> {
|
||||
fn get_type_name(&mut self, oid: Oid) -> Result<String> {
|
||||
if let Some(name) = self.unknown_types.find(&oid) {
|
||||
return Ok(name.clone());
|
||||
}
|
||||
@ -656,14 +657,14 @@ impl InnerPostgresConnection {
|
||||
self.canary
|
||||
}
|
||||
|
||||
fn wait_for_ready(&mut self) -> PostgresResult<()> {
|
||||
fn wait_for_ready(&mut self) -> Result<()> {
|
||||
match try_pg!(self.read_message()) {
|
||||
ReadyForQuery { .. } => Ok(()),
|
||||
_ => bad_response!(self)
|
||||
}
|
||||
}
|
||||
|
||||
fn quick_query(&mut self, query: &str) -> PostgresResult<Vec<Vec<Option<String>>>> {
|
||||
fn quick_query(&mut self, query: &str) -> Result<Vec<Vec<Option<String>>>> {
|
||||
check_desync!(self);
|
||||
try_pg!(self.write_messages([Query { query: query }]));
|
||||
|
||||
@ -693,7 +694,7 @@ impl InnerPostgresConnection {
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
fn finish_inner(&mut self) -> PostgresResult<()> {
|
||||
fn finish_inner(&mut self) -> Result<()> {
|
||||
check_desync!(self);
|
||||
self.canary = 0;
|
||||
try_pg!(self.write_messages([Terminate]));
|
||||
@ -761,7 +762,8 @@ impl PostgresConnection {
|
||||
/// let conn = try!(PostgresConnection::connect(params, &NoSsl));
|
||||
/// # Ok(()) };
|
||||
/// ```
|
||||
pub fn connect<T>(params: T, ssl: &SslMode) -> Result<PostgresConnection, PostgresConnectError>
|
||||
pub fn connect<T>(params: T, ssl: &SslMode)
|
||||
-> result::Result<PostgresConnection, PostgresConnectError>
|
||||
where T: IntoConnectParams {
|
||||
InnerPostgresConnection::connect(params, ssl).map(|conn| {
|
||||
PostgresConnection { conn: RefCell::new(conn) }
|
||||
@ -800,7 +802,7 @@ impl PostgresConnection {
|
||||
/// Ok(stmt) => stmt,
|
||||
/// Err(err) => panic!("Error preparing statement: {}", err)
|
||||
/// };
|
||||
pub fn prepare<'a>(&'a self, query: &str) -> PostgresResult<PostgresStatement<'a>> {
|
||||
pub fn prepare<'a>(&'a self, query: &str) -> Result<PostgresStatement<'a>> {
|
||||
let mut conn = self.conn.borrow_mut();
|
||||
if conn.trans_depth != 0 {
|
||||
return Err(PgWrongTransaction);
|
||||
@ -832,7 +834,7 @@ impl PostgresConnection {
|
||||
/// # Ok(()) };
|
||||
/// ```
|
||||
pub fn prepare_copy_in<'a>(&'a self, table: &str, rows: &[&str])
|
||||
-> PostgresResult<PostgresCopyInStatement<'a>> {
|
||||
-> Result<PostgresCopyInStatement<'a>> {
|
||||
let mut conn = self.conn.borrow_mut();
|
||||
if conn.trans_depth != 0 {
|
||||
return Err(PgWrongTransaction);
|
||||
@ -864,7 +866,7 @@ impl PostgresConnection {
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn transaction<'a>(&'a self) -> PostgresResult<PostgresTransaction<'a>> {
|
||||
pub fn transaction<'a>(&'a self) -> Result<PostgresTransaction<'a>> {
|
||||
let mut conn = self.conn.borrow_mut();
|
||||
check_desync!(conn);
|
||||
if conn.trans_depth != 0 {
|
||||
@ -886,7 +888,7 @@ impl PostgresConnection {
|
||||
/// or execution of the statement.
|
||||
///
|
||||
/// On success, returns the number of rows modified or 0 if not applicable.
|
||||
pub fn execute(&self, query: &str, params: &[&ToSql]) -> PostgresResult<uint> {
|
||||
pub fn execute(&self, query: &str, params: &[&ToSql]) -> Result<uint> {
|
||||
self.prepare(query).and_then(|stmt| stmt.execute(params))
|
||||
}
|
||||
|
||||
@ -907,8 +909,8 @@ impl PostgresConnection {
|
||||
/// ## Example
|
||||
///
|
||||
/// ```rust,no_run
|
||||
/// # use postgres::{PostgresConnection, PostgresResult};
|
||||
/// fn init_db(conn: &PostgresConnection) -> PostgresResult<()> {
|
||||
/// # use postgres::{PostgresConnection, Result};
|
||||
/// fn init_db(conn: &PostgresConnection) -> Result<()> {
|
||||
/// conn.batch_execute("
|
||||
/// CREATE TABLE person (
|
||||
/// id SERIAL PRIMARY KEY,
|
||||
@ -925,7 +927,7 @@ impl PostgresConnection {
|
||||
/// ")
|
||||
/// }
|
||||
/// ```
|
||||
pub fn batch_execute(&self, query: &str) -> PostgresResult<()> {
|
||||
pub fn batch_execute(&self, query: &str) -> Result<()> {
|
||||
let mut conn = self.conn.borrow_mut();
|
||||
if conn.trans_depth != 0 {
|
||||
return Err(PgWrongTransaction);
|
||||
@ -955,7 +957,7 @@ impl PostgresConnection {
|
||||
/// Functionally equivalent to the `Drop` implementation for
|
||||
/// `PostgresConnection` except that it returns any error encountered to
|
||||
/// the caller.
|
||||
pub fn finish(self) -> PostgresResult<()> {
|
||||
pub fn finish(self) -> Result<()> {
|
||||
let mut conn = self.conn.borrow_mut();
|
||||
conn.finished = true;
|
||||
conn.finish_inner()
|
||||
@ -1000,7 +1002,7 @@ impl<'conn> Drop for PostgresTransaction<'conn> {
|
||||
}
|
||||
|
||||
impl<'conn> PostgresTransaction<'conn> {
|
||||
fn finish_inner(&mut self) -> PostgresResult<()> {
|
||||
fn finish_inner(&mut self) -> Result<()> {
|
||||
let mut conn = self.conn.conn.borrow_mut();
|
||||
debug_assert!(self.depth == conn.trans_depth);
|
||||
let query = match (self.commit.get(), self.depth != 1) {
|
||||
@ -1014,7 +1016,7 @@ impl<'conn> PostgresTransaction<'conn> {
|
||||
}
|
||||
|
||||
/// Like `PostgresConnection::prepare`.
|
||||
pub fn prepare<'a>(&'a self, query: &str) -> PostgresResult<PostgresStatement<'a>> {
|
||||
pub fn prepare<'a>(&'a self, query: &str) -> Result<PostgresStatement<'a>> {
|
||||
let mut conn = self.conn.conn.borrow_mut();
|
||||
if conn.trans_depth != self.depth {
|
||||
return Err(PgWrongTransaction);
|
||||
@ -1024,7 +1026,7 @@ impl<'conn> PostgresTransaction<'conn> {
|
||||
|
||||
/// Like `PostgresConnection::prepare_copy_in`.
|
||||
pub fn prepare_copy_in<'a>(&'a self, table: &str, cols: &[&str])
|
||||
-> PostgresResult<PostgresCopyInStatement<'a>> {
|
||||
-> Result<PostgresCopyInStatement<'a>> {
|
||||
let mut conn = self.conn.conn.borrow_mut();
|
||||
if conn.trans_depth != self.depth {
|
||||
return Err(PgWrongTransaction);
|
||||
@ -1033,12 +1035,12 @@ impl<'conn> PostgresTransaction<'conn> {
|
||||
}
|
||||
|
||||
/// Like `PostgresConnection::execute`.
|
||||
pub fn execute(&self, query: &str, params: &[&ToSql]) -> PostgresResult<uint> {
|
||||
pub fn execute(&self, query: &str, params: &[&ToSql]) -> Result<uint> {
|
||||
self.prepare(query).and_then(|s| s.execute(params))
|
||||
}
|
||||
|
||||
/// Like `PostgresConnection::batch_execute`.
|
||||
pub fn batch_execute(&self, query: &str) -> PostgresResult<()> {
|
||||
pub fn batch_execute(&self, query: &str) -> Result<()> {
|
||||
let mut conn = self.conn.conn.borrow_mut();
|
||||
if conn.trans_depth != self.depth {
|
||||
return Err(PgWrongTransaction);
|
||||
@ -1047,7 +1049,7 @@ impl<'conn> PostgresTransaction<'conn> {
|
||||
}
|
||||
|
||||
/// Like `PostgresConnection::transaction`.
|
||||
pub fn transaction<'a>(&'a self) -> PostgresResult<PostgresTransaction<'a>> {
|
||||
pub fn transaction<'a>(&'a self) -> Result<PostgresTransaction<'a>> {
|
||||
let mut conn = self.conn.conn.borrow_mut();
|
||||
check_desync!(conn);
|
||||
if conn.trans_depth != self.depth {
|
||||
@ -1074,7 +1076,7 @@ impl<'conn> PostgresTransaction<'conn> {
|
||||
stmt: &'stmt PostgresStatement,
|
||||
params: &[&ToSql],
|
||||
row_limit: i32)
|
||||
-> PostgresResult<PostgresLazyRows<'trans, 'stmt>> {
|
||||
-> Result<PostgresLazyRows<'trans, 'stmt>> {
|
||||
if self.conn as *const _ != stmt.conn as *const _ {
|
||||
return Err(PgWrongConnection);
|
||||
}
|
||||
@ -1103,7 +1105,7 @@ impl<'conn> PostgresTransaction<'conn> {
|
||||
}
|
||||
|
||||
/// A convenience method which consumes and commits a transaction.
|
||||
pub fn commit(self) -> PostgresResult<()> {
|
||||
pub fn commit(self) -> Result<()> {
|
||||
self.set_commit();
|
||||
self.finish()
|
||||
}
|
||||
@ -1112,7 +1114,7 @@ impl<'conn> PostgresTransaction<'conn> {
|
||||
///
|
||||
/// Functionally equivalent to the `Drop` implementation of
|
||||
/// `PostgresTransaction` except that it returns any error to the caller.
|
||||
pub fn finish(mut self) -> PostgresResult<()> {
|
||||
pub fn finish(mut self) -> Result<()> {
|
||||
self.finished = true;
|
||||
self.finish_inner()
|
||||
}
|
||||
@ -1138,14 +1140,14 @@ impl<'conn> Drop for PostgresStatement<'conn> {
|
||||
}
|
||||
|
||||
impl<'conn> PostgresStatement<'conn> {
|
||||
fn finish_inner(&mut self) -> PostgresResult<()> {
|
||||
fn finish_inner(&mut self) -> Result<()> {
|
||||
let mut conn = self.conn.conn.borrow_mut();
|
||||
check_desync!(conn);
|
||||
conn.close_statement(self.name[])
|
||||
}
|
||||
|
||||
fn inner_execute(&self, portal_name: &str, row_limit: i32, params: &[&ToSql])
|
||||
-> PostgresResult<()> {
|
||||
-> Result<()> {
|
||||
let mut conn = self.conn.conn.borrow_mut();
|
||||
if self.param_types.len() != params.len() {
|
||||
return Err(PgWrongParamCount {
|
||||
@ -1186,7 +1188,7 @@ impl<'conn> PostgresStatement<'conn> {
|
||||
}
|
||||
|
||||
fn lazy_query<'a>(&'a self, row_limit: i32, params: &[&ToSql])
|
||||
-> PostgresResult<PostgresRows<'a>> {
|
||||
-> Result<PostgresRows<'a>> {
|
||||
let id = self.next_portal_id.get();
|
||||
self.next_portal_id.set(id + 1);
|
||||
let portal_name = format!("{}p{}", self.name, id);
|
||||
@ -1232,7 +1234,7 @@ impl<'conn> PostgresStatement<'conn> {
|
||||
/// Ok(count) => println!("{} row(s) updated", count),
|
||||
/// Err(err) => println!("Error executing query: {}", err)
|
||||
/// }
|
||||
pub fn execute(&self, params: &[&ToSql]) -> PostgresResult<uint> {
|
||||
pub fn execute(&self, params: &[&ToSql]) -> Result<uint> {
|
||||
check_desync!(self.conn);
|
||||
try!(self.inner_execute("", 0, params));
|
||||
|
||||
@ -1290,7 +1292,7 @@ impl<'conn> PostgresStatement<'conn> {
|
||||
/// println!("foo: {}", foo);
|
||||
/// }
|
||||
/// ```
|
||||
pub fn query<'a>(&'a self, params: &[&ToSql]) -> PostgresResult<PostgresRows<'a>> {
|
||||
pub fn query<'a>(&'a self, params: &[&ToSql]) -> Result<PostgresRows<'a>> {
|
||||
check_desync!(self.conn);
|
||||
self.lazy_query(0, params)
|
||||
}
|
||||
@ -1299,7 +1301,7 @@ impl<'conn> PostgresStatement<'conn> {
|
||||
///
|
||||
/// Functionally identical to the `Drop` implementation of the
|
||||
/// `PostgresStatement` except that it returns any error to the caller.
|
||||
pub fn finish(mut self) -> PostgresResult<()> {
|
||||
pub fn finish(mut self) -> Result<()> {
|
||||
self.finished = true;
|
||||
self.finish_inner()
|
||||
}
|
||||
@ -1334,7 +1336,7 @@ impl<'stmt> Drop for PostgresRows<'stmt> {
|
||||
}
|
||||
|
||||
impl<'stmt> PostgresRows<'stmt> {
|
||||
fn finish_inner(&mut self) -> PostgresResult<()> {
|
||||
fn finish_inner(&mut self) -> Result<()> {
|
||||
let mut conn = self.stmt.conn.conn.borrow_mut();
|
||||
check_desync!(conn);
|
||||
try_pg!(conn.write_messages([
|
||||
@ -1358,7 +1360,7 @@ impl<'stmt> PostgresRows<'stmt> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn read_rows(&mut self) -> PostgresResult<()> {
|
||||
fn read_rows(&mut self) -> Result<()> {
|
||||
let mut conn = self.stmt.conn.conn.borrow_mut();
|
||||
loop {
|
||||
match try_pg!(conn.read_message()) {
|
||||
@ -1391,7 +1393,7 @@ impl<'stmt> PostgresRows<'stmt> {
|
||||
conn.wait_for_ready()
|
||||
}
|
||||
|
||||
fn execute(&mut self) -> PostgresResult<()> {
|
||||
fn execute(&mut self) -> Result<()> {
|
||||
try_pg!(self.stmt.conn.write_messages([
|
||||
Execute {
|
||||
portal: self.name[],
|
||||
@ -1405,12 +1407,12 @@ impl<'stmt> PostgresRows<'stmt> {
|
||||
///
|
||||
/// Functionally identical to the `Drop` implementation on `PostgresRows`
|
||||
/// except that it returns any error to the caller.
|
||||
pub fn finish(mut self) -> PostgresResult<()> {
|
||||
pub fn finish(mut self) -> Result<()> {
|
||||
self.finished = true;
|
||||
self.finish_inner()
|
||||
}
|
||||
|
||||
fn try_next(&mut self) -> Option<PostgresResult<PostgresRow<'stmt>>> {
|
||||
fn try_next(&mut self) -> Option<Result<PostgresRow<'stmt>>> {
|
||||
if self.data.is_empty() && self.more_rows {
|
||||
if let Err(err) = self.execute() {
|
||||
return Some(Err(err));
|
||||
@ -1465,7 +1467,7 @@ impl<'stmt> PostgresRow<'stmt> {
|
||||
///
|
||||
/// Returns an `Error` value if the index does not reference a column or
|
||||
/// the return type is not compatible with the Postgres type.
|
||||
pub fn get_opt<I, T>(&self, idx: I) -> PostgresResult<T> where I: RowIndex, T: FromSql {
|
||||
pub fn get_opt<I, T>(&self, idx: I) -> Result<T> where I: RowIndex, T: FromSql {
|
||||
let idx = try!(idx.idx(self.stmt).ok_or(PgInvalidColumn));
|
||||
FromSql::from_sql(&self.stmt.result_desc[idx].ty, &self.data[idx])
|
||||
}
|
||||
@ -1533,15 +1535,15 @@ pub struct PostgresLazyRows<'trans, 'stmt> {
|
||||
impl<'trans, 'stmt> PostgresLazyRows<'trans, 'stmt> {
|
||||
/// Like `PostgresRows::finish`.
|
||||
#[inline]
|
||||
pub fn finish(self) -> PostgresResult<()> {
|
||||
pub fn finish(self) -> Result<()> {
|
||||
self.result.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'trans, 'stmt> Iterator<PostgresResult<PostgresRow<'stmt>>>
|
||||
impl<'trans, 'stmt> Iterator<Result<PostgresRow<'stmt>>>
|
||||
for PostgresLazyRows<'trans, 'stmt> {
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<PostgresResult<PostgresRow<'stmt>>> {
|
||||
fn next(&mut self) -> Option<Result<PostgresRow<'stmt>>> {
|
||||
self.result.try_next()
|
||||
}
|
||||
|
||||
@ -1569,7 +1571,7 @@ impl<'a> Drop for PostgresCopyInStatement<'a> {
|
||||
}
|
||||
|
||||
impl<'a> PostgresCopyInStatement<'a> {
|
||||
fn finish_inner(&mut self) -> PostgresResult<()> {
|
||||
fn finish_inner(&mut self) -> Result<()> {
|
||||
let mut conn = self.conn.conn.borrow_mut();
|
||||
check_desync!(conn);
|
||||
conn.close_statement(self.name[])
|
||||
@ -1586,7 +1588,7 @@ impl<'a> PostgresCopyInStatement<'a> {
|
||||
/// providing a single result row.
|
||||
///
|
||||
/// Returns the number of rows copied.
|
||||
pub fn execute<'b, I, J>(&self, mut rows: I) -> PostgresResult<uint>
|
||||
pub fn execute<'b, I, J>(&self, mut rows: I) -> Result<uint>
|
||||
where I: Iterator<J>, J: Iterator<&'b ToSql + 'b> {
|
||||
let mut conn = self.conn.conn.borrow_mut();
|
||||
|
||||
@ -1703,7 +1705,7 @@ impl<'a> PostgresCopyInStatement<'a> {
|
||||
/// Functionally identical to the `Drop` implementation of the
|
||||
/// `PostgresCopyInStatement` except that it returns any error to the
|
||||
/// caller.
|
||||
pub fn finish(mut self) -> PostgresResult<()> {
|
||||
pub fn finish(mut self) -> Result<()> {
|
||||
self.finished = true;
|
||||
self.finish_inner()
|
||||
}
|
||||
@ -1712,58 +1714,58 @@ impl<'a> PostgresCopyInStatement<'a> {
|
||||
/// A trait allowing abstraction over connections and transactions
|
||||
pub trait GenericConnection {
|
||||
/// Like `PostgresConnection::prepare`.
|
||||
fn prepare<'a>(&'a self, query: &str) -> PostgresResult<PostgresStatement<'a>>;
|
||||
fn prepare<'a>(&'a self, query: &str) -> Result<PostgresStatement<'a>>;
|
||||
|
||||
/// Like `PostgresConnection::execute`.
|
||||
fn execute(&self, query: &str, params: &[&ToSql]) -> PostgresResult<uint> {
|
||||
fn execute(&self, query: &str, params: &[&ToSql]) -> Result<uint> {
|
||||
self.prepare(query).and_then(|s| s.execute(params))
|
||||
}
|
||||
|
||||
/// Like `PostgresConnection::prepare_copy_in`.
|
||||
fn prepare_copy_in<'a>(&'a self, table: &str, columns: &[&str])
|
||||
-> PostgresResult<PostgresCopyInStatement<'a>>;
|
||||
-> Result<PostgresCopyInStatement<'a>>;
|
||||
|
||||
/// Like `PostgresConnection::transaction`.
|
||||
fn transaction<'a>(&'a self) -> PostgresResult<PostgresTransaction<'a>>;
|
||||
fn transaction<'a>(&'a self) -> Result<PostgresTransaction<'a>>;
|
||||
|
||||
/// Like `PostgresConnection::batch_execute`.
|
||||
fn batch_execute(&self, query: &str) -> PostgresResult<()>;
|
||||
fn batch_execute(&self, query: &str) -> Result<()>;
|
||||
}
|
||||
|
||||
impl GenericConnection for PostgresConnection {
|
||||
fn prepare<'a>(&'a self, query: &str) -> PostgresResult<PostgresStatement<'a>> {
|
||||
fn prepare<'a>(&'a self, query: &str) -> Result<PostgresStatement<'a>> {
|
||||
self.prepare(query)
|
||||
}
|
||||
|
||||
fn transaction<'a>(&'a self) -> PostgresResult<PostgresTransaction<'a>> {
|
||||
fn transaction<'a>(&'a self) -> Result<PostgresTransaction<'a>> {
|
||||
self.transaction()
|
||||
}
|
||||
|
||||
fn prepare_copy_in<'a>(&'a self, table: &str, columns: &[&str])
|
||||
-> PostgresResult<PostgresCopyInStatement<'a>> {
|
||||
-> Result<PostgresCopyInStatement<'a>> {
|
||||
self.prepare_copy_in(table, columns)
|
||||
}
|
||||
|
||||
fn batch_execute(&self, query: &str) -> PostgresResult<()> {
|
||||
fn batch_execute(&self, query: &str) -> Result<()> {
|
||||
self.batch_execute(query)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> GenericConnection for PostgresTransaction<'a> {
|
||||
fn prepare<'a>(&'a self, query: &str) -> PostgresResult<PostgresStatement<'a>> {
|
||||
fn prepare<'a>(&'a self, query: &str) -> Result<PostgresStatement<'a>> {
|
||||
self.prepare(query)
|
||||
}
|
||||
|
||||
fn transaction<'a>(&'a self) -> PostgresResult<PostgresTransaction<'a>> {
|
||||
fn transaction<'a>(&'a self) -> Result<PostgresTransaction<'a>> {
|
||||
self.transaction()
|
||||
}
|
||||
|
||||
fn prepare_copy_in<'a>(&'a self, table: &str, columns: &[&str])
|
||||
-> PostgresResult<PostgresCopyInStatement<'a>> {
|
||||
-> Result<PostgresCopyInStatement<'a>> {
|
||||
self.prepare_copy_in(table, columns)
|
||||
}
|
||||
|
||||
fn batch_execute(&self, query: &str) -> PostgresResult<()> {
|
||||
fn batch_execute(&self, query: &str) -> Result<()> {
|
||||
self.batch_execute(query)
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ use std::io::{AsRefReader, MemWriter, BufReader};
|
||||
use std::io::util::LimitReader;
|
||||
use time::Timespec;
|
||||
|
||||
use PostgresResult;
|
||||
use Result;
|
||||
use error::{PgWrongType, PgStreamError, PgWasNull, PgBadData};
|
||||
use types::array::{Array, ArrayBase, DimensionInfo};
|
||||
use types::range::{RangeBound, Inclusive, Exclusive, Range};
|
||||
@ -212,18 +212,18 @@ pub trait FromSql {
|
||||
/// Creates a new value of this type from a buffer of Postgres data.
|
||||
///
|
||||
/// If the value was `NULL`, the buffer will be `None`.
|
||||
fn from_sql(ty: &PostgresType, raw: &Option<Vec<u8>>) -> PostgresResult<Self>;
|
||||
fn from_sql(ty: &PostgresType, raw: &Option<Vec<u8>>) -> Result<Self>;
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
trait RawFromSql {
|
||||
fn raw_from_sql<R: Reader>(raw: &mut R) -> PostgresResult<Self>;
|
||||
fn raw_from_sql<R: Reader>(raw: &mut R) -> Result<Self>;
|
||||
}
|
||||
|
||||
macro_rules! raw_from_impl(
|
||||
($t:ty, $f:ident) => (
|
||||
impl RawFromSql for $t {
|
||||
fn raw_from_sql<R: Reader>(raw: &mut R) -> PostgresResult<$t> {
|
||||
fn raw_from_sql<R: Reader>(raw: &mut R) -> Result<$t> {
|
||||
Ok(try_pg!(raw.$f()))
|
||||
}
|
||||
}
|
||||
@ -231,19 +231,19 @@ macro_rules! raw_from_impl(
|
||||
)
|
||||
|
||||
impl RawFromSql for bool {
|
||||
fn raw_from_sql<R: Reader>(raw: &mut R) -> PostgresResult<bool> {
|
||||
fn raw_from_sql<R: Reader>(raw: &mut R) -> Result<bool> {
|
||||
Ok((try_pg!(raw.read_u8())) != 0)
|
||||
}
|
||||
}
|
||||
|
||||
impl RawFromSql for Vec<u8> {
|
||||
fn raw_from_sql<R: Reader>(raw: &mut R) -> PostgresResult<Vec<u8>> {
|
||||
fn raw_from_sql<R: Reader>(raw: &mut R) -> Result<Vec<u8>> {
|
||||
Ok(try_pg!(raw.read_to_end()))
|
||||
}
|
||||
}
|
||||
|
||||
impl RawFromSql for String {
|
||||
fn raw_from_sql<R: Reader>(raw: &mut R) -> PostgresResult<String> {
|
||||
fn raw_from_sql<R: Reader>(raw: &mut R) -> Result<String> {
|
||||
String::from_utf8(try_pg!(raw.read_to_end())).map_err(|_| PgBadData)
|
||||
}
|
||||
}
|
||||
@ -256,7 +256,7 @@ raw_from_impl!(f32, read_be_f32)
|
||||
raw_from_impl!(f64, read_be_f64)
|
||||
|
||||
impl RawFromSql for Timespec {
|
||||
fn raw_from_sql<R: Reader>(raw: &mut R) -> PostgresResult<Timespec> {
|
||||
fn raw_from_sql<R: Reader>(raw: &mut R) -> Result<Timespec> {
|
||||
let t = try_pg!(raw.read_be_i64());
|
||||
let mut sec = t / USEC_PER_SEC + TIME_SEC_CONVERSION;
|
||||
let mut usec = t % USEC_PER_SEC;
|
||||
@ -273,7 +273,7 @@ impl RawFromSql for Timespec {
|
||||
macro_rules! from_range_impl(
|
||||
($t:ty) => (
|
||||
impl RawFromSql for Range<$t> {
|
||||
fn raw_from_sql<R: Reader>(rdr: &mut R) -> PostgresResult<Range<$t>> {
|
||||
fn raw_from_sql<R: Reader>(rdr: &mut R) -> Result<Range<$t>> {
|
||||
let t = try_pg!(rdr.read_i8());
|
||||
|
||||
if t & RANGE_EMPTY != 0 {
|
||||
@ -322,7 +322,7 @@ from_range_impl!(i64)
|
||||
from_range_impl!(Timespec)
|
||||
|
||||
impl RawFromSql for Json {
|
||||
fn raw_from_sql<R: Reader>(raw: &mut R) -> PostgresResult<Json> {
|
||||
fn raw_from_sql<R: Reader>(raw: &mut R) -> Result<Json> {
|
||||
json::from_reader(raw).map_err(|_| PgBadData)
|
||||
}
|
||||
}
|
||||
@ -331,7 +331,7 @@ macro_rules! from_map_impl(
|
||||
($($expected:pat)|+, $t:ty, $blk:expr) => (
|
||||
impl FromSql for Option<$t> {
|
||||
fn from_sql(ty: &PostgresType, raw: &Option<Vec<u8>>)
|
||||
-> PostgresResult<Option<$t>> {
|
||||
-> Result<Option<$t>> {
|
||||
check_types!($($expected)|+, ty)
|
||||
match *raw {
|
||||
Some(ref buf) => ($blk)(buf).map(|ok| Some(ok)),
|
||||
@ -342,9 +342,9 @@ macro_rules! from_map_impl(
|
||||
|
||||
impl FromSql for $t {
|
||||
fn from_sql(ty: &PostgresType, raw: &Option<Vec<u8>>)
|
||||
-> PostgresResult<$t> {
|
||||
-> Result<$t> {
|
||||
// FIXME when you can specify Self types properly
|
||||
let ret: PostgresResult<Option<$t>> = FromSql::from_sql(ty, raw);
|
||||
let ret: Result<Option<$t>> = FromSql::from_sql(ty, raw);
|
||||
match ret {
|
||||
Ok(Some(val)) => Ok(val),
|
||||
Ok(None) => Err(PgWasNull),
|
||||
@ -432,7 +432,7 @@ from_array_impl!(PgInt8RangeArray, Range<i64>)
|
||||
|
||||
impl FromSql for Option<HashMap<String, Option<String>>> {
|
||||
fn from_sql(ty: &PostgresType, raw: &Option<Vec<u8>>)
|
||||
-> PostgresResult<Option<HashMap<String, Option<String>>>> {
|
||||
-> Result<Option<HashMap<String, Option<String>>>> {
|
||||
match *ty {
|
||||
PgUnknownType { ref name, .. } if "hstore" == name[] => {}
|
||||
_ => return Err(PgWrongType(ty.clone()))
|
||||
@ -475,9 +475,9 @@ impl FromSql for Option<HashMap<String, Option<String>>> {
|
||||
|
||||
impl FromSql for HashMap<String, Option<String>> {
|
||||
fn from_sql(ty: &PostgresType, raw: &Option<Vec<u8>>)
|
||||
-> PostgresResult<HashMap<String, Option<String>>> {
|
||||
-> Result<HashMap<String, Option<String>>> {
|
||||
// FIXME when you can specify Self types properly
|
||||
let ret: PostgresResult<Option<HashMap<String, Option<String>>>> =
|
||||
let ret: Result<Option<HashMap<String, Option<String>>>> =
|
||||
FromSql::from_sql(ty, raw);
|
||||
match ret {
|
||||
Ok(Some(val)) => Ok(val),
|
||||
@ -491,18 +491,18 @@ impl FromSql for HashMap<String, Option<String>> {
|
||||
pub trait ToSql {
|
||||
/// Converts the value of `self` into the binary format appropriate for the
|
||||
/// Postgres backend.
|
||||
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<Option<Vec<u8>>>;
|
||||
fn to_sql(&self, ty: &PostgresType) -> Result<Option<Vec<u8>>>;
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
trait RawToSql {
|
||||
fn raw_to_sql<W: Writer>(&self, w: &mut W) -> PostgresResult<()>;
|
||||
fn raw_to_sql<W: Writer>(&self, w: &mut W) -> Result<()>;
|
||||
}
|
||||
|
||||
macro_rules! raw_to_impl(
|
||||
($t:ty, $f:ident) => (
|
||||
impl RawToSql for $t {
|
||||
fn raw_to_sql<W: Writer>(&self, w: &mut W) -> PostgresResult<()> {
|
||||
fn raw_to_sql<W: Writer>(&self, w: &mut W) -> Result<()> {
|
||||
Ok(try_pg!(w.$f(*self)))
|
||||
}
|
||||
}
|
||||
@ -510,19 +510,19 @@ macro_rules! raw_to_impl(
|
||||
)
|
||||
|
||||
impl RawToSql for bool {
|
||||
fn raw_to_sql<W: Writer>(&self, w: &mut W) -> PostgresResult<()> {
|
||||
fn raw_to_sql<W: Writer>(&self, w: &mut W) -> Result<()> {
|
||||
Ok(try_pg!(w.write_u8(*self as u8)))
|
||||
}
|
||||
}
|
||||
|
||||
impl RawToSql for Vec<u8> {
|
||||
fn raw_to_sql<W: Writer>(&self, w: &mut W) -> PostgresResult<()> {
|
||||
fn raw_to_sql<W: Writer>(&self, w: &mut W) -> Result<()> {
|
||||
Ok(try_pg!(w.write(self[])))
|
||||
}
|
||||
}
|
||||
|
||||
impl RawToSql for String {
|
||||
fn raw_to_sql<W: Writer>(&self, w: &mut W) -> PostgresResult<()> {
|
||||
fn raw_to_sql<W: Writer>(&self, w: &mut W) -> Result<()> {
|
||||
Ok(try_pg!(w.write(self.as_bytes())))
|
||||
}
|
||||
}
|
||||
@ -535,7 +535,7 @@ raw_to_impl!(f32, write_be_f32)
|
||||
raw_to_impl!(f64, write_be_f64)
|
||||
|
||||
impl RawToSql for Timespec {
|
||||
fn raw_to_sql<W: Writer>(&self, w: &mut W) -> PostgresResult<()> {
|
||||
fn raw_to_sql<W: Writer>(&self, w: &mut W) -> Result<()> {
|
||||
let t = (self.sec - TIME_SEC_CONVERSION) * USEC_PER_SEC + self.nsec as i64 / NSEC_PER_USEC;
|
||||
Ok(try_pg!(w.write_be_i64(t)))
|
||||
}
|
||||
@ -544,7 +544,7 @@ impl RawToSql for Timespec {
|
||||
macro_rules! to_range_impl(
|
||||
($t:ty) => (
|
||||
impl RawToSql for Range<$t> {
|
||||
fn raw_to_sql<W: Writer>(&self, buf: &mut W) -> PostgresResult<()> {
|
||||
fn raw_to_sql<W: Writer>(&self, buf: &mut W) -> Result<()> {
|
||||
let mut tag = 0;
|
||||
if self.is_empty() {
|
||||
tag |= RANGE_EMPTY;
|
||||
@ -595,7 +595,7 @@ to_range_impl!(i64)
|
||||
to_range_impl!(Timespec)
|
||||
|
||||
impl RawToSql for Json {
|
||||
fn raw_to_sql<W: Writer>(&self, raw: &mut W) -> PostgresResult<()> {
|
||||
fn raw_to_sql<W: Writer>(&self, raw: &mut W) -> Result<()> {
|
||||
Ok(try_pg!(self.to_writer(raw as &mut Writer)))
|
||||
}
|
||||
}
|
||||
@ -603,7 +603,7 @@ impl RawToSql for Json {
|
||||
macro_rules! to_option_impl(
|
||||
($($oid:pat)|+, $t:ty) => (
|
||||
impl ToSql for Option<$t> {
|
||||
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<Option<Vec<u8>>> {
|
||||
fn to_sql(&self, ty: &PostgresType) -> Result<Option<Vec<u8>>> {
|
||||
check_types!($($oid)|+, ty)
|
||||
|
||||
match *self {
|
||||
@ -618,7 +618,7 @@ macro_rules! to_option_impl(
|
||||
macro_rules! to_option_impl_lifetime(
|
||||
($($oid:pat)|+, $t:ty) => (
|
||||
impl<'a> ToSql for Option<$t> {
|
||||
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<Option<Vec<u8>>> {
|
||||
fn to_sql(&self, ty: &PostgresType) -> Result<Option<Vec<u8>>> {
|
||||
check_types!($($oid)|+, ty)
|
||||
|
||||
match *self {
|
||||
@ -633,7 +633,7 @@ macro_rules! to_option_impl_lifetime(
|
||||
macro_rules! to_raw_to_impl(
|
||||
($($oid:ident)|+, $t:ty) => (
|
||||
impl ToSql for $t {
|
||||
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<Option<Vec<u8>>> {
|
||||
fn to_sql(&self, ty: &PostgresType) -> Result<Option<Vec<u8>>> {
|
||||
check_types!($($oid)|+, ty)
|
||||
|
||||
let mut writer = MemWriter::new();
|
||||
@ -661,7 +661,7 @@ to_raw_to_impl!(PgInt8Range, Range<i64>)
|
||||
to_raw_to_impl!(PgTsRange | PgTstzRange, Range<Timespec>)
|
||||
|
||||
impl<'a> ToSql for &'a str {
|
||||
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<Option<Vec<u8>>> {
|
||||
fn to_sql(&self, ty: &PostgresType) -> Result<Option<Vec<u8>>> {
|
||||
check_types!(PgVarchar | PgText | PgCharN | PgName, ty)
|
||||
Ok(Some(self.as_bytes().to_vec()))
|
||||
}
|
||||
@ -670,7 +670,7 @@ impl<'a> ToSql for &'a str {
|
||||
to_option_impl_lifetime!(PgVarchar | PgText | PgCharN | PgName, &'a str)
|
||||
|
||||
impl<'a> ToSql for &'a [u8] {
|
||||
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<Option<Vec<u8>>> {
|
||||
fn to_sql(&self, ty: &PostgresType) -> Result<Option<Vec<u8>>> {
|
||||
check_types!(PgByteA, ty)
|
||||
Ok(Some(self.to_vec()))
|
||||
}
|
||||
@ -683,7 +683,7 @@ to_raw_to_impl!(PgTimestamp | PgTimestampTZ, Timespec)
|
||||
macro_rules! to_array_impl(
|
||||
($($oid:ident)|+, $t:ty) => (
|
||||
impl ToSql for ArrayBase<Option<$t>> {
|
||||
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<Option<Vec<u8>>> {
|
||||
fn to_sql(&self, ty: &PostgresType) -> Result<Option<Vec<u8>>> {
|
||||
check_types!($($oid)|+, ty)
|
||||
let mut buf = MemWriter::new();
|
||||
|
||||
@ -733,7 +733,7 @@ to_array_impl!(PgInt8RangeArray, Range<i64>)
|
||||
to_array_impl!(PgJsonArray, Json)
|
||||
|
||||
impl ToSql for HashMap<String, Option<String>> {
|
||||
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<Option<Vec<u8>>> {
|
||||
fn to_sql(&self, ty: &PostgresType) -> Result<Option<Vec<u8>>> {
|
||||
match *ty {
|
||||
PgUnknownType { ref name, .. } if "hstore" == name[] => {}
|
||||
_ => return Err(PgWrongType(ty.clone()))
|
||||
@ -761,7 +761,7 @@ impl ToSql for HashMap<String, Option<String>> {
|
||||
}
|
||||
|
||||
impl ToSql for Option<HashMap<String, Option<String>>> {
|
||||
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<Option<Vec<u8>>> {
|
||||
fn to_sql(&self, ty: &PostgresType) -> Result<Option<Vec<u8>>> {
|
||||
match *ty {
|
||||
PgUnknownType { ref name, .. } if "hstore" == name[] => {}
|
||||
_ => return Err(PgWrongType(ty.clone()))
|
||||
|
Loading…
Reference in New Issue
Block a user