Use more idiomatic names for row iterators

This commit is contained in:
Steven Fackler 2014-04-02 00:16:21 -07:00
parent b2c7e08627
commit 3181d39be8
2 changed files with 27 additions and 33 deletions

View File

@ -981,15 +981,14 @@ impl<'conn> PostgresTransaction<'conn> {
stmt: &'stmt PostgresStatement, stmt: &'stmt PostgresStatement,
params: &[&ToSql], params: &[&ToSql],
row_limit: uint) row_limit: uint)
-> Result<PostgresLazyResult<'trans, -> Result<PostgresLazyRows<'trans, 'stmt>,
'stmt>,
PostgresError> { PostgresError> {
if self.conn as *PostgresConnection != stmt.conn as *PostgresConnection { if self.conn as *PostgresConnection != stmt.conn as *PostgresConnection {
return Err(PgWrongConnection); return Err(PgWrongConnection);
} }
check_desync!(self.conn); check_desync!(self.conn);
stmt.lazy_query(row_limit, params).map(|result| { stmt.lazy_query(row_limit, params).map(|result| {
PostgresLazyResult { PostgresLazyRows {
trans: self, trans: self,
result: result result: result
} }
@ -1087,14 +1086,14 @@ impl<'conn> PostgresStatement<'conn> {
} }
fn lazy_query<'a>(&'a self, row_limit: uint, params: &[&ToSql]) fn lazy_query<'a>(&'a self, row_limit: uint, params: &[&ToSql])
-> Result<PostgresResult<'a>, PostgresError> { -> Result<PostgresRows<'a>, PostgresError> {
let id = self.next_portal_id.get(); let id = self.next_portal_id.get();
self.next_portal_id.set(id + 1); self.next_portal_id.set(id + 1);
let portal_name = format!("{}_portal_{}", self.name, id); let portal_name = format!("{}_portal_{}", self.name, id);
try!(self.inner_execute(portal_name, row_limit, params)); try!(self.inner_execute(portal_name, row_limit, params));
let mut result = PostgresResult { let mut result = PostgresRows {
stmt: self, stmt: self,
name: portal_name, name: portal_name,
data: RingBuf::new(), data: RingBuf::new(),
@ -1187,7 +1186,7 @@ impl<'conn> PostgresStatement<'conn> {
/// } /// }
/// ``` /// ```
pub fn query<'a>(&'a self, params: &[&ToSql]) pub fn query<'a>(&'a self, params: &[&ToSql])
-> Result<PostgresResult<'a>, PostgresError> { -> Result<PostgresRows<'a>, PostgresError> {
check_desync!(self.conn); check_desync!(self.conn);
self.lazy_query(0, params) self.lazy_query(0, params)
} }
@ -1212,7 +1211,7 @@ pub struct ResultDescription {
} }
/// An iterator over the resulting rows of a query. /// An iterator over the resulting rows of a query.
pub struct PostgresResult<'stmt> { pub struct PostgresRows<'stmt> {
stmt: &'stmt PostgresStatement<'stmt>, stmt: &'stmt PostgresStatement<'stmt>,
name: ~str, name: ~str,
data: RingBuf<Vec<Option<~[u8]>>>, data: RingBuf<Vec<Option<~[u8]>>>,
@ -1222,7 +1221,7 @@ pub struct PostgresResult<'stmt> {
} }
#[unsafe_destructor] #[unsafe_destructor]
impl<'stmt> Drop for PostgresResult<'stmt> { impl<'stmt> Drop for PostgresRows<'stmt> {
fn drop(&mut self) { fn drop(&mut self) {
if !self.finished { if !self.finished {
match self.finish_inner() { match self.finish_inner() {
@ -1234,7 +1233,7 @@ impl<'stmt> Drop for PostgresResult<'stmt> {
} }
} }
impl<'stmt> PostgresResult<'stmt> { impl<'stmt> PostgresRows<'stmt> {
fn finish_inner(&mut self) -> Result<(), PostgresError> { fn finish_inner(&mut self) -> Result<(), PostgresError> {
check_desync!(self.stmt.conn); check_desync!(self.stmt.conn);
try_pg!(self.stmt.conn.write_messages([ try_pg!(self.stmt.conn.write_messages([
@ -1286,11 +1285,11 @@ impl<'stmt> PostgresResult<'stmt> {
} }
} }
impl<'stmt> PostgresResult<'stmt> { impl<'stmt> PostgresRows<'stmt> {
/// Consumes the `PostgresResult`, cleaning up associated state. /// Consumes the `PostgresRows`, cleaning up associated state.
/// ///
/// Functionally identical to the `Drop` implementation on /// Functionally identical to the `Drop` implementation on `PostgresRows`
/// `PostgresResult` except that it returns any error to the caller. /// except that it returns any error to the caller.
pub fn finish(mut self) -> Result<(), PostgresError> { pub fn finish(mut self) -> Result<(), PostgresError> {
self.finished = true; self.finished = true;
self.finish_inner() self.finish_inner()
@ -1314,7 +1313,7 @@ impl<'stmt> PostgresResult<'stmt> {
} }
} }
impl<'stmt> Iterator<PostgresRow<'stmt>> for PostgresResult<'stmt> { impl<'stmt> Iterator<PostgresRow<'stmt>> for PostgresRows<'stmt> {
fn next(&mut self) -> Option<PostgresRow<'stmt>> { fn next(&mut self) -> Option<PostgresRow<'stmt>> {
// we'll never hit the network on a non-lazy result // we'll never hit the network on a non-lazy result
self.try_next().map(|r| r.unwrap()) self.try_next().map(|r| r.unwrap())
@ -1363,7 +1362,8 @@ impl<'stmt> Container for PostgresRow<'stmt> {
} }
} }
impl<'stmt, I: RowIndex+Clone+fmt::Show, T: FromSql> Index<I, T> for PostgresRow<'stmt> { impl<'stmt, I: RowIndex+Clone+fmt::Show, T: FromSql> Index<I, T>
for PostgresRow<'stmt> {
/// Retreives the contents of a field of the row. /// Retreives the contents of a field of the row.
/// ///
/// A field can be accessed by the name or index of its column, though /// A field can be accessed by the name or index of its column, though
@ -1388,7 +1388,7 @@ impl<'stmt, I: RowIndex+Clone+fmt::Show, T: FromSql> Index<I, T> for PostgresRow
fn index(&self, idx: &I) -> T { fn index(&self, idx: &I) -> T {
match self.get(idx.clone()) { match self.get(idx.clone()) {
Ok(ok) => ok, Ok(ok) => ok,
Err(err) => fail!("error retrieving row[{}]: {}", idx, err) Err(err) => fail!("error retrieving row {}: {}", idx, err)
} }
} }
} }
@ -1435,20 +1435,20 @@ impl<'a> RowIndex for &'a str {
} }
/// A lazily-loaded iterator over the resulting rows of a query /// A lazily-loaded iterator over the resulting rows of a query
pub struct PostgresLazyResult<'trans, 'stmt> { pub struct PostgresLazyRows<'trans, 'stmt> {
result: PostgresResult<'stmt>, result: PostgresRows<'stmt>,
trans: &'trans PostgresTransaction<'trans>, trans: &'trans PostgresTransaction<'trans>,
} }
impl<'trans, 'stmt> PostgresLazyResult<'trans, 'stmt> { impl<'trans, 'stmt> PostgresLazyRows<'trans, 'stmt> {
/// Like `PostgresResult::finish`. /// Like `PostgresRows::finish`.
pub fn finish(self) -> Result<(), PostgresError> { pub fn finish(self) -> Result<(), PostgresError> {
self.result.finish() self.result.finish()
} }
} }
impl<'trans, 'stmt> Iterator<Result<PostgresRow<'stmt>, PostgresError>> impl<'trans, 'stmt> Iterator<Result<PostgresRow<'stmt>, PostgresError>>
for PostgresLazyResult<'trans, 'stmt> { for PostgresLazyRows<'trans, 'stmt> {
fn next(&mut self) -> Option<Result<PostgresRow<'stmt>, PostgresError>> { fn next(&mut self) -> Option<Result<PostgresRow<'stmt>, PostgresError>> {
self.result.try_next() self.result.try_next()
} }

View File

@ -538,29 +538,25 @@ macro_rules! raw_to_impl(
) )
impl RawToSql for bool { impl RawToSql for bool {
fn raw_to_sql<W: Writer>(&self, w: &mut W) fn raw_to_sql<W: Writer>(&self, w: &mut W) -> Result<(), PostgresError> {
-> Result<(), PostgresError> {
Ok(try_pg!(w.write_u8(*self as u8))) Ok(try_pg!(w.write_u8(*self as u8)))
} }
} }
impl RawToSql for ~[u8] { impl RawToSql for ~[u8] {
fn raw_to_sql<W: Writer>(&self, w: &mut W) fn raw_to_sql<W: Writer>(&self, w: &mut W) -> Result<(), PostgresError> {
-> Result<(), PostgresError> {
Ok(try_pg!(w.write(self.as_slice()))) Ok(try_pg!(w.write(self.as_slice())))
} }
} }
impl RawToSql for Vec<u8> { impl RawToSql for Vec<u8> {
fn raw_to_sql<W: Writer>(&self, w: &mut W) fn raw_to_sql<W: Writer>(&self, w: &mut W) -> Result<(), PostgresError> {
-> Result<(), PostgresError> {
Ok(try_pg!(w.write(self.as_slice()))) Ok(try_pg!(w.write(self.as_slice())))
} }
} }
impl RawToSql for ~str { impl RawToSql for ~str {
fn raw_to_sql<W: Writer>(&self, w: &mut W) fn raw_to_sql<W: Writer>(&self, w: &mut W) -> Result<(), PostgresError> {
-> Result<(), PostgresError> {
Ok(try_pg!(w.write(self.as_bytes()))) Ok(try_pg!(w.write(self.as_bytes())))
} }
} }
@ -573,8 +569,7 @@ raw_to_impl!(f32, write_be_f32)
raw_to_impl!(f64, write_be_f64) raw_to_impl!(f64, write_be_f64)
impl RawToSql for Timespec { impl RawToSql for Timespec {
fn raw_to_sql<W: Writer>(&self, w: &mut W) fn raw_to_sql<W: Writer>(&self, w: &mut W) -> Result<(), PostgresError> {
-> Result<(), PostgresError> {
let t = (self.sec - TIME_SEC_CONVERSION) * USEC_PER_SEC let t = (self.sec - TIME_SEC_CONVERSION) * USEC_PER_SEC
+ self.nsec as i64 / NSEC_PER_USEC; + self.nsec as i64 / NSEC_PER_USEC;
Ok(try_pg!(w.write_be_i64(t))) Ok(try_pg!(w.write_be_i64(t)))
@ -582,8 +577,7 @@ impl RawToSql for Timespec {
} }
impl RawToSql for Uuid { impl RawToSql for Uuid {
fn raw_to_sql<W: Writer>(&self, w: &mut W) fn raw_to_sql<W: Writer>(&self, w: &mut W) -> Result<(), PostgresError> {
-> Result<(), PostgresError> {
Ok(try_pg!(w.write(self.as_bytes()))) Ok(try_pg!(w.write(self.as_bytes())))
} }
} }