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

@ -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 }
}
}
#[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();
//!
//! connection.execute("
//! CREATE TABLE `users` (id INTEGER, name VARCHAR(255));
//! INSERT INTO `users` (id, name) VALUES (42, 'Alice');
//! CREATE TABLE users (id INTEGER, name VARCHAR(255));
//! INSERT INTO users (id, name) VALUES (42, 'Alice');
//! ").unwrap();
//!
//! connection.process("SELECT * FROM `users`", |pairs| {
//! connection.process("SELECT * FROM users", |pairs| {
//! for &(column, value) in pairs.iter() {
//! println!("{} = {}", column, value.unwrap());
//! }
@ -26,17 +26,17 @@
//! let connection = sqlite::open(":memory:").unwrap();
//!
//! connection.execute("
//! CREATE TABLE `users` (id INTEGER, name VARCHAR(255))
//! CREATE TABLE users (id INTEGER, name VARCHAR(255))
//! ");
//!
//! let mut statement = connection.prepare("
//! INSERT INTO `users` (id, name) VALUES (?, ?)
//! INSERT INTO users (id, name) VALUES (?, ?)
//! ").unwrap();
//! statement.bind(1, 42).unwrap();
//! statement.bind(2, "Alice").unwrap();
//! 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() {
//! println!("id = {}", statement.read::<i64>(0).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 error;
mod statement;

View File

@ -2,7 +2,7 @@ use ffi;
use libc::{c_double, c_int};
use std::marker::PhantomData;
use Result;
use {Result, Type};
/// A prepared statement.
pub struct Statement<'l> {
@ -38,10 +38,25 @@ pub trait Value {
impl<'l> Statement<'l> {
/// Return the number of columns.
#[inline]
pub fn columns(&mut self) -> usize {
pub fn columns(&self) -> 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.
///
/// The leftmost location has the index 1.