Skip to content

Commit

Permalink
Merge pull request #188 from minorsecond/update-develop
Browse files Browse the repository at this point in the history
Various bugfixes and optimizations for version 1.1.0
  • Loading branch information
minorsecond authored Sep 11, 2020
2 parents e7157db + 65a448f commit 2c9ef12
Show file tree
Hide file tree
Showing 19 changed files with 1,085 additions and 107 deletions.
12 changes: 6 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ include_directories(${SQLITE3_INCLUDE_DIRS})
message(STATUS ${CMAKE_BUILD_TYPE})
IF(CMAKE_BUILD_TYPE MATCHES Release)
message(STATUS "Building release.")
add_executable(Ownly WIN32 src/main.cpp src/Database.cpp src/Database.h src/main.h)
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)
ELSE()
message(STATUS "Building debug.")
add_executable(Ownly src/main.cpp src/Database.cpp src/Database.h src/main.h)
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)
ENDIF()

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

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>)
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>)
Expand Down Expand Up @@ -71,8 +71,8 @@ message(STATUS ${SQLite3_INCLUDE_DIRS})
target_include_directories(Ownly PRIVATE ${SQLite3_INCLUDE_DIRS})
target_link_libraries(Ownly Qt5::Core Qt5::Widgets SQLite3)

target_include_directories(database_functions_test PRIVATE ${SQLite3_INCLUDE_DIRS})
target_link_libraries(database_functions_test SQLite3)
target_include_directories(functions_test PRIVATE ${SQLite3_INCLUDE_DIRS})
target_link_libraries(functions_test SQLite3)
include(CTest)
include(ParseAndAddCatchTests)
ParseAndAddCatchTests(database_functions_test)
ParseAndAddCatchTests(functions_test)
6 changes: 4 additions & 2 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

