diff --git a/README.md b/README.md index b73dda6..59ccad1 100644 --- a/README.md +++ b/README.md @@ -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); diff --git a/src/iterator.rs b/src/cursor.rs similarity index 86% rename from src/iterator.rs rename to src/cursor.rs index 301382d..c4be496 100644 --- a/src/iterator.rs +++ b/src/cursor.rs @@ -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, values: Option>, 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> { - Ok(Iterator { state: None, values: None, statement: statement }) +pub fn new<'l>(statement: Statement<'l>) -> Result> { + Ok(Cursor { state: None, values: None, statement: statement }) } diff --git a/src/lib.rs b/src/lib.rs index 9100cd2..b544a23 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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. diff --git a/src/statement.rs b/src/statement.rs index 1849516..56e3c42 100644 --- a/src/statement.rs +++ b/src/statement.rs @@ -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::new(self) + pub fn cursor(self) -> Result> { + ::cursor::new(self) } } diff --git a/tests/lib.rs b/tests/lib.rs index 26282f2..bb7871b 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -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]