Use std::variant for Database::tryOpen (#2481)

This commit is contained in:
Bart Louwers
2024-06-11 16:45:24 +02:00
committed by GitHub
parent 8c743d2467
commit a31e01dd25
4 changed files with 16 additions and 14 deletions

View File

@@ -5,7 +5,7 @@
#include <stdexcept> #include <stdexcept>
#include <chrono> #include <chrono>
#include <memory> #include <memory>
#include <mapbox/variant.hpp> #include <variant>
namespace mapbox { namespace mapbox {
namespace sqlite { namespace sqlite {
@@ -77,7 +77,7 @@ private:
public: public:
Database(const Database&) = delete; Database(const Database&) = delete;
Database& operator=(const Database&) = delete; Database& operator=(const Database&) = delete;
static mapbox::util::variant<Database, Exception> tryOpen(const std::string& filename, int flags = 0); static std::variant<Database, Exception> tryOpen(const std::string& filename, int flags = 0);
static Database open(const std::string& filename, int flags = 0); static Database open(const std::string& filename, int flags = 0);
Database(Database&&) noexcept; Database(Database&&) noexcept;

View File

@@ -69,7 +69,8 @@ public:
~DatabaseImpl() { ~DatabaseImpl() {
const int error = sqlite3_close(db); const int error = sqlite3_close(db);
if (error != SQLITE_OK) { if (error != SQLITE_OK) {
mbgl::Log::Error(mbgl::Event::Database, error, "Failed to close database: %s", sqlite3_errmsg(db)); mbgl::Log::Error(
mbgl::Event::Database, error, std::string("Failed to close database: ") + sqlite3_errmsg(db));
} }
} }
@@ -108,7 +109,7 @@ public:
#ifndef NDEBUG #ifndef NDEBUG
void logSqlMessage(void*, const int err, const char* msg) { void logSqlMessage(void*, const int err, const char* msg) {
mbgl::Log::Record(mbgl::EventSeverity::Debug, mbgl::Event::Database, err, "%s", msg); mbgl::Log::Record(mbgl::EventSeverity::Debug, mbgl::Event::Database, std::to_string(err) + msg);
} }
#endif #endif
@@ -130,7 +131,7 @@ MBGL_CONSTRUCTOR(initialize) {
#endif #endif
} }
mapbox::util::variant<Database, Exception> Database::tryOpen(const std::string& filename, int flags) { std::variant<Database, Exception> Database::tryOpen(const std::string& filename, int flags) {
sqlite3* db = nullptr; sqlite3* db = nullptr;
const int error = sqlite3_open_v2(filename.c_str(), &db, flags | SQLITE_OPEN_URI, nullptr); const int error = sqlite3_open_v2(filename.c_str(), &db, flags | SQLITE_OPEN_URI, nullptr);
if (error != SQLITE_OK) { if (error != SQLITE_OK) {
@@ -143,10 +144,10 @@ mapbox::util::variant<Database, Exception> Database::tryOpen(const std::string&
Database Database::open(const std::string& filename, int flags) { Database Database::open(const std::string& filename, int flags) {
auto result = tryOpen(filename, flags); auto result = tryOpen(filename, flags);
if (result.is<Exception>()) { if (std::holds_alternative<Exception>(result)) {
throw std::move(result.get<Exception>()); throw std::move(std::get<Exception>(result));
} else { } else {
return std::move(result.get<Database>()); return std::move(std::get<Database>(result));
} }
} }

View File

@@ -14,6 +14,7 @@
#include <chrono> #include <chrono>
#include <limits> #include <limits>
#include <optional> #include <optional>
#include <variant>
#include <mbgl/util/chrono.hpp> #include <mbgl/util/chrono.hpp>
#include <mbgl/util/logging.hpp> #include <mbgl/util/logging.hpp>
@@ -77,7 +78,7 @@ public:
int64_t changes = 0; int64_t changes = 0;
}; };
mapbox::util::variant<Database, Exception> Database::tryOpen(const std::string& filename, int flags) { std::variant<Database, Exception> Database::tryOpen(const std::string& filename, int flags) {
if (!QSqlDatabase::drivers().contains("QSQLITE")) { if (!QSqlDatabase::drivers().contains("QSQLITE")) {
return Exception{ResultCode::CantOpen, "SQLite driver not found."}; return Exception{ResultCode::CantOpen, "SQLite driver not found."};
} }
@@ -112,10 +113,10 @@ mapbox::util::variant<Database, Exception> Database::tryOpen(const std::string&
Database Database::open(const std::string& filename, int flags) { Database Database::open(const std::string& filename, int flags) {
auto result = tryOpen(filename, flags); auto result = tryOpen(filename, flags);
if (result.is<Exception>()) { if (std::holds_alternative<Exception>(result)) {
throw result.get<Exception>(); throw std::get<Exception>(result);
} else { } else {
return std::move(result.get<Database>()); return std::move(std::get<Database>(result));
} }
} }

View File

@@ -37,8 +37,8 @@ TEST(SQLite, TEST_REQUIRES_WRITE(TryOpen)) {
// make sure all the backends behave the same way. // make sure all the backends behave the same way.
auto result = mapbox::sqlite::Database::tryOpen("test/fixtures/offline_database/foobar123.db", auto result = mapbox::sqlite::Database::tryOpen("test/fixtures/offline_database/foobar123.db",
mapbox::sqlite::ReadOnly); mapbox::sqlite::ReadOnly);
ASSERT_TRUE(result.is<mapbox::sqlite::Exception>()); ASSERT_TRUE(std::holds_alternative<mapbox::sqlite::Exception>(result));
ASSERT_EQ(result.get<mapbox::sqlite::Exception>().code, mapbox::sqlite::ResultCode::CantOpen); ASSERT_EQ(std::get<mapbox::sqlite::Exception>(result).code, mapbox::sqlite::ResultCode::CantOpen);
EXPECT_EQ(0u, log.uncheckedCount()); EXPECT_EQ(0u, log.uncheckedCount());
} }