mirror of
https://github.com/fluencelabs/sqlite-wasm-connector
synced 2025-03-15 06:20:50 +00:00
Rename Iterator to Cursor
This commit is contained in:
parent
162f27fa3c
commit
5072b6e2f3
16
README.md
16
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);
|
||||
|
@ -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 })
|
||||
}
|
20
src/lib.rs
20
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.
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
14
tests/lib.rs
14
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]
|
||||
|
Loading…
x
Reference in New Issue
Block a user