Compare commits

..

No commits in common. "300c883fdc6e2f3d788de810c1b21dcbd5a9bab7" and "2812f582a1a28370e73457a6618702ac93088902" have entirely different histories.

7 changed files with 23 additions and 54 deletions

View file

@ -1,36 +1,17 @@
#include "database.h" #include "database.h"
#include <filesystem>
#include <iostream> #include <iostream>
#include <stdexcept> #include <stdexcept>
#include <vector> #include <vector>
Database::Database(const std::string& dbname) Database::Database(const std::string& dbname) : db_(nullptr)
{ {
dbname_ = dbname; dbname_ = dbname;
init(); const int errCode = sqlite3_open(dbname_.c_str(), &db_);
} if (errCode) {
throw std::runtime_error("Could not open database file.");
Database::Database()
{
namespace fs = std::filesystem;
#if defined(__linux__)
fs::path dbpath = fs::path(std::getenv("HOME")) / ".local/share/kima2-cpp";
#elif defined(__WIN64) || defined(__WIN32)
fs::path dbpath = fs::path(std::getenv("LOCALAPPDATA") / "/kima2-cpp";
#else
throw std::runtime_error("Platform not supported.");
#endif
if (!fs::exists(dbpath)) {
try {
fs::create_directories(dbpath);
} catch (fs::filesystem_error& err) {
throw err;
} }
} sqlite3_db_config(db_, SQLITE_DBCONFIG_ENABLE_FKEY);
dbpath /= "kima2.db";
dbname_ = dbpath.string();
init();
} }
Database::~Database() { sqlite3_close(db_); } Database::~Database() { sqlite3_close(db_); }
@ -101,12 +82,6 @@ void Database::createNew()
void Database::init() void Database::init()
{ {
const int errCode = sqlite3_open(dbname_.c_str(), &db_);
if (errCode) {
throw std::runtime_error("Could not open database file.");
}
sqlite3_db_config(db_, SQLITE_DBCONFIG_ENABLE_FKEY);
int version = getVersion(); int version = getVersion();
switch (version) { switch (version) {

View file

@ -10,18 +10,17 @@
class Database class Database
{ {
public: public:
explicit Database(const std::string& dbname); Database(const std::string& dbname);
Database();
~Database(); ~Database();
Database(const Database&) = delete; Database(const Database&) = delete;
Database& operator=(const Database&) = delete; Database& operator=(const Database&) = delete;
void exec(const std::string& sql); void exec(const std::string& sql);
void init();
unsigned int storeSellers(std::vector<std::unique_ptr<Seller>>& sellers); unsigned int storeSellers(std::vector<std::unique_ptr<Seller>>& sellers);
private: private:
sqlite3* db_{nullptr}; sqlite3* db_;
std::string dbname_; std::string dbname_;
void init();
void beginTransaction(); void beginTransaction();
void endTransaction(); void endTransaction();
void createNew(); void createNew();

View file

@ -12,9 +12,10 @@ Marketplace::Marketplace()
void Marketplace::storeToDb() void Marketplace::storeToDb()
{ {
//const std::string DB_PATH{"/tmp/kima2.db"}; const std::string DB_PATH{"/tmp/kima2.db"};
Database db; Database db(DB_PATH);
db.init();
db.storeSellers(sellers_); db.storeSellers(sellers_);
} }

View file

@ -18,4 +18,4 @@ set(GUI_SOURCES
) )
add_executable(kima2 ${GUI_SOURCES}) add_executable(kima2 ${GUI_SOURCES})
target_link_libraries(kima2 core Qt5::Widgets stdc++fs) target_link_libraries(kima2 core Qt5::Widgets)

View file

@ -1,17 +1,16 @@
#include "mainwindow.h" #include "mainwindow.h"
#include <memory>
#include <QApplication> #include <QApplication>
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
// Q_INIT_RESOURCE(application); //Q_INIT_RESOURCE(application);
QApplication kimaApp{argc, argv}; QApplication kimaApp{argc, argv};
QCoreApplication::setOrganizationName("RustySoft");
QCoreApplication::setOrganizationDomain("rustysoft.de");
QCoreApplication::setApplicationName("KIMA2");
auto mainWin = std::make_unique<MainWindow>(); auto mainWin = std::make_unique<MainWindow>();
mainWin->show(); mainWin->show();

View file

@ -2,19 +2,14 @@
#include "sellerdialog.h" #include "sellerdialog.h"
constexpr int STATUSBAR_TIMEOUT = 5000;
MainWindow::MainWindow() MainWindow::MainWindow()
{ {
ui_.setupUi(this); ui_.setupUi(this);
connect(ui_.actionQuit, &QAction::triggered, qApp, QApplication::quit); connect(ui_.actionQuit, &QAction::triggered, qApp, QApplication::quit);
connect(ui_.actionEditSeller, &QAction::triggered, this, connect(ui_.actionEditSeller, &QAction::triggered, this, &MainWindow::on_actionEditSeller_triggered);
&MainWindow::on_actionEditSeller_triggered);
marketplace_ = std::make_unique<Marketplace>(); marketplace_ = std::make_unique<Marketplace>();
marketplace_->loadFromDb();
statusBar()->showMessage("Gespeicherte Daten wurden geladen.", STATUSBAR_TIMEOUT);
} }
void MainWindow::on_actionEditSeller_triggered() void MainWindow::on_actionEditSeller_triggered()
@ -23,11 +18,7 @@ void MainWindow::on_actionEditSeller_triggered()
int retCode = dialog->exec(); int retCode = dialog->exec();
if (retCode == QDialog::Accepted) { if (retCode == QDialog::Accepted) {
marketplace_->storeToDb(); marketplace_->storeToDb();
statusBar()->showMessage("Änderungen an den Verkäufer-Stammdaten gespeichert.",
STATUSBAR_TIMEOUT);
} else { } else {
marketplace_->loadFromDb(); marketplace_->loadFromDb();
statusBar()->showMessage("Änderungen an den Verkäufer-Stammdaten <b>verworfen</b>.",
STATUSBAR_TIMEOUT);
} }
} }

View file

@ -9,12 +9,14 @@
BOOST_AUTO_TEST_CASE(create_database) BOOST_AUTO_TEST_CASE(create_database)
{ {
BOOST_CHECK_NO_THROW(Database db(":memory:")); Database db(":memory:");
BOOST_CHECK_NO_THROW(db.init());
} }
BOOST_AUTO_TEST_CASE(store_seller_fail) BOOST_AUTO_TEST_CASE(store_seller_fail)
{ {
Database db(":memory:"); Database db(":memory:");
db.init();
std::vector<std::unique_ptr<Seller>> sellers; std::vector<std::unique_ptr<Seller>> sellers;
sellers.push_back(std::make_unique<Seller>()); sellers.push_back(std::make_unique<Seller>());
sellers.push_back(std::make_unique<Seller>()); sellers.push_back(std::make_unique<Seller>());
@ -23,6 +25,7 @@ BOOST_AUTO_TEST_CASE(store_seller_fail)
BOOST_AUTO_TEST_CASE(store_sellers_succ) BOOST_AUTO_TEST_CASE(store_sellers_succ)
{ {
Database db(":memory:"); Database db(":memory:");
db.init();
std::vector<std::unique_ptr<Seller>> sellers; std::vector<std::unique_ptr<Seller>> sellers;
auto a = std::make_unique<Seller>(); auto a = std::make_unique<Seller>();
a->createUuid(); a->createUuid();
@ -44,6 +47,7 @@ BOOST_AUTO_TEST_CASE(store_sellers_succ)
BOOST_AUTO_TEST_CASE(seller_states) BOOST_AUTO_TEST_CASE(seller_states)
{ {
Database db(":memory:"); Database db(":memory:");
db.init();
std::vector<std::unique_ptr<Seller>> sellers; std::vector<std::unique_ptr<Seller>> sellers;
auto a = std::make_unique<Seller>(); auto a = std::make_unique<Seller>();
a->setSellerNo(3); a->setSellerNo(3);