| 1 | #include <kodi/AddonBase.h> |
| 2 | #include "EpgDB.h" |
| 3 | |
| 4 | const int DB_VERSION = 3; |
| 5 | |
| 6 | class ProcessEpgDBInfoRowCallback : public ProcessRowCallback { |
| 7 | public: |
| 8 | virtual ~ProcessEpgDBInfoRowCallback() { } |
| 9 | |
| 10 | void ProcessRow(sqlite3_stmt* stmt) { |
| 11 | EpgDBInfo dbInfo; |
| 12 | dbInfo.programId = sqlite3_column_int(stmt, 0); |
| 13 | dbInfo.recordUntil = sqlite3_column_int(stmt, 1); |
| 14 | dbInfo.replayUntil = sqlite3_column_int(stmt, 2); |
| 15 | dbInfo.restartUntil = sqlite3_column_int(stmt, 3); |
| 16 | dbInfo.startTime = sqlite3_column_int(stmt, 4); |
| 17 | dbInfo.endTime = sqlite3_column_int(stmt, 5); |
| 18 | dbInfo.detailsLoaded = sqlite3_column_int(stmt, 6) > 0; |
| 19 | dbInfo.genre = std::string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 7))); |
| 20 | dbInfo.title = std::string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 8))); |
| 21 | dbInfo.subtitle = std::string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 9))); |
| 22 | dbInfo.description = std::string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 10))); |
| 23 | dbInfo.season = sqlite3_column_int(stmt, 11); |
| 24 | dbInfo.episode = sqlite3_column_int(stmt, 12); |
| 25 | dbInfo.imageToken = std::string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 13))); |
| 26 | dbInfo.cid = std::string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 14))); |
| 27 | |
| 28 | m_result.push_back(dbInfo); |
| 29 | } |
| 30 | |
| 31 | EpgDBInfo SingleResult() { |
| 32 | if (m_result.size() < 1) { |
| 33 | EpgDBInfo dbInfo; |
| 34 | return dbInfo; |
| 35 | } |
| 36 | return m_result.front(); |
| 37 | } |
| 38 | |
| 39 | std::list<EpgDBInfo> Result() { |
| 40 | return m_result; |
| 41 | } |
| 42 | |
| 43 | private: |
| 44 | std::list<EpgDBInfo> m_result; |
| 45 | }; |
| 46 | |
| 47 | EpgDB::EpgDB(std::string folder) |
| 48 | : SQLConnection("EPG-DB") { |
| 49 | std::string dbPath = folder + "epg.sqlite"; |
| 50 | Open(dbPath); |
| 51 | if (!MigrateDbIfRequired()) { |
| 52 | kodi::Log(ADDON_LOG_ERROR, "%s: Failed to migrate DB to version: %i", m_name.c_str(), DB_VERSION); |
| 53 | } |
| 54 | Cleanup(); |
| 55 | |
| 56 | std::string str = "insert into EPG_INFO values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; |
| 57 | int ret = sqlite3_prepare_v2(m_db, str.c_str(), str.size() + 1, &m_prepareInsertStatement, nullptr); |
| 58 | if (ret != SQLITE_OK) { |
| 59 | kodi::Log(ADDON_LOG_ERROR, "%s: Failed to prepare insert statement.", m_name.c_str()); |
| 60 | } |
| 61 | |
| 62 | str = "update EPG_INFO set RECORD_UNTIL = ?, REPLAY_UNTIL = ?, RESTART_UNTIL = ?, START_TIME = ?, END_TIME = ?, "; |
| 63 | str += "DETAILS_LOADED = ?, GENRE = ?, TITLE = ?, SUBTITLE = ?, DESCRIPTION = ?, SEASON = ?, EPISODE = ?, "; |
| 64 | str += "IMAGE_TOKEN = ?, CID = ? where PROGRAM_ID = ?"; |
| 65 | ret = sqlite3_prepare_v2(m_db, str.c_str(), str.size() + 1, &m_prepareUpdateStatement, nullptr); |
| 66 | if (ret != SQLITE_OK) { |
| 67 | kodi::Log(ADDON_LOG_ERROR, "%s: Failed to prepare update statement.", m_name.c_str()); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | EpgDB::~EpgDB() { |
| 72 | sqlite3_finalize(m_prepareInsertStatement); |
| 73 | sqlite3_finalize(m_prepareUpdateStatement); |
| 74 | } |
| 75 | |
| 76 | bool EpgDB::MigrateDbIfRequired() { |
| 77 | int currentVersion = GetVersion(); |
| 78 | while (currentVersion < DB_VERSION) { |
| 79 | if (currentVersion < 0) { |
| 80 | return false; |
| 81 | } |
| 82 | switch (currentVersion) { |
| 83 | case 0: |
| 84 | if (!Migrate0To1()) { |
| 85 | return false; |
| 86 | } |
| 87 | break; |
| 88 | case 1: |
| 89 | if (!Migrate1To2()) { |
| 90 | return false; |
| 91 | } |
| 92 | break; |
| 93 | case 2: |
| 94 | if (!Migrate2To3()) { |
| 95 | return false; |
| 96 | } |
| 97 | break; |
| 98 | } |
| 99 | currentVersion = GetVersion(); |
| 100 | } |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | bool EpgDB::Migrate0To1() { |
| 105 | kodi::Log(ADDON_LOG_INFO, "%s: Migrate to version 1.", m_name.c_str()); |
| 106 | std::string migrationScript = ""; |
| 107 | migrationScript += "create table EPG_INFO ("; |
| 108 | migrationScript += " PROGRAM_ID integer not null primary key,"; |
| 109 | migrationScript += " RECORD_UNTIL integer not null,"; |
| 110 | migrationScript += " REPLAY_UNTIL integer not null,"; |
| 111 | migrationScript += " RESTART_UNTIL integer not null"; |
| 112 | migrationScript += ")"; |
| 113 | if (!Execute(migrationScript)) { |
| 114 | return false; |
| 115 | } |
| 116 | return SetVersion(1); |
| 117 | } |
| 118 | |
| 119 | bool EpgDB::Migrate1To2() { |
| 120 | kodi::Log(ADDON_LOG_INFO, "%s: Migrate to version 2.", m_name.c_str()); |
| 121 | if (!Execute("alter table EPG_INFO add column START_TIME integer not null default 0;")) { |
| 122 | return false; |
| 123 | } |
| 124 | if (!Execute("alter table EPG_INFO add column END_TIME integer not null default 0;")) { |
| 125 | return false; |
| 126 | } |
| 127 | if (!Execute("alter table EPG_INFO add column DETAILS_LOADED integer not null default 0;")) { |
| 128 | return false; |
| 129 | } |
| 130 | if (!Execute("alter table EPG_INFO add column GENRE text;")) { |
| 131 | return false; |
| 132 | } |
| 133 | if (!Execute("alter table EPG_INFO add column TITLE text;")) { |
| 134 | return false; |
| 135 | } |
| 136 | if (!Execute("alter table EPG_INFO add column SUBTITLE text;")) { |
| 137 | return false; |
| 138 | } |
| 139 | if (!Execute("alter table EPG_INFO add column DESCRIPTION text;")) { |
| 140 | return false; |
| 141 | } |
| 142 | if (!Execute("alter table EPG_INFO add column SEASON integer;")) { |
| 143 | return false; |
| 144 | } |
| 145 | if (!Execute("alter table EPG_INFO add column EPISODE integer;")) { |
| 146 | return false; |
| 147 | } |
| 148 | if (!Execute("alter table EPG_INFO add column IMAGE_TOKEN text;")) { |
| 149 | return false; |
| 150 | } |
| 151 | if (!Execute("alter table EPG_INFO add column CID text;")) { |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | return SetVersion(2); |
| 156 | } |
| 157 | |
| 158 | bool EpgDB::Migrate2To3() { |
| 159 | kodi::Log(ADDON_LOG_INFO, "%s: Migrate to version 3.", m_name.c_str()); |
| 160 | if (!Execute("update EPG_INFO set DETAILS_LOADED = 0;")) { |
| 161 | return false; |
| 162 | } |
| 163 | return SetVersion(3); |
| 164 | } |
| 165 | |
| 166 | void EpgDB::Cleanup() { |
| 167 | time_t now; |
| 168 | time(&now); |
| 169 | if (now < m_nextCleanupDue) { |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | m_nextCleanupDue = now + 3600; |
| 174 | |
| 175 | if (!Execute("delete from EPG_INFO where END_TIME < " + std::to_string(now - 60 * 60 * 24 * 7))) { |
| 176 | kodi::Log(ADDON_LOG_ERROR, "%s: Failed to clean db", m_name.c_str()); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | bool EpgDB::Insert(EpgDBInfo &epgDBInfo) { |
| 181 | int ret = sqlite3_bind_int(m_prepareInsertStatement, 1, epgDBInfo.programId); |
| 182 | if (ret != SQLITE_OK) { |
| 183 | kodi::Log(ADDON_LOG_ERROR, "%s: Failed bind value 1.", m_name.c_str()); |
| 184 | return false; |
| 185 | } |
| 186 | ret = sqlite3_bind_int(m_prepareInsertStatement, 2, epgDBInfo.recordUntil); |
| 187 | if (ret != SQLITE_OK) { |
| 188 | kodi::Log(ADDON_LOG_ERROR, "%s: Failed bind value 2.", m_name.c_str()); |
| 189 | return false; |
| 190 | } |
| 191 | ret = sqlite3_bind_int(m_prepareInsertStatement, 3, epgDBInfo.replayUntil); |
| 192 | if (ret != SQLITE_OK) { |
| 193 | kodi::Log(ADDON_LOG_ERROR, "%s: Failed bind value 3.", m_name.c_str()); |
| 194 | return false; |
| 195 | } |
| 196 | ret = sqlite3_bind_int(m_prepareInsertStatement, 4, epgDBInfo.restartUntil); |
| 197 | if (ret != SQLITE_OK) { |
| 198 | kodi::Log(ADDON_LOG_ERROR, "%s: Failed bind value 4.", m_name.c_str()); |
| 199 | return false; |
| 200 | } |
| 201 | ret = sqlite3_bind_int(m_prepareInsertStatement, 5, epgDBInfo.startTime); |
| 202 | if (ret != SQLITE_OK) { |
| 203 | kodi::Log(ADDON_LOG_ERROR, "%s: Failed bind value 5.", m_name.c_str()); |
| 204 | return false; |
| 205 | } |
| 206 | ret = sqlite3_bind_int(m_prepareInsertStatement, 6, epgDBInfo.endTime); |
| 207 | if (ret != SQLITE_OK) { |
| 208 | kodi::Log(ADDON_LOG_ERROR, "%s: Failed bind value 6.", m_name.c_str()); |
| 209 | return false; |
| 210 | } |
| 211 | ret = sqlite3_bind_int(m_prepareInsertStatement, 7, epgDBInfo.detailsLoaded); |
| 212 | if (ret != SQLITE_OK) { |
| 213 | kodi::Log(ADDON_LOG_ERROR, "%s: Failed bind value 7.", m_name.c_str()); |
| 214 | return false; |
| 215 | } |
| 216 | ret = sqlite3_bind_text(m_prepareInsertStatement, 8, epgDBInfo.genre.c_str(), -1, nullptr); |
| 217 | if (ret != SQLITE_OK) { |
| 218 | kodi::Log(ADDON_LOG_ERROR, "%s: Failed bind value 8.", m_name.c_str()); |
| 219 | return false; |
| 220 | } |
| 221 | ret = sqlite3_bind_text(m_prepareInsertStatement, 9, epgDBInfo.title.c_str(), -1, nullptr); |
| 222 | if (ret != SQLITE_OK) { |
| 223 | kodi::Log(ADDON_LOG_ERROR, "%s: Failed bind value 9.", m_name.c_str()); |
| 224 | return false; |
| 225 | } |
| 226 | ret = sqlite3_bind_text(m_prepareInsertStatement, 10, epgDBInfo.subtitle.c_str(), -1, nullptr); |
| 227 | if (ret != SQLITE_OK) { |
| 228 | kodi::Log(ADDON_LOG_ERROR, "%s: Failed bind value 10.", m_name.c_str()); |
| 229 | return false; |
| 230 | } |
| 231 | ret = sqlite3_bind_text(m_prepareInsertStatement, 11, epgDBInfo.description.c_str(), -1, nullptr); |
| 232 | if (ret != SQLITE_OK) { |
| 233 | kodi::Log(ADDON_LOG_ERROR, "%s: Failed bind value 11.", m_name.c_str()); |
| 234 | return false; |
| 235 | } |
| 236 | ret = sqlite3_bind_int(m_prepareInsertStatement, 12, epgDBInfo.season); |
| 237 | if (ret != SQLITE_OK) { |
| 238 | kodi::Log(ADDON_LOG_ERROR, "%s: Failed bind value 12.", m_name.c_str()); |
| 239 | return false; |
| 240 | } |
| 241 | ret = sqlite3_bind_int(m_prepareInsertStatement, 13, epgDBInfo.episode); |
| 242 | if (ret != SQLITE_OK) { |
| 243 | kodi::Log(ADDON_LOG_ERROR, "%s: Failed bind value 13.", m_name.c_str()); |
| 244 | return false; |
| 245 | } |
| 246 | ret = sqlite3_bind_text(m_prepareInsertStatement, 14, epgDBInfo.imageToken.c_str(), -1, nullptr); |
| 247 | if (ret != SQLITE_OK) { |
| 248 | kodi::Log(ADDON_LOG_ERROR, "%s: Failed bind value 14.", m_name.c_str()); |
| 249 | return false; |
| 250 | } |
| 251 | ret = sqlite3_bind_text(m_prepareInsertStatement, 15, epgDBInfo.cid.c_str(), -1, nullptr); |
| 252 | if (ret != SQLITE_OK) { |
| 253 | kodi::Log(ADDON_LOG_ERROR, "%s: Failed bind value 15.", m_name.c_str()); |
| 254 | return false; |
| 255 | } |
| 256 | |
| 257 | ret = sqlite3_step(m_prepareInsertStatement); |
| 258 | |
| 259 | sqlite3_reset(m_prepareInsertStatement); |
| 260 | |
| 261 | return ret == SQLITE_DONE; |
| 262 | } |
| 263 | |
| 264 | bool EpgDB::Update(EpgDBInfo &epgDBInfo) { |
| 265 | int ret = sqlite3_bind_int(m_prepareUpdateStatement, 15, epgDBInfo.programId); |
| 266 | if (ret != SQLITE_OK) { |
| 267 | kodi::Log(ADDON_LOG_ERROR, "%s: Failed bind value 15.", m_name.c_str()); |
| 268 | return false; |
| 269 | } |
| 270 | ret = sqlite3_bind_int(m_prepareUpdateStatement, 1, epgDBInfo.recordUntil); |
| 271 | if (ret != SQLITE_OK) { |
| 272 | kodi::Log(ADDON_LOG_ERROR, "%s: Failed bind value 1.", m_name.c_str()); |
| 273 | return false; |
| 274 | } |
| 275 | ret = sqlite3_bind_int(m_prepareUpdateStatement, 2, epgDBInfo.replayUntil); |
| 276 | if (ret != SQLITE_OK) { |
| 277 | kodi::Log(ADDON_LOG_ERROR, "%s: Failed bind value 2.", m_name.c_str()); |
| 278 | return false; |
| 279 | } |
| 280 | ret = sqlite3_bind_int(m_prepareUpdateStatement, 3, epgDBInfo.restartUntil); |
| 281 | if (ret != SQLITE_OK) { |
| 282 | kodi::Log(ADDON_LOG_ERROR, "%s: Failed bind value 3.", m_name.c_str()); |
| 283 | return false; |
| 284 | } |
| 285 | ret = sqlite3_bind_int(m_prepareUpdateStatement, 4, epgDBInfo.startTime); |
| 286 | if (ret != SQLITE_OK) { |
| 287 | kodi::Log(ADDON_LOG_ERROR, "%s: Failed bind value 4.", m_name.c_str()); |
| 288 | return false; |
| 289 | } |
| 290 | ret = sqlite3_bind_int(m_prepareUpdateStatement, 5, epgDBInfo.endTime); |
| 291 | if (ret != SQLITE_OK) { |
| 292 | kodi::Log(ADDON_LOG_ERROR, "%s: Failed bind value 5.", m_name.c_str()); |
| 293 | return false; |
| 294 | } |
| 295 | ret = sqlite3_bind_int(m_prepareUpdateStatement, 6, epgDBInfo.detailsLoaded); |
| 296 | if (ret != SQLITE_OK) { |
| 297 | kodi::Log(ADDON_LOG_ERROR, "%s: Failed bind value 6.", m_name.c_str()); |
| 298 | return false; |
| 299 | } |
| 300 | ret = sqlite3_bind_text(m_prepareUpdateStatement, 7, epgDBInfo.genre.c_str(), -1, nullptr); |
| 301 | if (ret != SQLITE_OK) { |
| 302 | kodi::Log(ADDON_LOG_ERROR, "%s: Failed bind value 7.", m_name.c_str()); |
| 303 | return false; |
| 304 | } |
| 305 | ret = sqlite3_bind_text(m_prepareUpdateStatement, 8, epgDBInfo.title.c_str(), -1, nullptr); |
| 306 | if (ret != SQLITE_OK) { |
| 307 | kodi::Log(ADDON_LOG_ERROR, "%s: Failed bind value 8.", m_name.c_str()); |
| 308 | return false; |
| 309 | } |
| 310 | ret = sqlite3_bind_text(m_prepareUpdateStatement, 9, epgDBInfo.subtitle.c_str(), -1, nullptr); |
| 311 | if (ret != SQLITE_OK) { |
| 312 | kodi::Log(ADDON_LOG_ERROR, "%s: Failed bind value 9.", m_name.c_str()); |
| 313 | return false; |
| 314 | } |
| 315 | ret = sqlite3_bind_text(m_prepareUpdateStatement, 10, epgDBInfo.description.c_str(), -1, nullptr); |
| 316 | if (ret != SQLITE_OK) { |
| 317 | kodi::Log(ADDON_LOG_ERROR, "%s: Failed bind value 10.", m_name.c_str()); |
| 318 | return false; |
| 319 | } |
| 320 | ret = sqlite3_bind_int(m_prepareUpdateStatement, 11, epgDBInfo.season); |
| 321 | if (ret != SQLITE_OK) { |
| 322 | kodi::Log(ADDON_LOG_ERROR, "%s: Failed bind value 11.", m_name.c_str()); |
| 323 | return false; |
| 324 | } |
| 325 | ret = sqlite3_bind_int(m_prepareUpdateStatement, 12, epgDBInfo.episode); |
| 326 | if (ret != SQLITE_OK) { |
| 327 | kodi::Log(ADDON_LOG_ERROR, "%s: Failed bind value 12.", m_name.c_str()); |
| 328 | return false; |
| 329 | } |
| 330 | ret = sqlite3_bind_text(m_prepareUpdateStatement, 13, epgDBInfo.imageToken.c_str(), -1, nullptr); |
| 331 | if (ret != SQLITE_OK) { |
| 332 | kodi::Log(ADDON_LOG_ERROR, "%s: Failed bind value 13.", m_name.c_str()); |
| 333 | return false; |
| 334 | } |
| 335 | ret = sqlite3_bind_text(m_prepareUpdateStatement, 14, epgDBInfo.cid.c_str(), -1, nullptr); |
| 336 | if (ret != SQLITE_OK) { |
| 337 | kodi::Log(ADDON_LOG_ERROR, "%s: Failed bind value 14.", m_name.c_str()); |
| 338 | return false; |
| 339 | } |
| 340 | |
| 341 | ret = sqlite3_step(m_prepareUpdateStatement); |
| 342 | |
| 343 | sqlite3_reset(m_prepareUpdateStatement); |
| 344 | |
| 345 | return ret == SQLITE_DONE; |
| 346 | } |
| 347 | |
| 348 | EpgDBInfo EpgDB::Get(int programId) { |
| 349 | ProcessEpgDBInfoRowCallback callback; |
| 350 | if (!Query("select * from EPG_INFO where PROGRAM_ID = " + std::to_string(programId), callback)) { |
| 351 | kodi::Log(ADDON_LOG_ERROR, "%s: Failed to get info from db.", m_name.c_str()); |
| 352 | } |
| 353 | return callback.SingleResult(); |
| 354 | } |
| 355 | |
| 356 | std::list<EpgDBInfo> EpgDB::GetWithWhere(std::string where) { |
| 357 | ProcessEpgDBInfoRowCallback callback; |
| 358 | if (!Query("select * from EPG_INFO where " + where, callback)) { |
| 359 | kodi::Log(ADDON_LOG_ERROR, "%s: Failed to get info from db using where part.", m_name.c_str()); |
| 360 | } |
| 361 | return callback.Result(); |
| 362 | } |
| 363 |