drastically improve performance for loading sales
This commit is contained in:
parent
8f5797bbfb
commit
56931c2e27
1 changed files with 23 additions and 25 deletions
|
@ -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,36 +624,32 @@ 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;
|
||||||
retCode = sqlite3_prepare_v2(db_,
|
for (const auto& seller : sellers) {
|
||||||
"SELECT sale_id, article_id"
|
for (const auto article : seller->getArticles(false)) {
|
||||||
" FROM sales_items"
|
artMap.insert(std::make_pair(article->getUuidAsString(), article));
|
||||||
" WHERE sale_id = :sale_uuid",
|
}
|
||||||
-1, &stmt, nullptr);
|
}
|
||||||
if (retCode != SQLITE_OK)
|
|
||||||
throw std::runtime_error(sqlite3_errmsg(db_));
|
|
||||||
|
|
||||||
sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, ":sale_uuid"),
|
retCode = sqlite3_prepare_v2(db_,
|
||||||
sale->getUuidAsString().c_str(), -1, SQLITE_TRANSIENT);
|
"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);
|
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;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue