From fc8ab8779770fd3409f1cdb492e0946f48c97ecd Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Tue, 15 Dec 2015 22:12:43 -0800 Subject: [PATCH] Cleanup --- src/notification.rs | 2 +- src/rows.rs | 57 +++++++++++++++++++++++++-------------------- src/stmt.rs | 24 +++++++++---------- 3 files changed, 45 insertions(+), 38 deletions(-) diff --git a/src/notification.rs b/src/notification.rs index 2ba7df53..610ea9f9 100644 --- a/src/notification.rs +++ b/src/notification.rs @@ -18,7 +18,7 @@ pub struct Notification { pub payload: String, } -/// An iterator over asynchronous notifications. +/// Notifications from the Postgres backend. pub struct Notifications<'conn> { conn: &'conn Connection, } diff --git a/src/rows.rs b/src/rows.rs index 4be49720..5496dbe7 100644 --- a/src/rows.rs +++ b/src/rows.rs @@ -163,29 +163,6 @@ impl<'a> Row<'a> { self.stmt.columns() } - /// Retrieves the contents of a field of the row. - /// - /// A field can be accessed by the name or index of its column, though - /// access by index is more efficient. Rows are 0-indexed. - /// - /// 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(&self, idx: I) -> Result - where I: RowIndex, - T: FromSql - { - let idx = try!(idx.idx(self.stmt).ok_or(Error::InvalidColumn)); - let ty = self.stmt.columns()[idx].type_(); - if !::accepts(ty) { - return Err(Error::WrongType(ty.clone())); - } - let conn = self.stmt.conn().conn.borrow(); - match self.data[idx] { - Some(ref data) => FromSql::from_sql(ty, &mut &**data, &SessionInfo::new(&*conn)), - None => FromSql::from_sql_null(ty, &SessionInfo::new(&*conn)), - } - } - /// Retrieves the contents of a field of the row. /// /// A field can be accessed by the name or index of its column, though @@ -209,15 +186,45 @@ impl<'a> Row<'a> { /// } /// ``` pub fn get(&self, idx: I) -> T - where I: RowIndex + fmt::Debug + Clone, + where I: RowIndex + fmt::Debug, T: FromSql { - match self.get_opt(idx.clone()) { + match self.get_inner(&idx) { Ok(ok) => ok, Err(err) => panic!("error retrieving column {:?}: {:?}", idx, err), } } + /// Retrieves the contents of a field of the row. + /// + /// A field can be accessed by the name or index of its column, though + /// access by index is more efficient. Rows are 0-indexed. + /// + /// 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(&self, idx: I) -> Result + where I: RowIndex, + T: FromSql + { + self.get_inner(&idx) + } + + fn get_inner(&self, idx: &I) -> Result + where I: RowIndex, + T: FromSql + { + let idx = try!(idx.idx(self.stmt).ok_or(Error::InvalidColumn)); + let ty = self.stmt.columns()[idx].type_(); + if !::accepts(ty) { + return Err(Error::WrongType(ty.clone())); + } + let conn = self.stmt.conn().conn.borrow(); + match self.data[idx] { + Some(ref data) => FromSql::from_sql(ty, &mut &**data, &SessionInfo::new(&*conn)), + None => FromSql::from_sql_null(ty, &SessionInfo::new(&*conn)), + } + } + /// Retrieves the specified field as a raw buffer of Postgres data. /// /// # Panics diff --git a/src/stmt.rs b/src/stmt.rs index f9e05b1a..40a41148 100644 --- a/src/stmt.rs +++ b/src/stmt.rs @@ -538,6 +538,18 @@ impl ColumnNew for Column { } } +impl Column { + /// The name of the column. + pub fn name(&self) -> &str { + &self.name + } + + /// The type of the data in the column. + pub fn type_(&self) -> &Type { + &self.type_ + } +} + /// A struct containing information relevant for a `COPY` operation. pub struct CopyInfo<'a> { conn: RefMut<'a, InnerConnection>, @@ -590,18 +602,6 @@ impl WriteWithInfo for W { } } -impl Column { - /// The name of the column. - pub fn name(&self) -> &str { - &self.name - } - - /// The type of the data in the column. - pub fn type_(&self) -> &Type { - &self.type_ - } -} - /// The format of a portion of COPY query data. #[derive(Debug, Copy, Clone, PartialEq, Eq)] pub enum Format {