aboutsummaryrefslogtreecommitdiff
path: root/core/src/Model/TransactionDatabase.cpp
blob: c28db0d5ca934684388457353430e4ff94fc3e72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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;
}