load sellers

This commit is contained in:
Martin Brodbeck 2018-07-17 11:09:35 +02:00
parent 300c883fdc
commit 90e132bcc8
4 changed files with 43 additions and 4 deletions

View file

@ -399,3 +399,39 @@ unsigned int Database::storeArticles(std::vector<Article*> articles)
return count; return count;
} }
unsigned int Database::loadSellers(std::vector<std::unique_ptr<Seller>>& sellers)
{
int retCode{};
int count{};
sqlite3_stmt* stmt;
retCode = sqlite3_prepare_v2(db_,
"SELECT id, seller_no, first_name, last_name, "
"num_offered_articles FROM sellers ORDER BY seller_no",
-1, &stmt, nullptr);
if (retCode != SQLITE_OK)
throw std::runtime_error(sqlite3_errmsg(db_));
retCode = sqlite3_step(stmt);
sellers.clear();
while (retCode != SQLITE_DONE) {
++count;
auto seller = std::make_unique<Seller>();
seller->setUuidFromString(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)));
seller->setSellerNo(sqlite3_column_int(stmt, 1));
seller->setFirstName(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 2)));
seller->setLastName(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 3)));
seller->setNumArticlesOffered(sqlite3_column_int(stmt, 4));
seller->setState(Seller::State::OK);
sellers.push_back(std::move(seller));
retCode = sqlite3_step(stmt);
}
sqlite3_finalize(stmt);
return count;
}

View file

@ -17,6 +17,7 @@ class Database
Database& operator=(const Database&) = delete; Database& operator=(const Database&) = delete;
void exec(const std::string& sql); void exec(const std::string& sql);
unsigned int storeSellers(std::vector<std::unique_ptr<Seller>>& sellers); unsigned int storeSellers(std::vector<std::unique_ptr<Seller>>& sellers);
unsigned int loadSellers(std::vector<std::unique_ptr<Seller>>& sellers);
private: private:
sqlite3* db_{nullptr}; sqlite3* db_{nullptr};

View file

@ -12,13 +12,15 @@ Marketplace::Marketplace()
void Marketplace::storeToDb() void Marketplace::storeToDb()
{ {
//const std::string DB_PATH{"/tmp/kima2.db"};
Database db; Database db;
db.storeSellers(sellers_); db.storeSellers(sellers_);
} }
void Marketplace::loadFromDb() {} void Marketplace::loadFromDb()
{
Database db;
db.loadSellers(sellers_);
}
std::vector<std::unique_ptr<Seller>>& Marketplace::getSellers() { return sellers_; } std::vector<std::unique_ptr<Seller>>& Marketplace::getSellers() { return sellers_; }

View file

@ -27,7 +27,7 @@ void MainWindow::on_actionEditSeller_triggered()
STATUSBAR_TIMEOUT); STATUSBAR_TIMEOUT);
} else { } else {
marketplace_->loadFromDb(); marketplace_->loadFromDb();
statusBar()->showMessage("Änderungen an den Verkäufer-Stammdaten <b>verworfen</b>.", statusBar()->showMessage("Änderungen an den Verkäufer-Stammdaten verworfen!",
STATUSBAR_TIMEOUT); STATUSBAR_TIMEOUT);
} }
} }