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:
faumarayandClaude Opus 4.8 committed 2026-06-22 16:40:55 +03:00
1 parent 914eb0a232
commit 9ff27483ab
2 files changed
+26 -20

No files matched your search

+1
View File
@@ -106,6 +106,7 @@ target_link_libraries(EchoionCore
Void::Time
Void::Math
Void::Net
Void::Sqlite
PRIVATE
Threads::Threads
qjs
+25 -20
View File
@@ -1,6 +1,7 @@
#include <Database.hpp>
#include "echoion/core/Logger.hpp"
#include <Void/Math/Random.hpp>
#include <Void/Sqlite/Sqlite.hpp>
#include <cstring>
#include <format>
#include <shared_mutex>
@@ -9,12 +10,20 @@
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
{
public:
sqlite3* db = nullptr;
Void::Sqlite::Database voidDb;
sqlite3* db = nullptr; // non-owning, == voidDb.handle()
DatabaseConfig config;
bool initialized = false;
int version = 0;
@@ -36,25 +45,21 @@ namespace Echoion::Api
impl_->config = config;
int rc = sqlite3_open(config.path.c_str(), &impl_->db);
if (rc != SQLITE_OK) {
// Open via Void::Sqlite (RAII; sets WAL + busy_timeout + foreign_keys
// 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(
Error(ErrorCode::DatabaseError,
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
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
// Honor the configured busy timeout (Void defaults to 5s).
sqlite3_busy_timeout(impl_->db,
static_cast<int>(config.busyTimeout.count()));
@@ -83,10 +88,10 @@ namespace Echoion::Api
std::unique_lock<std::shared_mutex> lock(mutex_);
if (impl_->db) {
sqlite3_close(impl_->db);
impl_->db = nullptr;
}
// Drop the borrowed handle first, then let Void::Sqlite close the
// connection via RAII (move-assign an empty Database).
impl_->db = nullptr;
impl_->voidDb = Void::Sqlite::Database{};
impl_->initialized = false;
}