Own the database connection via Void::Sqlite
DatabaseImpl now holds a Void::Sqlite::Database (RAII open/close, WAL + busy-timeout + foreign_keys); the existing raw-sqlite3 query methods borrow its handle() unchanged. Removes the manual sqlite3_open/close + journal-mode boilerplate. Deliberately keeps echoion's schema_migrations migration framework rather than switching to Void's PRAGMA user_version Migrator: existing databases record versions in schema_migrations with user_version=0, and migrations 2/3 DROP tables — re-running them would destroy live data. Verified with an integration test (data survives close+reopen; migrate() stays idempotent). Link Void::Sqlite. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
914eb0a232
commit
9ff27483ab
2 files changed
+26
-20
No files matched your search
@@ -106,6 +106,7 @@ target_link_libraries(EchoionCore
|
|||||||
Void::Time
|
Void::Time
|
||||||
Void::Math
|
Void::Math
|
||||||
Void::Net
|
Void::Net
|
||||||
|
Void::Sqlite
|
||||||
PRIVATE
|
PRIVATE
|
||||||
Threads::Threads
|
Threads::Threads
|
||||||
qjs
|
qjs
|
||||||
|
|||||||
+25
-20
@@ -1,6 +1,7 @@
|
|||||||
#include <Database.hpp>
|
#include <Database.hpp>
|
||||||
#include "echoion/core/Logger.hpp"
|
#include "echoion/core/Logger.hpp"
|
||||||
#include <Void/Math/Random.hpp>
|
#include <Void/Math/Random.hpp>
|
||||||
|
#include <Void/Sqlite/Sqlite.hpp>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <format>
|
#include <format>
|
||||||
#include <shared_mutex>
|
#include <shared_mutex>
|
||||||
@@ -9,12 +10,20 @@
|
|||||||
namespace Echoion::Api
|
namespace Echoion::Api
|
||||||
{
|
{
|
||||||
|
|
||||||
// Internal implementation
|
// Internal implementation. The connection lifecycle (open/close, WAL,
|
||||||
|
// busy-timeout, foreign keys) is owned by Void::Sqlite::Database; `db` is a
|
||||||
|
// non-owning handle into it so the existing raw-sqlite3 query methods and
|
||||||
|
// the schema_migrations framework keep working byte-identically. The
|
||||||
|
// migration mechanism is deliberately NOT switched to Void's user_version
|
||||||
|
// Migrator: existing databases track versions in schema_migrations with
|
||||||
|
// user_version=0, and migrations 2/3 DROP tables — re-running them would
|
||||||
|
// destroy live data.
|
||||||
class DatabaseImpl
|
class DatabaseImpl
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
sqlite3* db = nullptr;
|
Void::Sqlite::Database voidDb;
|
||||||
|
sqlite3* db = nullptr; // non-owning, == voidDb.handle()
|
||||||
DatabaseConfig config;
|
DatabaseConfig config;
|
||||||
bool initialized = false;
|
bool initialized = false;
|
||||||
int version = 0;
|
int version = 0;
|
||||||
@@ -36,25 +45,21 @@ namespace Echoion::Api
|
|||||||
|
|
||||||
impl_->config = config;
|
impl_->config = config;
|
||||||
|
|
||||||
int rc = sqlite3_open(config.path.c_str(), &impl_->db);
|
// Open via Void::Sqlite (RAII; sets WAL + busy_timeout + foreign_keys
|
||||||
if (rc != SQLITE_OK) {
|
// for read-write modes). The raw handle is borrowed for the existing
|
||||||
|
// query methods.
|
||||||
|
auto opened = Void::Sqlite::Database::open(
|
||||||
|
config.path, Void::Sqlite::OpenMode::ReadWriteCreate);
|
||||||
|
if (!opened) {
|
||||||
return std::unexpected(
|
return std::unexpected(
|
||||||
Error(ErrorCode::DatabaseError,
|
Error(ErrorCode::DatabaseError,
|
||||||
std::format("Failed to open database: {}",
|
std::format("Failed to open database: {}",
|
||||||
sqlite3_errmsg(impl_->db))));
|
opened.error().message())));
|
||||||
}
|
}
|
||||||
|
impl_->voidDb = std::move(*opened);
|
||||||
|
impl_->db = impl_->voidDb.handle();
|
||||||
|
|
||||||
// Enable WAL mode for better concurrency
|
// Honor the configured busy timeout (Void defaults to 5s).
|
||||||
if (config.walMode) {
|
|
||||||
char* errMsg = nullptr;
|
|
||||||
sqlite3_exec(impl_->db, "PRAGMA journal_mode=WAL;", nullptr,
|
|
||||||
nullptr, &errMsg);
|
|
||||||
if (errMsg) {
|
|
||||||
sqlite3_free(errMsg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set busy timeout
|
|
||||||
sqlite3_busy_timeout(impl_->db,
|
sqlite3_busy_timeout(impl_->db,
|
||||||
static_cast<int>(config.busyTimeout.count()));
|
static_cast<int>(config.busyTimeout.count()));
|
||||||
|
|
||||||
@@ -83,10 +88,10 @@ namespace Echoion::Api
|
|||||||
|
|
||||||
std::unique_lock<std::shared_mutex> lock(mutex_);
|
std::unique_lock<std::shared_mutex> lock(mutex_);
|
||||||
|
|
||||||
if (impl_->db) {
|
// Drop the borrowed handle first, then let Void::Sqlite close the
|
||||||
sqlite3_close(impl_->db);
|
// connection via RAII (move-assign an empty Database).
|
||||||
impl_->db = nullptr;
|
impl_->db = nullptr;
|
||||||
}
|
impl_->voidDb = Void::Sqlite::Database{};
|
||||||
impl_->initialized = false;
|
impl_->initialized = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user