Rename Iterator to Cursor

This commit is contained in:
Ivan Ukhov 2015-08-03 17:10:30 -04:00
parent 162f27fa3c
commit 5072b6e2f3
5 changed files with 34 additions and 34 deletions

View File

@ -60,7 +60,7 @@ while let State::Row = statement.next().unwrap() {
}
```
The same example using iterators:
The same example using cursors:
```rust
use sqlite::Value;
@ -71,23 +71,23 @@ connection.execute("
CREATE TABLE users (name TEXT, age INTEGER)
").unwrap();
let mut iterator = connection.prepare("
let mut cursor = connection.prepare("
INSERT INTO users (name, age) VALUES (?, ?)
").unwrap().into_iter().unwrap();
").unwrap().cursor().unwrap();
iterator.bind(&[
cursor.bind(&[
Value::String("Alice".to_string()), Value::Integer(42),
]).unwrap();
iterator.bind(&[
cursor.bind(&[
Value::String("Bob".to_string()), Value::Integer(69),
]).unwrap();
let mut iterator = connection.prepare("
let mut cursor = connection.prepare("
SELECT * FROM users WHERE age > 50
").unwrap().into_iter().unwrap();
").unwrap().cursor().unwrap();
while let Some(row) = iterator.next().unwrap() {
while let Some(row) = cursor.next().unwrap() {
match (&row[0], &row[1]) {
(&Value::String(ref name), &Value::Integer(age)) => {
println!("name = {}", name);

View File

@ -1,14 +1,14 @@
use statement::{State, Statement, Bindable, Readable};
use {Result, Value};
/// An iterator over the resulting rows of a prepared statement.
pub struct Iterator<'l> {
/// An iterator over rows.
pub struct Cursor<'l> {
state: Option<State>,
values: Option<Vec<Value>>,
statement: Statement<'l>,
}
impl<'l> Iterator<'l> {
impl<'l> Cursor<'l> {
/// Bind values to all parameters.
pub fn bind(&mut self, values: &[Value]) -> Result<()> {
try!(self.statement.reset());
@ -51,6 +51,6 @@ impl<'l> Iterator<'l> {
}
#[inline]
pub fn new<'l>(statement: Statement<'l>) -> Result<Iterator<'l>> {
Ok(Iterator { state: None, values: None, statement: statement })
pub fn new<'l>(statement: Statement<'l>) -> Result<Cursor<'l>> {
Ok(Cursor { state: None, values: None, statement: statement })
}

View File

@ -56,7 +56,7 @@
//! }
//! ```
//!
//! The same example using iterators:
//! The same example using cursors:
//!
//! ```
//! use sqlite::Value;
@ -67,23 +67,23 @@
//! CREATE TABLE users (name TEXT, age INTEGER)
//! ").unwrap();
//!
//! let mut iterator = connection.prepare("
//! let mut cursor = connection.prepare("
//! INSERT INTO users (name, age) VALUES (?, ?)
//! ").unwrap().into_iter().unwrap();
//! ").unwrap().cursor().unwrap();
//!
//! iterator.bind(&[
//! cursor.bind(&[
//! Value::String("Alice".to_string()), Value::Integer(42),
//! ]).unwrap();
//!
//! iterator.bind(&[
//! cursor.bind(&[
//! Value::String("Bob".to_string()), Value::Integer(69),
//! ]).unwrap();
//!
//! let mut iterator = connection.prepare("
//! let mut cursor = connection.prepare("
//! SELECT * FROM users WHERE age > 50
//! ").unwrap().into_iter().unwrap();
//! ").unwrap().cursor().unwrap();
//!
//! while let Some(row) = iterator.next().unwrap() {
//! while let Some(row) = cursor.next().unwrap() {
//! match (&row[0], &row[1]) {
//! (&Value::String(ref name), &Value::Integer(age)) => {
//! println!("name = {}", name);
@ -214,11 +214,11 @@ impl error::Error for Error {
}
mod connection;
mod iterator;
mod cursor;
mod statement;
pub use connection::Connection;
pub use iterator::Iterator;
pub use cursor::Cursor;
pub use statement::{Statement, State, Bindable, Readable};
/// Open a connection to a new or existing database.

View File

@ -2,7 +2,7 @@ use ffi;
use libc::{c_double, c_int};
use std::marker::PhantomData;
use {Iterator, Result, Type, Value};
use {Cursor, Result, Type, Value};
/// A prepared statement.
pub struct Statement<'l> {
@ -101,10 +101,10 @@ impl<'l> Statement<'l> {
Ok(())
}
/// Upgrade to an iterator.
/// Upgrade to a cursor.
#[inline]
pub fn into_iter(self) -> Result<Iterator<'l>> {
::iterator::new(self)
pub fn cursor(self) -> Result<Cursor<'l>> {
::cursor::new(self)
}
}

View File

@ -70,18 +70,18 @@ fn connection_set_busy_handler() {
}
#[test]
fn iterator() {
fn cursor() {
let connection = setup(":memory:");
let statement = "SELECT id, name FROM users WHERE id = ?";
let mut iterator = ok!(connection.prepare(statement)).into_iter().unwrap();
let mut cursor = ok!(connection.prepare(statement)).cursor().unwrap();
ok!(iterator.bind(&[Value::Integer(1)]));
assert_eq!(ok!(ok!(iterator.next())), &[Value::Integer(1),
ok!(cursor.bind(&[Value::Integer(1)]));
assert_eq!(ok!(ok!(cursor.next())), &[Value::Integer(1),
Value::String("Alice".to_string())]);
assert_eq!(ok!(iterator.next()), None);
assert_eq!(ok!(cursor.next()), None);
ok!(iterator.bind(&[Value::Integer(42)]));
assert_eq!(ok!(iterator.next()), None);
ok!(cursor.bind(&[Value::Integer(42)]));
assert_eq!(ok!(cursor.next()), None);
}
#[test]