Skip to content

Commit 6d089fc

Browse files
committed
add unit test & tidy diff
1 parent 49a568f commit 6d089fc

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

include/SQLiteCpp/Database.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ class Database
434434
/// Return UTF-8 encoded English language explanation of the most recent failed API call (if any).
435435
const char* getErrorMsg() const noexcept;
436436

437-
/// Return the filename used to open the database; empty if the Database wrapped existing sqlite3*
437+
/// Return the filename used to open the database.
438438
const std::string& getFilename() const noexcept
439439
{
440440
return mFilename;
@@ -556,7 +556,10 @@ class Database
556556
static Header getHeaderInfo(const std::string& aFilename);
557557

558558
// Parse SQLite header data from a database file.
559-
Header getHeaderInfo();
559+
Header getHeaderInfo()
560+
{
561+
return getHeaderInfo(mFilename);
562+
}
560563

561564
/**
562565
* @brief BackupType for the backup() method

src/Database.cpp

+5-12
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ Database::Database(sqlite3* apSQLite,
9494
{
9595
SQLITECPP_ASSERT(apSQLite != nullptr, "Database(nullptr)");
9696
mSQLitePtr.reset(apSQLite);
97+
const char *zFilename = sqlite3_db_filename(mSQLitePtr.get(), nullptr);
98+
if (zFilename)
99+
{
100+
mFilename = zFilename;
101+
}
97102
if (aBusyTimeoutMs > 0)
98103
{
99104
setBusyTimeout(aBusyTimeoutMs);
@@ -446,18 +451,6 @@ Header Database::getHeaderInfo(const std::string& aFilename)
446451
return h;
447452
}
448453

449-
Header Database::getHeaderInfo()
450-
{
451-
if (!mFilename.empty())
452-
{
453-
return getHeaderInfo(mFilename);
454-
}
455-
const char *zFilename = sqlite3_db_filename(mSQLitePtr.get(), nullptr);
456-
return getHeaderInfo(std::string(zFilename ? zFilename : ""));
457-
}
458-
459-
460-
461454
void Database::backup(const char* apFilename, BackupType aType)
462455
{
463456
// Open the database file identified by apFilename

tests/Database_test.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,28 @@ TEST(Database, createCloseReopen)
121121
remove("test.db3");
122122
}
123123

124+
TEST(Database, wrapper)
125+
{
126+
remove("test.db4");
127+
// Create a new database using SQLite3 directly
128+
sqlite3 *dbconn = nullptr;
129+
int rc = sqlite3_open_v2("test.db4", &dbconn, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, nullptr);
130+
EXPECT_EQ(rc, SQLITE_OK);
131+
{
132+
// instantiate SQLite::Database wrapper
133+
SQLite::Database db(dbconn, 5000);
134+
EXPECT_FALSE(db.tableExists("test"));
135+
db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)");
136+
EXPECT_TRUE(db.tableExists("test"));
137+
std::string filename = db.getFilename();
138+
EXPECT_STREQ(filename.substr(filename.rfind('/')+1).c_str(), "test.db4");
139+
}
140+
// dbconn remains open after db destruction
141+
rc = sqlite3_close(dbconn);
142+
EXPECT_EQ(rc, SQLITE_OK);
143+
remove("test.db4");
144+
}
145+
124146
TEST(Database, inMemory)
125147
{
126148
{

0 commit comments

Comments
 (0)