Get rid of StringRow

It's not exposed by any methods, and the API isn't great.
This commit is contained in:
Steven Fackler 2019-01-14 21:44:59 -08:00
parent e3f2eb7244
commit 07078871d9
3 changed files with 8 additions and 54 deletions

View File

@ -1,5 +1,6 @@
#![allow(clippy::large_enum_variant)]
use fallible_iterator::FallibleIterator;
use futures::{try_ready, Async, Future, Poll, Stream};
use state_machine_future::{transition, RentToOwn, StateMachineFuture};
use std::io;
@ -92,7 +93,8 @@ where
match try_ready!(state.stream.poll()) {
Some(row) => {
if row.get(0) == Some("on") {
let range = row.ranges().next().map_err(Error::parse)?.and_then(|r| r);
if range.map(|r| &row.buffer()[r]) == Some(b"on") {
Err(Error::connect(io::Error::new(
io::ErrorKind::PermissionDenied,
"database does not allow writes",

View File

@ -1,10 +1,10 @@
use futures::sync::mpsc;
use futures::{Async, Poll, Stream};
use postgres_protocol::message::backend::Message;
use postgres_protocol::message::backend::{DataRowBody, Message};
use std::mem;
use crate::proto::client::{Client, PendingRequest};
use crate::{Error, StringRow};
use crate::Error;
pub enum State {
Start {
@ -20,10 +20,10 @@ pub enum State {
pub struct SimpleQueryStream(State);
impl Stream for SimpleQueryStream {
type Item = StringRow;
type Item = DataRowBody;
type Error = Error;
fn poll(&mut self) -> Poll<Option<StringRow>, Error> {
fn poll(&mut self) -> Poll<Option<DataRowBody>, Error> {
loop {
match mem::replace(&mut self.0, State::Done) {
State::Start { client, request } => {
@ -48,8 +48,7 @@ impl Stream for SimpleQueryStream {
}
Some(Message::DataRow(body)) => {
self.0 = State::ReadResponse { receiver };
let row = StringRow::new(body)?;
return Ok(Async::Ready(Some(row)));
return Ok(Async::Ready(Some(body)));
}
Some(Message::ErrorResponse(body)) => return Err(Error::db(body)),
Some(Message::ReadyForQuery(_)) => return Ok(Async::Ready(None)),

View File

@ -134,50 +134,3 @@ impl Row {
value.map(Some).map_err(Error::from_sql)
}
}
pub struct StringRow {
body: DataRowBody,
ranges: Vec<Option<Range<usize>>>,
}
impl StringRow {
#[allow(clippy::new_ret_no_self)]
pub(crate) fn new(body: DataRowBody) -> Result<StringRow, Error> {
let ranges = body.ranges().collect().map_err(Error::parse)?;
Ok(StringRow { body, ranges })
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn len(&self) -> usize {
self.ranges.len()
}
pub fn get(&self, idx: usize) -> Option<&str> {
match self.try_get(idx) {
Ok(Some(ok)) => ok,
Err(err) => panic!("error retrieving column {}: {}", idx, err),
Ok(None) => panic!("no such column {}", idx),
}
}
#[allow(clippy::option_option)] // FIXME
pub fn try_get(&self, idx: usize) -> Result<Option<Option<&str>>, Error> {
let buf = match self.ranges.get(idx) {
Some(range) => range.clone().map(|r| &self.body.buffer()[r]),
None => return Ok(None),
};
let v = match buf {
Some(buf) => {
let s = str::from_utf8(buf).map_err(|e| Error::from_sql(Box::new(e)))?;
Some(s)
}
None => None,
};
Ok(Some(v))
}
}