Debian Codegraph @

src/sql/ParameterDB.cpp

raw symbols SCIP
1 #include <kodi/AddonBase.h>
2 #include "ParameterDB.h"
3
4 const int DB_VERSION = 1;
5
6 class ProcessParameterRowCallback : public ProcessRowCallback {
7 public:
8 virtual ~ProcessParameterRowCallback() { }
9
10 void ProcessRow(sqlite3_stmt* stmt) {
11 m_result = std::string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)));
12 }
13
14 std::string Result() {
15 return m_result;
16 }
17
18 private:
19 std::string m_result = "";
20 };
21
22 ParameterDB::ParameterDB(std::string folder)
23 : SQLConnection("PARAMS-DB") {
24 std::string dbPath = folder + "parameter.sqlite";
25 Open(dbPath);
26 if (!MigrateDbIfRequired()) {
27 kodi::Log(ADDON_LOG_ERROR, "%s: Failed to migrate DB to version: %i", m_name.c_str(), DB_VERSION);
28 }
29 }
30
31 ParameterDB::~ParameterDB() {
32 }
33
34 bool ParameterDB::MigrateDbIfRequired() {
35 int currentVersion = GetVersion();
36 while (currentVersion < DB_VERSION) {
37 if (currentVersion < 0) {
38 return false;
39 }
40 switch (currentVersion) {
41 case 0:
42 if (!Migrate0To1()) {
43 return false;
44 }
45 break;
46 }
47 currentVersion = GetVersion();
48 }
49 return true;
50 }
51
52 bool ParameterDB::Migrate0To1() {
53 kodi::Log(ADDON_LOG_INFO, "%s: Migrate to version 1.", m_name.c_str());
54 std::string migrationScript = "";
55 migrationScript += "create table PARAMETER (";
56 migrationScript += " KEY text not null primary key,";
57 migrationScript += " VALUE text not null";
58 migrationScript += ")";
59 if (!Execute(migrationScript)) {
60 return false;
61 }
62 return SetVersion(1);
63 }
64
65 bool ParameterDB::Set(std::string key, std::string value) {
66 std::string insert = "replace into PARAMETER VALUES ";
67 insert += "('" + key + "','" + value + "')";
68
69 if (!Execute(insert)) {
70 kodi::Log(ADDON_LOG_ERROR, "%s: Failed to insert", m_name.c_str());
71 return false;
72 }
73 return true;
74 }
75
76 std::string ParameterDB::Get(std::string key) {
77 ProcessParameterRowCallback callback;
78 if (!Query("select VALUE from PARAMETER where KEY = '" + key + "'", callback)) {
79 kodi::Log(ADDON_LOG_ERROR, "%s: Failed to get parameter from db.", m_name.c_str());
80 }
81 return callback.Result();
82 }
83