From 2562d0179651ad1a46bcc26f8ceb695e1fe53b17 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Mon, 26 Dec 2016 16:29:30 -0500 Subject: [PATCH] Shift stuff around --- postgres-shared/src/lib.rs | 115 +----------------------------------- postgres-shared/src/rows.rs | 90 ++++++++++++++++++++++++++++ postgres-shared/src/stmt.rs | 24 ++++++++ postgres-tokio/src/lib.rs | 2 +- postgres-tokio/src/rows.rs | 5 +- postgres-tokio/src/stmt.rs | 2 +- postgres/src/lib.rs | 2 +- postgres/src/rows.rs | 2 +- postgres/src/stmt.rs | 2 +- 9 files changed, 124 insertions(+), 120 deletions(-) create mode 100644 postgres-shared/src/rows.rs create mode 100644 postgres-shared/src/stmt.rs diff --git a/postgres-shared/src/lib.rs b/postgres-shared/src/lib.rs index bc5b290f..8218efb5 100644 --- a/postgres-shared/src/lib.rs +++ b/postgres-shared/src/lib.rs @@ -5,15 +5,11 @@ extern crate fallible_iterator; extern crate phf; extern crate postgres_protocol; -use fallible_iterator::{FallibleIterator, FromFallibleIterator}; -use std::ascii::AsciiExt; -use std::ops::Range; - -use types::Type; - pub mod error; pub mod params; pub mod types; +pub mod rows; +pub mod stmt; /// Contains information necessary to cancel queries for a session. #[derive(Copy, Clone, Debug)] @@ -23,110 +19,3 @@ pub struct CancelData { /// The secret key for the session. pub secret_key: i32, } - -pub struct RowData { - buf: Vec, - indices: Vec>>, -} - -impl<'a> FromFallibleIterator> for RowData { - fn from_fallible_iterator(mut it: I) -> Result - where I: FallibleIterator> - { - let mut row = RowData { - buf: vec![], - indices: Vec::with_capacity(it.size_hint().0), - }; - - while let Some(cell) = try!(it.next()) { - let index = match cell { - Some(cell) => { - let base = row.buf.len(); - row.buf.extend_from_slice(cell); - Some(base..row.buf.len()) - } - None => None, - }; - row.indices.push(index); - } - - Ok(row) - } -} - -impl RowData { - pub fn len(&self) -> usize { - self.indices.len() - } - - pub fn get(&self, index: usize) -> Option<&[u8]> { - match &self.indices[index] { - &Some(ref range) => Some(&self.buf[range.clone()]), - &None => None, - } - } -} - -pub struct Column { - name: String, - type_: Type, -} - -impl Column { - #[doc(hidden)] - pub fn new(name: String, type_: Type) -> Column { - Column { - name: name, - type_: type_, - } - } - - pub fn name(&self) -> &str { - &self.name - } - - pub fn type_(&self) -> &Type { - &self.type_ - } -} - -/// A trait implemented by types that can index into columns of a row. -pub trait RowIndex { - /// Returns the index of the appropriate column, or `None` if no such - /// column exists. - fn idx(&self, stmt: &[Column]) -> Option; -} - -impl RowIndex for usize { - #[inline] - fn idx(&self, stmt: &[Column]) -> Option { - if *self >= stmt.len() { - None - } else { - Some(*self) - } - } -} - -impl<'a> RowIndex for str { - #[inline] - fn idx(&self, stmt: &[Column]) -> Option { - if let Some(idx) = stmt.iter().position(|d| d.name() == self) { - return Some(idx); - }; - - // FIXME ASCII-only case insensitivity isn't really the right thing to - // do. Postgres itself uses a dubious wrapper around tolower and JDBC - // uses the US locale. - stmt.iter().position(|d| d.name().eq_ignore_ascii_case(self)) - } -} - -impl<'a, T: ?Sized> RowIndex for &'a T - where T: RowIndex -{ - #[inline] - fn idx(&self, columns: &[Column]) -> Option { - T::idx(*self, columns) - } -} diff --git a/postgres-shared/src/rows.rs b/postgres-shared/src/rows.rs new file mode 100644 index 00000000..6ba2357b --- /dev/null +++ b/postgres-shared/src/rows.rs @@ -0,0 +1,90 @@ +use fallible_iterator::{FallibleIterator, FromFallibleIterator}; +use std::ascii::AsciiExt; +use std::ops::Range; + +use stmt::Column; + +/// A trait implemented by types that can index into columns of a row. +pub trait RowIndex { + /// Returns the index of the appropriate column, or `None` if no such + /// column exists. + fn idx(&self, stmt: &[Column]) -> Option; +} + +impl RowIndex for usize { + #[inline] + fn idx(&self, stmt: &[Column]) -> Option { + if *self >= stmt.len() { + None + } else { + Some(*self) + } + } +} + +impl<'a> RowIndex for str { + #[inline] + fn idx(&self, stmt: &[Column]) -> Option { + if let Some(idx) = stmt.iter().position(|d| d.name() == self) { + return Some(idx); + }; + + // FIXME ASCII-only case insensitivity isn't really the right thing to + // do. Postgres itself uses a dubious wrapper around tolower and JDBC + // uses the US locale. + stmt.iter().position(|d| d.name().eq_ignore_ascii_case(self)) + } +} + +impl<'a, T: ?Sized> RowIndex for &'a T +where T: RowIndex +{ + #[inline] + fn idx(&self, columns: &[Column]) -> Option { + T::idx(*self, columns) + } +} + +#[doc(hidden)] +pub struct RowData { + buf: Vec, + indices: Vec>>, +} + +impl<'a> FromFallibleIterator> for RowData { + fn from_fallible_iterator(mut it: I) -> Result + where I: FallibleIterator> + { + let mut row = RowData { + buf: vec![], + indices: Vec::with_capacity(it.size_hint().0), + }; + + while let Some(cell) = try!(it.next()) { + let index = match cell { + Some(cell) => { + let base = row.buf.len(); + row.buf.extend_from_slice(cell); + Some(base..row.buf.len()) + } + None => None, + }; + row.indices.push(index); + } + + Ok(row) + } +} + +impl RowData { + pub fn len(&self) -> usize { + self.indices.len() + } + + pub fn get(&self, index: usize) -> Option<&[u8]> { + match &self.indices[index] { + &Some(ref range) => Some(&self.buf[range.clone()]), + &None => None, + } + } +} diff --git a/postgres-shared/src/stmt.rs b/postgres-shared/src/stmt.rs new file mode 100644 index 00000000..8493ad0a --- /dev/null +++ b/postgres-shared/src/stmt.rs @@ -0,0 +1,24 @@ +use types::Type; + +pub struct Column { + name: String, + type_: Type, +} + +impl Column { + #[doc(hidden)] + pub fn new(name: String, type_: Type) -> Column { + Column { + name: name, + type_: type_, + } + } + + pub fn name(&self) -> &str { + &self.name + } + + pub fn type_(&self) -> &Type { + &self.type_ + } +} diff --git a/postgres-tokio/src/lib.rs b/postgres-tokio/src/lib.rs index 6294cf2d..a04b05e4 100644 --- a/postgres-tokio/src/lib.rs +++ b/postgres-tokio/src/lib.rs @@ -22,7 +22,7 @@ use futures_state_stream::{StreamEvent, StateStream, BoxStateStream, FutureExt}; use postgres_protocol::authentication; use postgres_protocol::message::{backend, frontend}; use postgres_protocol::message::backend::{ErrorResponseBody, ErrorFields}; -use postgres_shared::RowData; +use postgres_shared::rows::RowData; use std::collections::HashMap; use std::fmt; use std::io; diff --git a/postgres-tokio/src/rows.rs b/postgres-tokio/src/rows.rs index 8705fb9c..40606355 100644 --- a/postgres-tokio/src/rows.rs +++ b/postgres-tokio/src/rows.rs @@ -1,13 +1,14 @@ //! Postgres rows. -use postgres_shared::{RowData, Column}; +use postgres_shared::rows::RowData; +use postgres_shared::stmt::Column; use std::collections::HashMap; use std::error::Error; use std::fmt; use std::sync::Arc; #[doc(inline)] -pub use postgres_shared::RowIndex; +pub use postgres_shared::rows::RowIndex; use RowNew; use types::{WrongType, FromSql, SessionInfo}; diff --git a/postgres-tokio/src/stmt.rs b/postgres-tokio/src/stmt.rs index 0bf17275..2edb0073 100644 --- a/postgres-tokio/src/stmt.rs +++ b/postgres-tokio/src/stmt.rs @@ -5,7 +5,7 @@ use std::sync::Arc; use std::sync::mpsc::Sender; #[doc(inline)] -pub use postgres_shared::Column; +pub use postgres_shared::stmt::Column; use StatementNew; use types::Type; diff --git a/postgres/src/lib.rs b/postgres/src/lib.rs index 987f93c9..857f3480 100644 --- a/postgres/src/lib.rs +++ b/postgres/src/lib.rs @@ -91,7 +91,7 @@ use std::time::Duration; use postgres_protocol::authentication; use postgres_protocol::message::backend::{self, ErrorFields}; use postgres_protocol::message::frontend; -use postgres_shared::RowData; +use postgres_shared::rows::RowData; use error::{Error, ConnectError, SqlState, DbError}; use tls::TlsHandshake; diff --git a/postgres/src/rows.rs b/postgres/src/rows.rs index 1c3a47a0..35c7e09d 100644 --- a/postgres/src/rows.rs +++ b/postgres/src/rows.rs @@ -2,7 +2,7 @@ use fallible_iterator::FallibleIterator; use postgres_protocol::message::frontend; -use postgres_shared::RowData; +use postgres_shared::rows::RowData; use std::ascii::AsciiExt; use std::collections::VecDeque; use std::fmt; diff --git a/postgres/src/stmt.rs b/postgres/src/stmt.rs index 88761b18..6c3a011e 100644 --- a/postgres/src/stmt.rs +++ b/postgres/src/stmt.rs @@ -7,7 +7,7 @@ use std::fmt; use std::io::{self, Read, Write}; use std::sync::Arc; use postgres_protocol::message::{backend, frontend}; -use postgres_shared::RowData; +use postgres_shared::rows::RowData; use error::Error; use types::{SessionInfo, Type, ToSql};