From fce61e13ad2f02665a3e7c13465fd975aadadb9e Mon Sep 17 00:00:00 2001 From: Jayson Reis Date: Fri, 22 Jun 2018 15:38:53 +0200 Subject: [PATCH 1/3] Implement sqlite3_column_name and helper function to return column names --- src/statement.rs | 15 +++++++++++++++ tests/lib.rs | 6 +++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/statement.rs b/src/statement.rs index 0487115..b864d46 100644 --- a/src/statement.rs +++ b/src/statement.rs @@ -55,6 +55,21 @@ impl<'l> Statement<'l> { 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 { + (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. diff --git a/tests/lib.rs b/tests/lib.rs index f69cb87..dbbc89d 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -137,11 +137,15 @@ fn cursor_workflow() { #[test] fn statement_columns() { 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)); 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!(statement.columns(), 4); From beda4c82c863ca053c10bd46f74899eb591b63b3 Mon Sep 17 00:00:00 2001 From: Jayson Reis Date: Mon, 25 Jun 2018 17:56:05 +0200 Subject: [PATCH 2/3] Change column_name to &str to avoid double allocation --- src/statement.rs | 6 +++--- tests/lib.rs | 8 +++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/statement.rs b/src/statement.rs index b864d46..1a79d5e 100644 --- a/src/statement.rs +++ b/src/statement.rs @@ -57,16 +57,16 @@ impl<'l> Statement<'l> { /// Return the name of a column in the statement #[inline] - pub fn column_name(&self, i: usize) -> String { + pub fn column_name(&self, i: usize) -> &str { unsafe { let ret = ffi::sqlite3_column_name(self.raw.0, i as c_int); - c_str_to_string!(ret) + c_str_to_str!(ret).unwrap() } } /// Return column names in the statement #[inline] - pub fn column_names(&self) -> Vec { + pub fn column_names(&self) -> Vec<&str> { (0..self.columns()).map(|i| self.column_name(i)).collect() } diff --git a/tests/lib.rs b/tests/lib.rs index dbbc89d..e81fc25 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -142,9 +142,11 @@ fn statement_columns() { 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)); + { + 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); From 4fa8989faa4b17400bb910f7781821c669307ab0 Mon Sep 17 00:00:00 2001 From: Jayson Reis Date: Tue, 10 Jul 2018 21:32:29 +0200 Subject: [PATCH 3/3] Changes for core review --- src/statement.rs | 5 +++-- tests/lib.rs | 24 +++++++++++++++--------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/statement.rs b/src/statement.rs index 1a79d5e..f817b9e 100644 --- a/src/statement.rs +++ b/src/statement.rs @@ -55,16 +55,17 @@ impl<'l> Statement<'l> { unsafe { ffi::sqlite3_column_count(self.raw.0) as usize } } - /// Return the name of a column in the statement + /// 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 in the statement + /// Return column names. #[inline] pub fn column_names(&self) -> Vec<&str> { (0..self.columns()).map(|i| self.column_name(i)).collect() diff --git a/tests/lib.rs b/tests/lib.rs index e81fc25..9295089 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -137,22 +137,28 @@ fn cursor_workflow() { #[test] fn statement_columns() { let connection = setup_users(":memory:"); - let statement = "SELECT id, name, age, photo as user_photo FROM users"; + let statement = "SELECT * FROM users"; let mut 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)); - } - 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:");