Merge branch 'jaysonsantos-column-name'

This commit is contained in:
Ivan Ukhov 2018-10-06 10:03:05 +02:00
commit cf7a3d7500
2 changed files with 30 additions and 2 deletions

View File

@ -55,6 +55,22 @@ impl<'l> Statement<'l> {
unsafe { ffi::sqlite3_column_count(self.raw.0) as usize }
}
/// Return the name of a column.
#[inline]
pub fn column_name(&self, i: usize) -> &str {
debug_assert!(i < self.columns(), format!("column position has to be between 0 and {}", self.columns() - 1));
unsafe {
let ret = ffi::sqlite3_column_name(self.raw.0, i as c_int);
c_str_to_str!(ret).unwrap()
}
}
/// Return column names.
#[inline]
pub fn column_names(&self) -> Vec<&str> {
(0..self.columns()).map(|i| self.column_name(i)).collect()
}
/// Return the type of a column.
///
/// The type is revealed after the first step has been taken.

View File

@ -140,13 +140,25 @@ fn statement_columns() {
let statement = "SELECT * FROM users";
let mut statement = ok!(connection.prepare(statement));
assert_eq!(statement.columns(), 4);
assert_eq!(ok!(statement.next()), State::Row);
assert_eq!(statement.columns(), 4);
}
#[test]
fn statement_column_name() {
let connection = setup_users(":memory:");
let statement = "SELECT id, name, age, photo as user_photo FROM users";
let statement = ok!(connection.prepare(statement));
assert_eq!(statement.columns(), 4);
let column_names = statement.column_names();
assert_eq!(column_names, vec!["id", "name", "age", "user_photo"]);
assert_eq!("user_photo", statement.column_name(3));
}
#[test]
fn statement_kind() {
let connection = setup_users(":memory:");