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 <chrono>
#include <memory>
#include <mapbox/variant.hpp>
#include <variant>
namespace mapbox {
namespace sqlite {
@@ -77,7 +77,7 @@ private:
public:
Database(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);
Database(Database&&) noexcept;

View File

@@ -69,7 +69,8 @@ public:
~DatabaseImpl() {
const int error = sqlite3_close(db);
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
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
@@ -130,7 +131,7 @@ MBGL_CONSTRUCTOR(initialize) {
#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;
const int error = sqlite3_open_v2(filename.c_str(), &db, flags | SQLITE_OPEN_URI, nullptr);
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) {
auto result = tryOpen(filename, flags);
if (result.is<Exception>()) {
throw std::move(result.get<Exception>());
if (std::holds_alternative<Exception>(result)) {
throw std::move(std::get<Exception>(result));
} else {
return std::move(result.get<Database>());
return std::move(std::get<Database>(result));
}
}

View File

@@ -14,6 +14,7 @@
#include <chrono>
#include <limits>
#include <optional>
#include <variant>
#include <mbgl/util/chrono.hpp>
#include <mbgl/util/logging.hpp>
@@ -77,7 +78,7 @@ public:
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")) {
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) {
auto result = tryOpen(filename, flags);
if (result.is<Exception>()) {
throw result.get<Exception>();
if (std::holds_alternative<Exception>(result)) {
throw std::get<Exception>(result);
} 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.
auto result = mapbox::sqlite::Database::tryOpen("test/fixtures/offline_database/foobar123.db",
mapbox::sqlite::ReadOnly);
ASSERT_TRUE(result.is<mapbox::sqlite::Exception>());
ASSERT_EQ(result.get<mapbox::sqlite::Exception>().code, mapbox::sqlite::ResultCode::CantOpen);
ASSERT_TRUE(std::holds_alternative<mapbox::sqlite::Exception>(result));
ASSERT_EQ(std::get<mapbox::sqlite::Exception>(result).code, mapbox::sqlite::ResultCode::CantOpen);
EXPECT_EQ(0u, log.uncheckedCount());
}