Skip to content

Commit

Permalink
Cache column_count (I am not sure it's worth it)
Browse files Browse the repository at this point in the history
  • Loading branch information
gwenn committed Aug 1, 2015
1 parent c31c68d commit f91db1b
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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() });
}
Expand Down

0 comments on commit f91db1b

Please sign in to comment.