Skip to content

Commit 2c9ef12

Browse files
authored
Merge pull request #188 from minorsecond/update-develop
Various bugfixes and optimizations for version 1.1.0
2 parents e7157db + 65a448f commit 2c9ef12

19 files changed

+1085
-107
lines changed

CMakeLists.txt

+6-6
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ include_directories(${SQLITE3_INCLUDE_DIRS})
3131
message(STATUS ${CMAKE_BUILD_TYPE})
3232
IF(CMAKE_BUILD_TYPE MATCHES Release)
3333
message(STATUS "Building release.")
34-
add_executable(Ownly WIN32 src/main.cpp src/Database.cpp src/Database.h src/main.h)
34+
add_executable(Ownly WIN32 src/main.cpp src/Database.cpp src/Database.h src/main.h src/exporters.cpp src/exporters.h src/ExportOptions.cpp src/ExportOptions.h src/ClearWarning.cpp src/ClearWarning.h)
3535
ELSE()
3636
message(STATUS "Building debug.")
37-
add_executable(Ownly src/main.cpp src/Database.cpp src/Database.h src/main.h)
37+
add_executable(Ownly src/main.cpp src/Database.cpp src/Database.h src/main.h src/exporters.cpp src/exporters.h src/ExportOptions.cpp src/ExportOptions.h src/ClearWarning.cpp src/ClearWarning.h)
3838
ENDIF()
3939

40-
add_executable(database_functions_test src/Database.cpp test/test-main.cpp test/test_database_functions.cpp)
40+
add_executable(functions_test src/Database.cpp test/test-main.cpp test/test_database_functions.cpp test/test_export_functions.cpp src/exporters.cpp)
4141

4242
add_custom_command(TARGET Ownly POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different "C:/msys64/mingw64/bin/libsqlite3-0.dll" $<TARGET_FILE_DIR:Ownly>)
4343
add_custom_command(TARGET Ownly POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different "C:/msys64/mingw64/bin/libgcc_s_seh-1.dll" $<TARGET_FILE_DIR:Ownly>)
@@ -71,8 +71,8 @@ message(STATUS ${SQLite3_INCLUDE_DIRS})
7171
target_include_directories(Ownly PRIVATE ${SQLite3_INCLUDE_DIRS})
7272
target_link_libraries(Ownly Qt5::Core Qt5::Widgets SQLite3)
7373

74-
target_include_directories(database_functions_test PRIVATE ${SQLite3_INCLUDE_DIRS})
75-
target_link_libraries(database_functions_test SQLite3)
74+
target_include_directories(functions_test PRIVATE ${SQLite3_INCLUDE_DIRS})
75+
target_link_libraries(functions_test SQLite3)
7676
include(CTest)
7777
include(ParseAndAddCatchTests)
78-
ParseAndAddCatchTests(database_functions_test)
78+
ParseAndAddCatchTests(functions_test)

