From 237ae8c16ab363f6bc1c2bf17f34d3075a7ff7ab Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sun, 29 Sep 2013 23:44:45 -0700 Subject: [PATCH] Move convenience methods to default methods --- src/postgres/lib.rs | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/src/postgres/lib.rs b/src/postgres/lib.rs index a803d753..704742f9 100644 --- a/src/postgres/lib.rs +++ b/src/postgres/lib.rs @@ -716,7 +716,12 @@ pub trait PostgresStatement { /// A convenience function wrapping `try_update`. /// /// Fails if there was an error executing the statement. - fn update(&self, params: &[&ToSql]) -> uint; + fn update(&self, params: &[&ToSql]) -> uint { + match self.try_update(params) { + Ok(count) => count, + Err(err) => fail2!("Error running update\n{}", err.to_str()) + } + } /// Attempts to execute the prepared statement, returning an iterator over /// the resulting rows. @@ -729,7 +734,12 @@ pub trait PostgresStatement { /// A convenience function wrapping `try_query`. /// /// Fails if there was an error executing the statement. - fn query<'a>(&'a self, params: &[&ToSql]) -> PostgresResult<'a>; + fn query<'a>(&'a self, params: &[&ToSql]) -> PostgresResult<'a> { + match self.try_query(params) { + Ok(result) => result, + Err(err) => fail2!("Error executing query:\n{}", err.to_str()) + } + } /// Returns the index of the first result column with the specified name, /// or `None` if not found. @@ -850,13 +860,6 @@ impl<'self> PostgresStatement for NormalPostgresStatement<'self> { self.result_desc.as_slice() } - fn update(&self, params: &[&ToSql]) -> uint { - match self.try_update(params) { - Ok(count) => count, - Err(err) => fail2!("Error running update\n{}", err.to_str()) - } - } - fn try_update(&self, params: &[&ToSql]) -> Result { match self.execute("", 0, params) { @@ -895,11 +898,6 @@ impl<'self> PostgresStatement for NormalPostgresStatement<'self> { Ok(num) } - fn query<'a>(&'a self, params: &[&ToSql]) - -> PostgresResult<'a> { - self.lazy_query(0, params) - } - fn try_query<'a>(&'a self, params: &[&ToSql]) -> Result, PostgresDbError> { self.try_lazy_query(0, params) @@ -947,18 +945,10 @@ impl<'self> PostgresStatement for TransactionalPostgresStatement<'self> { (**self).result_descriptions() } - fn update(&self, params: &[&ToSql]) -> uint { - (**self).update(params) - } - fn try_update(&self, params: &[&ToSql]) -> Result { (**self).try_update(params) } - fn query<'a>(&'a self, params: &[&ToSql]) -> PostgresResult<'a> { - (**self).query(params) - } - fn try_query<'a>(&'a self, params: &[&ToSql]) -> Result, PostgresDbError> { (**self).try_query(params)