Add Statement::columns

This commit is contained in:
Ivan Ukhov 2015-08-01 13:55:34 -04:00
parent 2275f8157f
commit 3d0bf10912
3 changed files with 9 additions and 1 deletions

View File

@ -58,7 +58,7 @@ macro_rules! raise(
macro_rules! error(
($connection:expr, $code:expr) => (match ::error::last($connection) {
Some(error) => return Err(error),
None => return Err(::Error::from(::ErrorKind::from($code as isize))),
_ => return Err(::Error::from(::ErrorKind::from($code as isize))),
});
);

View File

@ -36,6 +36,12 @@ pub trait Value {
}
impl<'l> Statement<'l> {
/// Return the number of columns.
#[inline]
pub fn columns(&mut self) -> usize {
unsafe { ffi::sqlite3_column_count(self.raw.0) as usize }
}
/// Bind the parameter at a specific location.
///
/// The leftmost location has the index 1.

View File

@ -21,6 +21,7 @@ fn workflow() {
{
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));
@ -44,6 +45,7 @@ fn workflow() {
{
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"));