Rename name to column_name

This commit is contained in:
Ivan Ukhov 2020-06-13 08:55:43 +02:00
parent f36787e376
commit 949e2e2d2b
2 changed files with 20 additions and 20 deletions

View File

@ -55,6 +55,23 @@ impl<'l> Statement<'l> {
unsafe { ffi::sqlite3_column_count(self.raw.0) as usize } 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.column_count(), "the index is out of range");
unsafe {
let pointer = ffi::sqlite3_column_name(self.raw.0, i as c_int);
debug_assert!(!pointer.is_null());
c_str_to_str!(pointer).unwrap()
}
}
/// Return column names.
#[inline]
pub fn column_names(&self) -> Vec<&str> {
(0..self.column_count()).map(|i| self.column_name(i)).collect()
}
/// Return the type of a column. /// Return the type of a column.
/// ///
/// The type becomes available after taking a step. /// The type becomes available after taking a step.
@ -70,23 +87,6 @@ impl<'l> Statement<'l> {
} }
} }
/// Return the name of a column.
#[inline]
pub fn name(&self, i: usize) -> &str {
debug_assert!(i < self.column_count(), "the index is out of range");
unsafe {
let pointer = ffi::sqlite3_column_name(self.raw.0, i as c_int);
debug_assert!(!pointer.is_null());
c_str_to_str!(pointer).unwrap()
}
}
/// Return column names.
#[inline]
pub fn names(&self) -> Vec<&str> {
(0..self.column_count()).map(|i| self.name(i)).collect()
}
/// Advance to the next state. /// Advance to the next state.
/// ///
/// The function should be called multiple times until `State::Done` is /// The function should be called multiple times until `State::Done` is

View File

@ -244,14 +244,14 @@ fn statement_column_count() {
} }
#[test] #[test]
fn statement_name() { fn statement_column_name() {
let connection = setup_users(":memory:"); let connection = setup_users(":memory:");
let statement = "SELECT id, name, age, photo AS user_photo FROM users"; let statement = "SELECT id, name, age, photo AS user_photo FROM users";
let statement = ok!(connection.prepare(statement)); let statement = ok!(connection.prepare(statement));
let names = statement.names(); let names = statement.column_names();
assert_eq!(names, vec!["id", "name", "age", "user_photo"]); assert_eq!(names, vec!["id", "name", "age", "user_photo"]);
assert_eq!("user_photo", statement.name(3)); assert_eq!("user_photo", statement.column_name(3));
} }
#[test] #[test]