summaryrefslogtreecommitdiff
path: root/core/src/Model/TransactionDatabase.cpp
diff options
context:
space:
mode:
authorrtk0c <[email protected]>2021-04-01 22:18:46 -0700
committerrtk0c <[email protected]>2021-04-01 22:18:46 -0700
commit2f4b9db39239ed5150094a81743beea42a3eedc2 (patch)
tree7d5795f7c23eb4f92ccffe2476da019db9977f2b /core/src/Model/TransactionDatabase.cpp
parent44f5fa5c8f258e8fc1f7d7e2e45e0485bd6cc490 (diff)
Initial work on SQLite database
Diffstat (limited to 'core/src/Model/TransactionDatabase.cpp')
-rw-r--r--core/src/Model/TransactionDatabase.cpp41
1 files changed, 41 insertions, 0 deletions
diff --git a/core/src/Model/TransactionDatabase.cpp b/core/src/Model/TransactionDatabase.cpp
new file mode 100644
index 0000000..c28db0d
--- /dev/null
+++ b/core/src/Model/TransactionDatabase.cpp
@@ -0,0 +1,41 @@
+#include "TransactionDatabase.hpp"
+
+#include "Model/Project.hpp"
+
+#include <filesystem>
+#include <stdexcept>
+#include <string>
+
+namespace fs = std::filesystem;
+
+static bool TableExists(sqlite3* db, const char* table, const char* column = nullptr) {
+ return sqlite3_table_column_metadata(db, nullptr, table, column, nullptr, nullptr, nullptr, nullptr, nullptr) == SQLITE_OK;
+}
+
+TransactionDatabase::TransactionDatabase(Project& project)
+ : mProject{ &project }
+ , mDatabase{ nullptr } {
+
+ fs::path dbDir = project.GetPath() / "databases";
+ fs::create_directories(dbDir);
+
+ fs::path dbPath = dbDir / "transactions.sqlite3";
+#if PLATFORM_WIN32
+ if (int rc = sqlite3_open16(dbPath.c_str(), &mDatabase); rc) {
+#else
+ if (int rc = sqlite3_open(transactionDbPath.c_str(), &mDatabase); rc) {
+#endif
+ sqlite3_close(mDatabase);
+
+ std::string message;
+ message += "Failed to open SQLite database for transactions. Error code: ";
+ message += rc;
+ message += ".";
+ throw std::runtime_error(message);
+ }
+}
+
+TransactionDatabase::~TransactionDatabase() {
+ sqlite3_close(mDatabase);
+ mDatabase = nullptr;
+}