From ddfb9471a8883f32fa420a19a43671744df76750 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Wed, 11 Jul 2018 15:57:39 +0200 Subject: [PATCH 1/4] more test cases --- test/test_database.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/test/test_database.cpp b/test/test_database.cpp index de5c004..b963d8b 100644 --- a/test/test_database.cpp +++ b/test/test_database.cpp @@ -1,6 +1,7 @@ #define BOOST_TEST_MODULE database #include "../src/core/database.h" +#include "../src/core/seller.h" #include @@ -11,5 +12,46 @@ BOOST_AUTO_TEST_CASE(create_database) Database db(":memory:"); BOOST_CHECK_NO_THROW(db.init()); +} +BOOST_AUTO_TEST_CASE(store_seller_fail) +{ + Database db(":memory:"); + db.init(); + std::vector sellers; + sellers.push_back({}); + sellers.push_back({}); + BOOST_CHECK_THROW(db.storeSellers(sellers), std::runtime_error); +} +BOOST_AUTO_TEST_CASE(store_sellers_succ) +{ + Database db(":memory:"); + db.init(); + std::vector sellers; + Seller a{}; + a.createUuid(); + a.setSellerNo(1); + Seller b{}; + b.createUuid(); + b.setSellerNo(2); + BOOST_TEST(a.getUuid() != b.getUuid()); + sellers.push_back(a); + sellers.push_back(b); + BOOST_CHECK_NO_THROW(db.storeSellers(sellers)); +} + +BOOST_AUTO_TEST_CASE(seller_states) +{ + Database db(":memory:"); + db.init(); + std::vector sellers; + Seller a; + a.setSellerNo(3); + a.createUuid(); + sellers.push_back(a); + std::cout << "Anzahl sellers: " << sellers.size() << "\n"; + BOOST_TEST((sellers.at(0).getState() == Entity::State::NEW)); + BOOST_TEST(db.storeSellers(sellers) == 1); + BOOST_TEST((sellers.at(0).getState() == Entity::State::CLEAN)); + BOOST_TEST(db.storeSellers(sellers) == 0); } \ No newline at end of file From 976dcbcbeb417061d7a66ff11c83bfcfdca33c54 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Wed, 11 Jul 2018 15:58:09 +0200 Subject: [PATCH 2/4] store sellers with state == NEW --- src/core/database.cpp | 53 +++++++++++++++++++++++++++++++++++++++++-- src/core/database.h | 3 +++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/core/database.cpp b/src/core/database.cpp index fc1a1c8..086dfc9 100644 --- a/src/core/database.cpp +++ b/src/core/database.cpp @@ -41,7 +41,7 @@ void Database::createNew() "seller_no INTEGER, " "first_name TEXT, " "last_name TEXT, " - "offered_articles INTEGER, " + "num_offered_articles INTEGER, " "UNIQUE (seller_no)" ");"}; sqlStrings.push_back(sqlCreateSellers); @@ -143,4 +143,53 @@ int Database::getVersion() void Database::beginTransaction() { exec("BEGIN TRANSACTION"); } -void Database::endTransaction() { exec("END TRANSACTION"); } \ No newline at end of file +void Database::endTransaction() { exec("END TRANSACTION"); } + +unsigned int Database::storeSellers(std::vector& sellers) +{ + int retCode{}; + int count{}; + sqlite3_stmt* stmt; + + beginTransaction(); + + for (auto& seller : sellers) { + if (seller.getState() == Entity::State::NEW) { + retCode = sqlite3_prepare_v2( + db_, + "INSERT INTO sellers" + " (id, seller_no, first_name, last_name, num_offered_articles)" + " VALUES (:uuid, :seller_no, :first_name, :last_name, :num_offered_articles)", + -1, &stmt, nullptr); + + if (retCode != SQLITE_OK) + throw std::runtime_error(sqlite3_errmsg(db_)); + + sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, ":uuid"), + boost::uuids::to_string(seller.getUuid()).c_str(), -1, nullptr); + sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":seller_no"), + seller.getSellerNo()); + sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, ":first_name"), + seller.getFirstName().c_str(), -1, nullptr); + sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, ":last_name"), + seller.getLastName().c_str(), -1, nullptr); + sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":num_offered_articles"), + seller.getNumberOfOfferedArticles()); + + retCode = sqlite3_step(stmt); + + if (retCode != SQLITE_DONE) { + + std::string errMsg(sqlite3_errmsg(db_)); + sqlite3_finalize(stmt); + throw std::runtime_error(errMsg); + } + seller.setState(Seller::State::CLEAN); + ++count; + sqlite3_finalize(stmt); + } + } + + endTransaction(); + return count; +} \ No newline at end of file diff --git a/src/core/database.h b/src/core/database.h index ff6f3d8..83fe27c 100644 --- a/src/core/database.h +++ b/src/core/database.h @@ -1,6 +1,8 @@ #ifndef DATABASE_H #define DATABASE_H +#include "seller.h" + #include #include @@ -14,6 +16,7 @@ public: Database& operator=(const Database&) = delete; void exec(const std::string& sql); void init(); + unsigned int storeSellers(std::vector& sellers); private: sqlite3 *db_; std::string dbname_; From 4fdbc268a5e7c1bd470a6d03d8ce4f3b4ce6aaf6 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Wed, 11 Jul 2018 15:59:08 +0200 Subject: [PATCH 3/4] more on states --- src/core/entity.cpp | 2 +- src/core/entity.h | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/core/entity.cpp b/src/core/entity.cpp index c2a0313..6a6e5d8 100644 --- a/src/core/entity.cpp +++ b/src/core/entity.cpp @@ -19,7 +19,7 @@ void Entity::createUuidFromString(const std::string& uuidString) uuid_ = generator(uuidString); } -inline Entity::State Entity::getState() +inline Entity::State Entity::getState() const { return state_; } \ No newline at end of file diff --git a/src/core/entity.h b/src/core/entity.h index d67cc73..786d393 100644 --- a/src/core/entity.h +++ b/src/core/entity.h @@ -4,17 +4,19 @@ #include #include +#include class Entity { public: - enum class State { NEW, UPDATED, READ }; - // Entity(); + enum class State { NEW, UPDATED, CLEAN }; + virtual ~Entity() = 0; const boost::uuids::uuid& getUuid() const { return uuid_; }; void createUuid(); void createUuidFromString(const std::string& uuidString); - State getState(); + virtual State getState() const; + void setState(State state) { state_ = state; } private: boost::uuids::uuid uuid_{}; From b2650fad269a2a1e4171da4e2ae18da080c44dc1 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Wed, 11 Jul 2018 15:59:38 +0200 Subject: [PATCH 4/4] some getters added --- src/core/seller.cpp | 14 +++++++++----- src/core/seller.h | 5 ++++- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/core/seller.cpp b/src/core/seller.cpp index 634e585..ab83c55 100644 --- a/src/core/seller.cpp +++ b/src/core/seller.cpp @@ -12,7 +12,7 @@ Seller::Seller(const std::string& firstName, const std::string& lastName, int se numberOfOfferedArticles_ = numberOfArticles; } -inline void Seller::setSellerNo(int seller_no) { sellerNo_ = seller_no; } +void Seller::setSellerNo(int seller_no) { sellerNo_ = seller_no; } inline void Seller::setFirstName(const std::string& firstName) { firstName_ = firstName; } @@ -20,11 +20,15 @@ inline void Seller::setLastName(const std::string& lastName) { lastName_ = lastN inline void Seller::setNumberOfOfferedArticles(int number) { numberOfOfferedArticles_ = number; } -inline size_t Seller::getNumberOfOfferedArticles() { return articles_.size(); } +int Seller::getNumberOfOfferedArticles() const { return static_cast(articles_.size()); } -void Seller::addArticle(Article article) { - articles_.push_back(article); -} +void Seller::addArticle(Article article) { articles_.push_back(article); } + +std::string Seller::getFirstName() const { return firstName_; } + +std::string Seller::getLastName() const { return lastName_; } + +int Seller::getSellerNo() const { return sellerNo_; } std::vector Seller::getArticles(bool onlySold) { diff --git a/src/core/seller.h b/src/core/seller.h index 4ac6a98..eeeac29 100644 --- a/src/core/seller.h +++ b/src/core/seller.h @@ -22,8 +22,11 @@ class Seller : public Entity void setNumberOfOfferedArticles(int number); void addArticle(Article article); + std::string getFirstName() const; + std::string getLastName() const; + int getSellerNo() const; + int getNumberOfOfferedArticles() const; std::vector getArticles(bool onlySold = false); - size_t getNumberOfOfferedArticles(); private: int sellerNo_{-1};