Browse Source

create db at the right place

allow_new_sellers
Martin Brodbeck 4 years ago
parent
commit
5eef98074e
  1. 37
      src/core/database.cpp
  2. 7
      src/core/database.h
  3. 5
      src/core/marketplace.cpp
  4. 2
      src/gui/CMakeLists.txt
  5. 5
      src/gui/kima2.cpp
  6. 6
      test/test_database.cpp

37
src/core/database.cpp

@ -1,17 +1,36 @@
#include "database.h"
#include <filesystem>
#include <iostream>
#include <stdexcept>
#include <vector>
Database::Database(const std::string& dbname) : db_(nullptr)
Database::Database(const std::string& dbname)
{
dbname_ = dbname;
const int errCode = sqlite3_open(dbname_.c_str(), &db_);
if (errCode) {
throw std::runtime_error("Could not open database file.");
init();
}
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_); }
@ -82,6 +101,12 @@ void Database::createNew()
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();
switch (version) {
@ -253,7 +278,7 @@ unsigned int Database::storeSellers(std::vector<std::unique_ptr<Seller>>& seller
endTransaction();
// Everything went fine, so we can now update our objects
// Everything went fine, so we can now update our objects
sellers.erase(std::remove_if(sellers.begin(), sellers.end(),
[](const std::unique_ptr<Seller>& seller) {
return (seller->getState() == Seller::State::DELETE);

7
src/core/database.h

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

5
src/core/marketplace.cpp

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

2
src/gui/CMakeLists.txt

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

5
src/gui/kima2.cpp

@ -1,13 +1,10 @@
#include "mainwindow.h"
#include <memory>
#include <QApplication>
int main(int argc, char* argv[])
{
//Q_INIT_RESOURCE(application);
// Q_INIT_RESOURCE(application);
QApplication kimaApp{argc, argv};

6
test/test_database.cpp

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

Loading…
Cancel
Save