SQL: change cache status to integer using pre-existing enum

master
Tomasz Golinski 2020-09-14 19:31:01 +02:00
rodzic e23fdcfcb8
commit 6aa8e1426e
1 zmienionych plików z 18 dodań i 21 usunięć

Wyświetl plik

@ -55,7 +55,7 @@ bool OCdb::init(const std::string& dump_path) {
throw 1;
if (!request("CREATE TABLE IF NOT EXISTS revision (revision INTEGER PRIMARY KEY);") ||
!request("CREATE TABLE IF NOT EXISTS caches (code TEXT PRIMARY KEY, name TEXT, location TEXT, type TEXT, status TEXT, size TEXT, difficulty INTEGER, terrain INTEGER, country TEXT, region TEXT, owner TEXT);") ||
!request("CREATE TABLE IF NOT EXISTS caches (code TEXT PRIMARY KEY, name TEXT, location TEXT, type TEXT, status INTEGER, size TEXT, difficulty INTEGER, terrain INTEGER, country TEXT, region TEXT, owner TEXT);") ||
!request("CREATE TABLE IF NOT EXISTS logs (uuid TEXT PRIMARY KEY, cache_code TEXT, date TEXT, user TEXT, type INTEGER);") ||
!request("CREATE INDEX idx_user on logs (user, type);"))
throw 1;
@ -142,7 +142,7 @@ bool OCdb::parse_item(const json& j) {
}
bool OCdb::update_cache(const json& j) {
// (code TEXT PRIMARY KEY, name TEXT, location TEXT, type TEXT, status TEXT, size TEXT, difficulty REAL, terrain REAL, country TEXT, region TEXT, owner TEXT)
// (code TEXT PRIMARY KEY, name TEXT, location TEXT, type TEXT, status INTEGER, size TEXT, difficulty REAL, terrain REAL, country TEXT, region TEXT, owner TEXT)
std::map<std::string, std::string> fields;
std::map<std::string, short> fields2;
int res;
@ -158,8 +158,13 @@ bool OCdb::update_cache(const json& j) {
fields["location"] = j["data"]["location"].get<std::string>();
if (j["data"].count("type") && !j["data"]["type"].is_null())
fields["type"] = j["data"]["type"].get<std::string>();
if (j["data"].count("status") && !j["data"]["status"].is_null())
fields["status"] = j["data"]["status"].get<std::string>();
if (j["data"].count("status") && !j["data"]["status"].is_null()) {
std::string tmp = j["data"]["status"].get<std::string>();
if (tmp == "Available") fields2["status"] = ok;
else if (tmp == "Archived") fields2["status"] = archived;
else if (tmp == "Temporarily unavailable") fields2["status"] = disabled;
else fields2["status"] = unknown;
}
if (j["data"].count("size2") && !j["data"]["size2"].is_null())
fields["size"] = j["data"]["size2"].get<std::string>();
if (j["data"].count("difficulty") && !j["data"]["difficulty"].is_null())
@ -315,7 +320,9 @@ Caches OCdb::get_user_caches_not_found(const std::string& uuid) const {
int res;
Caches cc;
sql = "SELECT code, location, region FROM caches WHERE status = 'Available' AND NOT EXISTS (SELECT cache_code FROM logs WHERE cache_code = code AND type = 1 and user = ?1) AND owner != ?1;";
sql = "SELECT code, location, region FROM caches WHERE status = ";
sql += ok;
sql += " AND NOT EXISTS (SELECT cache_code FROM logs WHERE cache_code = code AND type = 1 and user = ?1) AND owner != ?1;";
res = sqlite3_prepare_v2(db, sql.c_str(), sql.length() + 1, &stmt, NULL);
if (res != SQLITE_OK) {
@ -348,7 +355,7 @@ Caches OCdb::get_user_caches_not_found(const std::string& uuid) const {
Caches OCdb::get_user_caches(const std::string& uuid, __attribute__((unused)) int count) const {
int res;
//code TEXT PRIMARY KEY, name TEXT, location TEXT, type TEXT, status TEXT, size TEXT, difficulty REAL, terrain REAL, country TEXT, region TEXT, owner TEXT)
//code TEXT PRIMARY KEY, name TEXT, location TEXT, type TEXT, status INTEGER, size TEXT, difficulty REAL, terrain REAL, country TEXT, region TEXT, owner TEXT)
sql = "SELECT code, location, type, size, difficulty, terrain, country, region, owner, status, name FROM caches WHERE EXISTS (SELECT cache_code FROM logs WHERE cache_code = code AND type = 1 and user = ?);";
res = sqlite3_prepare_v2(db, sql.c_str(), sql.length() + 1, &stmt, NULL);
@ -393,21 +400,9 @@ Caches OCdb::parse_sql_caches() const {
//c.country = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 6));
if (sqlite3_column_text(stmt, 7)) c.region = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 7));
if (sqlite3_column_text(stmt, 8)) c.owner_uuid = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 8)); // TODO: we don't know owner's nick
if (sqlite3_column_text(stmt, 9)) c.status = static_cast<Status>(sqlite3_column_int(stmt, 9));
if (sqlite3_column_text(stmt, 10)) c.name = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 10));
if (sqlite3_column_text(stmt, 9)) {
std::string tmp = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 9));
if (tmp == "Available")
c.status = ok;
else if (tmp == "Temporarily unavailable")
c.status = disabled;
else if (tmp == "Archived")
c.status = archived;
else
c.status = unknown;
} else
c.status = unknown;
cc.push_back(c);
res = sqlite3_step(stmt);
}
@ -452,8 +447,10 @@ std::map<std::string, int> OCdb::get_region_stats() {
"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)
sql = "SELECT COUNT(code) FROM caches WHERE status = 'Available' AND region = ?;";
//code TEXT PRIMARY KEY, name TEXT, location TEXT, type TEXT, status INTEGER, size TEXT, difficulty REAL, terrain REAL, country TEXT, region TEXT, owner TEXT)
sql = "SELECT COUNT(code) FROM caches WHERE status = ";
sql += ok;
sql += " AND region = ?;";
res = sqlite3_prepare_v2(db, sql.c_str(), sql.length() + 1, &stmt, NULL);
if (res != SQLITE_OK) {