diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-30 01:29:58 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-30 01:29:58 +0100 |
commit | b9e738502260b8f448289c9888203971c7749c76 (patch) | |
tree | 09477251ba49307173a112e0cd5dbdd3633346ce /frontend/DB.cpp | |
parent | e4261302944303781c952943e3290c99e2cabc52 (diff) |
WIP SQL gedoe
Diffstat (limited to 'frontend/DB.cpp')
-rw-r--r-- | frontend/DB.cpp | 50 |
1 files changed, 42 insertions, 8 deletions
diff --git a/frontend/DB.cpp b/frontend/DB.cpp index 82933b8..d51601e 100644 --- a/frontend/DB.cpp +++ b/frontend/DB.cpp @@ -1,9 +1,11 @@ #include "DB.h" #include "Exception.h" -DB::DB(const std::string & path) { +using namespace std; + +DB::DB(const string & path) { sqlite3 * db = NULL; - int ret = sqlite3_open_v2(path.c_str(), &db, SQLITE_OPEN_READONLY, NULL); + int ret = sqlite3_open_v2(path.c_str(), &db, SQLITE_OPEN_READWRITE, NULL); this->db = { db, [] (sqlite3 * db) { @@ -11,20 +13,52 @@ DB::DB(const std::string & path) { }, }; if (ret != SQLITE_OK) - throw Exception("sqlite3_open_v2"); + throw Exception("sqlite3_open_v2: %d", ret); +} + +DBStatement DB::prepare(const string & query) { + return DBStatement(*this, query); } -DB::unique_sqlite3_stmt DB::prepare(const std::string & query) { +DBStatement::DBStatement(DB & db, const string & query) : db(db) { sqlite3_stmt * stmt = NULL; - int ret = sqlite3_prepare_v2(this->db.get(), query.c_str(), query.size(), &stmt, NULL); - unique_sqlite3_stmt uniq_stmt = { + int ret = sqlite3_prepare_v2(this->db.db.get(), query.c_str(), query.size(), &stmt, NULL); + this->stmt = { stmt, [] (sqlite3_stmt * stmt) { sqlite3_finalize(stmt); }, }; if (ret != SQLITE_OK) - throw Exception("sqlite3_prepare_v2"); - return uniq_stmt; + throw Exception("sqlite3_prepare_v2: %d", ret); +} + +DBStatement & DBStatement::bind(const string & text) { + int ret = sqlite3_bind_text(this->stmt.get(), this->param_index, text.data(), text.size(), NULL); + if (ret != SQLITE_OK) + throw Exception("sqlite3_bind_text: %d", ret); + + this->param_index++; + return *this; +} + +DBStatement & DBStatement::bind(const int & number) { + int ret = sqlite3_bind_int(this->stmt.get(), this->param_index, number); + if (ret != SQLITE_OK) + throw Exception("sqlite3_bind_int: %d", ret); + + this->param_index++; + return *this; +} + +DBStatement & DBStatement::unbind() { + this->param_index = 1; + return *this; +} + +void DBStatement::execute() { + int ret = sqlite3_step(this->stmt.get()); + if (ret != SQLITE_DONE) + throw Exception("sqlite3_step: %d", ret); } |