drastically improve performance for loading sales

This commit is contained in:
Martin Brodbeck 2018-08-10 15:15:29 +02:00
parent 8f5797bbfb
commit 56931c2e27

View file

@ -608,6 +608,7 @@ unsigned int Database::loadSales(std::vector<std::unique_ptr<Sale>>& sales,
sales.clear();
std::map<std::string, Sale*> saleMap;
while (retCode != SQLITE_DONE) {
++count;
auto sale = std::make_unique<Sale>();
@ -615,6 +616,7 @@ unsigned int Database::loadSales(std::vector<std::unique_ptr<Sale>>& sales,
sale->setSourceNo(sqlite3_column_int(stmt, 1));
sale->setTimestamp(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 2)));
sale->setState(Sale::State::OK);
saleMap.insert(std::make_pair(sale->getUuidAsString(), sale.get()));
sales.push_back(std::move(sale));
retCode = sqlite3_step(stmt);
@ -622,36 +624,32 @@ unsigned int Database::loadSales(std::vector<std::unique_ptr<Sale>>& sales,
sqlite3_finalize(stmt);
for (auto& sale : sales) {
retCode = sqlite3_prepare_v2(db_,
"SELECT sale_id, article_id"
" FROM sales_items"
" WHERE sale_id = :sale_uuid",
-1, &stmt, nullptr);
if (retCode != SQLITE_OK)
throw std::runtime_error(sqlite3_errmsg(db_));
std::map<std::string, Article*> artMap;
for (const auto& seller : sellers) {
for (const auto article : seller->getArticles(false)) {
artMap.insert(std::make_pair(article->getUuidAsString(), article));
}
}
sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, ":sale_uuid"),
sale->getUuidAsString().c_str(), -1, SQLITE_TRANSIENT);
retCode = sqlite3_prepare_v2(db_,
"SELECT sale_id, article_id"
" FROM sales_items",
-1, &stmt, nullptr);
if (retCode != SQLITE_OK)
throw std::runtime_error(sqlite3_errmsg(db_));
retCode = sqlite3_step(stmt);
while (retCode != SQLITE_DONE) {
saleMap[reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0))]->addArticle(
artMap[reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1))]);
retCode = sqlite3_step(stmt);
while (retCode != SQLITE_DONE) {
for (auto& seller : sellers) {
auto article = seller->getArticleByUuid(
reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1)));
if (article != nullptr) {
sale->addArticle(article);
break;
}
}
retCode = sqlite3_step(stmt);
}
sqlite3_finalize(stmt);
}
sqlite3_finalize(stmt);
//}
return count;
}