Fixed #349; Column throw when constructed with nullptr

This commit is contained in:
Kacperos155 2022-01-26 03:22:17 +01:00
parent 354323a875
commit 10d779a349
5 changed files with 20 additions and 9 deletions

View File

@ -246,7 +246,7 @@ public:
*
* @see getString
*/
explicit operator std::string() const
operator std::string() const
{
return getString();
}

View File

@ -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, int>;
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<std::string, int> mColumnNames{};
};

View File

@ -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)

View File

@ -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

View File

@ -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