Shift stuff around

This commit is contained in:
Steven Fackler
2016-12-26 16:29:30 -05:00
parent 2dc80bec2e
commit 2562d01796
9 changed files with 124 additions and 120 deletions

View File

@@ -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<u8>,
indices: Vec<Option<Range<usize>>>,
}
impl<'a> FromFallibleIterator<Option<&'a [u8]>> for RowData {
fn from_fallible_iterator<I>(mut it: I) -> Result<RowData, I::Error>
where I: FallibleIterator<Item = Option<&'a [u8]>>
{
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<usize>;
}
impl RowIndex for usize {
#[inline]
fn idx(&self, stmt: &[Column]) -> Option<usize> {
if *self >= stmt.len() {
None
} else {
Some(*self)
}
}
}
impl<'a> RowIndex for str {
#[inline]
fn idx(&self, stmt: &[Column]) -> Option<usize> {
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<usize> {
T::idx(*self, columns)
}
}

View File

@@ -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<usize>;
}
impl RowIndex for usize {
#[inline]
fn idx(&self, stmt: &[Column]) -> Option<usize> {
if *self >= stmt.len() {
None
} else {
Some(*self)
}
}
}
impl<'a> RowIndex for str {
#[inline]
fn idx(&self, stmt: &[Column]) -> Option<usize> {
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<usize> {
T::idx(*self, columns)
}
}
#[doc(hidden)]
pub struct RowData {
buf: Vec<u8>,
indices: Vec<Option<Range<usize>>>,
}
impl<'a> FromFallibleIterator<Option<&'a [u8]>> for RowData {
fn from_fallible_iterator<I>(mut it: I) -> Result<RowData, I::Error>
where I: FallibleIterator<Item = Option<&'a [u8]>>
{
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,
}
}
}

View File

@@ -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_
}
}

View File

@@ -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;

View File

@@ -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};

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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};