mirror of
https://github.com/cuberite/SQLiteCpp.git
synced 2025-08-05 10:16:01 -04:00
Fixed #349; Column throw when constructed with nullptr
This commit is contained in:
parent
354323a875
commit
10d779a349
@ -246,7 +246,7 @@ public:
|
||||
*
|
||||
* @see getString
|
||||
*/
|
||||
explicit operator std::string() const
|
||||
operator std::string() const
|
||||
{
|
||||
return getString();
|
||||
}
|
||||
|
@ -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{};
|
||||
};
|
||||
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user