Jenkinsfile

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
pipeline {
33
agent { label 'CI-W10-Agent'}
4+
45
options {
56
buildDiscarder(logRotator(numToKeepStr: '10'))
67
}
@@ -59,8 +60,9 @@ pipeline {
5960
bat 'del /F/Q/S artifacts\\database_functions_test_autogen'
6061
bat 'del /F/Q/S artifacts\\Ownly_autogen'
6162
bat 'del /F/Q/S artifacts\\src'
62-
bat 'del /F/Q/S artifacts\\database_functions_test_autogen'
63-
archiveArtifacts artifacts: 'artifacts/**/**', excludes: "artifacts/Testing/**,artifacts/*.cmake, artifacts/*.tcl,artifacts/*CMake*,artifacts/*autogen*,artifacts/Makefile,artifacts/*cbp,artifacts/database_functions_test.exe,artifacts/test.xml,artifacts/testdb.sqlite"
63+
bat 'del /F/Q/S artifacts\\functions_test_autogen'
64+
bat 'del /F/Q/S artifacts\\Testing'
65+
archiveArtifacts artifacts: 'artifacts/**/**', excludes: "artifacts/Testing/**,artifacts/*.cmake, artifacts/*.tcl,artifacts/*CMake*,artifacts/*autogen*,artifacts/Makefile,artifacts/*cbp,artifacts/functions_test.exe,artifacts/test.csv,artifacts/testdb.sqlite"
6466
}
6567
post {
6668
always {

src/ClearWarning.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// Created by rwardrup on 9/9/2020.
3+
//
4+
5+
#include "ClearWarning.h"
6+
#include "Database.h"
7+
#include <QPushButton>
8+
9+
ClearWarning::ClearWarning(QWidget *parent) {
10+
ui.setupUi(this);
11+
12+
this->setFixedSize(400, 99);
13+
14+
// Change the Clear button attributes
15+
ui.clearWarningButtons->button(QDialogButtonBox::Ok)->setText("Clear");
16+
QColor clear_button_color = QColor(250, 180, 174);
17+
ui.clearWarningButtons->button(QDialogButtonBox::Ok)->setStyleSheet(QString("background:%1").arg(clear_button_color.name()));
18+
19+
connect(ui.clearWarningButtons, SIGNAL(accepted()), this, SLOT(clear_database()));
20+
}
21+
22+
void ClearWarning::clear_database() {
23+
std::string database_path = Database::set_db_path();
24+
Storage storage = initStorage(database_path);
25+
Database::truncate(storage);
26+
}

src/ClearWarning.h

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// Created by rwardrup on 9/9/2020.
3+
//
4+
5+
#ifndef OWNLY_CLEARWARNING_H
6+
#define OWNLY_CLEARWARNING_H
7+
8+
#include <QDialog>
9+
#include "ui_clear_warning.h"
10+
11+
12+
class ClearWarning : public QDialog, public Ui::ClearDialog {
13+
/*
14+
* QDialog methods
15+
*/
16+
17+
Q_OBJECT
18+
Ui::ClearDialog ui{};
19+
20+
public:
21+
explicit ClearWarning(QWidget *parent = nullptr);
22+
23+
private slots:
24+
static void clear_database();
25+
};
26+
27+
#endif //OWNLY_CLEARWARNING_H

src/Database.cpp

+84-12
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,36 @@
66
#include "../include/sqlite_orm.h"
77
#include <unistd.h>
88
#include <iostream>
9+
#include <utility>
910
#include <vector>
10-
#include <string.h>
1111

1212
using namespace sqlite_orm;
1313

14-
std::vector<Item> Database::read(std::string file_name) {
15-
Storage storage = initStorage(file_name);
14+
std::vector<Item> Database::read(std::string database_path) {
15+
/*
16+
* Read items from sqlite database.
17+
* The Item struct is defined in Database.h
18+
*
19+
* @param: file_name String denoting file name of sqlite file.
20+
* @return: vector of Items.
21+
*/
22+
23+
Storage storage = initStorage(std::move(database_path));
1624
std::vector<Item> allItems = storage.get_all<Item>();
1725

1826
return allItems;
1927
}
2028

21-
Storage Database::write(Item item) {
22-
Storage storage = initStorage("ownly.db");
29+
Storage Database::write(Item item, std::string database_path) {
30+
/*
31+
* Write an Item to the sqlite database.
32+
*
33+
* @param: item an Item containing attributes of a household item
34+
* @return: Storage instance.
35+
*/
36+
37+
38+
Storage storage = initStorage(std::move(database_path));
2339

2440
auto insertedId = storage.insert(item);
2541
std::cout << "insertedId = " << insertedId << std::endl;
@@ -30,38 +46,94 @@ Storage Database::write(Item item) {
3046
return storage;
3147
}
3248

33-
int Database::writeDbToDisk(Storage storage) {
49+
void Database::writeDbToDisk(Storage storage) {
50+
/*
51+
* Write in-memory items to sqlite file.
52+
* @param storage: A storage instance.
53+
*/
3454
std::map<std::string, sqlite_orm::sync_schema_result> schema_sync_result = storage.sync_schema(false);
35-
return 0;
3655
}
3756

3857
void Database::truncate(Storage storage) {
58+
/*
59+
* Remove all rows from the sqlite database.
60+
* @param storage: A storage instance.
61+
*/
62+
3963
storage.remove_all<Item>();
4064
writeDbToDisk(storage);
4165
}
4266

4367
void Database::deleteRow(Storage storage, int row_number) {
68+
/*
69+
* Delete a specific row from the sqlite database.
70+
* @param storage: A storage instance.
71+
* @param row_number: Row number to delete. This is the primary key in the sqlite file.
72+
*/
73+
4474
storage.remove<Item>(row_number);
75+
writeDbToDisk(storage);
4576
}
4677

4778
Item Database::read_row(Storage storage, int row) {
79+
/*
80+
* Read a specific row from the sqlite database.
81+
* @param storage: A storage instance.
82+
* @param row: The primary key id denoting the row to read.
83+
*/
84+
4885
Item item = storage.get<Item>(row);
4986
return item;
5087
}
5188

52-
void Database::update(const Item& item) {
53-
Storage storage = initStorage("ownly.db");
89+
void Database::update(const Item& item, std::string database_path) {
90+
/*
91+
* Update an Item that exists in the database.
92+
* @param item: The Item to update.
93+
*/
94+
95+
Storage storage = initStorage(std::move(database_path));
5496
storage.update(item);
5597
}
5698

57-
std::vector<Item> Database::filter(std::string category, std::string file_name) {
58-
Storage storage = initStorage(file_name);
99+
std::vector<Item> Database::filter(const std::string& category, const std::string& database_path) {
100+
/*
101+
* Filter database by a category.
102+
* @param category: Category to filter database on.
103+
* @param file_name: file name of sqlite file.
104+
*/
105+
106+
Storage storage = initStorage(database_path);
59107
std::vector<Item> items_by_category;
60108
if (category == "All Items") {
61-
items_by_category = read(file_name);
109+
items_by_category = read(database_path);
62110
} else {
63111
items_by_category = storage.get_all<Item>(sqlite_orm::where(sqlite_orm::c(&Item::category) == category));
64112
}
65113

66114
return items_by_category;
67115
}
116+
117+
std::string Database::set_db_path() {
118+
/*
119+
* Get the path to the users APPDATA directory for database storage.
120+
* @return database_path: Path where database will be stored.
121+
*/
122+
123+
std::string database_path;
124+
PWSTR localAppData = nullptr;
125+
if (SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, nullptr, &localAppData) == S_OK) {
126+
std::wstring ws_path(localAppData);
127+
std::string database_directory;
128+
using convert_type = std::codecvt_utf8<wchar_t>;
129+
std::wstring_convert<convert_type, wchar_t> converter;
130+
database_directory = converter.to_bytes(ws_path) + "\\Ownly";
131+
database_path = database_directory + "\\ownly_data.db";
132+
CoTaskMemFree(static_cast<void*>(localAppData));
133+
134+
CreateDirectory(database_directory.c_str(), nullptr);
135+
std::cout << "DB path: " << database_path << std::endl;
136+
}
137+
138+
return database_path;
139+
}

src/Database.h

+31-13
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@
77
#include "string"
88
#include "../include/sqlite_orm.h"
99
#include <vector>
10+
#include <windows.h>
11+
#include <shlobj.h>
12+
#include <locale>
13+
#include <codecvt>
1014

11-
struct Item {
15+
#include <iostream>
16+
17+
struct Item { // Struct for storing item attributes
1218
int id;
1319
std::string itemName;
1420
std::string category;
@@ -17,12 +23,20 @@ struct Item {
1723
int purchaseDay;
1824
double purchasePrice;
1925
int count;
20-
bool usedInLastSixMonths;
26+
bool usedFrequently;
2127
std::string notes;
2228
};
2329

24-
inline static auto initStorage(const std::string& file_name) {
25-
return sqlite_orm::make_storage(file_name,
30+
inline static auto initStorage(const std::string& database_path) {
31+
/*
32+
* Initialize the sqlite database. This creates a new file
33+
* if one doesn't already exist. If one already exists, it
34+
* will use it.
35+
* @param file_name: Path to location of sqlite file
36+
* @return: A Storage instance.
37+
*/
38+
39+
return sqlite_orm::make_storage(database_path,
2640
sqlite_orm::make_table("items",
2741
sqlite_orm::make_column("id", &Item::id, sqlite_orm::autoincrement(), sqlite_orm::primary_key()),
2842
sqlite_orm::make_column("item_name", &Item::itemName),
@@ -32,7 +46,7 @@ inline static auto initStorage(const std::string& file_name) {
3246
sqlite_orm::make_column("purchase_day", &Item::purchaseDay),
3347
sqlite_orm::make_column("purchase_price", &Item::purchasePrice),
3448
sqlite_orm::make_column("count", &Item::count),
35-
sqlite_orm::make_column("used_in_last_six_months", &Item::usedInLastSixMonths, sqlite_orm::default_value(
49+
sqlite_orm::make_column("used_frequently", &Item::usedFrequently, sqlite_orm::default_value(
3650
false)),
3751
sqlite_orm::make_column("notes", &Item::notes)));
3852

@@ -41,15 +55,19 @@ inline static auto initStorage(const std::string& file_name) {
4155
using Storage = decltype(initStorage("")); // Get Storage return type
4256

4357
class Database {
58+
/*
59+
* Various database related methods.
60+
*/
4461
public:
45-
void deleteRow(Storage storage, int row_number);
46-
int writeDbToDisk(Storage storage);
47-
std::vector<Item> read(std::string);
48-
Storage write(Item item);
49-
void truncate(Storage);
50-
Item read_row(Storage storage, int row);
51-
std::vector<Item> filter(std::string category, std::string file_name);
52-
static void update(const Item& item);
62+
static void deleteRow(Storage storage, int row_number);
63+
static void writeDbToDisk(Storage storage); // Flush in-memory data to file
64+
static std::vector<Item> read(std::string database_path);
65+
static Storage write(Item item, std::string database_path);
66+
static void truncate(Storage);
67+
static Item read_row(Storage storage, int row);
68+
static std::vector<Item> filter(const std::string& category, const std::string& database_path);
69+
static void update(const Item& item, std::string database_path);
70+
static std::string set_db_path();
5371
};
5472

5573
#endif //NOTCH_DATABASE_H

0 commit comments

Comments
 (0)