From f91db1b3503a8186d08f797ae0a3e32f2d9129a9 Mon Sep 17 00:00:00 2001 From: gwenn Date: Sat, 1 Aug 2015 10:08:28 +0200 Subject: [PATCH] Cache column_count (I am not sure it's worth it) --- src/lib.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9ef89c3..0435cb3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -571,16 +571,18 @@ pub struct SqliteStatement<'conn> { conn: &'conn SqliteConnection, stmt: *mut ffi::sqlite3_stmt, needs_reset: bool, + column_count: c_int, } impl<'conn> SqliteStatement<'conn> { fn new(conn: &SqliteConnection, stmt: *mut ffi::sqlite3_stmt) -> SqliteStatement { - SqliteStatement{ conn: conn, stmt: stmt, needs_reset: false } + SqliteStatement{ conn: conn, stmt: stmt, needs_reset: false, + column_count: unsafe { ffi::sqlite3_column_count(stmt) }} } /// Get all the column names in the result set of the prepared statement. pub fn column_names(&self) -> Vec<&str> { - let n = self.column_count(); + let n = self.column_count; let mut cols = Vec::with_capacity(n as usize); for i in 0..n { let slice = unsafe { @@ -627,7 +629,7 @@ impl<'conn> SqliteStatement<'conn> { let r = ffi::sqlite3_step(self.stmt); match r { ffi::SQLITE_DONE => { - if self.column_count() != 0 { + if self.column_count != 0 { Err(SqliteError{ code: ffi::SQLITE_MISUSE, message: "Unexpected column count - did you mean to call query?".to_string() }) } else { @@ -717,10 +719,6 @@ impl<'conn> SqliteStatement<'conn> { } } - fn column_count(&self) -> c_int { - unsafe { ffi::sqlite3_column_count(self.stmt) } - } - fn finalize_(&mut self) -> SqliteResult<()> { let r = unsafe { ffi::sqlite3_finalize(self.stmt) }; self.stmt = ptr::null_mut(); @@ -906,7 +904,7 @@ impl<'stmt> SqliteRow<'stmt> { message: "Cannot get values from a row after advancing to next row".to_string() }); } unsafe { - if idx < 0 || idx >= self.stmt.column_count() { + if idx < 0 || idx >= self.stmt.column_count { return Err(SqliteError{ code: ffi::SQLITE_MISUSE, message: "Invalid column index".to_string() }); }