Skip to content

Commit 27a3252

Browse files
committed
Add test for Column std::shared_ptr; remove noexcept from throwing Column constructor
1 parent 10d779a commit 27a3252

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

include/SQLiteCpp/Column.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class Column
5454
* @param[in] aStmtPtr Shared pointer to the prepared SQLite Statement Object.
5555
* @param[in] aIndex Index of the column in the row of result, starting at 0
5656
*/
57-
explicit Column(const Statement::TStatementPtr& aStmtPtr, int aIndex) noexcept;
57+
explicit Column(const Statement::TStatementPtr& aStmtPtr, int aIndex);
5858

5959
// default destructor: the finalization will be done by the destructor of the last shared pointer
6060
// default copy constructor and assignment operator are perfectly suited :

src/Column.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const int Null = SQLITE_NULL;
2626

2727

2828
// Encapsulation of a Column in a row of the result pointed by the prepared Statement.
29-
Column::Column(const Statement::TStatementPtr& aStmtPtr, int aIndex) noexcept :
29+
Column::Column(const Statement::TStatementPtr& aStmtPtr, int aIndex) :
3030
mStmtPtr(aStmtPtr),
3131
mIndex(aIndex)
3232
{

tests/Column_test.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,39 @@ TEST(Column, stream)
241241
std::string content = ss.str();
242242
EXPECT_EQ(content, str);
243243
}
244+
245+
TEST(Column, shared_ptr)
246+
{
247+
// Create a new database
248+
SQLite::Database db(":memory:", SQLite::OPEN_READWRITE|SQLite::OPEN_CREATE);
249+
EXPECT_EQ(0, db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, msg TEXT)"));
250+
EXPECT_EQ(1, db.exec(R"(INSERT INTO test VALUES (42, "fortytwo"))"));
251+
const char* query_str = "SELECT id, msg FROM test";
252+
253+
std::unique_ptr<SQLite::Statement> query{ new SQLite::Statement(db, query_str) };
254+
query->executeStep();
255+
256+
auto column0 = query->getColumn(0);
257+
auto column1 = query->getColumn(1);
258+
query.reset();
259+
260+
EXPECT_EQ(42, column0.getInt());
261+
EXPECT_STREQ("fortytwo", column1.getText());
262+
263+
query.reset(new SQLite::Statement(db, query_str));
264+
query->executeStep();
265+
column0 = query->getColumn(0);
266+
EXPECT_EQ(true, column0.isInteger());
267+
query->executeStep(); // query is done
268+
269+
// Undefined behavior
270+
// auto x = column0.getInt();
271+
272+
query.reset();
273+
274+
// Undefined behavior
275+
// auto x = column0.getInt();
276+
// bool isInt = column0.isInteger();
277+
278+
EXPECT_STREQ("id", column0.getName());
279+
}

0 commit comments

Comments
 (0)