From be9c951d025fc7083c736b7cb6a091446f686b9e Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Tue, 10 Jul 2018 12:51:03 +0200 Subject: [PATCH 1/4] more work on entities --- src/core/entity.cpp | 11 +++++++---- src/core/entity.h | 9 ++++++--- src/core/seller.cpp | 10 +++++----- src/core/seller.h | 10 +++++----- 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/core/entity.cpp b/src/core/entity.cpp index df76d01..c2a0313 100644 --- a/src/core/entity.cpp +++ b/src/core/entity.cpp @@ -5,18 +5,21 @@ #include #include -//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); + uuid_ = generator(uuidString); +} + +inline Entity::State Entity::getState() +{ + return state_; } \ No newline at end of file diff --git a/src/core/entity.h b/src/core/entity.h index e416ea7..d67cc73 100644 --- a/src/core/entity.h +++ b/src/core/entity.h @@ -8,14 +8,17 @@ class Entity { public: - //Entity(); + enum class State { NEW, UPDATED, READ }; + // 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{}; + boost::uuids::uuid uuid_{}; + State state_{State::NEW}; }; #endif // ENTITY_H \ No newline at end of file diff --git a/src/core/seller.cpp b/src/core/seller.cpp index 58861bd..e4de084 100644 --- a/src/core/seller.cpp +++ b/src/core/seller.cpp @@ -2,25 +2,25 @@ void Seller::setSellerNo(int seller_no) { - this->sellerNo = seller_no; + sellerNo_ = seller_no; } void Seller::setFirstName(const std::string& firstName) { - this->firstName = firstName; + firstName_ = firstName; } void Seller::setLastName(const std::string& lastName) { - this->lastName = lastName; + lastName_ = lastName; } void Seller::setNumberOfOfferedArticles(int number) { - numberOfOfferedArticles = number; + numberOfOfferedArticles_ = number; } size_t Seller::getNumberOfOfferedArticles() { - return articles.size(); + return articles_.size(); } \ No newline at end of file diff --git a/src/core/seller.h b/src/core/seller.h index 16edd98..2848579 100644 --- a/src/core/seller.h +++ b/src/core/seller.h @@ -20,11 +20,11 @@ class Seller : public Entity size_t getNumberOfOfferedArticles(); private: - int sellerNo{}; - int numberOfOfferedArticles{}; - std::string firstName{}; - std::string lastName{}; - std::vector
articles{}; + int sellerNo_{-1}; + int numberOfOfferedArticles_{}; + std::string firstName_{}; + std::string lastName_{}; + std::vector
articles_{}; }; #endif \ No newline at end of file From af72567bf701ffd3c5aaaa63eb2dc0dc7572ae72 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Tue, 10 Jul 2018 12:51:15 +0200 Subject: [PATCH 2/4] tests renamed --- test/CMakeLists.txt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index f89cd4a..388f410 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,9 +1,9 @@ find_package(Boost COMPONENTS filesystem unit_test_framework REQUIRED) -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_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(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) +add_executable(test_database test_database.cpp) +target_link_libraries(test_database core Boost::filesystem Boost::unit_test_framework) +add_test(Database ${CMAKE_BINARY_DIR}/bin/test_database) From 79d96c22bc73e8a3cf33d147ade5f5534e6e9e11 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Tue, 10 Jul 2018 12:51:23 +0200 Subject: [PATCH 3/4] transactions --- src/core/database.cpp | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/core/database.cpp b/src/core/database.cpp index 0b3207a..0e8fe45 100644 --- a/src/core/database.cpp +++ b/src/core/database.cpp @@ -20,12 +20,6 @@ void Database::exec(const std::string& sql) } } -void Database::beginTransaction() -{ - exec("BEGIN TRANSACTION"); -} +void Database::beginTransaction() { exec("BEGIN TRANSACTION"); } -void Database::endTransaction() -{ - exec("END TRANSACTION"); -} \ No newline at end of file +void Database::endTransaction() { exec("END TRANSACTION"); } \ No newline at end of file From 9705426c3159bd5f8f2dd5ab3bfbbe157d1e3c47 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Tue, 10 Jul 2018 14:12:37 +0200 Subject: [PATCH 4/4] create db with sellers and articles --- src/core/database.cpp | 45 ++++++++++++++++++++++++++++++++++++++---- src/core/database.h | 3 ++- test/CMakeLists.txt | 4 ++-- test/test_database.cpp | 7 ++++++- 4 files changed, 51 insertions(+), 8 deletions(-) diff --git a/src/core/database.cpp b/src/core/database.cpp index 0e8fe45..5fc1ca5 100644 --- a/src/core/database.cpp +++ b/src/core/database.cpp @@ -1,25 +1,62 @@ #include "database.h" #include +#include -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() +{ + 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(); +} + void Database::beginTransaction() { exec("BEGIN TRANSACTION"); } void Database::endTransaction() { exec("END TRANSACTION"); } \ No newline at end of file diff --git a/src/core/database.h b/src/core/database.h index 5fcf027..c0b4de4 100644 --- a/src/core/database.h +++ b/src/core/database.h @@ -13,8 +13,9 @@ 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(); }; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 388f410..02007e1 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,9 +1,9 @@ -find_package(Boost COMPONENTS filesystem unit_test_framework REQUIRED) +find_package(Boost COMPONENTS 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(test_database test_database.cpp) -target_link_libraries(test_database core Boost::filesystem Boost::unit_test_framework) +target_link_libraries(test_database core Boost::unit_test_framework stdc++fs) add_test(Database ${CMAKE_BINARY_DIR}/bin/test_database) diff --git a/test/test_database.cpp b/test/test_database.cpp index 982a174..8e6d8ba 100644 --- a/test/test_database.cpp +++ b/test/test_database.cpp @@ -2,10 +2,15 @@ #include "../src/core/database.h" +#include + #include //using namespace boost::unit_test; BOOST_AUTO_TEST_CASE( create_database ) { - Database db("test.db"); + std::filesystem::path testPath = std::filesystem::temp_directory_path() / "test.db"; + Database db(testPath.c_str()); + db.init(); + std::filesystem::remove(testPath); } \ No newline at end of file