OCDB: Function to get cache count for regions

sql-rework
Tomasz Golinski 2020-06-03 00:24:54 +02:00
rodzic fe071c6e67
commit 942c52b998
2 zmienionych plików z 51 dodań i 0 usunięć

Wyświetl plik

@ -407,3 +407,52 @@ void OCdb::set_revision(int rev) {
revision = rev;
request("UPDATE revision SET revision = " + std::to_string(revision) + ';');
}
std::map<std::string, int> OCdb::get_region_stats() {
std::map<std::string, int> count;
int res;
std::vector<std::string> regions = {
"dolnośląskie",
"śląskie",
"kujawsko-pomorskie",
"łódzkie",
"lubelskie",
"lubuskie",
"małopolskie",
"mazowieckie",
"opolskie",
"podkarpackie",
"podlaskie",
"pomorskie",
"świętokrzyskie",
"warmińsko-mazurskie",
"wielkopolskie",
"zachodniopomorskie"
};
//code TEXT PRIMARY KEY, name TEXT, location TEXT, type TEXT, status TEXT, size TEXT, difficulty REAL, terrain REAL, country TEXT, region TEXT, owner TEXT)
std::string sql = "SELECT COUNT(code) FROM caches WHERE status = 'Available' AND region = ?;";
res = sqlite3_prepare_v2(db, sql.c_str(), sql.length() + 1, &stmt, NULL);
if (res != SQLITE_OK) {
Debug(1) << "Request \"" << sql << "\" failed:\n"
<< sqlite3_errmsg(db);
throw 0;
}
for (auto reg : regions) {
sqlite3_bind_text(stmt, 1, reg.c_str(), -1, nullptr);
res = sqlite3_step(stmt);
if (res != SQLITE_ROW) {
Debug(1) << "Request \"" << sql << "\" failed:\n"
<< sqlite3_errmsg(db);
throw 0;
}
count[reg] = sqlite3_column_int(stmt, 0);
sqlite3_reset(stmt);
sqlite3_clear_bindings(stmt);
}
sqlite3_finalize(stmt);
return count;
}

2
ocdb.h
Wyświetl plik

@ -4,6 +4,7 @@
#include "api.h"
#include <string>
#include <map>
#include <nlohmann/json_fwd.hpp>
typedef struct sqlite3 sqlite3;
@ -36,4 +37,5 @@ public:
Caches get_user_caches_not_found(const std::string& uuid) const;
Caches get_user_caches(const std::string& uuid, int count = 0) const override;
std::map<std::string, int> get_region_stats();
};