Another run of clang-format

sql-rework
Tomasz Goliński 2019-11-04 21:44:08 +01:00
rodzic dc42212e31
commit b40b29de80
4 zmienionych plików z 53 dodań i 71 usunięć

4
api.h
Wyświetl plik

@ -15,7 +15,7 @@ public:
protected:
Position get_lat_lon(std::string loc) {
int pos = loc.find("|");
return {stof(loc.substr(0, pos)), stof(loc.substr(pos + 1))};
int pos = loc.find("|");
return { stof(loc.substr(0, pos)), stof(loc.substr(pos + 1)) };
}
};

Wyświetl plik

@ -13,7 +13,6 @@ using json = nlohmann::json;
const std::string Database = "ocpl.sqlite";
int main(int argc, char** argv) {
std::string Dump_path;
bool update = 0;
@ -24,7 +23,6 @@ int main(int argc, char** argv) {
case 'd':
try {
if (std::stoi(optarg) > 0) {
// Debug(1) << "Setting debug level to " << optarg;
Debug::set_debug_level(std::stoi(optarg));
}
}
@ -41,29 +39,6 @@ int main(int argc, char** argv) {
case 'u':
update = 1;
break;
// case 's':
// try {
// if (std::stoi(optarg) > 0) {
// heat_stamp_size = std::stoi(optarg);
// }
// }
// catch (...) {
// std::cout << "Option \"-s\" requires a valid number as an argument\n";
// std::exit(EXIT_FAILURE);
// }
// break;
// case 'm':
// heat_map = optarg;
// break;
// case 'e':
// heat_exp = 1;
// break;
// case 'L':
// show_list = 1;
// break;
// case 'T':
// show_dt = 1;
// break;
case 'h':
case '?':
default:
@ -80,7 +55,7 @@ int main(int argc, char** argv) {
db.init(Dump_path);
if (update) {
std::string ocpl_url = "https://opencaching.pl/okapi/";
#include "config_user_key.h"
#include "config_user_key.h"
Okapi OCpl(ocpl_url, ocpl_key);
db.update(OCpl);
}

Wyświetl plik

@ -20,7 +20,7 @@ OCdb::OCdb(std::string db_file) {
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 REAL, terrain REAL, country TEXT, region TEXT, owner TEXT);") ||
!request("CREATE TABLE IF NOT EXISTS caches (code TEXT PRIMARY KEY, name TEXT, location TEXT, type TEXT, status TEXT, size TEXT, difficulty REAL, terrain REAL, 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);"))
throw 1;
if (!read_revision()) {
@ -38,12 +38,14 @@ bool OCdb::request(std::string req) {
res = sqlite3_prepare_v2(db, req.c_str(), req.length() + 1, &stmt, NULL);
if (res != SQLITE_OK) {
Debug(1) << "Request \"" << req << "\" failed:\n" << sqlite3_errmsg(db);
Debug(1) << "Request \"" << req << "\" failed:\n"
<< sqlite3_errmsg(db);
return 0;
}
res = sqlite3_step(stmt);
if (res != SQLITE_DONE) {
Debug(1) << "Request \"" << req << "\" failed:\n" << sqlite3_errmsg(db);
Debug(1) << "Request \"" << req << "\" failed:\n"
<< sqlite3_errmsg(db);
return 0;
}
sqlite3_finalize(stmt);
@ -54,7 +56,7 @@ bool OCdb::init(std::string dump_path) {
request("DELETE FROM caches;");
request("DELETE FROM logs;");
request("DELETE FROM revision;");
std::ifstream file(dump_path + "index.json");
json j;
file >> j;
@ -71,7 +73,7 @@ bool OCdb::init(std::string dump_path) {
bool OCdb::init_part(std::string json_file) {
Debug(2) << "Processing file: " << json_file << '\n';
std::ifstream file(json_file);
json j;
file >> j;
@ -103,42 +105,36 @@ bool OCdb::parse_item(json j) {
if (j.count("object_type") > 0 && j["object_type"] == "geocache") {
if (j.count("change_type") > 0 && j["change_type"] == "replace") {
Debug(5) << "Inserting/updating cache " << j["object_key"]["code"].get<std::string>() << ".\n";
// if (j["object_key"]["code"] != j["data"]["code"]) {
// Debug(1) << "Code change " << j["object_key"]["code"] << " -> " << j["data"]["code"] <<'\n';
// }
// if (j["object_key"]["code"] != j["data"]["code"]) {
// Debug(1) << "Code change " << j["object_key"]["code"] << " -> " << j["data"]["code"] <<'\n';
// }
update_cache(j);
}
else if (j.count("change_type") > 0 && j["change_type"] == "delete") {
} else if (j.count("change_type") > 0 && j["change_type"] == "delete") {
Debug(2) << "Deleting cache " << j["object_key"]["code"].get<std::string>() << ".\n";
std::string sql = "DELETE FROM caches WHERE code='" + j["object_key"]["code"].get<std::string>() + "';";
request(sql);
}
else {
} else {
Debug(1) << "Incorrect change type: " << j["change_type"] << ".\n";
}
}
else if (j.count("object_type") > 0 && j["object_type"] == "log") {
} else if (j.count("object_type") > 0 && j["object_type"] == "log") {
if (j["change_type"] == "replace") {
Debug(3) << "Updating log " << j["object_key"]["uuid"] << ".\n";
update_log(j);
}
else if (j.count("change_type") > 0 && j["change_type"] == "delete") {
} else if (j.count("change_type") > 0 && j["change_type"] == "delete") {
Debug(2) << "Deleting log " << j["object_key"]["uuid"] << ".\n";
std::string sql = "DELETE FROM logs WHERE uuid='" + j["object_key"]["uuid"].get<std::string>() + "';";
request(sql);
}
else {
} else {
Debug(1) << "Incorrect change type: " << j["change_type"] << ".\n";
}
}
else {
} else {
Debug(1) << "Incorrect object type: " << j["object_type"] << ".\n";
}
return 1;
}
bool OCdb::update_cache(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 TEXT, size TEXT, difficulty REAL, terrain REAL, country TEXT, region TEXT, owner TEXT)
std::map<std::string, std::string> fields;
std::map<std::string, float> fields2;
int res;
@ -194,7 +190,8 @@ bool OCdb::update_cache(json j) {
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);
Debug(1) << "Request \"" << sql << "\" failed:\n"
<< sqlite3_errmsg(db);
return 0;
}
int n = 1;
@ -206,7 +203,8 @@ bool OCdb::update_cache(json j) {
}
res = sqlite3_step(stmt);
if (res != SQLITE_DONE) {
Debug(1) << "Request \"" << sql << "\" failed:\n" << sqlite3_errmsg(db);
Debug(1) << "Request \"" << sql << "\" failed:\n"
<< sqlite3_errmsg(db);
return 0;
}
sqlite3_finalize(stmt);
@ -214,7 +212,7 @@ bool OCdb::update_cache(json j) {
}
bool OCdb::update_log(json j) {
// logs (uuid TEXT PRIMARY KEY, cache_code TEXT, date TEXT, user TEXT, type TEXT);"))
// logs (uuid TEXT PRIMARY KEY, cache_code TEXT, date TEXT, user TEXT, type TEXT);"))
std::map<std::string, std::string> fields;
int res;
@ -247,7 +245,8 @@ bool OCdb::update_log(json j) {
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);
Debug(1) << "Request \"" << sql << "\" failed:\n"
<< sqlite3_errmsg(db);
return 0;
}
int n = 1;
@ -256,7 +255,8 @@ bool OCdb::update_log(json j) {
}
res = sqlite3_step(stmt);
if (res != SQLITE_DONE) {
Debug(1) << "Request \"" << sql << "\" failed:\n" << sqlite3_errmsg(db);
Debug(1) << "Request \"" << sql << "\" failed:\n"
<< sqlite3_errmsg(db);
return 0;
}
sqlite3_finalize(stmt);
@ -269,12 +269,14 @@ bool OCdb::read_revision() {
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);
Debug(1) << "Request \"" << sql << "\" failed:\n"
<< sqlite3_errmsg(db);
return 0;
}
res = sqlite3_step(stmt);
if (res != SQLITE_ROW) {
Debug(1) << "Request \"" << sql << "\" failed:\n" << sqlite3_errmsg(db);
Debug(1) << "Request \"" << sql << "\" failed:\n"
<< sqlite3_errmsg(db);
return 0;
}
revision = sqlite3_column_int(stmt, 0);
@ -293,7 +295,8 @@ Caches OCdb::get_user_caches_not_found(std::string uuid) {
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);
Debug(1) << "Request \"" << sql << "\" failed:\n"
<< sqlite3_errmsg(db);
throw 0;
}
sqlite3_bind_text(stmt, 1, uuid.c_str(), -1, nullptr);
@ -306,8 +309,9 @@ Caches OCdb::get_user_caches_not_found(std::string uuid) {
res = sqlite3_step(stmt);
}
if (res != SQLITE_DONE) {
Debug(1) << "Request \"" << sql << "\" failed:\n" << sqlite3_errmsg(db);
if (res != SQLITE_DONE) {
Debug(1) << "Request \"" << sql << "\" failed:\n"
<< sqlite3_errmsg(db);
throw 0;
}
sqlite3_finalize(stmt);
@ -318,12 +322,13 @@ Caches OCdb::get_user_caches(std::string uuid, __attribute__((unused)) int count
int res;
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)
//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 = ?);";
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);
Debug(1) << "Request \"" << sql << "\" failed:\n"
<< sqlite3_errmsg(db);
throw 0;
}
sqlite3_bind_text(stmt, 1, uuid.c_str(), -1, nullptr);
@ -332,22 +337,23 @@ Caches OCdb::get_user_caches(std::string uuid, __attribute__((unused)) int count
while (res == SQLITE_ROW) {
c.code = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0));
c.pos = get_lat_lon(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1)));
c.type = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 2));
c.size = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 3));
c.diff = sqlite3_column_double(stmt, 4);
c.terr = sqlite3_column_double(stmt, 5);
// c.country = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 6));
c.region = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 7));
c.owner_uuid = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 8)); // TODO: we don't know owner's nick
c.type = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 2));
c.size = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 3));
c.diff = sqlite3_column_double(stmt, 4);
c.terr = sqlite3_column_double(stmt, 5);
//c.country = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 6));
c.region = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 7));
c.owner_uuid = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 8)); // TODO: we don't know owner's nick
cc.insert(c);
res = sqlite3_step(stmt);
}
if (res != SQLITE_DONE) {
Debug(1) << "Request \"" << sql << "\" failed:\n" << sqlite3_errmsg(db);
if (res != SQLITE_DONE) {
Debug(1) << "Request \"" << sql << "\" failed:\n"
<< sqlite3_errmsg(db);
throw 0;
}
// sqlite3_finalize(stmt);
//sqlite3_finalize(stmt);
return cc;
}

1
ocdb.h
Wyświetl plik

@ -24,6 +24,7 @@ private:
bool update_cache(json j);
bool update_log(json j);
bool read_revision();
public:
OCdb(std::string db_file);
~OCdb();