Skip to content

Commit 10d779a

Browse files
committed
Fixed #349; Column throw when constructed with nullptr
1 parent 354323a commit 10d779a

File tree

5 files changed

+20
-9
lines changed

5 files changed

+20
-9
lines changed

include/SQLiteCpp/Column.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ class Column
246246
*
247247
* @see getString
248248
*/
249-
explicit operator std::string() const
249+
operator std::string() const
250250
{
251251
return getString();
252252
}

include/SQLiteCpp/Statement.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -699,17 +699,15 @@ class Statement
699699
*/
700700
sqlite3_stmt* getPreparedStatement() const;
701701

702-
703-
/// Map of columns index by name (mutable so getColumnIndex can be const)
704-
using TColumnNames = std::map<std::string, int>;
705-
706702
std::string mQuery; //!< UTF-8 SQL Query
707703
sqlite3* mpSQLite; //!< Pointer to SQLite Database Connection Handle
708704
TStatementPtr mpPreparedStatement; //!< Shared Pointer to the prepared SQLite Statement Object
709705
int mColumnCount{0}; //!< Number of columns in the result of the prepared statement
710-
mutable TColumnNames mColumnNames; //!< Map of columns index by name (mutable so getColumnIndex can be const)
711706
bool mbHasRow{false}; //!< true when a row has been fetched with executeStep()
712707
bool mbDone{false}; //!< true when the last executeStep() had no more row to fetch
708+
709+
/// Map of columns index by name (mutable so getColumnIndex can be const)
710+
mutable std::map<std::string, int> mColumnNames{};
713711
};
714712

715713

src/Column.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ Column::Column(const Statement::TStatementPtr& aStmtPtr, int aIndex) noexcept :
3030
mStmtPtr(aStmtPtr),
3131
mIndex(aIndex)
3232
{
33+
if (!aStmtPtr)
34+
{
35+
throw SQLite::Exception("Statement was destroyed");
36+
}
3337
}
3438

3539
// Return the named assigned to this result column (potentially aliased)

src/Statement.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,13 +264,13 @@ int Statement::getColumnIndex(const char* apName) const
264264
}
265265
}
266266

267-
const TColumnNames::const_iterator iIndex = mColumnNames.find(apName);
267+
const auto iIndex = mColumnNames.find(apName);
268268
if (iIndex == mColumnNames.end())
269269
{
270270
throw SQLite::Exception("Unknown column name.");
271271
}
272272

273-
return (*iIndex).second;
273+
return iIndex->second;
274274
}
275275

276276
const char * Statement::getColumnDeclaredType(const int aIndex) const

tests/Statement_test.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ TEST(Statement, moveConstructor)
119119
EXPECT_EQ(2, query.getColumnCount());
120120
SQLite::Statement moved = std::move(query);
121121
EXPECT_TRUE(query.getQuery().empty());
122-
EXPECT_EQ(0, query.getColumnCount());
123122
EXPECT_FALSE(moved.getQuery().empty());
124123
EXPECT_EQ(2, moved.getColumnCount());
125124
// Execute
@@ -128,6 +127,16 @@ TEST(Statement, moveConstructor)
128127
EXPECT_FALSE(moved.isDone());
129128
EXPECT_FALSE(query.hasRow());
130129
EXPECT_FALSE(query.isDone());
130+
131+
// Const statement lookup
132+
const auto const_query = std::move(moved);
133+
auto index = const_query.getColumnIndex("value");
134+
EXPECT_EQ(1, index);
135+
EXPECT_NO_THROW(const_query.getColumn(index));
136+
137+
// Moved statements should throw
138+
EXPECT_THROW(query.getColumnIndex("value"), SQLite::Exception);
139+
EXPECT_THROW(query.getColumn(index), SQLite::Exception);
131140
}
132141

133142
#endif

0 commit comments

Comments
 (0)