update article in db

This commit is contained in:
Martin Brodbeck 2018-07-13 13:14:08 +02:00
parent 3be2418102
commit ba86af4064
1 changed files with 46 additions and 4 deletions

View File

@ -166,7 +166,8 @@ unsigned int Database::storeSellers(std::vector<std::shared_ptr<Seller>>& seller
throw std::runtime_error(sqlite3_errmsg(db_));
int test = sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, ":uuid"),
boost::uuids::to_string(seller->getUuid()).c_str(), -1, SQLITE_TRANSIENT);
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());
@ -202,7 +203,8 @@ unsigned int Database::storeSellers(std::vector<std::shared_ptr<Seller>>& seller
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);
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"),
@ -255,7 +257,8 @@ unsigned int Database::storeArticles(sqlite3_stmt* stmt, std::vector<Article*> a
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);
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);
@ -263,7 +266,7 @@ unsigned int Database::storeArticles(sqlite3_stmt* stmt, std::vector<Article*> a
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"),
sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, ":desc"),
article->getDescription().c_str(), -1, SQLITE_TRANSIENT);
sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":price"),
article->getPrice());
@ -280,6 +283,45 @@ unsigned int Database::storeArticles(sqlite3_stmt* stmt, std::vector<Article*> a
++count;
sqlite3_finalize(stmt);
} else if (article->getState() == Article::State::UPDATE) {
retCode = sqlite3_prepare_v2(
db_,
"UPDATE articles SET"
" seller_id = seller_id, source_no = :source_no, article_no = :article_no,"
" description = :desc, price = :price"
" 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(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, ":desc"),
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::DELETE) {
// TODO
}
}