diff --git a/src/Database.cpp b/src/Database.cpp index ceda427..ad05452 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -5,25 +5,21 @@ #include #include #include -#include namespace Echoion::Api { - // 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. + // Internal implementation. The connection (open/close, WAL, busy-timeout, + // foreign keys) is owned by Void::Sqlite::Database; all queries go through + // its Statement/Transaction API. 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: Void::Sqlite::Database voidDb; - sqlite3* db = nullptr; // non-owning, == voidDb.handle() DatabaseConfig config; bool initialized = false; int version = 0; @@ -46,8 +42,7 @@ namespace Echoion::Api impl_->config = config; // 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. + // for read-write modes). auto opened = Void::Sqlite::Database::open( config.path, Void::Sqlite::OpenMode::ReadWriteCreate); if (!opened) { @@ -56,11 +51,11 @@ namespace Echoion::Api opened.error().message())); } impl_->voidDb = std::move(*opened); - impl_->db = impl_->voidDb.handle(); // Honor the configured busy timeout (Void defaults to 5s). - sqlite3_busy_timeout(impl_->db, - static_cast(config.busyTimeout.count())); + [[maybe_unused]] auto bt = impl_->voidDb.exec(std::format( + "PRAGMA busy_timeout = {}", + static_cast(config.busyTimeout.count()))); // Run migrations (without lock to avoid deadlock, since migrate() will // acquire lock) @@ -87,9 +82,7 @@ namespace Echoion::Api std::unique_lock lock(mutex_); - // Drop the borrowed handle first, then let Void::Sqlite close the - // connection via RAII (move-assign an empty Database). - impl_->db = nullptr; + // Close the connection via RAII (move-assign an empty Database). impl_->voidDb = Void::Sqlite::Database{}; impl_->initialized = false; } @@ -113,25 +106,18 @@ namespace Echoion::Api ); )"; - char* errMsg = nullptr; - sqlite3_exec(impl_->db, createMigrationsTable, nullptr, nullptr, - &errMsg); - if (errMsg) { - std::string error = errMsg; - sqlite3_free(errMsg); - return fail(ErrorCode::DatabaseError, error); + if (auto r = impl_->voidDb.exec(createMigrationsTable); !r) { + return fail(ErrorCode::DatabaseError, r.error().message()); } // Get current version - sqlite3_stmt* stmt; int currentVersion = 0; - if (sqlite3_prepare_v2(impl_->db, - "SELECT MAX(version) FROM schema_migrations", -1, - &stmt, nullptr) == SQLITE_OK) { - if (sqlite3_step(stmt) == SQLITE_ROW) { - currentVersion = sqlite3_column_int(stmt, 0); + if (auto vstmt = impl_->voidDb.prepare( + "SELECT MAX(version) FROM schema_migrations")) { + auto& stmt = *vstmt; + if (stmt.step().value_or(false)) { + currentVersion = stmt.column_int(0); } - sqlite3_finalize(stmt); } // Migration 1: Initial schema @@ -195,16 +181,12 @@ namespace Echoion::Api CREATE INDEX IF NOT EXISTS idx_playlists_external_id ON playlists(external_id); )"; - sqlite3_exec(impl_->db, migration1, nullptr, nullptr, &errMsg); - if (errMsg) { - std::string error = errMsg; - sqlite3_free(errMsg); - return fail(ErrorCode::DatabaseError, error); + if (auto r = impl_->voidDb.exec(migration1); !r) { + return fail(ErrorCode::DatabaseError, r.error().message()); } - sqlite3_exec(impl_->db, - "INSERT INTO schema_migrations (version) VALUES (1)", - nullptr, nullptr, nullptr); + [[maybe_unused]] auto ins = impl_->voidDb.exec( + "INSERT INTO schema_migrations (version) VALUES (1)"); Void::Log::info("Database", "{}", "Database migration 1 applied"); } @@ -223,16 +205,12 @@ namespace Echoion::Api CREATE INDEX IF NOT EXISTS idx_subscriptions_uid ON subscriptions(uid); )"; - sqlite3_exec(impl_->db, migration2, nullptr, nullptr, &errMsg); - if (errMsg) { - std::string error = errMsg; - sqlite3_free(errMsg); - return fail(ErrorCode::DatabaseError, error); + if (auto r = impl_->voidDb.exec(migration2); !r) { + return fail(ErrorCode::DatabaseError, r.error().message()); } - sqlite3_exec(impl_->db, - "INSERT INTO schema_migrations (version) VALUES (2)", - nullptr, nullptr, nullptr); + [[maybe_unused]] auto ins = impl_->voidDb.exec( + "INSERT INTO schema_migrations (version) VALUES (2)"); Void::Log::info("Database", "{}", "Database migration 2 applied: subscriptions now use UID"); @@ -256,16 +234,10 @@ namespace Echoion::Api CREATE INDEX IF NOT EXISTS idx_playlists_external_id ON playlists(external_id); )"; - sqlite3_exec(impl_->db, migration3, nullptr, nullptr, &errMsg); - if (errMsg) { - // Column might already exist, ignore error - sqlite3_free(errMsg); - errMsg = nullptr; - } + [[maybe_unused]] auto r3 = impl_->voidDb.exec(migration3); - sqlite3_exec(impl_->db, - "INSERT INTO schema_migrations (version) VALUES (3)", - nullptr, nullptr, nullptr); + [[maybe_unused]] auto ins = impl_->voidDb.exec( + "INSERT INTO schema_migrations (version) VALUES (3)"); Void::Log::info("Database", "{}", "Database migration 3 applied: playlists now " "support external_id"); @@ -285,16 +257,12 @@ namespace Echoion::Api CREATE INDEX IF NOT EXISTS idx_rpc_tokens_uid ON rpc_tokens(uid); )"; - sqlite3_exec(impl_->db, migration4, nullptr, nullptr, &errMsg); - if (errMsg) { - std::string error = errMsg; - sqlite3_free(errMsg); - return fail(ErrorCode::DatabaseError, error); + if (auto r = impl_->voidDb.exec(migration4); !r) { + return fail(ErrorCode::DatabaseError, r.error().message()); } - sqlite3_exec(impl_->db, - "INSERT INTO schema_migrations (version) VALUES (4)", - nullptr, nullptr, nullptr); + [[maybe_unused]] auto ins = impl_->voidDb.exec( + "INSERT INTO schema_migrations (version) VALUES (4)"); Void::Log::info("Database", "{}", "Database migration 4 applied: RPC tokens table added"); @@ -323,30 +291,25 @@ namespace Echoion::Api VALUES (?, ?, ?, ?, ?); )"; - sqlite3_stmt* stmt; - if (sqlite3_prepare_v2(impl_->db, sql, -1, &stmt, nullptr) != - SQLITE_OK) { - return fail(ErrorCode::DatabaseError, sqlite3_errmsg(impl_->db)); + auto stmtRes = impl_->voidDb.prepare(sql); + if (!stmtRes) { + return fail(ErrorCode::DatabaseError, stmtRes.error().message()); } + auto& stmt = *stmtRes; - sqlite3_bind_text(stmt, 1, name.c_str(), -1, SQLITE_STATIC); - sqlite3_bind_text(stmt, 2, description.c_str(), -1, SQLITE_STATIC); - sqlite3_bind_int64(stmt, 3, static_cast(createdBy)); - sqlite3_bind_int(stmt, 4, isPublic ? 1 : 0); + stmt.bind(1, name) + .bind(2, description) + .bind(3, static_cast(createdBy)) + .bind(4, isPublic ? 1 : 0); if (!externalId.empty()) { - sqlite3_bind_text(stmt, 5, externalId.c_str(), -1, SQLITE_STATIC); + stmt.bind(5, externalId); } - if (sqlite3_step(stmt) != SQLITE_DONE) { - sqlite3_finalize(stmt); - return fail(ErrorCode::DatabaseError, sqlite3_errmsg(impl_->db)); + if (auto stepRes = stmt.step(); !stepRes) { + return fail(ErrorCode::DatabaseError, stepRes.error().message()); } - uint64_t id = - static_cast(sqlite3_last_insert_rowid(impl_->db)); - sqlite3_finalize(stmt); - - return id; + return static_cast(impl_->voidDb.last_insert_rowid()); } Result Database::getOrCreatePlaylistByExternalId( @@ -363,18 +326,12 @@ namespace Echoion::Api SELECT id FROM playlists WHERE external_id = ?; )"; - sqlite3_stmt* stmt; - if (sqlite3_prepare_v2(impl_->db, selectSql, -1, &stmt, nullptr) == - SQLITE_OK) { - sqlite3_bind_text(stmt, 1, externalId.c_str(), -1, SQLITE_STATIC); - - if (sqlite3_step(stmt) == SQLITE_ROW) { - uint64_t existingId = - static_cast(sqlite3_column_int64(stmt, 0)); - sqlite3_finalize(stmt); - return existingId; + if (auto stmtRes = impl_->voidDb.prepare(selectSql)) { + auto& stmt = *stmtRes; + stmt.bind(1, externalId); + if (auto s = stmt.step(); s && *s) { + return static_cast(stmt.column_int(0)); } - sqlite3_finalize(stmt); } // Not found, create new playlist with external_id @@ -392,31 +349,25 @@ namespace Echoion::Api FROM playlists WHERE id = ?; )"; - sqlite3_stmt* stmt; - if (sqlite3_prepare_v2(impl_->db, sql, -1, &stmt, nullptr) != - SQLITE_OK) { - return fail(ErrorCode::DatabaseError, sqlite3_errmsg(impl_->db)); + auto stmtRes = impl_->voidDb.prepare(sql); + if (!stmtRes) { + return fail(ErrorCode::DatabaseError, stmtRes.error().message()); } - - sqlite3_bind_int64(stmt, 1, static_cast(id)); + auto& stmt = *stmtRes; + stmt.bind(1, static_cast(id)); Playlist playlist; bool found = false; - if (sqlite3_step(stmt) == SQLITE_ROW) { + if (auto s = stmt.step(); s && *s) { found = true; - playlist.id = static_cast(sqlite3_column_int64(stmt, 0)); - playlist.name = - reinterpret_cast(sqlite3_column_text(stmt, 1)); - playlist.description = - reinterpret_cast(sqlite3_column_text(stmt, 2)); - playlist.createdBy = - static_cast(sqlite3_column_int64(stmt, 3)); - playlist.isPublic = sqlite3_column_int(stmt, 6) != 0; + playlist.id = static_cast(stmt.column_int(0)); + playlist.name = stmt.column_text(1); + playlist.description = stmt.column_text(2); + playlist.createdBy = static_cast(stmt.column_int(3)); + playlist.isPublic = stmt.column_int(6) != 0; } - sqlite3_finalize(stmt); - if (!found) { return std::optional{}; } @@ -429,26 +380,24 @@ namespace Echoion::Api ORDER BY position; )"; - if (sqlite3_prepare_v2(impl_->db, tracksSql, -1, &stmt, nullptr) == - SQLITE_OK) { - sqlite3_bind_int64(stmt, 1, static_cast(id)); + if (auto tracksRes = impl_->voidDb.prepare(tracksSql)) { + auto& tracks = *tracksRes; + tracks.bind(1, static_cast(id)); - while (sqlite3_step(stmt) == SQLITE_ROW) { + while (true) { + auto s = tracks.step(); + if (!s || !*s) { + break; + } TrackInfo track; - track.originalUrl = - reinterpret_cast(sqlite3_column_text(stmt, 0)); - track.title = - reinterpret_cast(sqlite3_column_text(stmt, 1)); - track.artist = - reinterpret_cast(sqlite3_column_text(stmt, 2)); - if (sqlite3_column_text(stmt, 3)) { - track.thumbnailUrl = reinterpret_cast( - sqlite3_column_text(stmt, 3)); + track.originalUrl = tracks.column_text(0); + track.title = tracks.column_text(1); + track.artist = tracks.column_text(2); + if (!tracks.is_null(3)) { + track.thumbnailUrl = tracks.column_text(3); } playlist.tracks.push_back(track); } - - sqlite3_finalize(stmt); } return playlist; @@ -466,26 +415,22 @@ namespace Echoion::Api ORDER BY updated_at DESC; )"; - sqlite3_stmt* stmt; - if (sqlite3_prepare_v2(impl_->db, sql, -1, &stmt, nullptr) != - SQLITE_OK) { - return fail(ErrorCode::DatabaseError, sqlite3_errmsg(impl_->db)); + auto stmtRes = impl_->voidDb.prepare(sql); + if (!stmtRes) { + return fail(ErrorCode::DatabaseError, stmtRes.error().message()); } + auto& stmt = *stmtRes; + stmt.bind(1, static_cast(userId)); - sqlite3_bind_int64(stmt, 1, static_cast(userId)); - - while (sqlite3_step(stmt) == SQLITE_ROW) { + while (stmt.step().value_or(false)) { Playlist playlist; - playlist.id = static_cast(sqlite3_column_int64(stmt, 0)); - playlist.name = - reinterpret_cast(sqlite3_column_text(stmt, 1)); - playlist.description = - reinterpret_cast(sqlite3_column_text(stmt, 2)); - playlist.isPublic = sqlite3_column_int(stmt, 4) != 0; + playlist.id = static_cast(stmt.column_int(0)); + playlist.name = stmt.column_text(1); + playlist.description = stmt.column_text(2); + playlist.isPublic = stmt.column_int(4) != 0; playlists.push_back(playlist); } - sqlite3_finalize(stmt); return playlists; } @@ -494,15 +439,13 @@ namespace Echoion::Api std::unique_lock lock(mutex_); const char* sql = "DELETE FROM playlists WHERE id = ?;"; - sqlite3_stmt* stmt; - if (sqlite3_prepare_v2(impl_->db, sql, -1, &stmt, nullptr) != - SQLITE_OK) { - return fail(ErrorCode::DatabaseError, sqlite3_errmsg(impl_->db)); + auto stmtRes = impl_->voidDb.prepare(sql); + if (!stmtRes) { + return fail(ErrorCode::DatabaseError, stmtRes.error().message()); } - - sqlite3_bind_int64(stmt, 1, static_cast(id)); - sqlite3_step(stmt); - sqlite3_finalize(stmt); + auto& stmt = *stmtRes; + stmt.bind(1, static_cast(id)); + [[maybe_unused]] auto stepRes = stmt.step(); return {}; } @@ -519,32 +462,24 @@ namespace Echoion::Api VALUES (?, ?, ?, ?, ?, ?); )"; - sqlite3_stmt* stmt; - if (sqlite3_prepare_v2(impl_->db, sql, -1, &stmt, nullptr) != - SQLITE_OK) { - return fail(ErrorCode::DatabaseError, sqlite3_errmsg(impl_->db)); + auto stmtRes = impl_->voidDb.prepare(sql); + if (!stmtRes) { + return fail(ErrorCode::DatabaseError, stmtRes.error().message()); } - - sqlite3_bind_int64(stmt, 1, static_cast(playlistId)); - sqlite3_bind_text(stmt, 2, track.originalUrl.c_str(), -1, - SQLITE_STATIC); - sqlite3_bind_text(stmt, 3, track.title.c_str(), -1, SQLITE_STATIC); - sqlite3_bind_text(stmt, 4, track.artist.c_str(), -1, SQLITE_STATIC); - sqlite3_bind_text(stmt, 5, - track.thumbnailUrl ? track.thumbnailUrl->c_str() : "", - -1, SQLITE_STATIC); - sqlite3_bind_int(stmt, 6, position); - - sqlite3_step(stmt); - sqlite3_finalize(stmt); + auto& stmt = *stmtRes; + stmt.bind(1, static_cast(playlistId)) + .bind(2, track.originalUrl) + .bind(3, track.title) + .bind(4, track.artist) + .bind(5, track.thumbnailUrl.value_or("")) + .bind(6, static_cast(position)); + [[maybe_unused]] auto stepRes = stmt.step(); // Update playlist timestamp - sqlite3_exec(impl_->db, - std::format("UPDATE playlists SET updated_at = " - "CURRENT_TIMESTAMP WHERE id = {}", - playlistId) - .c_str(), - nullptr, nullptr, nullptr); + [[maybe_unused]] auto upd = impl_->voidDb.exec( + std::format("UPDATE playlists SET updated_at = " + "CURRENT_TIMESTAMP WHERE id = {}", + playlistId)); return {}; } @@ -563,22 +498,18 @@ namespace Echoion::Api VALUES (?, ?, ?, ?, ?, ?); )"; - sqlite3_stmt* stmt; - if (sqlite3_prepare_v2(impl_->db, sql, -1, &stmt, nullptr) != - SQLITE_OK) { - return fail(ErrorCode::DatabaseError, sqlite3_errmsg(impl_->db)); + auto stmtRes = impl_->voidDb.prepare(sql); + if (!stmtRes) { + return fail(ErrorCode::DatabaseError, stmtRes.error().message()); } - - sqlite3_bind_text(stmt, 1, track.originalUrl.c_str(), -1, - SQLITE_STATIC); - sqlite3_bind_text(stmt, 2, track.title.c_str(), -1, SQLITE_STATIC); - sqlite3_bind_text(stmt, 3, track.artist.c_str(), -1, SQLITE_STATIC); - sqlite3_bind_int64(stmt, 4, static_cast(playedBy)); - sqlite3_bind_int64(stmt, 5, duration.count()); - sqlite3_bind_int(stmt, 6, completed ? 1 : 0); - - sqlite3_step(stmt); - sqlite3_finalize(stmt); + auto& stmt = *stmtRes; + stmt.bind(1, track.originalUrl) + .bind(2, track.title) + .bind(3, track.artist) + .bind(4, static_cast(playedBy)) + .bind(5, static_cast(duration.count())) + .bind(6, completed ? 1 : 0); + [[maybe_unused]] auto stepRes = stmt.step(); return {}; } @@ -599,33 +530,28 @@ namespace Echoion::Api LIMIT ? OFFSET ?; )"; - sqlite3_stmt* stmt; - if (sqlite3_prepare_v2(impl_->db, sql, -1, &stmt, nullptr) != - SQLITE_OK) { - return fail(ErrorCode::DatabaseError, sqlite3_errmsg(impl_->db)); + auto stmtRes = impl_->voidDb.prepare(sql); + if (!stmtRes) { + return fail(ErrorCode::DatabaseError, stmtRes.error().message()); } + auto& stmt = *stmtRes; + stmt.bind(1, static_cast(userId)) + .bind(2, static_cast(limit)) + .bind(3, static_cast(offset)); - sqlite3_bind_int64(stmt, 1, static_cast(userId)); - sqlite3_bind_int64(stmt, 2, static_cast(limit)); - sqlite3_bind_int64(stmt, 3, static_cast(offset)); - - while (sqlite3_step(stmt) == SQLITE_ROW) { + while (stmt.step().value_or(false)) { PlaybackHistoryEntry entry; - entry.id = static_cast(sqlite3_column_int64(stmt, 0)); - entry.track.originalUrl = - reinterpret_cast(sqlite3_column_text(stmt, 1)); - entry.track.title = - reinterpret_cast(sqlite3_column_text(stmt, 2)); - entry.track.artist = - reinterpret_cast(sqlite3_column_text(stmt, 3)); + entry.id = static_cast(stmt.column_int(0)); + entry.track.originalUrl = stmt.column_text(1); + entry.track.title = stmt.column_text(2); + entry.track.artist = stmt.column_text(3); entry.playedBy = userId; entry.duration = - std::chrono::milliseconds(sqlite3_column_int64(stmt, 5)); - entry.completed = sqlite3_column_int(stmt, 6) != 0; + std::chrono::milliseconds(stmt.column_int(5)); + entry.completed = stmt.column_int(6) != 0; history.push_back(entry); } - sqlite3_finalize(stmt); return history; } @@ -639,20 +565,18 @@ namespace Echoion::Api VALUES (?, ?, ?, ?); )"; - sqlite3_stmt* stmt; - if (sqlite3_prepare_v2(impl_->db, sql, -1, &stmt, nullptr) != - SQLITE_OK) { - return fail(ErrorCode::DatabaseError, sqlite3_errmsg(impl_->db)); + auto stmtRes = impl_->voidDb.prepare(sql); + if (!stmtRes) { + return fail(ErrorCode::DatabaseError, stmtRes.error().message()); } + auto& stmt = *stmtRes; - sqlite3_bind_int64(stmt, 1, static_cast(userId)); - sqlite3_bind_text(stmt, 2, track.originalUrl.c_str(), -1, - SQLITE_STATIC); - sqlite3_bind_text(stmt, 3, track.title.c_str(), -1, SQLITE_STATIC); - sqlite3_bind_text(stmt, 4, track.artist.c_str(), -1, SQLITE_STATIC); + stmt.bind(1, static_cast(userId)); + stmt.bind(2, track.originalUrl); + stmt.bind(3, track.title); + stmt.bind(4, track.artist); - sqlite3_step(stmt); - sqlite3_finalize(stmt); + [[maybe_unused]] auto stepRes = stmt.step(); return {}; } @@ -664,17 +588,16 @@ namespace Echoion::Api const char* sql = "DELETE FROM user_favorites WHERE user_id = ? AND track_url = ?;"; - sqlite3_stmt* stmt; - if (sqlite3_prepare_v2(impl_->db, sql, -1, &stmt, nullptr) != - SQLITE_OK) { - return fail(ErrorCode::DatabaseError, sqlite3_errmsg(impl_->db)); + auto stmtRes = impl_->voidDb.prepare(sql); + if (!stmtRes) { + return fail(ErrorCode::DatabaseError, stmtRes.error().message()); } + auto& stmt = *stmtRes; - sqlite3_bind_int64(stmt, 1, static_cast(userId)); - sqlite3_bind_text(stmt, 2, trackUrl.c_str(), -1, SQLITE_STATIC); + stmt.bind(1, static_cast(userId)); + stmt.bind(2, trackUrl); - sqlite3_step(stmt); - sqlite3_finalize(stmt); + [[maybe_unused]] auto stepRes = stmt.step(); return {}; } @@ -686,17 +609,16 @@ namespace Echoion::Api const char* sql = "SELECT 1 FROM user_favorites WHERE user_id = ? AND " "track_url = ? LIMIT 1;"; - sqlite3_stmt* stmt; - if (sqlite3_prepare_v2(impl_->db, sql, -1, &stmt, nullptr) != - SQLITE_OK) { - return fail(ErrorCode::DatabaseError, sqlite3_errmsg(impl_->db)); + auto stmtRes = impl_->voidDb.prepare(sql); + if (!stmtRes) { + return fail(ErrorCode::DatabaseError, stmtRes.error().message()); } + auto& stmt = *stmtRes; - sqlite3_bind_int64(stmt, 1, static_cast(userId)); - sqlite3_bind_text(stmt, 2, trackUrl.c_str(), -1, SQLITE_STATIC); + stmt.bind(1, static_cast(userId)); + stmt.bind(2, trackUrl); - bool found = sqlite3_step(stmt) == SQLITE_ROW; - sqlite3_finalize(stmt); + bool found = stmt.step().value_or(false); return found; } @@ -713,27 +635,26 @@ namespace Echoion::Api ORDER BY added_at DESC; )"; - sqlite3_stmt* stmt; - if (sqlite3_prepare_v2(impl_->db, sql, -1, &stmt, nullptr) != - SQLITE_OK) { - return fail(ErrorCode::DatabaseError, sqlite3_errmsg(impl_->db)); + auto stmtRes = impl_->voidDb.prepare(sql); + if (!stmtRes) { + return fail(ErrorCode::DatabaseError, stmtRes.error().message()); } + auto& stmt = *stmtRes; - sqlite3_bind_int64(stmt, 1, static_cast(userId)); + stmt.bind(1, static_cast(userId)); - while (sqlite3_step(stmt) == SQLITE_ROW) { + while (stmt.step().value_or(false)) { FavoriteTrack fav; fav.track.originalUrl = - reinterpret_cast(sqlite3_column_text(stmt, 0)); + stmt.column_text(0); fav.track.title = - reinterpret_cast(sqlite3_column_text(stmt, 1)); + stmt.column_text(1); fav.track.artist = - reinterpret_cast(sqlite3_column_text(stmt, 2)); + stmt.column_text(2); fav.userId = userId; favorites.push_back(fav); } - sqlite3_finalize(stmt); return favorites; } @@ -750,18 +671,17 @@ namespace Echoion::Api ); )"; - sqlite3_stmt* stmt; - if (sqlite3_prepare_v2(impl_->db, sql, -1, &stmt, nullptr) != - SQLITE_OK) { - return fail(ErrorCode::DatabaseError, sqlite3_errmsg(impl_->db)); + auto stmtRes = impl_->voidDb.prepare(sql); + if (!stmtRes) { + return fail(ErrorCode::DatabaseError, stmtRes.error().message()); } + auto& stmt = *stmtRes; - sqlite3_bind_int64(stmt, 1, static_cast(keepCount)); - sqlite3_step(stmt); - sqlite3_finalize(stmt); + stmt.bind(1, static_cast(keepCount)); + [[maybe_unused]] auto stepRes = stmt.step(); // Vacuum to reclaim space - sqlite3_exec(impl_->db, "VACUUM;", nullptr, nullptr, nullptr); + [[maybe_unused]] auto execRes = impl_->voidDb.exec("VACUUM;"); return {}; } @@ -773,14 +693,11 @@ namespace Echoion::Api const char* sql2 = "DELETE FROM playback_history WHERE played_by = ?;"; const char* sql3 = "DELETE FROM playlists WHERE created_by = ?;"; - sqlite3_stmt* stmt; - for (const char* sql : {sql1, sql2, sql3}) { - if (sqlite3_prepare_v2(impl_->db, sql, -1, &stmt, nullptr) == - SQLITE_OK) { - sqlite3_bind_int64(stmt, 1, static_cast(userId)); - sqlite3_step(stmt); - sqlite3_finalize(stmt); + if (auto stmtRes = impl_->voidDb.prepare(sql)) { + auto& stmt = *stmtRes; + stmt.bind(1, static_cast(userId)); + [[maybe_unused]] auto stepRes = stmt.step(); } } @@ -790,7 +707,7 @@ namespace Echoion::Api Result Database::vacuum() { std::unique_lock lock(mutex_); - sqlite3_exec(impl_->db, "VACUUM;", nullptr, nullptr, nullptr); + [[maybe_unused]] auto execRes = impl_->voidDb.exec("VACUUM;"); return {}; } @@ -804,16 +721,15 @@ namespace Echoion::Api VALUES (?, ?, CURRENT_TIMESTAMP); )"; - sqlite3_stmt* stmt; - if (sqlite3_prepare_v2(impl_->db, sql, -1, &stmt, nullptr) != - SQLITE_OK) { - return fail(ErrorCode::DatabaseError, sqlite3_errmsg(impl_->db)); + auto stmtRes = impl_->voidDb.prepare(sql); + if (!stmtRes) { + return fail(ErrorCode::DatabaseError, stmtRes.error().message()); } + auto& stmt = *stmtRes; - sqlite3_bind_text(stmt, 1, uid.c_str(), -1, SQLITE_STATIC); - sqlite3_bind_text(stmt, 2, nickname.c_str(), -1, SQLITE_STATIC); - sqlite3_step(stmt); - sqlite3_finalize(stmt); + stmt.bind(1, uid); + stmt.bind(2, nickname); + [[maybe_unused]] auto stepRes = stmt.step(); return {}; } @@ -823,15 +739,14 @@ namespace Echoion::Api std::unique_lock lock(mutex_); const char* sql = "DELETE FROM subscriptions WHERE uid = ?;"; - sqlite3_stmt* stmt; - if (sqlite3_prepare_v2(impl_->db, sql, -1, &stmt, nullptr) != - SQLITE_OK) { - return fail(ErrorCode::DatabaseError, sqlite3_errmsg(impl_->db)); + auto stmtRes = impl_->voidDb.prepare(sql); + if (!stmtRes) { + return fail(ErrorCode::DatabaseError, stmtRes.error().message()); } + auto& stmt = *stmtRes; - sqlite3_bind_text(stmt, 1, uid.c_str(), -1, SQLITE_STATIC); - sqlite3_step(stmt); - sqlite3_finalize(stmt); + stmt.bind(1, uid); + [[maybe_unused]] auto stepRes = stmt.step(); return {}; } @@ -841,15 +756,14 @@ namespace Echoion::Api std::shared_lock lock(mutex_); const char* sql = "SELECT 1 FROM subscriptions WHERE uid = ? LIMIT 1;"; - sqlite3_stmt* stmt; - if (sqlite3_prepare_v2(impl_->db, sql, -1, &stmt, nullptr) != - SQLITE_OK) { - return fail(ErrorCode::DatabaseError, sqlite3_errmsg(impl_->db)); + auto stmtRes = impl_->voidDb.prepare(sql); + if (!stmtRes) { + return fail(ErrorCode::DatabaseError, stmtRes.error().message()); } + auto& stmt = *stmtRes; - sqlite3_bind_text(stmt, 1, uid.c_str(), -1, SQLITE_STATIC); - bool found = sqlite3_step(stmt) == SQLITE_ROW; - sqlite3_finalize(stmt); + stmt.bind(1, uid); + bool found = stmt.step().value_or(false); return found; } @@ -862,32 +776,30 @@ namespace Echoion::Api const char* sql = "SELECT uid, nickname, subscribed_at FROM subscriptions;"; - sqlite3_stmt* stmt; - if (sqlite3_prepare_v2(impl_->db, sql, -1, &stmt, nullptr) != - SQLITE_OK) { - return fail(ErrorCode::DatabaseError, sqlite3_errmsg(impl_->db)); + auto stmtRes = impl_->voidDb.prepare(sql); + if (!stmtRes) { + return fail(ErrorCode::DatabaseError, stmtRes.error().message()); } + auto& stmt = *stmtRes; - while (sqlite3_step(stmt) == SQLITE_ROW) { + while (stmt.step().value_or(false)) { SubscriberInfo info; info.uid = - reinterpret_cast(sqlite3_column_text(stmt, 0)); + stmt.column_text(0); info.nickname = - reinterpret_cast(sqlite3_column_text(stmt, 1)); + stmt.column_text(1); info.subscribedAt = std::chrono::system_clock::from_time_t( - sqlite3_column_int64(stmt, 2)); + stmt.column_int(2)); subscribers.push_back(std::move(info)); } - sqlite3_finalize(stmt); return subscribers; } Result Database::clearAllSubscribers() { std::unique_lock lock(mutex_); - sqlite3_exec(impl_->db, "DELETE FROM subscriptions;", nullptr, nullptr, - nullptr); + [[maybe_unused]] auto execRes = impl_->voidDb.exec("DELETE FROM subscriptions;"); return {}; } @@ -896,18 +808,17 @@ namespace Echoion::Api std::shared_lock lock(mutex_); const char* sql = "SELECT COUNT(*) FROM subscriptions;"; - sqlite3_stmt* stmt; - if (sqlite3_prepare_v2(impl_->db, sql, -1, &stmt, nullptr) != - SQLITE_OK) { - return fail(ErrorCode::DatabaseError, sqlite3_errmsg(impl_->db)); + auto stmtRes = impl_->voidDb.prepare(sql); + if (!stmtRes) { + return fail(ErrorCode::DatabaseError, stmtRes.error().message()); } + auto& stmt = *stmtRes; size_t count = 0; - if (sqlite3_step(stmt) == SQLITE_ROW) { - count = static_cast(sqlite3_column_int64(stmt, 0)); + if (stmt.step().value_or(false)) { + count = static_cast(stmt.column_int(0)); } - sqlite3_finalize(stmt); return count; } @@ -997,15 +908,14 @@ namespace Echoion::Api std::unique_lock lock(mutex_); // Check if subscriber exists - sqlite3_stmt* checkStmt; - if (sqlite3_prepare_v2(impl_->db, - "SELECT 1 FROM subscriptions WHERE uid = ?", -1, - &checkStmt, nullptr) != SQLITE_OK) { - return fail(ErrorCode::DatabaseError, sqlite3_errmsg(impl_->db)); + auto checkRes = + impl_->voidDb.prepare("SELECT 1 FROM subscriptions WHERE uid = ?"); + if (!checkRes) { + return fail(ErrorCode::DatabaseError, checkRes.error().message()); } - sqlite3_bind_text(checkStmt, 1, uid.c_str(), -1, SQLITE_STATIC); - bool exists = sqlite3_step(checkStmt) == SQLITE_ROW; - sqlite3_finalize(checkStmt); + auto& checkStmt = *checkRes; + checkStmt.bind(1, uid); + bool exists = checkStmt.step().value_or(false); if (!exists) { return fail(ErrorCode::NotFound, "Subscriber not found"); @@ -1021,21 +931,19 @@ namespace Echoion::Api VALUES (?, ?, datetime('now')); )"; - sqlite3_stmt* stmt; - if (sqlite3_prepare_v2(impl_->db, sql, -1, &stmt, nullptr) != - SQLITE_OK) { - return fail(ErrorCode::DatabaseError, sqlite3_errmsg(impl_->db)); + auto stmtRes = impl_->voidDb.prepare(sql); + if (!stmtRes) { + return fail(ErrorCode::DatabaseError, stmtRes.error().message()); + } + auto& stmt = *stmtRes; + + stmt.bind(1, token); + stmt.bind(2, uid); + + if (auto stepRes = stmt.step(); !stepRes) { + return fail(ErrorCode::DatabaseError, stepRes.error().message()); } - sqlite3_bind_text(stmt, 1, token.c_str(), -1, SQLITE_STATIC); - sqlite3_bind_text(stmt, 2, uid.c_str(), -1, SQLITE_STATIC); - - if (sqlite3_step(stmt) != SQLITE_DONE) { - sqlite3_finalize(stmt); - return fail(ErrorCode::DatabaseError, sqlite3_errmsg(impl_->db)); - } - - sqlite3_finalize(stmt); return token; } @@ -1048,24 +956,21 @@ namespace Echoion::Api SELECT token FROM rpc_tokens WHERE uid = ? LIMIT 1; )"; - sqlite3_stmt* stmt; - if (sqlite3_prepare_v2(impl_->db, sql, -1, &stmt, nullptr) != - SQLITE_OK) { - return fail(ErrorCode::DatabaseError, sqlite3_errmsg(impl_->db)); + auto stmtRes = impl_->voidDb.prepare(sql); + if (!stmtRes) { + return fail(ErrorCode::DatabaseError, stmtRes.error().message()); } + auto& stmt = *stmtRes; - sqlite3_bind_text(stmt, 1, uid.c_str(), -1, SQLITE_STATIC); + stmt.bind(1, uid); std::optional token; - if (sqlite3_step(stmt) == SQLITE_ROW) { - const char* tokenStr = - reinterpret_cast(sqlite3_column_text(stmt, 0)); - if (tokenStr) { - token = tokenStr; + if (stmt.step().value_or(false)) { + if (!stmt.is_null(0)) { + token = stmt.column_text(0); } } - sqlite3_finalize(stmt); return token; } @@ -1077,29 +982,25 @@ namespace Echoion::Api SELECT 1 FROM rpc_tokens WHERE token = ?; )"; - sqlite3_stmt* stmt; - if (sqlite3_prepare_v2(impl_->db, sql, -1, &stmt, nullptr) != - SQLITE_OK) { - return fail(ErrorCode::DatabaseError, sqlite3_errmsg(impl_->db)); + auto stmtRes = impl_->voidDb.prepare(sql); + if (!stmtRes) { + return fail(ErrorCode::DatabaseError, stmtRes.error().message()); } + auto& stmt = *stmtRes; - sqlite3_bind_text(stmt, 1, token.c_str(), -1, SQLITE_STATIC); + stmt.bind(1, token); - bool valid = sqlite3_step(stmt) == SQLITE_ROW; - sqlite3_finalize(stmt); + bool valid = stmt.step().value_or(false); if (valid) { // Update last_used_at const char* updateSql = R"( UPDATE rpc_tokens SET last_used_at = datetime('now') WHERE token = ?; )"; - sqlite3_stmt* updateStmt; - if (sqlite3_prepare_v2(impl_->db, updateSql, -1, &updateStmt, - nullptr) == SQLITE_OK) { - sqlite3_bind_text(updateStmt, 1, token.c_str(), -1, - SQLITE_STATIC); - sqlite3_step(updateStmt); - sqlite3_finalize(updateStmt); + if (auto updateRes = impl_->voidDb.prepare(updateSql)) { + auto& updateStmt = *updateRes; + updateStmt.bind(1, token); + [[maybe_unused]] auto stepRes = updateStmt.step(); } } @@ -1115,24 +1016,21 @@ namespace Echoion::Api SELECT uid FROM rpc_tokens WHERE token = ?; )"; - sqlite3_stmt* stmt; - if (sqlite3_prepare_v2(impl_->db, sql, -1, &stmt, nullptr) != - SQLITE_OK) { - return fail(ErrorCode::DatabaseError, sqlite3_errmsg(impl_->db)); + auto stmtRes = impl_->voidDb.prepare(sql); + if (!stmtRes) { + return fail(ErrorCode::DatabaseError, stmtRes.error().message()); } + auto& stmt = *stmtRes; - sqlite3_bind_text(stmt, 1, token.c_str(), -1, SQLITE_STATIC); + stmt.bind(1, token); std::optional uid; - if (sqlite3_step(stmt) == SQLITE_ROW) { - const char* uidStr = - reinterpret_cast(sqlite3_column_text(stmt, 0)); - if (uidStr) { - uid = uidStr; + if (stmt.step().value_or(false)) { + if (!stmt.is_null(0)) { + uid = stmt.column_text(0); } } - sqlite3_finalize(stmt); return uid; } @@ -1145,24 +1043,21 @@ namespace Echoion::Api SELECT uid FROM subscriptions WHERE nickname = ? COLLATE NOCASE; )"; - sqlite3_stmt* stmt; - if (sqlite3_prepare_v2(impl_->db, sql, -1, &stmt, nullptr) != - SQLITE_OK) { - return fail(ErrorCode::DatabaseError, sqlite3_errmsg(impl_->db)); + auto stmtRes = impl_->voidDb.prepare(sql); + if (!stmtRes) { + return fail(ErrorCode::DatabaseError, stmtRes.error().message()); } + auto& stmt = *stmtRes; - sqlite3_bind_text(stmt, 1, nickname.c_str(), -1, SQLITE_STATIC); + stmt.bind(1, nickname); std::optional uid; - if (sqlite3_step(stmt) == SQLITE_ROW) { - const char* uidStr = - reinterpret_cast(sqlite3_column_text(stmt, 0)); - if (uidStr) { - uid = uidStr; + if (stmt.step().value_or(false)) { + if (!stmt.is_null(0)) { + uid = stmt.column_text(0); } } - sqlite3_finalize(stmt); return uid; } @@ -1174,20 +1069,18 @@ namespace Echoion::Api DELETE FROM rpc_tokens WHERE token = ?; )"; - sqlite3_stmt* stmt; - if (sqlite3_prepare_v2(impl_->db, sql, -1, &stmt, nullptr) != - SQLITE_OK) { - return fail(ErrorCode::DatabaseError, sqlite3_errmsg(impl_->db)); + auto stmtRes = impl_->voidDb.prepare(sql); + if (!stmtRes) { + return fail(ErrorCode::DatabaseError, stmtRes.error().message()); + } + auto& stmt = *stmtRes; + + stmt.bind(1, token); + + if (auto stepRes = stmt.step(); !stepRes) { + return fail(ErrorCode::DatabaseError, stepRes.error().message()); } - sqlite3_bind_text(stmt, 1, token.c_str(), -1, SQLITE_STATIC); - - if (sqlite3_step(stmt) != SQLITE_DONE) { - sqlite3_finalize(stmt); - return fail(ErrorCode::DatabaseError, sqlite3_errmsg(impl_->db)); - } - - sqlite3_finalize(stmt); return {}; } @@ -1199,22 +1092,19 @@ namespace Echoion::Api SELECT uid FROM subscriptions; )"; - sqlite3_stmt* stmt; - if (sqlite3_prepare_v2(impl_->db, sql, -1, &stmt, nullptr) != - SQLITE_OK) { - return fail(ErrorCode::DatabaseError, sqlite3_errmsg(impl_->db)); + auto stmtRes = impl_->voidDb.prepare(sql); + if (!stmtRes) { + return fail(ErrorCode::DatabaseError, stmtRes.error().message()); } + auto& stmt = *stmtRes; std::vector uids; - while (sqlite3_step(stmt) == SQLITE_ROW) { - const char* uidStr = - reinterpret_cast(sqlite3_column_text(stmt, 0)); - if (uidStr) { - uids.push_back(uidStr); + while (stmt.step().value_or(false)) { + if (!stmt.is_null(0)) { + uids.push_back(stmt.column_text(0)); } } - sqlite3_finalize(stmt); return uids; }