Debian Codegraph @

src/sql/SQLConnection.cpp

raw symbols SCIP
1 #include <kodi/AddonBase.h>
2 #include "SQLConnection.h"
3
4 class ProcessSingleIntRowCallback : public ProcessRowCallback {
5 public:
6 virtual ~ProcessSingleIntRowCallback() { }
7
8 void ProcessRow(sqlite3_stmt* stmt) {
9 m_result = sqlite3_column_int(stmt, 0);
10 }
11
12 int GetResult() {
13 return m_result;
14 }
15
16 private:
17 int m_result = -1;
18 };
19
20 class NoopRowCallback : public ProcessRowCallback {
21 public:
22 void ProcessRow(sqlite3_stmt* stmt) {
23 }
24 };
25
26
27 SQLConnection::SQLConnection(std::string name):
28 m_db(nullptr),
29 m_name(name) {
30 }
31
32 SQLConnection::~SQLConnection() {
33 sqlite3_close(m_db);
34 }
35
36 bool SQLConnection::Open(std::string& file) {
37 int rc = sqlite3_open(file.c_str(), &m_db);
38 if(rc) {
39 kodi::Log(ADDON_LOG_ERROR, "%s: Can't open database: %s", m_name.c_str(), sqlite3_errmsg(m_db));
40 return false;
41 }
42 sqlite3_exec(m_db, "PRAGMA synchronous = OFF;", NULL, NULL, NULL);
43 sqlite3_exec(m_db, "PRAGMA journal_mode = OFF;", NULL, NULL, NULL);
44 EnsureVersionTable();
45 return true;
46 }
47
48 bool SQLConnection::Query(std::string query, ProcessRowCallback& callback) {
49 sqlite3_stmt* stmt;
50 int ret = sqlite3_prepare(m_db, query.c_str(), query.length(), &stmt, NULL);
51
52 if (ret != SQLITE_OK) {
53 sqlite3_finalize(stmt);
54 kodi::Log(ADDON_LOG_ERROR, "%s: Query failed: %s", m_name.c_str(), sqlite3_errmsg(m_db));
55 return false;
56 }
57
58 bool err = false;
59 bool done = false;
60 while (!done) {
61 switch (sqlite3_step(stmt)) {
62 case SQLITE_ROW:
63 callback.ProcessRow(stmt);
64 break;
65
66 case SQLITE_DONE:
67 done = true;
68 break;
69
70 default:
71 kodi::Log(ADDON_LOG_ERROR, "%s: Query failed.", m_name.c_str());
72 err = true;
73 done = true;
74 }
75 }
76
77 sqlite3_finalize(stmt);
78 return !err;
79 }
80
81 bool SQLConnection::Execute(std::string query) {
82 NoopRowCallback callback;
83 return Query(query, callback);
84 }
85
86 bool SQLConnection::EnsureVersionTable() {
87 ProcessSingleIntRowCallback callback;
88 if (!Query("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='SCHEMA_VERSION'", callback)) {
89 return false;
90 }
91
92 if (callback.GetResult() == 0) {
93 kodi::Log(ADDON_LOG_INFO, "%s: SCHEMA_VERSION does not exist. Creating Table.", m_name.c_str());
94 if (!Execute("create table SCHEMA_VERSION (VERSION integer NOT NULL)")) {
95 return false;
96 }
97 return Execute("insert into SCHEMA_VERSION VALUES (0)");
98 }
99
100 return true;
101 }
102
103 int SQLConnection::GetVersion() {
104 ProcessSingleIntRowCallback callback;
105 if (!Query("select VERSION from SCHEMA_VERSION", callback)) {
106 kodi::Log(ADDON_LOG_INFO, "%s: Failed to get current version.", m_name.c_str());
107 return -1;
108 }
109 int currentVersion = callback.GetResult();
110 kodi::Log(ADDON_LOG_INFO, "%s: Current version: %d", m_name.c_str(), currentVersion);
111 return currentVersion;
112 }
113
114 bool SQLConnection::SetVersion(int newVersion) {
115 return Execute("update SCHEMA_VERSION set VERSION = " + std::to_string(newVersion));
116 }
117
118 void SQLConnection::BeginTransaction() {
119 sqlite3_exec(m_db, "BEGIN TRANSACTION;", NULL, NULL, NULL);
120 }
121
122 void SQLConnection::EndTransaction() {
123 sqlite3_exec(m_db, "END TRANSACTION;", NULL, NULL, NULL);
124 }
125