From 5c34847218e8d754447f5cf71ed595bbb412eee7 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 29 Oct 2024 23:30:57 +0100 Subject: more WIP --- frontend/DB.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 frontend/DB.cpp (limited to 'frontend/DB.cpp') diff --git a/frontend/DB.cpp b/frontend/DB.cpp new file mode 100644 index 0000000..82933b8 --- /dev/null +++ b/frontend/DB.cpp @@ -0,0 +1,30 @@ +#include "DB.h" +#include "Exception.h" + +DB::DB(const std::string & path) { + sqlite3 * db = NULL; + int ret = sqlite3_open_v2(path.c_str(), &db, SQLITE_OPEN_READONLY, NULL); + this->db = { + db, + [] (sqlite3 * db) { + sqlite3_close_v2(db); + }, + }; + if (ret != SQLITE_OK) + throw Exception("sqlite3_open_v2"); +} + +DB::unique_sqlite3_stmt DB::prepare(const std::string & query) { + sqlite3_stmt * stmt = NULL; + int ret = sqlite3_prepare_v2(this->db.get(), query.c_str(), query.size(), &stmt, NULL); + unique_sqlite3_stmt uniq_stmt = { + stmt, + [] (sqlite3_stmt * stmt) { + sqlite3_finalize(stmt); + }, + }; + if (ret != SQLITE_OK) + throw Exception("sqlite3_prepare_v2"); + return uniq_stmt; +} + -- cgit v1.2.3