diff --git a/include/SQLiteCpp/Database.h b/include/SQLiteCpp/Database.h index 1584a6e5..44064e76 100644 --- a/include/SQLiteCpp/Database.h +++ b/include/SQLiteCpp/Database.h @@ -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. * @@ -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. * diff --git a/src/Database.cpp b/src/Database.cpp index 88a35b1e..d88acead 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -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, @@ -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. * diff --git a/tests/Database_test.cpp b/tests/Database_test.cpp index b9973e3f..64925421 100644 --- a/tests/Database_test.cpp +++ b/tests/Database_test.cpp @@ -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");