aboutsummaryrefslogtreecommitdiff
path: root/core/src/Model
diff options
context:
space:
mode:
authorrtk0c <[email protected]>2021-04-09 23:17:45 -0700
committerrtk0c <[email protected]>2021-04-09 23:17:45 -0700
commit4303d0be47526b35e5bb3e3be001da227dae5d96 (patch)
tree6e79502e9966f835756d30929ea3f7127ba3080b /core/src/Model
parentce8660cc5bfc12e6e3f75d4cce22492783ca9066 (diff)
Fix crash on load entries
- TODO format time properly - TODO add Purchases table visualization
Diffstat (limited to 'core/src/Model')
-rw-r--r--core/src/Model/Project.cpp4
-rw-r--r--core/src/Model/Project.hpp13
-rw-r--r--core/src/Model/TransactionDatabase.cpp160
-rw-r--r--core/src/Model/TransactionsModel.cpp139
-rw-r--r--core/src/Model/TransactionsModel.hpp (renamed from core/src/Model/TransactionDatabase.hpp)23
-rw-r--r--core/src/Model/fwd.hpp2
6 files changed, 160 insertions, 181 deletions
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 <json/forwards.h>
#include <filesystem>
@@ -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 <filesystem>
-#include <stdexcept>
-
-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/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 <filesystem>
+#include <stdexcept>
+
+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/TransactionDatabase.hpp b/core/src/Model/TransactionsModel.hpp
index 9c869c4..beed663 100644
--- a/core/src/Model/TransactionDatabase.hpp
+++ b/core/src/Model/TransactionsModel.hpp
@@ -12,31 +12,36 @@ public:
SQLite::Statement FilterRowsStatement;
public:
- SalesTable(TransactionDatabase& db);
-
- int GetEntryCont() const;
+ SalesTable(TransactionModel& db);
};
class PurchasesTable {
public:
- PurchasesTable(TransactionDatabase& db);
+ PurchasesTable(TransactionModel& db);
};
class DeliveryTable {
public:
- DeliveryTable(TransactionDatabase& db);
+ DeliveryTable(TransactionModel& db);
};
-class TransactionDatabase {
+// 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;
- SQLite::Database mDb;
+ DatabaseWrapper mDbWrapper;
SalesTable mSales;
PurchasesTable mPurchases;
DeliveryTable mDeliveries;
public:
- TransactionDatabase(Project& project);
+ TransactionModel(Project& project);
const SQLite::Database& GetSQLite() const;
SQLite::Database& GetSQLite();
@@ -46,5 +51,5 @@ public:
const PurchasesTable& GetPurchases() const;
PurchasesTable& GetPurchases();
const DeliveryTable& GetDeliveries() const;
- DeliveryTable& GetDeliveries()
+ 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;