Store type of log as an integer value in SQLite. 1 for found, 0 for not found, -1 for other

sql-rework
Tomasz Golinski 2020-03-02 21:55:19 +01:00
rodzic 40a8143338
commit 92bfb82eb2
1 zmienionych plików z 19 dodań i 6 usunięć

Wyświetl plik

@ -56,7 +56,7 @@ bool OCdb::init(const std::string& dump_path) {
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 logs (uuid TEXT PRIMARY KEY, cache_code TEXT, date TEXT, user TEXT, type 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;
request("COMMIT;");
@ -222,6 +222,7 @@ bool OCdb::update_cache(const json& j) {
bool OCdb::update_log(const json& j) {
// logs (uuid TEXT PRIMARY KEY, cache_code TEXT, date TEXT, user TEXT, type TEXT);"))
std::map<std::string, std::string> fields;
std::map<std::string, short> fields2;
int res;
std::string uuid = j["object_key"]["uuid"].get<std::string>();
@ -231,19 +232,28 @@ bool OCdb::update_log(const json& j) {
fields["date"] = j["data"]["date"].get<std::string>();
if (j["data"]["user"].count("uuid") > 0 && !j["data"]["user"]["uuid"].is_null())
fields["user"] = j["data"]["user"]["uuid"].get<std::string>();
if (j["data"].count("type") > 0 && !j["data"]["type"].is_null())
fields["type"] = j["data"]["type"].get<std::string>();
if (j["data"].count("type") > 0 && !j["data"]["type"].is_null()) {
if (j["data"]["type"].get<std::string>() == "Didn't find it")
fields2["type"] = 0;
else if (j["data"]["type"].get<std::string>() == "Found it")
fields2["type"] = 1;
else
fields2["type"] = -1;
}
if (fields.empty())
return 1;
std::string sql = "INSERT INTO logs (uuid,";
for (auto& i : fields)
sql += i.first + ',';
for (auto& i : fields2)
sql += i.first + ',';
sql.pop_back();
sql += ") VALUES ('" + uuid + "',";
for (__attribute__((unused)) auto& i : fields)
sql += "?,";
for (__attribute__((unused)) auto& i : fields2)
sql += "?,";
sql.pop_back();
sql += ") ON CONFLICT(uuid) DO UPDATE SET ";
for (auto& i : fields)
@ -261,6 +271,9 @@ bool OCdb::update_log(const json& j) {
for (auto& i : fields) {
sqlite3_bind_text(stmt, n++, i.second.c_str(), -1, nullptr);
}
for (auto& i : fields2) {
sqlite3_bind_int(stmt, n++, i.second);
}
res = sqlite3_step(stmt);
if (res != SQLITE_DONE) {
Debug(1) << "Request \"" << sql << "\" failed:\n"
@ -299,7 +312,7 @@ Caches OCdb::get_user_caches_not_found(const std::string& uuid) const {
Caches cc;
Cache c;
std::string sql = "SELECT code, location FROM caches WHERE status = 'Available' AND NOT EXISTS (SELECT cache_code FROM logs WHERE cache_code = code AND type = 'Found it' and user = ?);";
std::string sql = "SELECT code, location FROM caches WHERE status = 'Available' AND NOT 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);
if (res != SQLITE_OK) {
@ -331,7 +344,7 @@ Caches OCdb::get_user_caches(const std::string& uuid, __attribute__((unused)) in
Caches cc;
Cache c;
//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 code, location, type, size, difficulty, terrain, country, region, owner FROM caches WHERE EXISTS (SELECT cache_code FROM logs WHERE cache_code = code AND type = 'Found it' and user = ?);";
std::string sql = "SELECT code, location, type, size, difficulty, terrain, country, region, owner 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);
if (res != SQLITE_OK) {