diff options
author | rtk0c <[email protected]> | 2021-04-08 08:49:10 -0700 |
---|---|---|
committer | rtk0c <[email protected]> | 2021-04-08 08:49:10 -0700 |
commit | ce8660cc5bfc12e6e3f75d4cce22492783ca9066 (patch) | |
tree | f5c1088def48e60570a1225d6440f304b37b5b5c /core/src/Model/TransactionDatabase.cpp | |
parent | 2f4b9db39239ed5150094a81743beea42a3eedc2 (diff) |
Initial work on table visualizer
Diffstat (limited to 'core/src/Model/TransactionDatabase.cpp')
-rw-r--r-- | core/src/Model/TransactionDatabase.cpp | 161 |
1 files changed, 140 insertions, 21 deletions
diff --git a/core/src/Model/TransactionDatabase.cpp b/core/src/Model/TransactionDatabase.cpp index c28db0d..766727d 100644 --- a/core/src/Model/TransactionDatabase.cpp +++ b/core/src/Model/TransactionDatabase.cpp @@ -4,38 +4,157 @@ #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; +SalesTable::SalesTable(TransactionDatabase& db) + // language=SQLite + : GetRowsStatement(db.GetSQLite(), R"""( +SELECT * FROM Sales WHERE rowid >= ? AND rowid < ? +)""") + // language=SQLite + // TODO + , FilterRowsStatement(db.GetSQLite(), R"""( +)""") { +} + +int SalesTable::GetEntryCont() const { + // TODO + return 0; +} + +DeliveryTable::DeliveryTable(TransactionDatabase& db) { +} + +PurchasesTable::PurchasesTable(TransactionDatabase& db) { +} + +static std::string GetDatabaseFilePath(const Project& project) { + auto dbsDir = project.GetPath() / "databases"; + fs::create_directories(dbsDir); + + auto dbFile = dbsDir / "transactions.sqlite3"; + return dbFile.string(); } TransactionDatabase::TransactionDatabase(Project& project) : mProject{ &project } - , mDatabase{ nullptr } { + , mDb(GetDatabaseFilePath(project), SQLite::OPEN_READWRITE) + , mSales(*this) + , mPurchases(*this) + , mDeliveries(*this) { + // Schema + // - Customer: the customer item ID + // - Deadline: unix epoch time of order deadline + // - DeliveryTime: the time this order was completed (through a set of deliveries) + if (!mDb.tableExists("Sales")) { + // language=SQLite + mDb.exec(R"""( +CREATE TABLE Sales( + INT PRIMARY KEY, + Customer INT, + Deadline DATETIME, + DeliveryTime DATETIME +); +)"""); + } + + if (!mDb.tableExists("SalesItems")) { + // language=SQLite + mDb.exec(R"""( +CREATE TABLE SalesItems( + SaleId INT, + ItemId INT, + Count INT +); +)"""); + } - fs::path dbDir = project.GetPath() / "databases"; - fs::create_directories(dbDir); + // Schema + // - Factory: the factory id, + // - OrderTime: the time this order was made + // - DeliveryTime: the time this order was completed (through a set of deliveries) + if (!mDb.tableExists("Purchases")) { + // language=SQLite + mDb.exec(R"""( +CREATE TABLE Purchases( + INT PRIMARY KEY, + Factory INT, + OrderTime DATETIME, + DeliveryTime DATETIME +); +)"""); + } + + if (!mDb.tableExists("PurchasesItems")) { + // language=SQLite + mDb.exec(R"""( +CREATE TABLE PurchasesItems( + PurchaseId INT, + ItemId INT, + Count INT +); +)"""); + } - 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); + // Schema + // - SendTime: unix epoch time of sending to delivery + // - ArriveTime: unix epoch time of delivery arrived at warehouse; 0 if not arrived yet + // - AssociatedOrder: rowid of the order that this delivery is completing (which table: Outgoing=true -> Sales, Outgoing=false -> Purchases) + // - Outgoing: true if the delivery is from warehouse to customer; false if the delivery is from factory to warehouse + if (!mDb.tableExists("Deliveries")) { + // language=SQLite + mDb.exec(R"""( +CREATE TABLE Deliveries( + INT PRIMARY KEY, + SendTime DATETIME, + ArriveTime DATETIME, + AssociatedOrder INT, + Outgoing BOOLEAN +); +)"""); + } - std::string message; - message += "Failed to open SQLite database for transactions. Error code: "; - message += rc; - message += "."; - throw std::runtime_error(message); + if (!mDb.tableExists("DeliveriesItems")) { + // language=SQLite + mDb.exec(R"""( +CREATE TABLE DeliveriesItems( + DeliveryId INT, + ItemId INT, + Count INT +); +)"""); } } -TransactionDatabase::~TransactionDatabase() { - sqlite3_close(mDatabase); - mDatabase = nullptr; +const SQLite::Database& TransactionDatabase::GetSQLite() const { + return mDb; +} + +SQLite::Database& TransactionDatabase::GetSQLite() { + return mDb; +} + +const SalesTable& TransactionDatabase::GetSales() const { + return mSales; +} + +SalesTable& TransactionDatabase::GetSales() { + return mSales; +} + +const PurchasesTable& TransactionDatabase::GetPurchases() const { + return mPurchases; +} + +PurchasesTable& TransactionDatabase::GetPurchases() { + return mPurchases; +} + +const DeliveryTable& TransactionDatabase::GetDeliveries() const { + return mDeliveries; +} + +DeliveryTable& TransactionDatabase::GetDeliveries() { + return mDeliveries; } |