-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVEXSQL.cpp
More file actions
71 lines (63 loc) · 2.21 KB
/
VEXSQL.cpp
File metadata and controls
71 lines (63 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "VEXSQL.hpp"
#include "ViaAPI.hpp"
#include <vector>
std::vector<std::string> split(std::string s, const std::string& delimiter) {
std::vector<std::string> ret;
size_t pos = 0;
while ((pos = s.find(delimiter)) != std::string::npos) {
ret.push_back(s.substr(0, pos));
s.erase(0, pos + delimiter.length());
}
if(!s.empty()) ret.push_back(s);
return ret;
}
void ScopedSQLite3::executeScript(const std::string& data) {
char* err = nullptr;
for(auto& line: split(data, ";\n")) {
if(line.empty()) continue;
sqlite3_exec(ptr, line.c_str(), nullptr, nullptr, &err);
sqlite3_cpp_error(err);
}
}
void ScopedSQLite3::exec(const char* query) {
char* err = nullptr;
sqlite3_exec(ptr, query, nullptr, nullptr, &err);
sqlite3_cpp_error(err);
}
bool ScopedSQLite3::applyEndpoint(const std::string &endpoint, int timeout) {
std::string downloadedData;
bool gotGoodResponse = false;
do {
std::cerr << "Downloading SQL data..." << std::endl;
downloadedData = apiCall(endpoint, getLastModified(), getSchema(), timeout);
if(!downloadedData.empty()) gotGoodResponse = true;
std::cerr << "Executing SQL script..." << std::endl;
executeScript(downloadedData);
} while(!downloadedData.empty());
return gotGoodResponse;
}
int ScopedSQLite3::getQueryWithIntResult(const char* query) {
int ret = 0;
exec_lambda(query, [&](int colCount, char** colValues, char** colNames) -> int {
if(!colValues[0]) return 0;
ret = (int)strtol(colValues[0], nullptr, 10);
return 0;
});
return ret;
}
std::unique_ptr<ScopedSQLite3> eventListDB;
bool updateEventList(int timeout) {
auto& db = dbForEventList();
return db.applyEndpoint("events", timeout);
}
ScopedSQLite3& dbForEventList(bool update) {
if(!eventListDB) {
eventListDB = std::make_unique<ScopedSQLite3>("evt_list.db");
eventListDB->exec("PRAGMA JOURNAL_MODE = OFF;");
eventListDB->exec("PRAGMA SYNCHRONOUS = OFF;");
eventListDB->exec("PRAGMA LOCKING_MODE = EXCLUSIVE;");
eventListDB->seqTableName = "events_sequence";
if(update) updateEventList();
}
return *eventListDB;
}