From a03e770141da69027aff43c1b47dabdaa20af6dd Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Mon, 26 Dec 2016 15:50:36 -0500 Subject: [PATCH] Move row out --- postgres-tokio/src/lib.rs | 55 ++++---------------------- postgres-tokio/src/rows.rs | 64 +++++++++++++++++++++++++++++++ postgres-tokio/src/transaction.rs | 3 +- 3 files changed, 74 insertions(+), 48 deletions(-) create mode 100644 postgres-tokio/src/rows.rs diff --git a/postgres-tokio/src/lib.rs b/postgres-tokio/src/lib.rs index c8fa1de7..07cee1e5 100644 --- a/postgres-tokio/src/lib.rs +++ b/postgres-tokio/src/lib.rs @@ -21,7 +21,6 @@ use postgres_protocol::message::{backend, frontend}; use postgres_protocol::message::backend::{ErrorResponseBody, ErrorFields}; use postgres_shared::RowData; use std::collections::HashMap; -use std::error::Error as StdError; use std::fmt; use std::io; use std::mem; @@ -37,9 +36,11 @@ use params::{ConnectParams, IntoConnectParams}; use stream::PostgresStream; use tls::Handshake; use transaction::Transaction; -use types::{Oid, Type, ToSql, SessionInfo, IsNull, FromSql, WrongType, Other, Kind, Field}; +use types::{Oid, Type, ToSql, SessionInfo, IsNull, FromSql, Other, Kind, Field}; +use rows::Row; pub mod error; +pub mod rows; mod stream; pub mod tls; pub mod transaction; @@ -900,7 +901,7 @@ impl Connection { -> BoxStateStream { let columns = statement.columns.clone(); self.raw_execute(&statement.name, "", &statement.params, params) - .map(|c| c.read_rows().map(move |r| Row { columns: columns.clone(), data: r })) + .map(|c| c.read_rows().map(move |r| Row::new(columns.clone(), r))) .flatten_state_stream() .boxed() } @@ -949,50 +950,6 @@ impl Statement { } } -pub struct Row { - columns: Arc>, - data: RowData, -} - -impl Row { - pub fn columns(&self) -> &[Column] { - &self.columns - } - - pub fn len(&self) -> usize { - self.columns.len() - } - - pub fn get(&self, idx: I) -> T - where T: FromSql, - I: RowIndex + fmt::Debug - { - match self.try_get(&idx) { - Ok(Some(v)) => v, - Ok(None) => panic!("no such column {:?}", idx), - Err(e) => panic!("error retrieving row {:?}: {}", idx, e), - } - } - - pub fn try_get(&self, idx: I) -> Result, Box> - where T: FromSql, - I: RowIndex - { - let idx = match idx.idx(&self.columns) { - Some(idx) => idx, - None => return Ok(None), - }; - - let ty = self.columns[idx].type_(); - if !T::accepts(ty) { - return Err(Box::new(WrongType::new(ty.clone()))); - } - - // FIXME - T::from_sql_nullable(ty, self.data.get(idx), &SessionInfo::new(&HashMap::new())).map(Some) - } -} - fn connect_err(fields: &mut ErrorFields) -> ConnectError { match DbError::new(fields) { Ok(err) => ConnectError::Db(Box::new(err)), @@ -1006,6 +963,10 @@ fn bad_message() -> T io::Error::new(io::ErrorKind::InvalidInput, "unexpected message").into() } +trait RowNew { + fn new(columns: Arc>, data: RowData) -> Row; +} + trait TransactionNew { fn new(c: Connection) -> Transaction; } diff --git a/postgres-tokio/src/rows.rs b/postgres-tokio/src/rows.rs new file mode 100644 index 00000000..238d6882 --- /dev/null +++ b/postgres-tokio/src/rows.rs @@ -0,0 +1,64 @@ +use postgres_shared::{RowData, Column}; +use std::collections::HashMap; +use std::error::Error; +use std::fmt; +use std::sync::Arc; + +#[doc(inline)] +pub use postgres_shared::RowIndex; + +use RowNew; +use types::{WrongType, FromSql, SessionInfo}; + +pub struct Row { + columns: Arc>, + data: RowData, +} + +impl RowNew for Row { + fn new(columns: Arc>, data: RowData) -> Row { + Row { + columns: columns, + data: data, + } + } +} + +impl Row { + pub fn columns(&self) -> &[Column] { + &self.columns + } + + pub fn len(&self) -> usize { + self.columns.len() + } + + pub fn get(&self, idx: I) -> T + where T: FromSql, + I: RowIndex + fmt::Debug + { + match self.try_get(&idx) { + Ok(Some(v)) => v, + Ok(None) => panic!("no such column {:?}", idx), + Err(e) => panic!("error retrieving row {:?}: {}", idx, e), + } + } + + pub fn try_get(&self, idx: I) -> Result, Box> + where T: FromSql, + I: RowIndex + { + let idx = match idx.idx(&self.columns) { + Some(idx) => idx, + None => return Ok(None), + }; + + let ty = self.columns[idx].type_(); + if !T::accepts(ty) { + return Err(Box::new(WrongType::new(ty.clone()))); + } + + // FIXME + T::from_sql_nullable(ty, self.data.get(idx), &SessionInfo::new(&HashMap::new())).map(Some) + } +} diff --git a/postgres-tokio/src/transaction.rs b/postgres-tokio/src/transaction.rs index de4dff1f..31d1d812 100644 --- a/postgres-tokio/src/transaction.rs +++ b/postgres-tokio/src/transaction.rs @@ -1,9 +1,10 @@ use futures::{Future, BoxFuture}; use futures_state_stream::{StateStream, BoxStateStream}; -use {Connection, Statement, Row, TransactionNew}; +use {Connection, Statement, TransactionNew}; use error::Error; use types::ToSql; +use rows::Row; #[derive(Debug)] pub struct Transaction(Connection);