diff --git a/include/SQLiteCpp/Column.h b/include/SQLiteCpp/Column.h index 5e341b3..057b25f 100644 --- a/include/SQLiteCpp/Column.h +++ b/include/SQLiteCpp/Column.h @@ -246,7 +246,7 @@ public: * * @see getString */ - explicit operator std::string() const + operator std::string() const { return getString(); } diff --git a/include/SQLiteCpp/Statement.h b/include/SQLiteCpp/Statement.h index 6c68fb4..b467cf4 100644 --- a/include/SQLiteCpp/Statement.h +++ b/include/SQLiteCpp/Statement.h @@ -699,17 +699,15 @@ private: */ sqlite3_stmt* getPreparedStatement() const; - - /// Map of columns index by name (mutable so getColumnIndex can be const) - using TColumnNames = std::map; - std::string mQuery; //!< UTF-8 SQL Query sqlite3* mpSQLite; //!< Pointer to SQLite Database Connection Handle TStatementPtr mpPreparedStatement; //!< Shared Pointer to the prepared SQLite Statement Object int mColumnCount{0}; //!< Number of columns in the result of the prepared statement - mutable TColumnNames mColumnNames; //!< Map of columns index by name (mutable so getColumnIndex can be const) bool mbHasRow{false}; //!< true when a row has been fetched with executeStep() bool mbDone{false}; //!< true when the last executeStep() had no more row to fetch + + /// Map of columns index by name (mutable so getColumnIndex can be const) + mutable std::map mColumnNames{}; }; diff --git a/src/Column.cpp b/src/Column.cpp index cf25608..2edff46 100644 --- a/src/Column.cpp +++ b/src/Column.cpp @@ -30,6 +30,10 @@ Column::Column(const Statement::TStatementPtr& aStmtPtr, int aIndex) noexcept : mStmtPtr(aStmtPtr), mIndex(aIndex) { + if (!aStmtPtr) + { + throw SQLite::Exception("Statement was destroyed"); + } } // Return the named assigned to this result column (potentially aliased) diff --git a/src/Statement.cpp b/src/Statement.cpp index 135534d..677cf89 100644 --- a/src/Statement.cpp +++ b/src/Statement.cpp @@ -264,13 +264,13 @@ int Statement::getColumnIndex(const char* apName) const } } - const TColumnNames::const_iterator iIndex = mColumnNames.find(apName); + const auto iIndex = mColumnNames.find(apName); if (iIndex == mColumnNames.end()) { throw SQLite::Exception("Unknown column name."); } - return (*iIndex).second; + return iIndex->second; } const char * Statement::getColumnDeclaredType(const int aIndex) const diff --git a/tests/Statement_test.cpp b/tests/Statement_test.cpp index fbaec4b..8a79a18 100644 --- a/tests/Statement_test.cpp +++ b/tests/Statement_test.cpp @@ -119,7 +119,6 @@ TEST(Statement, moveConstructor) EXPECT_EQ(2, query.getColumnCount()); SQLite::Statement moved = std::move(query); EXPECT_TRUE(query.getQuery().empty()); - EXPECT_EQ(0, query.getColumnCount()); EXPECT_FALSE(moved.getQuery().empty()); EXPECT_EQ(2, moved.getColumnCount()); // Execute @@ -128,6 +127,16 @@ TEST(Statement, moveConstructor) EXPECT_FALSE(moved.isDone()); EXPECT_FALSE(query.hasRow()); EXPECT_FALSE(query.isDone()); + + // Const statement lookup + const auto const_query = std::move(moved); + auto index = const_query.getColumnIndex("value"); + EXPECT_EQ(1, index); + EXPECT_NO_THROW(const_query.getColumn(index)); + + // Moved statements should throw + EXPECT_THROW(query.getColumnIndex("value"), SQLite::Exception); + EXPECT_THROW(query.getColumn(index), SQLite::Exception); } #endif