From 4303d0be47526b35e5bb3e3be001da227dae5d96 Mon Sep 17 00:00:00 2001 From: rtk0c Date: Fri, 9 Apr 2021 23:17:45 -0700 Subject: Fix crash on load entries - TODO format time properly - TODO add Purchases table visualization --- core/src/Model/Project.cpp | 4 +- core/src/Model/Project.hpp | 13 +-- core/src/Model/TransactionDatabase.cpp | 160 --------------------------------- core/src/Model/TransactionDatabase.hpp | 50 ----------- core/src/Model/TransactionsModel.cpp | 139 ++++++++++++++++++++++++++++ core/src/Model/TransactionsModel.hpp | 55 ++++++++++++ core/src/Model/fwd.hpp | 2 +- 7 files changed, 201 insertions(+), 222 deletions(-) delete mode 100644 core/src/Model/TransactionDatabase.cpp delete mode 100644 core/src/Model/TransactionDatabase.hpp create mode 100644 core/src/Model/TransactionsModel.cpp create mode 100644 core/src/Model/TransactionsModel.hpp (limited to 'core/src/Model') diff --git a/core/src/Model/Project.cpp b/core/src/Model/Project.cpp index 2a79e3f..e762efb 100644 --- a/core/src/Model/Project.cpp +++ b/core/src/Model/Project.cpp @@ -82,11 +82,11 @@ void Project::SetName(std::string name) { mName = std::move(name); } -const TransactionDatabase& Project::GetDatabase() const { +const TransactionModel& Project::GetTransactionsModel() const { return mDb; } -TransactionDatabase& Project::GetDatabase() { +TransactionModel& Project::GetTransactionsModel() { return mDb; } diff --git a/core/src/Model/Project.hpp b/core/src/Model/Project.hpp index dca10d0..748ca82 100644 --- a/core/src/Model/Project.hpp +++ b/core/src/Model/Project.hpp @@ -1,7 +1,7 @@ #pragma once #include "Model/Items.hpp" -#include "Model/TransactionDatabase.hpp" +#include "Model/TransactionsModel.hpp" #include #include @@ -17,7 +17,7 @@ private: std::filesystem::path mRootPath; std::string mRootPathString; std::string mName; - TransactionDatabase mDb; + TransactionModel mDb; public: /// Load the project from a directory containing the cplt_project.json file. @@ -27,11 +27,6 @@ public: /// This function assumes the given directory will exist and is empty. Project(std::filesystem::path rootPath, std::string name); - Project(const Project&) = delete; - Project& operator=(const Project&) = delete; - Project(Project&&) = default; - Project& operator=(Project&&) = default; - /// Path to a *directory* that contains the project file. const std::filesystem::path& GetPath() const; const std::string& GetPathString() const; @@ -39,8 +34,8 @@ public: const std::string& GetName() const; void SetName(std::string name); - const TransactionDatabase& GetDatabase() const; - TransactionDatabase& GetDatabase(); + const TransactionModel& GetTransactionsModel() const; + TransactionModel& GetTransactionsModel(); Json::Value Serialize(); void WriteToDisk(); diff --git a/core/src/Model/TransactionDatabase.cpp b/core/src/Model/TransactionDatabase.cpp deleted file mode 100644 index 766727d..0000000 --- a/core/src/Model/TransactionDatabase.cpp +++ /dev/null @@ -1,160 +0,0 @@ -#include "TransactionDatabase.hpp" - -#include "Model/Project.hpp" - -#include -#include - -namespace fs = std::filesystem; - -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 } - , 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 -); -)"""); - } - - // 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 -); -)"""); - } - - // 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 -); -)"""); - } - - if (!mDb.tableExists("DeliveriesItems")) { - // language=SQLite - mDb.exec(R"""( -CREATE TABLE DeliveriesItems( - DeliveryId INT, - ItemId INT, - Count INT -); -)"""); - } -} - -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; -} diff --git a/core/src/Model/TransactionDatabase.hpp b/core/src/Model/TransactionDatabase.hpp deleted file mode 100644 index 9c869c4..0000000 --- a/core/src/Model/TransactionDatabase.hpp +++ /dev/null @@ -1,50 +0,0 @@ -#pragma once - -#include "cplt_fwd.hpp" - -#include -#include -#include - -class SalesTable { -public: - SQLite::Statement GetRowsStatement; - SQLite::Statement FilterRowsStatement; - -public: - SalesTable(TransactionDatabase& db); - - int GetEntryCont() const; -}; - -class PurchasesTable { -public: - PurchasesTable(TransactionDatabase& db); -}; - -class DeliveryTable { -public: - DeliveryTable(TransactionDatabase& db); -}; - -class TransactionDatabase { -private: - Project* mProject; - SQLite::Database mDb; - SalesTable mSales; - PurchasesTable mPurchases; - DeliveryTable mDeliveries; - -public: - TransactionDatabase(Project& project); - - const SQLite::Database& GetSQLite() const; - SQLite::Database& GetSQLite(); - - const SalesTable& GetSales() const; - SalesTable& GetSales(); - const PurchasesTable& GetPurchases() const; - PurchasesTable& GetPurchases(); - const DeliveryTable& GetDeliveries() const; - DeliveryTable& GetDeliveries() -}; diff --git a/core/src/Model/TransactionsModel.cpp b/core/src/Model/TransactionsModel.cpp new file mode 100644 index 0000000..6035528 --- /dev/null +++ b/core/src/Model/TransactionsModel.cpp @@ -0,0 +1,139 @@ +#include "TransactionsModel.hpp" + +#include "Model/Project.hpp" + +#include +#include + +namespace fs = std::filesystem; + +SalesTable::SalesTable(TransactionModel& db) + // language=SQLite + : GetRowsStatement(db.GetSQLite(), R"""( +SELECT * FROM Sales WHERE rowid >= ? AND rowid < ? +)""") + // language=SQLite + // TODO + , FilterRowsStatement(db.GetSQLite(), R"""( +)""") { +} + +DeliveryTable::DeliveryTable(TransactionModel& db) { +} + +PurchasesTable::PurchasesTable(TransactionModel& 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(); +} + +/// Wrapper for SQLite::Database that creates the default tables +TransactionModel::DatabaseWrapper::DatabaseWrapper(TransactionModel& self) + : mSqlite(GetDatabaseFilePath(*self.mProject), SQLite::OPEN_READWRITE) { + + // If this table doesn't exist, the database probably just got initialized + if (mSqlite.tableExists("Sales")) { + return; + } + + // 'Sales' 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) + + // 'Purchases' schema + // - Factory: the factory id, + // - OrderTime: the time this order was made + // - DeliveryTime: the time this order was completed (through a set of deliveries) + + // 'Deliveries' 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 + + // language=SQLite + mSqlite.exec(R"""( +CREATE TABLE IF NOT EXISTS Sales( + INT PRIMARY KEY, + Customer INT, + Deadline DATETIME, + DeliveryTime DATETIME +); +CREATE TABLE IF NOT EXISTS SalesItems( + SaleId INT, + ItemId INT, + Count INT +); + +CREATE TABLE IF NOT EXISTS Purchases( + INT PRIMARY KEY, + Factory INT, + OrderTime DATETIME, + DeliveryTime DATETIME +); +CREATE TABLE IF NOT EXISTS PurchasesItems( + PurchaseId INT, + ItemId INT, + Count INT +); + +CREATE TABLE IF NOT EXISTS Deliveries( + INT PRIMARY KEY, + SendTime DATETIME, + ArriveTime DATETIME, + AssociatedOrder INT, + Outgoing BOOLEAN +); +CREATE TABLE IF NOT EXISTS DeliveriesItems( + DeliveryId INT, + ItemId INT, + Count INT +); +)"""); +} + +TransactionModel::TransactionModel(Project& project) + : mProject{ &project } + , mDbWrapper(*this) + , mSales(*this) + , mPurchases(*this) + , mDeliveries(*this) { +} + +const SQLite::Database& TransactionModel::GetSQLite() const { + return mDbWrapper.mSqlite; +} + +SQLite::Database& TransactionModel::GetSQLite() { + return mDbWrapper.mSqlite; +} + +const SalesTable& TransactionModel::GetSales() const { + return mSales; +} + +SalesTable& TransactionModel::GetSales() { + return mSales; +} + +const PurchasesTable& TransactionModel::GetPurchases() const { + return mPurchases; +} + +PurchasesTable& TransactionModel::GetPurchases() { + return mPurchases; +} + +const DeliveryTable& TransactionModel::GetDeliveries() const { + return mDeliveries; +} + +DeliveryTable& TransactionModel::GetDeliveries() { + return mDeliveries; +} diff --git a/core/src/Model/TransactionsModel.hpp b/core/src/Model/TransactionsModel.hpp new file mode 100644 index 0000000..beed663 --- /dev/null +++ b/core/src/Model/TransactionsModel.hpp @@ -0,0 +1,55 @@ +#pragma once + +#include "cplt_fwd.hpp" + +#include +#include +#include + +class SalesTable { +public: + SQLite::Statement GetRowsStatement; + SQLite::Statement FilterRowsStatement; + +public: + SalesTable(TransactionModel& db); +}; + +class PurchasesTable { +public: + PurchasesTable(TransactionModel& db); +}; + +class DeliveryTable { +public: + DeliveryTable(TransactionModel& db); +}; + +// TODO fuck SQLite::Statement has move ctor but not move assignment operator +class TransactionModel { +private: + class DatabaseWrapper { + public: + SQLite::Database mSqlite; + DatabaseWrapper(TransactionModel& self); + }; + + Project* mProject; + DatabaseWrapper mDbWrapper; + SalesTable mSales; + PurchasesTable mPurchases; + DeliveryTable mDeliveries; + +public: + TransactionModel(Project& project); + + const SQLite::Database& GetSQLite() const; + SQLite::Database& GetSQLite(); + + const SalesTable& GetSales() const; + SalesTable& GetSales(); + const PurchasesTable& GetPurchases() const; + PurchasesTable& GetPurchases(); + const DeliveryTable& GetDeliveries() const; + DeliveryTable& GetDeliveries(); +}; diff --git a/core/src/Model/fwd.hpp b/core/src/Model/fwd.hpp index e153923..4bd508c 100644 --- a/core/src/Model/fwd.hpp +++ b/core/src/Model/fwd.hpp @@ -22,4 +22,4 @@ class Project; class SalesTable; class PurchasesTable; class DeliveryTable; -class TransactionDatabase; +class TransactionModel; -- cgit v1.2.3-70-g09d2