Compare commits

..

No commits in common. "b2650fad269a2a1e4171da4e2ae18da080c44dc1" and "f42855dc501e28ac681c0e1408c59b175dfca97a" have entirely different histories.

7 changed files with 12 additions and 115 deletions

View file

@ -41,7 +41,7 @@ void Database::createNew()
"seller_no INTEGER, " "seller_no INTEGER, "
"first_name TEXT, " "first_name TEXT, "
"last_name TEXT, " "last_name TEXT, "
"num_offered_articles INTEGER, " "offered_articles INTEGER, "
"UNIQUE (seller_no)" "UNIQUE (seller_no)"
");"}; ");"};
sqlStrings.push_back(sqlCreateSellers); sqlStrings.push_back(sqlCreateSellers);
@ -144,52 +144,3 @@ int Database::getVersion()
void Database::beginTransaction() { exec("BEGIN TRANSACTION"); } void Database::beginTransaction() { exec("BEGIN TRANSACTION"); }
void Database::endTransaction() { exec("END TRANSACTION"); } void Database::endTransaction() { exec("END TRANSACTION"); }
unsigned int Database::storeSellers(std::vector<Seller>& 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;
}

View file

@ -1,8 +1,6 @@
#ifndef DATABASE_H #ifndef DATABASE_H
#define DATABASE_H #define DATABASE_H
#include "seller.h"
#include <string> #include <string>
#include <sqlite3.h> #include <sqlite3.h>
@ -16,7 +14,6 @@ public:
Database& operator=(const Database&) = delete; Database& operator=(const Database&) = delete;
void exec(const std::string& sql); void exec(const std::string& sql);
void init(); void init();
unsigned int storeSellers(std::vector<Seller>& sellers);
private: private:
sqlite3 *db_; sqlite3 *db_;
std::string dbname_; std::string dbname_;

View file

@ -19,7 +19,7 @@ void Entity::createUuidFromString(const std::string& uuidString)
uuid_ = generator(uuidString); uuid_ = generator(uuidString);
} }
inline Entity::State Entity::getState() const inline Entity::State Entity::getState()
{ {
return state_; return state_;
} }

View file

@ -4,19 +4,17 @@
#include <string> #include <string>
#include <boost/uuid/uuid.hpp> #include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_io.hpp>
class Entity class Entity
{ {
public: public:
enum class State { NEW, UPDATED, CLEAN }; enum class State { NEW, UPDATED, READ };
// Entity();
virtual ~Entity() = 0; virtual ~Entity() = 0;
const boost::uuids::uuid& getUuid() const { return uuid_; }; const boost::uuids::uuid& getUuid() const { return uuid_; };
void createUuid(); void createUuid();
void createUuidFromString(const std::string& uuidString); void createUuidFromString(const std::string& uuidString);
virtual State getState() const; State getState();
void setState(State state) { state_ = state; }
private: private:
boost::uuids::uuid uuid_{}; boost::uuids::uuid uuid_{};

View file

@ -12,7 +12,7 @@ Seller::Seller(const std::string& firstName, const std::string& lastName, int se
numberOfOfferedArticles_ = numberOfArticles; numberOfOfferedArticles_ = numberOfArticles;
} }
void Seller::setSellerNo(int seller_no) { sellerNo_ = seller_no; } inline void Seller::setSellerNo(int seller_no) { sellerNo_ = seller_no; }
inline void Seller::setFirstName(const std::string& firstName) { firstName_ = firstName; } inline void Seller::setFirstName(const std::string& firstName) { firstName_ = firstName; }
@ -20,15 +20,11 @@ inline void Seller::setLastName(const std::string& lastName) { lastName_ = lastN
inline void Seller::setNumberOfOfferedArticles(int number) { numberOfOfferedArticles_ = number; } inline void Seller::setNumberOfOfferedArticles(int number) { numberOfOfferedArticles_ = number; }
int Seller::getNumberOfOfferedArticles() const { return static_cast<int>(articles_.size()); } inline size_t Seller::getNumberOfOfferedArticles() { return 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<Article*> Seller::getArticles(bool onlySold) std::vector<Article*> Seller::getArticles(bool onlySold)
{ {

View file

@ -22,11 +22,8 @@ class Seller : public Entity
void setNumberOfOfferedArticles(int number); void setNumberOfOfferedArticles(int number);
void addArticle(Article article); void addArticle(Article article);
std::string getFirstName() const;
std::string getLastName() const;
int getSellerNo() const;
int getNumberOfOfferedArticles() const;
std::vector<Article*> getArticles(bool onlySold = false); std::vector<Article*> getArticles(bool onlySold = false);
size_t getNumberOfOfferedArticles();
private: private:
int sellerNo_{-1}; int sellerNo_{-1};

View file

@ -1,7 +1,6 @@
#define BOOST_TEST_MODULE database #define BOOST_TEST_MODULE database
#include "../src/core/database.h" #include "../src/core/database.h"
#include "../src/core/seller.h"
#include <filesystem> #include <filesystem>
@ -12,46 +11,5 @@ BOOST_AUTO_TEST_CASE(create_database)
Database db(":memory:"); Database db(":memory:");
BOOST_CHECK_NO_THROW(db.init()); BOOST_CHECK_NO_THROW(db.init());
}
BOOST_AUTO_TEST_CASE(store_seller_fail)
{
Database db(":memory:");
db.init();
std::vector<Seller> 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<Seller> 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<Seller> 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);
} }