pipeline {
agent { label 'CI-W10-Agent'}

options {
buildDiscarder(logRotator(numToKeepStr: '10'))
}
Expand Down Expand Up @@ -59,8 +60,9 @@ pipeline {
bat 'del /F/Q/S artifacts\\database_functions_test_autogen'
bat 'del /F/Q/S artifacts\\Ownly_autogen'
bat 'del /F/Q/S artifacts\\src'
bat 'del /F/Q/S artifacts\\database_functions_test_autogen'
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"
bat 'del /F/Q/S artifacts\\functions_test_autogen'
bat 'del /F/Q/S artifacts\\Testing'
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"
}
post {
always {
Expand Down
26 changes: 26 additions & 0 deletions src/ClearWarning.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// Created by rwardrup on 9/9/2020.
//

#include "ClearWarning.h"
#include "Database.h"
#include <QPushButton>

ClearWarning::ClearWarning(QWidget *parent) {
ui.setupUi(this);

this->setFixedSize(400, 99);

// Change the Clear button attributes
ui.clearWarningButtons->button(QDialogButtonBox::Ok)->setText("Clear");
QColor clear_button_color = QColor(250, 180, 174);
ui.clearWarningButtons->button(QDialogButtonBox::Ok)->setStyleSheet(QString("background:%1").arg(clear_button_color.name()));

connect(ui.clearWarningButtons, SIGNAL(accepted()), this, SLOT(clear_database()));
}

void ClearWarning::clear_database() {
std::string database_path = Database::set_db_path();
Storage storage = initStorage(database_path);
Database::truncate(storage);
}
27 changes: 27 additions & 0 deletions src/ClearWarning.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// Created by rwardrup on 9/9/2020.
//

#ifndef OWNLY_CLEARWARNING_H
#define OWNLY_CLEARWARNING_H

#include <QDialog>
#include "ui_clear_warning.h"


class ClearWarning : public QDialog, public Ui::ClearDialog {
/*
* QDialog methods
*/

Q_OBJECT
Ui::ClearDialog ui{};

public:
explicit ClearWarning(QWidget *parent = nullptr);

private slots:
static void clear_database();
};

#endif //OWNLY_CLEARWARNING_H
96 changes: 84 additions & 12 deletions src/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,36 @@
#include "../include/sqlite_orm.h"
#include <unistd.h>
#include <iostream>
#include <utility>
#include <vector>
#include <string.h>

using namespace sqlite_orm;

std::vector<Item> Database::read(std::string file_name) {
Storage storage = initStorage(file_name);
std::vector<Item> Database::read(std::string database_path) {
/*
* Read items from sqlite database.
* The Item struct is defined in Database.h
*
* @param: file_name String denoting file name of sqlite file.
* @return: vector of Items.
*/

Storage storage = initStorage(std::move(database_path));
std::vector<Item> allItems = storage.get_all<Item>();

return allItems;
}

Storage Database::write(Item item) {
Storage storage = initStorage("ownly.db");
Storage Database::write(Item item, std::string database_path) {
/*
* Write an Item to the sqlite database.
*
* @param: item an Item containing attributes of a household item
* @return: Storage instance.
*/


Storage storage = initStorage(std::move(database_path));

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

int Database::writeDbToDisk(Storage storage) {
void Database::writeDbToDisk(Storage storage) {
/*
* Write in-memory items to sqlite file.
* @param storage: A storage instance.
*/
std::map<std::string, sqlite_orm::sync_schema_result> schema_sync_result = storage.sync_schema(false);
return 0;
}

void Database::truncate(Storage storage) {
/*
* Remove all rows from the sqlite database.
* @param storage: A storage instance.
*/

storage.remove_all<Item>();
writeDbToDisk(storage);
}

void Database::deleteRow(Storage storage, int row_number) {
/*
* Delete a specific row from the sqlite database.
* @param storage: A storage instance.
* @param row_number: Row number to delete. This is the primary key in the sqlite file.
*/

storage.remove<Item>(row_number);
writeDbToDisk(storage);
}

Item Database::read_row(Storage storage, int row) {
/*
* Read a specific row from the sqlite database.
* @param storage: A storage instance.
* @param row: The primary key id denoting the row to read.
*/

Item item = storage.get<Item>(row);
return item;
}

void Database::update(const Item& item) {
Storage storage = initStorage("ownly.db");
void Database::update(const Item& item, std::string database_path) {
/*
* Update an Item that exists in the database.
* @param item: The Item to update.
*/

Storage storage = initStorage(std::move(database_path));
storage.update(item);
}

std::vector<Item> Database::filter(std::string category, std::string file_name) {
Storage storage = initStorage(file_name);
std::vector<Item> Database::filter(const std::string& category, const std::string& database_path) {
/*
* Filter database by a category.
* @param category: Category to filter database on.
* @param file_name: file name of sqlite file.
*/

Storage storage = initStorage(database_path);
std::vector<Item> items_by_category;
if (category == "All Items") {
items_by_category = read(file_name);
items_by_category = read(database_path);
} else {
items_by_category = storage.get_all<Item>(sqlite_orm::where(sqlite_orm::c(&Item::category) == category));
}

return items_by_category;
}

std::string Database::set_db_path() {
/*
* Get the path to the users APPDATA directory for database storage.
* @return database_path: Path where database will be stored.
*/

std::string database_path;
PWSTR localAppData = nullptr;
if (SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, nullptr, &localAppData) == S_OK) {
std::wstring ws_path(localAppData);
std::string database_directory;
using convert_type = std::codecvt_utf8<wchar_t>;
std::wstring_convert<convert_type, wchar_t> converter;
database_directory = converter.to_bytes(ws_path) + "\\Ownly";
database_path = database_directory + "\\ownly_data.db";
CoTaskMemFree(static_cast<void*>(localAppData));

CreateDirectory(database_directory.c_str(), nullptr);
std::cout << "DB path: " << database_path << std::endl;
}

return database_path;
}
44 changes: 31 additions & 13 deletions src/Database.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@
#include "string"
#include "../include/sqlite_orm.h"
#include <vector>
#include <windows.h>
#include <shlobj.h>
#include <locale>
#include <codecvt>

struct Item {
#include <iostream>

struct Item { // Struct for storing item attributes
int id;
std::string itemName;
std::string category;
Expand All @@ -17,12 +23,20 @@ struct Item {
int purchaseDay;
double purchasePrice;
int count;
bool usedInLastSixMonths;
bool usedFrequently;
std::string notes;
};

inline static auto initStorage(const std::string& file_name) {
return sqlite_orm::make_storage(file_name,
inline static auto initStorage(const std::string& database_path) {
/*
* Initialize the sqlite database. This creates a new file
* if one doesn't already exist. If one already exists, it
* will use it.
* @param file_name: Path to location of sqlite file
* @return: A Storage instance.
*/

return sqlite_orm::make_storage(database_path,
sqlite_orm::make_table("items",
sqlite_orm::make_column("id", &Item::id, sqlite_orm::autoincrement(), sqlite_orm::primary_key()),
sqlite_orm::make_column("item_name", &Item::itemName),
Expand All @@ -32,7 +46,7 @@ inline static auto initStorage(const std::string& file_name) {
sqlite_orm::make_column("purchase_day", &Item::purchaseDay),
sqlite_orm::make_column("purchase_price", &Item::purchasePrice),
sqlite_orm::make_column("count", &Item::count),
sqlite_orm::make_column("used_in_last_six_months", &Item::usedInLastSixMonths, sqlite_orm::default_value(
sqlite_orm::make_column("used_frequently", &Item::usedFrequently, sqlite_orm::default_value(
false)),
sqlite_orm::make_column("notes", &Item::notes)));

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

class Database {
/*
* Various database related methods.
*/
public:
void deleteRow(Storage storage, int row_number);
int writeDbToDisk(Storage storage);
std::vector<Item> read(std::string);
Storage write(Item item);
void truncate(Storage);
Item read_row(Storage storage, int row);
std::vector<Item> filter(std::string category, std::string file_name);
static void update(const Item& item);
static void deleteRow(Storage storage, int row_number);
static void writeDbToDisk(Storage storage); // Flush in-memory data to file
static std::vector<Item> read(std::string database_path);
static Storage write(Item item, std::string database_path);
static void truncate(Storage);
static Item read_row(Storage storage, int row);
static std::vector<Item> filter(const std::string& category, const std::string& database_path);
static void update(const Item& item, std::string database_path);
static std::string set_db_path();
};

#endif //NOTCH_DATABASE_H
Loading

0 comments on commit 2c9ef12

Please sign in to comment.