Introduce Type and Statement::kind for reading it

This commit is contained in:
Ivan Ukhov
2015-08-01 16:23:05 -04:00
parent e73c9cd508
commit ea9eff2fd2
5 changed files with 147 additions and 92 deletions

View File

@ -10,11 +10,11 @@ The package provides an interface to [SQLite][1].
let connection = sqlite::open(":memory:").unwrap(); let connection = sqlite::open(":memory:").unwrap();
connection.execute(" connection.execute("
CREATE TABLE `users` (id INTEGER, name VARCHAR(255)); CREATE TABLE users (id INTEGER, name VARCHAR(255));
INSERT INTO `users` (id, name) VALUES (42, 'Alice'); INSERT INTO users (id, name) VALUES (42, 'Alice');
").unwrap(); ").unwrap();
connection.process("SELECT * FROM `users`", |pairs| { connection.process("SELECT * FROM users", |pairs| {
for &(column, value) in pairs.iter() { for &(column, value) in pairs.iter() {
println!("{} = {}", column, value.unwrap()); println!("{} = {}", column, value.unwrap());
} }
@ -30,17 +30,17 @@ use sqlite::State;
let connection = sqlite::open(":memory:").unwrap(); let connection = sqlite::open(":memory:").unwrap();
connection.execute(" connection.execute("
CREATE TABLE `users` (id INTEGER, name VARCHAR(255)) CREATE TABLE users (id INTEGER, name VARCHAR(255))
"); ");
let mut statement = connection.prepare(" let mut statement = connection.prepare("
INSERT INTO `users` (id, name) VALUES (?, ?) INSERT INTO users (id, name) VALUES (?, ?)
").unwrap(); ").unwrap();
statement.bind(1, 42).unwrap(); statement.bind(1, 42).unwrap();
statement.bind(2, "Alice").unwrap(); statement.bind(2, "Alice").unwrap();
assert_eq!(statement.step().unwrap(), State::Done); assert_eq!(statement.step().unwrap(), State::Done);
let mut statement = connection.prepare("SELECT * FROM `users`").unwrap(); let mut statement = connection.prepare("SELECT * FROM users").unwrap();
while let State::Row = statement.step().unwrap() { while let State::Row = statement.step().unwrap() {
println!("id = {}", statement.read::<i64>(0).unwrap()); println!("id = {}", statement.read::<i64>(0).unwrap());
println!("name = {}", statement.read::<String>(1).unwrap()); println!("name = {}", statement.read::<String>(1).unwrap());

View File

@ -146,24 +146,3 @@ extern fn process_callback<F>(callback: *mut c_void, count: c_int, values: *mut
if (*(callback as *mut F))(&pairs) { 0 } else { 1 } if (*(callback as *mut F))(&pairs) { 0 } else { 1 }
} }
} }
#[cfg(test)]
mod tests {
use super::Connection;
#[test]
fn execute() {
let connection = Connection::open(":memory:").unwrap();
match connection.execute(":)") {
Err(error) => assert_eq!(error.message,
Some(String::from(r#"unrecognized token: ":""#))),
_ => unreachable!(),
}
}
#[test]
fn set_busy_handler() {
let mut connection = Connection::open(":memory:").unwrap();
connection.set_busy_handler(|_| true).unwrap();
}
}

View File

@ -6,11 +6,11 @@
//! let connection = sqlite::open(":memory:").unwrap(); //! let connection = sqlite::open(":memory:").unwrap();
//! //!
//! connection.execute(" //! connection.execute("
//! CREATE TABLE `users` (id INTEGER, name VARCHAR(255)); //! CREATE TABLE users (id INTEGER, name VARCHAR(255));
//! INSERT INTO `users` (id, name) VALUES (42, 'Alice'); //! INSERT INTO users (id, name) VALUES (42, 'Alice');
//! ").unwrap(); //! ").unwrap();
//! //!
//! connection.process("SELECT * FROM `users`", |pairs| { //! connection.process("SELECT * FROM users", |pairs| {
//! for &(column, value) in pairs.iter() { //! for &(column, value) in pairs.iter() {
//! println!("{} = {}", column, value.unwrap()); //! println!("{} = {}", column, value.unwrap());
//! } //! }
@ -26,17 +26,17 @@
//! let connection = sqlite::open(":memory:").unwrap(); //! let connection = sqlite::open(":memory:").unwrap();
//! //!
//! connection.execute(" //! connection.execute("
//! CREATE TABLE `users` (id INTEGER, name VARCHAR(255)) //! CREATE TABLE users (id INTEGER, name VARCHAR(255))
//! "); //! ");
//! //!
//! let mut statement = connection.prepare(" //! let mut statement = connection.prepare("
//! INSERT INTO `users` (id, name) VALUES (?, ?) //! INSERT INTO users (id, name) VALUES (?, ?)
//! ").unwrap(); //! ").unwrap();
//! statement.bind(1, 42).unwrap(); //! statement.bind(1, 42).unwrap();
//! statement.bind(2, "Alice").unwrap(); //! statement.bind(2, "Alice").unwrap();
//! assert_eq!(statement.step().unwrap(), State::Done); //! assert_eq!(statement.step().unwrap(), State::Done);
//! //!
//! let mut statement = connection.prepare("SELECT * FROM `users`").unwrap(); //! let mut statement = connection.prepare("SELECT * FROM users").unwrap();
//! while let State::Row = statement.step().unwrap() { //! while let State::Row = statement.step().unwrap() {
//! println!("id = {}", statement.read::<i64>(0).unwrap()); //! println!("id = {}", statement.read::<i64>(0).unwrap());
//! println!("name = {}", statement.read::<String>(1).unwrap()); //! println!("name = {}", statement.read::<String>(1).unwrap());
@ -98,6 +98,21 @@ macro_rules! str_to_cstr(
}); });
); );
/// A data type.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Type {
/// Binary data.
Blob,
/// A 64-bit IEEE floating-point number.
Float,
/// A 64-bit signed integer.
Integer,
/// An absence of a value.
Null,
/// A string.
String,
}
mod connection; mod connection;
mod error; mod error;
mod statement; mod statement;

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 Result; use {Result, Type};
/// A prepared statement. /// A prepared statement.
pub struct Statement<'l> { pub struct Statement<'l> {
@ -38,10 +38,25 @@ pub trait Value {
impl<'l> Statement<'l> { impl<'l> Statement<'l> {
/// Return the number of columns. /// Return the number of columns.
#[inline] #[inline]
pub fn columns(&mut self) -> usize { pub fn columns(&self) -> usize {
unsafe { ffi::sqlite3_column_count(self.raw.0) as usize } unsafe { ffi::sqlite3_column_count(self.raw.0) as usize }
} }
/// Return the type of a column.
///
/// The type is revealed after the first step has been taken.
#[inline]
pub fn kind(&self, i: usize) -> Type {
match unsafe { ffi::sqlite3_column_type(self.raw.0, i as c_int) } {
ffi::SQLITE_BLOB => Type::Blob,
ffi::SQLITE_FLOAT => Type::Float,
ffi::SQLITE_INTEGER => Type::Integer,
ffi::SQLITE_NULL => Type::Null,
ffi::SQLITE_TEXT => Type::String,
_ => unreachable!(),
}
}
/// Bind the parameter at a specific location. /// Bind the parameter at a specific location.
/// ///
/// The leftmost location has the index 1. /// The leftmost location has the index 1.

View File

@ -1,82 +1,61 @@
extern crate sqlite; extern crate sqlite;
extern crate temporary; extern crate temporary;
use sqlite::{Connection, State, Type};
use std::path::Path;
macro_rules! ok( macro_rules! ok(
($result:expr) => ($result.unwrap()); ($result:expr) => ($result.unwrap());
); );
#[test] #[test]
fn workflow() { fn connection_execute() {
use sqlite::State; let connection = setup(":memory:");
match connection.execute(":)") {
Err(error) => assert_eq!(error.message, Some(String::from(r#"unrecognized token: ":""#))),
_ => unreachable!(),
}
}
#[test]
fn connection_process() {
macro_rules! pair( macro_rules! pair(
($one:expr, $two:expr) => (($one, Some($two))); ($one:expr, $two:expr) => (($one, Some($two)));
); );
let connection = ok!(sqlite::open(":memory:")); let connection = setup(":memory:");
let sql = "CREATE TABLE `users` (id INTEGER, name VARCHAR(255), age REAL)";
ok!(connection.execute(sql));
{
let sql = "INSERT INTO `users` (id, name, age) VALUES (?, ?, ?)";
let mut statement = ok!(connection.prepare(sql));
assert_eq!(statement.columns(), 0);
ok!(statement.bind(1, 1i64));
ok!(statement.bind(2, "Alice"));
ok!(statement.bind(3, 20.99));
assert_eq!(ok!(statement.step()), State::Done);
}
{
let mut done = false; let mut done = false;
let sql = "SELECT * FROM `users`"; let query = "SELECT * FROM users";
ok!(connection.process(sql, |pairs| { ok!(connection.process(query, |pairs| {
assert_eq!(pairs.len(), 3); assert_eq!(pairs.len(), 3);
assert_eq!(pairs[0], pair!("id", "1")); assert_eq!(pairs[0], pair!("id", "1"));
assert_eq!(pairs[1], pair!("name", "Alice")); assert_eq!(pairs[1], pair!("name", "Alice"));
assert_eq!(pairs[2], pair!("age", "20.99")); assert_eq!(pairs[2], pair!("age", "42.69"));
done = true; done = true;
true true
})); }));
assert!(done); assert!(done);
} }
{
let sql = "SELECT * FROM `users`";
let mut statement = ok!(connection.prepare(sql));
assert_eq!(statement.columns(), 3);
assert_eq!(ok!(statement.step()), State::Row);
assert_eq!(ok!(statement.read::<i64>(0)), 1);
assert_eq!(ok!(statement.read::<String>(1)), String::from("Alice"));
assert_eq!(ok!(statement.read::<f64>(2)), 20.99);
assert_eq!(ok!(statement.step()), State::Done);
}
}
#[test] #[test]
fn stress() { fn connection_set_busy_handler() {
use sqlite::State;
use std::path::PathBuf;
use std::thread; use std::thread;
use temporary::Directory; use temporary::Directory;
let directory = ok!(Directory::new("sqlite")); let directory = ok!(Directory::new("sqlite"));
let path = directory.path().join("database.sqlite3"); let path = directory.path().join("database.sqlite3");
setup(&path);
let connection = ok!(sqlite::open(&path));
let sql = "CREATE TABLE `users` (id INTEGER, name VARCHAR(255), age REAL)";
ok!(connection.execute(sql));
let guards = (0..100).map(|_| { let guards = (0..100).map(|_| {
let path = PathBuf::from(&path); let path = path.to_path_buf();
thread::spawn(move || { thread::spawn(move || {
let mut connection = ok!(sqlite::open(&path)); let mut connection = ok!(sqlite::open(&path));
ok!(connection.set_busy_handler(|_| true)); ok!(connection.set_busy_handler(|_| true));
let sql = "INSERT INTO `users` (id, name, age) VALUES (?, ?, ?)"; let query = "INSERT INTO `users` (id, name, age) VALUES (?, ?, ?)";
let mut statement = ok!(connection.prepare(sql)); let mut statement = ok!(connection.prepare(query));
ok!(statement.bind(1, 1i64)); ok!(statement.bind(1, 2i64));
ok!(statement.bind(2, "Alice")); ok!(statement.bind(2, "Bob"));
ok!(statement.bind(3, 20.99)); ok!(statement.bind(3, 20.99));
assert_eq!(ok!(statement.step()), State::Done); assert_eq!(ok!(statement.step()), State::Done);
true true
@ -87,3 +66,70 @@ fn stress() {
assert!(guard.join().unwrap()); assert!(guard.join().unwrap());
} }
} }
#[test]
fn statement_columns() {
let connection = setup(":memory:");
let query = "SELECT * FROM users";
let mut statement = ok!(connection.prepare(query));
assert_eq!(statement.columns(), 3);
assert_eq!(ok!(statement.step()), State::Row);
assert_eq!(statement.columns(), 3);
}
#[test]
fn statement_kind() {
let connection = setup(":memory:");
let query = "SELECT * FROM users";
let mut statement = ok!(connection.prepare(query));
assert_eq!(statement.kind(0), Type::Null);
assert_eq!(statement.kind(1), Type::Null);
assert_eq!(statement.kind(2), Type::Null);
assert_eq!(ok!(statement.step()), State::Row);
assert_eq!(statement.kind(0), Type::Integer);
assert_eq!(statement.kind(1), Type::String);
assert_eq!(statement.kind(2), Type::Float);
}
#[test]
fn statement_insert() {
let connection = setup(":memory:");
let query = "INSERT INTO users (id, name, age) VALUES (?, ?, ?)";
let mut statement = ok!(connection.prepare(query));
ok!(statement.bind(1, 2i64));
ok!(statement.bind(2, "Bob"));
ok!(statement.bind(3, 20.99));
assert_eq!(ok!(statement.step()), State::Done);
}
#[test]
fn statement_select() {
let connection = setup(":memory:");
let query = "SELECT * FROM users";
let mut statement = ok!(connection.prepare(query));
assert_eq!(ok!(statement.step()), State::Row);
assert_eq!(ok!(statement.read::<i64>(0)), 1);
assert_eq!(ok!(statement.read::<String>(1)), String::from("Alice"));
assert_eq!(ok!(statement.read::<f64>(2)), 42.69);
assert_eq!(ok!(statement.step()), State::Done);
}
fn setup<T: AsRef<Path>>(path: T) -> Connection {
let connection = ok!(sqlite::open(path));
let query = "CREATE TABLE users (id INTEGER, name VARCHAR(255), age REAL)";
ok!(connection.execute(query));
let query = "INSERT INTO users (id, name, age) VALUES (1, 'Alice', 42.69)";
ok!(connection.execute(query));
connection
}