From c7d8eddf86af6fbc5c008e6aecdb279825de6cc6 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Fri, 13 Jul 2018 13:05:19 +0200 Subject: [PATCH] now storing and updateing sellers, storing articles --- src/core/database.cpp | 110 ++++++++++++++++++++++++++++++++++++++---- src/core/database.h | 10 ++-- 2 files changed, 107 insertions(+), 13 deletions(-) diff --git a/src/core/database.cpp b/src/core/database.cpp index 43c82c3..e7b4168 100644 --- a/src/core/database.cpp +++ b/src/core/database.cpp @@ -145,7 +145,7 @@ void Database::beginTransaction() { exec("BEGIN TRANSACTION"); } void Database::endTransaction() { exec("END TRANSACTION"); } -unsigned int Database::storeSellers(std::vector& sellers) +unsigned int Database::storeSellers(std::vector>& sellers) { int retCode{}; int count{}; @@ -154,7 +154,7 @@ unsigned int Database::storeSellers(std::vector& sellers) beginTransaction(); for (auto& seller : sellers) { - if (seller.getState() == Entity::State::NEW) { + if (seller->getState() == Seller::State::NEW) { retCode = sqlite3_prepare_v2( db_, "INSERT INTO sellers" @@ -165,16 +165,17 @@ unsigned int Database::storeSellers(std::vector& sellers) 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); + int test = sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, ":uuid"), + boost::uuids::to_string(seller->getUuid()).c_str(), -1, SQLITE_TRANSIENT); + std::cout << "!!! TEST: " << test << "\n"; sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":seller_no"), - seller.getSellerNo()); + seller->getSellerNo()); sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, ":first_name"), - seller.getFirstName().c_str(), -1, nullptr); + seller->getFirstName().c_str(), -1, SQLITE_TRANSIENT); sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, ":last_name"), - seller.getLastName().c_str(), -1, nullptr); + seller->getLastName().c_str(), -1, SQLITE_TRANSIENT); sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":num_offered_articles"), - seller.numArticlesOffered()); + seller->numArticlesOffered()); retCode = sqlite3_step(stmt); @@ -184,12 +185,103 @@ unsigned int Database::storeSellers(std::vector& sellers) sqlite3_finalize(stmt); throw std::runtime_error(errMsg); } - seller.setState(Seller::State::OK); + seller->setState(Seller::State::OK); ++count; sqlite3_finalize(stmt); + } else if (seller->getState() == Seller::State::UPDATE) { + // TODO + retCode = sqlite3_prepare_v2( + db_, + "UPDATE sellers SET" + " seller_no = :seller_no, first_name = :first_name," + " last_name = :last_name, num_offered_articles = :num_offered_articles" + " WHERE id = :uuid", + -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, SQLITE_TRANSIENT); + 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, SQLITE_TRANSIENT); + sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, ":last_name"), + seller->getLastName().c_str(), -1, SQLITE_TRANSIENT); + sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":num_offered_articles"), + seller->numArticlesOffered()); + + 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::OK); + ++count; + sqlite3_finalize(stmt); + } else if (seller->getState() == Seller::State::DELETE) { + count += static_cast(seller->getArticles(false).size()); + // TODO + } + + if (seller->getState() != Seller::State::DELETE) { + count += storeArticles(stmt, seller->getArticles(false)); } } endTransaction(); + return count; +} + +unsigned int Database::storeArticles(sqlite3_stmt* stmt, std::vector articles) +{ + int retCode{}; + int count{}; + + for (auto& article : articles) { + if (article->getState() == Article::State::NEW) { + retCode = sqlite3_prepare_v2( + db_, + "INSERT INTO articles" + " (id, seller_id, source_no, article_no, description, price)" + " VALUES (:uuid, :seller_id, :source_no, :article_no, :desc, :price)", + -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(article->getUuid()).c_str(), -1, SQLITE_TRANSIENT); + sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, ":seller_id"), + boost::uuids::to_string(article->getSeller()->getUuid()).c_str(), -1, + SQLITE_TRANSIENT); + sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":source_no"), + article->getSourceNo()); + sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":article_no"), + article->getArticleNo()); + sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, ":desctiption"), + article->getDescription().c_str(), -1, SQLITE_TRANSIENT); + sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":price"), + article->getPrice()); + + retCode = sqlite3_step(stmt); + + if (retCode != SQLITE_DONE) { + + std::string errMsg(sqlite3_errmsg(db_)); + sqlite3_finalize(stmt); + throw std::runtime_error(errMsg); + } + article->setState(Seller::State::OK); + ++count; + sqlite3_finalize(stmt); + } else if (article->getState() == Article::State::UPDATE) { + } + } + return count; } \ No newline at end of file diff --git a/src/core/database.h b/src/core/database.h index 83fe27c..447fdb4 100644 --- a/src/core/database.h +++ b/src/core/database.h @@ -9,21 +9,23 @@ class Database { -public: + public: Database(const std::string& dbname); ~Database(); Database(const Database&) = delete; Database& operator=(const Database&) = delete; void exec(const std::string& sql); void init(); - unsigned int storeSellers(std::vector& sellers); -private: - sqlite3 *db_; + unsigned int storeSellers(std::vector>& sellers); + + private: + sqlite3* db_; std::string dbname_; void beginTransaction(); void endTransaction(); void createNew(); int getVersion(); + unsigned int storeArticles(sqlite3_stmt* stmt, std::vector articles); }; #endif // DATABASE_H \ No newline at end of file