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 ```rust
use sqlite::Value; use sqlite::Value;
@ -71,23 +71,23 @@ connection.execute("
CREATE TABLE users (name TEXT, age INTEGER) CREATE TABLE users (name TEXT, age INTEGER)
").unwrap(); ").unwrap();
let mut iterator = connection.prepare(" let mut cursor = connection.prepare("
INSERT INTO users (name, age) VALUES (?, ?) 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), Value::String("Alice".to_string()), Value::Integer(42),
]).unwrap(); ]).unwrap();
iterator.bind(&[ cursor.bind(&[
Value::String("Bob".to_string()), Value::Integer(69), Value::String("Bob".to_string()), Value::Integer(69),
]).unwrap(); ]).unwrap();
let mut iterator = connection.prepare(" let mut cursor = connection.prepare("
SELECT * FROM users WHERE age > 50 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]) { match (&row[0], &row[1]) {
(&Value::String(ref name), &Value::Integer(age)) => { (&Value::String(ref name), &Value::Integer(age)) => {
println!("name = {}", name); println!("name = {}", name);

View File

@ -1,14 +1,14 @@
use statement::{State, Statement, Bindable, Readable}; use statement::{State, Statement, Bindable, Readable};
use {Result, Value}; use {Result, Value};
/// An iterator over the resulting rows of a prepared statement. /// An iterator over rows.
pub struct Iterator<'l> { pub struct Cursor<'l> {
state: Option<State>, state: Option<State>,
values: Option<Vec<Value>>, values: Option<Vec<Value>>,
statement: Statement<'l>, statement: Statement<'l>,
} }
impl<'l> Iterator<'l> { impl<'l> Cursor<'l> {
/// Bind values to all parameters. /// Bind values to all parameters.
pub fn bind(&mut self, values: &[Value]) -> Result<()> { pub fn bind(&mut self, values: &[Value]) -> Result<()> {
try!(self.statement.reset()); try!(self.statement.reset());
@ -51,6 +51,6 @@ impl<'l> Iterator<'l> {
} }
#[inline] #[inline]
pub fn new<'l>(statement: Statement<'l>) -> Result<Iterator<'l>> { pub fn new<'l>(statement: Statement<'l>) -> Result<Cursor<'l>> {
Ok(Iterator { state: None, values: None, statement: statement }) 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; //! use sqlite::Value;
@ -67,23 +67,23 @@
//! CREATE TABLE users (name TEXT, age INTEGER) //! CREATE TABLE users (name TEXT, age INTEGER)
//! ").unwrap(); //! ").unwrap();
//! //!
//! let mut iterator = connection.prepare(" //! let mut cursor = connection.prepare("
//! INSERT INTO users (name, age) VALUES (?, ?) //! 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), //! Value::String("Alice".to_string()), Value::Integer(42),
//! ]).unwrap(); //! ]).unwrap();
//! //!
//! iterator.bind(&[ //! cursor.bind(&[
//! Value::String("Bob".to_string()), Value::Integer(69), //! Value::String("Bob".to_string()), Value::Integer(69),
//! ]).unwrap(); //! ]).unwrap();
//! //!
//! let mut iterator = connection.prepare(" //! let mut cursor = connection.prepare("
//! SELECT * FROM users WHERE age > 50 //! 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]) { //! match (&row[0], &row[1]) {
//! (&Value::String(ref name), &Value::Integer(age)) => { //! (&Value::String(ref name), &Value::Integer(age)) => {
//! println!("name = {}", name); //! println!("name = {}", name);
@ -214,11 +214,11 @@ impl error::Error for Error {
} }
mod connection; mod connection;
mod iterator; mod cursor;
mod statement; mod statement;
pub use connection::Connection; pub use connection::Connection;
pub use iterator::Iterator; pub use cursor::Cursor;
pub use statement::{Statement, State, Bindable, Readable}; pub use statement::{Statement, State, Bindable, Readable};
/// Open a connection to a new or existing database. /// Open a connection to a new or existing database.

View File

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

View File

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