From 134efd639244d94ffd112f97e80e6d93651ddb5f Mon Sep 17 00:00:00 2001 From: "Jack.Yuan" Date: Fri, 17 Apr 2015 15:58:36 +0800 Subject: [PATCH 1/2] Update Statement.cpp --- src/Statement.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/Statement.cpp b/src/Statement.cpp index d606e4d..ac18de4 100644 --- a/src/Statement.cpp +++ b/src/Statement.cpp @@ -256,6 +256,33 @@ Column Statement::getColumn(const int aIndex) return Column(mStmtPtr, aIndex); } +// Return a copy of the column data specified by its column name starting at 0 +// (use the Column copy-constructor) +Column Statement::getColumn(const char* aName) +{ + int aIndex = -1; + + if (false == mbOk) + { + throw SQLite::Exception("No row to get a column from"); + } + else + { + for (int i = 0; i < mColumnCount; i++) { + if (sqlite3_column_name(mStmtPtr, i) == aName) + break; + } + + if ((aIndex < 0) || (aIndex >= mColumnCount)) { + throw SQLite::Exception("Column index out of range"); + } + + } + + // Share the Statement Object handle with the new Column created + return Column(mStmtPtr, aIndex); +} + // Test if the column is NULL bool Statement::isColumnNull(const int aIndex) const { From b6fdf50669eb0af70981c0dc3253a59856115f8a Mon Sep 17 00:00:00 2001 From: "Jack.Yuan" Date: Fri, 17 Apr 2015 15:59:58 +0800 Subject: [PATCH 2/2] Fix issue: Column by name #23 add method `Column getColumn(const char* aName);` in Statement.h --- include/SQLiteCpp/Statement.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/include/SQLiteCpp/Statement.h b/include/SQLiteCpp/Statement.h index 521d38f..7dab620 100644 --- a/include/SQLiteCpp/Statement.h +++ b/include/SQLiteCpp/Statement.h @@ -295,6 +295,33 @@ public: */ Column getColumn(const int aIndex); + /** + * @brief Return a copy of the column data specified by its column name + * + * Can be used to access the data of the current row of result when applicable, + * while the executeStep() method returns true. + * + * Throw an exception if there is no row to return a Column from : + * - before any executeStep() call + * - after the last executeStep() returned false + * - after a reset() call + * + * Throw an exception if the specified index is out of the [0, getColumnCount()) range. + * + * @param[in] aName Name of the column, starting at index 0 + * + * @note This method is no more const, starting in v0.5, + * which reflects the fact that the returned Column object will + * share the ownership of the underlying sqlite3_stmt. + * + * @warning The resulting Column object must not be memorized "as-is". + * Is is only a wrapper around the current result row, so it is only valid + * while the row from the Statement remains valid, that is only until next executeStep() call. + * Thus, you should instead extract immediately its data (getInt(), getText()...) + * and use or copy this data for any later usage. + */ + Column getColumn(const char* aName); + /** * @brief Test if the column value is NULL *