mirror of
https://github.com/fluencelabs/sqlite-wasm-connector
synced 2025-04-25 08:42:14 +00:00
Implement sqlite3_column_name and helper function to return column names
This commit is contained in:
parent
e0793a38a6
commit
fce61e13ad
@ -55,6 +55,21 @@ 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 in the statement
|
||||||
|
#[inline]
|
||||||
|
pub fn column_name(&self, i: usize) -> String {
|
||||||
|
unsafe {
|
||||||
|
let ret = ffi::sqlite3_column_name(self.raw.0, i as c_int);
|
||||||
|
c_str_to_string!(ret)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return column names in the statement
|
||||||
|
#[inline]
|
||||||
|
pub fn column_names(&self) -> Vec<String> {
|
||||||
|
(0..self.columns()).map(|i| self.column_name(i)).collect()
|
||||||
|
}
|
||||||
|
|
||||||
/// Return the type of a column.
|
/// Return the type of a column.
|
||||||
///
|
///
|
||||||
/// The type is revealed after the first step has been taken.
|
/// The type is revealed after the first step has been taken.
|
||||||
|
@ -137,11 +137,15 @@ fn cursor_workflow() {
|
|||||||
#[test]
|
#[test]
|
||||||
fn statement_columns() {
|
fn statement_columns() {
|
||||||
let connection = setup_users(":memory:");
|
let connection = setup_users(":memory:");
|
||||||
let statement = "SELECT * FROM users";
|
let statement = "SELECT id, name, age, photo as user_photo FROM users";
|
||||||
let mut statement = ok!(connection.prepare(statement));
|
let mut statement = ok!(connection.prepare(statement));
|
||||||
|
|
||||||
assert_eq!(statement.columns(), 4);
|
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));
|
||||||
|
|
||||||
assert_eq!(ok!(statement.next()), State::Row);
|
assert_eq!(ok!(statement.next()), State::Row);
|
||||||
|
|
||||||
assert_eq!(statement.columns(), 4);
|
assert_eq!(statement.columns(), 4);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user