Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions include/SQLiteCpp/Database.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ class Database
friend class Statement; // Give Statement constructor access to the mpSQLite Connection Handle

public:
// Empty constructor
Database();

/**
* @brief Open the provided database UTF-8 filename.
*
Expand Down Expand Up @@ -144,6 +147,17 @@ class Database
*/
~Database();

/**
* @brief Open the provided database UTF-8 filename. For use instead constructor.
* Example:
* Database db;
* db.open("database.db3", SQLite::OPEN_READWRITE|SQLite::OPEN_CREATE)
*/
void open(const char* apFilename,
const int aFlags = SQLite::OPEN_READONLY,
const int aBusyTimeoutMs = 0,
const char* apVfs = nullptr);

/**
* @brief Set a busy handler that sleeps for a specified amount of time when a table is locked.
*
Expand Down
29 changes: 29 additions & 0 deletions src/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ int getLibVersionNumber() noexcept // nothrow
return sqlite3_libversion_number();
}

// Empty constructor
Database::Database() {}


// Open the provided database UTF-8 filename with SQLite::OPEN_xxx provided flags.
Database::Database(const char* apFilename,
Expand Down Expand Up @@ -104,6 +107,32 @@ Database::~Database()
SQLITECPP_ASSERT(SQLITE_OK == ret, "database is locked"); // See SQLITECPP_ENABLE_ASSERT_HANDLER
}

/**
* @brief Open the provided database UTF-8 filename. For use instead constructor.
* Example:
* Database db;
* db.open("database.db3", SQLite::OPEN_READWRITE|SQLite::OPEN_CREATE)
*/
void Database::open(const char* apFilename,
const int aFlags /* = SQLite::OPEN_READONLY*/,
const int aBusyTimeoutMs /* = 0 */,
const char* apVfs /* = nullptr*/)
{
mpSQLite = nullptr;
mFilename = apFilename;
const int ret = sqlite3_open_v2(apFilename, &mpSQLite, aFlags, apVfs);
if (SQLITE_OK != ret)
{
const SQLite::Exception exception(mpSQLite, ret); // must create before closing
sqlite3_close(mpSQLite); // close is required even in case of error on opening
throw exception;
}
if (aBusyTimeoutMs > 0)
{
setBusyTimeout(aBusyTimeoutMs);
}
}

/**
* @brief Set a busy handler that sleeps for a specified amount of time when a table is locked.
*
Expand Down
18 changes: 18 additions & 0 deletions tests/Database_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,24 @@ TEST(Database, moveConstructor)

#endif

TEST(Database, emptyConstructor)
{
remove("test.db3");
{
// Create a new object of Database
SQLite::Database db;

// Open a database or create a new one
db.open("test.db3", SQLite::OPEN_READWRITE|SQLite::OPEN_CREATE);

// Create test table
EXPECT_FALSE(db.tableExists("test"));
db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)");
EXPECT_TRUE(db.tableExists("test"));
}
remove("test.db3");
}

TEST(Database, createCloseReopen)
{
remove("test.db3");
Expand Down