Compare commits

..

No commits in common. "9705426c3159bd5f8f2dd5ab3bfbbe157d1e3c47" and "97e444c91ce0dfe1396d621d3afd64e61047ad9e" have entirely different histories.

8 changed files with 36 additions and 79 deletions

View file

@ -1,62 +1,31 @@
#include "database.h"
#include <stdexcept>
#include <iostream>
Database::Database(const std::string& dbname) : db_(nullptr)
Database::Database(const std::string& dbname) : db(nullptr)
{
const int errCode = sqlite3_open(dbname.c_str(), &db_);
const int errCode = sqlite3_open(dbname.c_str(), &db);
if (errCode) {
throw std::runtime_error("Could not open database file.");
}
exec("PRAGMA foreign_key = 1");
}
Database::~Database() { sqlite3_close(db_); }
Database::~Database() { sqlite3_close(db); }
void Database::exec(const std::string& sql)
{
const int errCode = sqlite3_exec(db_, sql.c_str(), nullptr, nullptr, nullptr);
const int errCode = sqlite3_exec(db, sql.c_str(), nullptr, nullptr, nullptr);
if (errCode) {
throw std::runtime_error("Error in SQL execution.");
}
}
void Database::init()
void Database::beginTransaction()
{
std::string sqlCreateSellers{
"CREATE TABLE IF NOT EXISTS sellers ("
"id TEXT PRIMARY KEY NOT NULL, "
"seller_no INTEGER, "
"first_name TEXT, "
"last_name TEXT, "
"offered_articles INTEGER, "
"UNIQUE (seller_no)"
");"
};
std::string sqlCreateArticles{
"CREATE TABLE IF NOT EXISTS articles ("
"id TEXT PRIMARY KEY NOT NULL, "
"seller_id TEXT NOT NULL, "
"source_no INTEGER NOT NULL, "
"article_no INTEGER NOT NULL, "
"description TEXT, "
"price INTEGER NOT NULL, "
"UNIQUE (source_no, article_no), "
"FOREIGN KEY (seller_id) REFERENCES sellers(id) ON DELETE CASCADE, "
"CHECK (article_no BETWEEN 0 AND 99999)"
");"
};
std::cout << sqlCreateArticles << "\n";
beginTransaction();
exec(sqlCreateSellers);
exec(sqlCreateArticles);
endTransaction();
exec("BEGIN TRANSACTION");
}
void Database::beginTransaction() { exec("BEGIN TRANSACTION"); }
void Database::endTransaction() { exec("END TRANSACTION"); }
void Database::endTransaction()
{
exec("END TRANSACTION");
}

View file

@ -13,9 +13,8 @@ public:
Database(const Database&) = delete;
Database& operator=(const Database&) = delete;
void exec(const std::string& sql);
void init();
private:
sqlite3 *db_;
sqlite3 *db;
void beginTransaction();
void endTransaction();
};

View file

@ -5,21 +5,18 @@
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
//Entity::Entity() {}
Entity::~Entity() {}
void Entity::createUuid()
{
static boost::uuids::random_generator generator{};
uuid_ = generator();
uuid = generator();
}
void Entity::createUuidFromString(const std::string& uuidString)
{
boost::uuids::string_generator generator{};
uuid_ = generator(uuidString);
}
inline Entity::State Entity::getState()
{
return state_;
uuid = generator(uuidString);
}

View file

@ -8,17 +8,14 @@
class Entity
{
public:
enum class State { NEW, UPDATED, READ };
// Entity();
//Entity();
virtual ~Entity() = 0;
const boost::uuids::uuid& getUuid() const { return uuid_; };
const boost::uuids::uuid& getUuid() const { return uuid; };
void createUuid();
void createUuidFromString(const std::string& uuidString);
State getState();
private:
boost::uuids::uuid uuid_{};
State state_{State::NEW};
boost::uuids::uuid uuid{};
};
#endif // ENTITY_H

View file

@ -2,25 +2,25 @@
void Seller::setSellerNo(int seller_no)
{
sellerNo_ = seller_no;
this->sellerNo = seller_no;
}
void Seller::setFirstName(const std::string& firstName)
{
firstName_ = firstName;
this->firstName = firstName;
}
void Seller::setLastName(const std::string& lastName)
{
lastName_ = lastName;
this->lastName = lastName;
}
void Seller::setNumberOfOfferedArticles(int number)
{
numberOfOfferedArticles_ = number;
numberOfOfferedArticles = number;
}
size_t Seller::getNumberOfOfferedArticles()
{
return articles_.size();
return articles.size();
}

View file

@ -20,11 +20,11 @@ class Seller : public Entity
size_t getNumberOfOfferedArticles();
private:
int sellerNo_{-1};
int numberOfOfferedArticles_{};
std::string firstName_{};
std::string lastName_{};
std::vector<Article> articles_{};
int sellerNo{};
int numberOfOfferedArticles{};
std::string firstName{};
std::string lastName{};
std::vector<Article> articles{};
};
#endif

View file

@ -1,9 +1,9 @@
find_package(Boost COMPONENTS unit_test_framework REQUIRED)
find_package(Boost COMPONENTS filesystem unit_test_framework REQUIRED)
add_executable(test_seller test_seller.cpp)
target_link_libraries(test_seller core Boost::unit_test_framework)
add_test(Seller ${CMAKE_BINARY_DIR}/bin/test_seller)
add_executable(sellertest test_seller.cpp)
target_link_libraries(sellertest core Boost::boost Boost::filesystem Boost::unit_test_framework)
add_test(Seller ${CMAKE_BINARY_DIR}/bin/sellertest)
add_executable(test_database test_database.cpp)
target_link_libraries(test_database core Boost::unit_test_framework stdc++fs)
add_test(Database ${CMAKE_BINARY_DIR}/bin/test_database)
add_executable(databasetest test_database.cpp)
target_link_libraries(databasetest core Boost::boost Boost::filesystem Boost::unit_test_framework)
add_test(Database ${CMAKE_BINARY_DIR}/bin/databasetest)

View file

@ -2,15 +2,10 @@
#include "../src/core/database.h"
#include <filesystem>
#include <boost/test/included/unit_test.hpp>
//using namespace boost::unit_test;
BOOST_AUTO_TEST_CASE( create_database ) {
std::filesystem::path testPath = std::filesystem::temp_directory_path() / "test.db";
Database db(testPath.c_str());
db.init();
std::filesystem::remove(testPath);
Database db("test.db");
}