database.cpp¶
src/storage/database.cpp
@file database.cpp @brief SqliteDatabase implementation with migration runner.
Owners: @tvanfossen
close
void entropic::SqliteDatabase::close()
Close database connection.
src/storage/database.cpp:221
Source · 7 lines
void SqliteDatabase::close() {
std::lock_guard lock(mutex_);
if (db_) {
sqlite3_close(db_);
db_ = nullptr;
}
}Activity · 4 steps
Requirements
execute
bool entropic::SqliteDatabase::execute(std::string_view sql, std::function< void(sqlite3_stmt *)> binder=nullptr)
Execute a write statement (INSERT, UPDATE, DELETE).
src/storage/database.cpp:269
Called by SCENARIO, complete_delegation, create_conversation, create_delegation, delete_conversation, save_messages, save_snapshot, update_title
Calls prepare
Source · 18 lines
bool SqliteDatabase::execute(std::string_view sql,
std::function<void(sqlite3_stmt*)> binder) {
std::lock_guard lock(mutex_);
if (!db_) return false;
auto* stmt = prepare(sql);
if (!stmt) return false;
if (binder) binder(stmt);
int rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
bool ok = (rc == SQLITE_DONE);
if (!ok) {
logger->error("SQL execute failed: {}", sqlite3_errmsg(db_));
}
return ok;
}Activity · 14 steps
Requirements
execute_raw
bool entropic::SqliteDatabase::execute_raw(std::string_view sql)
Execute raw SQL (multiple statements, no binding).
src/storage/database.cpp:295
Source · 17 lines
bool SqliteDatabase::execute_raw(std::string_view sql) {
std::lock_guard lock(mutex_);
if (!db_) return false;
char* err_msg = nullptr;
int rc = sqlite3_exec(
db_, std::string(sql).c_str(),
nullptr, nullptr, &err_msg);
if (rc != SQLITE_OK) {
logger->error("SQL exec failed: {}",
err_msg ? err_msg : "unknown error");
sqlite3_free(err_msg);
return false;
}
return true;
}Activity · 10 steps
Requirements
fetch_all
size_t entropic::SqliteDatabase::fetch_all(std::string_view sql, std::function< void(sqlite3_stmt *)> binder, std::function< void(sqlite3_stmt *)> row_handler)
Fetch all matching rows.
src/storage/database.cpp:353
Called by SCENARIO, get_delegation_by_id, get_delegations, latest_delegation_for_target, list_conversations, load_conversation, search_conversations, search_delegations
Calls prepare
Source · 21 lines
size_t SqliteDatabase::fetch_all(
std::string_view sql,
std::function<void(sqlite3_stmt*)> binder,
std::function<void(sqlite3_stmt*)> row_handler) {
std::lock_guard lock(mutex_);
if (!db_) return 0;
auto* stmt = prepare(sql);
if (!stmt) return 0;
if (binder) binder(stmt);
size_t count = 0;
while (sqlite3_step(stmt) == SQLITE_ROW) {
if (row_handler) row_handler(stmt);
++count;
}
sqlite3_finalize(stmt);
return count;
}Activity · 15 steps
Requirements
fetch_one
bool entropic::SqliteDatabase::fetch_one(std::string_view sql, std::function< void(sqlite3_stmt *)> binder, std::function< void(sqlite3_stmt *)> extractor)
Fetch a single row.
src/storage/database.cpp:322
Source · 21 lines
bool SqliteDatabase::fetch_one(
std::string_view sql,
std::function<void(sqlite3_stmt*)> binder,
std::function<void(sqlite3_stmt*)> extractor) {
std::lock_guard lock(mutex_);
if (!db_) return false;
auto* stmt = prepare(sql);
if (!stmt) return false;
if (binder) binder(stmt);
int rc = sqlite3_step(stmt);
bool found = (rc == SQLITE_ROW);
if (found && extractor) {
extractor(stmt);
}
sqlite3_finalize(stmt);
return found;
}Activity · 14 steps
Requirements
initialize
bool entropic::SqliteDatabase::initialize()
Initialize database and run pending migrations.
src/storage/database.cpp:183
Source · 32 lines
bool SqliteDatabase::initialize() {
std::lock_guard lock(mutex_);
// Ensure parent directory exists
auto parent = db_path_.parent_path();
if (!parent.empty()) {
std::filesystem::create_directories(parent);
}
int rc = sqlite3_open_v2(
db_path_.c_str(), &db_,
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX,
nullptr);
if (rc != SQLITE_OK) {
logger->error("Failed to open database {}: {}",
db_path_.string(), sqlite3_errmsg(db_));
return false;
}
// Enable foreign keys and WAL mode
sqlite3_exec(db_, "PRAGMA foreign_keys = ON", nullptr, nullptr, nullptr);
sqlite3_exec(db_, "PRAGMA journal_mode = WAL", nullptr, nullptr, nullptr);
if (!run_migrations()) {
logger->error("Migration failed for {}", db_path_.string());
return false;
}
logger->info("Database initialized: {}", db_path_.string());
return true;
}Activity · 15 steps
Requirements
is_open
bool entropic::SqliteDatabase::is_open() const
Check if database is open.
src/storage/database.cpp:235
Source · 4 lines
bool SqliteDatabase::is_open() const {
std::lock_guard lock(mutex_);
return db_ != nullptr;
}Activity · 2 steps
Requirements
prepare
sqlite3_stmt * entropic::SqliteDatabase::prepare(std::string_view sql)
Prepare a SQL statement.
src/storage/database.cpp:247
Source · 13 lines
sqlite3_stmt* SqliteDatabase::prepare(std::string_view sql) {
sqlite3_stmt* stmt = nullptr;
int rc = sqlite3_prepare_v2(
db_, sql.data(), static_cast<int>(sql.size()),
&stmt, nullptr);
if (rc != SQLITE_OK) {
logger->error("SQL prepare failed: {} — {}",
sqlite3_errmsg(db_), std::string(sql));
return nullptr;
}
return stmt;
}Activity · 6 steps
Requirements
run_migrations
bool entropic::SqliteDatabase::run_migrations()
Run all pending migrations.
src/storage/database.cpp:469
Source · 24 lines
bool SqliteDatabase::run_migrations() {
char* err_msg = nullptr;
int rc = sqlite3_exec(db_,
"CREATE TABLE IF NOT EXISTS migrations ("
" id INTEGER PRIMARY KEY,"
" name TEXT UNIQUE,"
" applied_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP"
")",
nullptr, nullptr, &err_msg);
if (rc != SQLITE_OK) {
logger->error("Failed to create migrations table: {}",
err_msg ? err_msg : "unknown");
sqlite3_free(err_msg);
return false;
}
auto applied = get_applied_migrations(db_);
for (const auto& mig : MIGRATIONS) {
if (is_applied(applied, mig.name)) continue;
if (!apply_migration(db_, mig)) return false;
}
return true;
}