Add Database constructor for filesystem::path #296 (#314)

* Add Database constructor for filesystem::path #296

* Fixed incorrect MSVC version value for C++17

* Updated another incorrect version string

* Updated MSVC compiler check again. The <filesystem> header wasn't transitioned from std::experimental until MSVC 15.7

* Changed version check to look at c++ version no MSVC version
This commit is contained in:
Patrick Servello 2021-01-06 01:41:32 -06:00 committed by GitHub
parent 080bfbda57
commit 81e5a1f910
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 2 deletions

View File

@ -11,6 +11,11 @@
#pragma once
#include <SQLiteCpp/Column.h>
#if (__cplusplus >= 201703L) || ( defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L)) // c++17: Visual Studio 2017 version 15.7
#include <filesystem>
#endif // c++17
#include <memory>
#include <string.h>
@ -150,6 +155,36 @@ public:
{
}
#if (__cplusplus >= 201703L) || ( defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L)) // c++17: Visual Studio 2017 version 15.7
/**
* @brief Open the provided database std::filesystem::path.
*
* @note This feature requires std=C++17
*
* Uses sqlite3_open_v2() with readonly default flag, which is the opposite behavior
* of the old sqlite3_open() function (READWRITE+CREATE).
* This makes sense if you want to use it on a readonly filesystem
* or to prevent creation of a void file when a required file is missing.
*
* Exception is thrown in case of error, then the Database object is NOT constructed.
*
* @param[in] apFilename Path/uri to the database file ("filename" sqlite3 parameter)
* @param[in] aFlags SQLite::OPEN_READONLY/SQLite::OPEN_READWRITE/SQLite::OPEN_CREATE...
* @param[in] aBusyTimeoutMs Amount of milliseconds to wait before returning SQLITE_BUSY (see setBusyTimeout())
* @param[in] apVfs UTF-8 name of custom VFS to use, or nullptr for sqlite3 default
*
* @throw SQLite::Exception in case of error
*/
Database(const std::filesystem::path& apFilename,
const int aFlags = SQLite::OPEN_READONLY,
const int aBusyTimeoutMs = 0,
const std::string& aVfs = "") :
Database(apFilename.c_str(), aFlags, aBusyTimeoutMs, aVfs.empty() ? nullptr : aVfs.c_str())
{
}
#endif // c++17
// Database is non-copyable
Database(const Database&) = delete;
Database& operator=(const Database&) = delete;

View File

@ -15,6 +15,10 @@
#include <gtest/gtest.h>
#if (__cplusplus >= 201703L) || ( defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L)) // c++17: Visual Studio 2017 version 15.7
#include <filesystem>
#endif // c++17
#include <cstdio>
#include <fstream>
@ -46,8 +50,13 @@ TEST(Database, ctorExecCreateDropExist)
std::string filename = "test.db3";
EXPECT_THROW(SQLite::Database not_found(filename), SQLite::Exception);
// Create a new database
// Create a new database using a string or a std::filesystem::path if using c++17
#if (__cplusplus >= 201703L) || ( defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L)) // c++17: Visual Studio 2017 version 15.7
SQLite::Database db(std::filesystem::path("test.db3"), SQLite::OPEN_READWRITE|SQLite::OPEN_CREATE);
#else
SQLite::Database db("test.db3", SQLite::OPEN_READWRITE|SQLite::OPEN_CREATE);
#endif // c++17
EXPECT_STREQ("test.db3", db.getFilename().c_str());
EXPECT_FALSE(db.tableExists("test"));
EXPECT_FALSE(db.tableExists(std::string("test")));