insert/update/delete completed

This commit is contained in:
Martin Brodbeck 2018-07-13 13:58:32 +02:00
parent 9ec6ac383f
commit 78e7dc772b

View file

@ -186,11 +186,9 @@ unsigned int Database::storeSellers(std::vector<std::shared_ptr<Seller>>& seller
sqlite3_finalize(stmt); sqlite3_finalize(stmt);
throw std::runtime_error(errMsg); throw std::runtime_error(errMsg);
} }
seller->setState(Seller::State::OK);
++count; ++count;
sqlite3_finalize(stmt); sqlite3_finalize(stmt);
} else if (seller->getState() == Seller::State::UPDATE) { } else if (seller->getState() == Seller::State::UPDATE) {
// TODO
retCode = sqlite3_prepare_v2( retCode = sqlite3_prepare_v2(
db_, db_,
"UPDATE sellers SET" "UPDATE sellers SET"
@ -222,12 +220,31 @@ unsigned int Database::storeSellers(std::vector<std::shared_ptr<Seller>>& seller
sqlite3_finalize(stmt); sqlite3_finalize(stmt);
throw std::runtime_error(errMsg); throw std::runtime_error(errMsg);
} }
seller->setState(Seller::State::OK);
++count; ++count;
sqlite3_finalize(stmt); sqlite3_finalize(stmt);
} else if (seller->getState() == Seller::State::DELETE) { } else if (seller->getState() == Seller::State::DELETE) {
count += static_cast<int>(seller->getArticles(false).size()); count += static_cast<int>(seller->getArticles(false).size());
// TODO
retCode =
sqlite3_prepare_v2(db_, "DELETE FROM sellers 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);
retCode = sqlite3_step(stmt);
if (retCode != SQLITE_DONE) {
std::string errMsg(sqlite3_errmsg(db_));
sqlite3_finalize(stmt);
throw std::runtime_error(errMsg);
}
++count;
sqlite3_finalize(stmt);
} }
if (seller->getState() != Seller::State::DELETE) { if (seller->getState() != Seller::State::DELETE) {
@ -236,6 +253,17 @@ unsigned int Database::storeSellers(std::vector<std::shared_ptr<Seller>>& seller
} }
endTransaction(); endTransaction();
// Everything went fine, so we can now update our objects
sellers.erase(
std::remove_if(begin(sellers), end(sellers), [](const std::shared_ptr<Seller>& seller) {
return seller->getState() == Seller::State::DELETE;
}));
for (const auto& seller : sellers) {
seller->cleanupArticles();
seller->setState(Seller::State::OK);
}
return count; return count;
} }
@ -279,7 +307,6 @@ unsigned int Database::storeArticles(sqlite3_stmt* stmt, std::vector<Article*> a
sqlite3_finalize(stmt); sqlite3_finalize(stmt);
throw std::runtime_error(errMsg); throw std::runtime_error(errMsg);
} }
article->setState(Seller::State::OK);
++count; ++count;
sqlite3_finalize(stmt); sqlite3_finalize(stmt);
} else if (article->getState() == Article::State::UPDATE) { } else if (article->getState() == Article::State::UPDATE) {
@ -317,11 +344,29 @@ unsigned int Database::storeArticles(sqlite3_stmt* stmt, std::vector<Article*> a
sqlite3_finalize(stmt); sqlite3_finalize(stmt);
throw std::runtime_error(errMsg); throw std::runtime_error(errMsg);
} }
article->setState(Seller::State::OK);
++count; ++count;
sqlite3_finalize(stmt); sqlite3_finalize(stmt);
} else if (article->getState() == Article::State::DELETE) { } else if (article->getState() == Article::State::DELETE) {
// TODO retCode = sqlite3_prepare_v2(db_, "DELETE FROM 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(article->getUuid()).c_str(), -1,
SQLITE_TRANSIENT);
retCode = sqlite3_step(stmt);
if (retCode != SQLITE_DONE) {
std::string errMsg(sqlite3_errmsg(db_));
sqlite3_finalize(stmt);
throw std::runtime_error(errMsg);
}
++count;
sqlite3_finalize(stmt);
} }
} }