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