| 1 | #include <kodi/AddonBase.h> |
| 2 | #include "RecordingsDB.h" |
| 3 | |
| 4 | const int DB_VERSION = 1; |
| 5 | |
| 6 | class ProcessRecordingDBInfoRowCallback : public ProcessRowCallback { |
| 7 | public: |
| 8 | virtual ~ProcessRecordingDBInfoRowCallback() { } |
| 9 | |
| 10 | void ProcessRow(sqlite3_stmt* stmt) { |
| 11 | m_result.recordingId = std::string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0))); |
| 12 | m_result.playCount = sqlite3_column_int(stmt, 1); |
| 13 | m_result.lastPlayedPosition = sqlite3_column_int(stmt, 2); |
| 14 | m_result.lastSeen = sqlite3_column_int(stmt, 3); |
| 15 | } |
| 16 | |
| 17 | RecordingDBInfo Result() { |
| 18 | return m_result; |
| 19 | } |
| 20 | |
| 21 | private: |
| 22 | RecordingDBInfo m_result; |
| 23 | }; |
| 24 | |
| 25 | RecordingsDB::RecordingsDB(std::string folder) |
| 26 | : SQLConnection("REC-DB") { |
| 27 | std::string dbPath = folder + "recordings.sqlite"; |
| 28 | Open(dbPath); |
| 29 | if (!MigrateDbIfRequired()) { |
| 30 | kodi::Log(ADDON_LOG_ERROR, "%s: Failed to migrate DB to version: %i", m_name.c_str(), DB_VERSION); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | RecordingsDB::~RecordingsDB() { |
| 35 | } |
| 36 | |
| 37 | bool RecordingsDB::MigrateDbIfRequired() { |
| 38 | int currentVersion = GetVersion(); |
| 39 | while (currentVersion < DB_VERSION) { |
| 40 | if (currentVersion < 0) { |
| 41 | return false; |
| 42 | } |
| 43 | switch (currentVersion) { |
| 44 | case 0: |
| 45 | if (!Migrate0To1()) { |
| 46 | return false; |
| 47 | } |
| 48 | break; |
| 49 | } |
| 50 | currentVersion = GetVersion(); |
| 51 | } |
| 52 | return true; |
| 53 | } |
| 54 | |
| 55 | bool RecordingsDB::Migrate0To1() { |
| 56 | kodi::Log(ADDON_LOG_INFO, "%s: Migrate to version 1.", m_name.c_str()); |
| 57 | std::string migrationScript = ""; |
| 58 | migrationScript += "create table RECORDING_INFO ("; |
| 59 | migrationScript += " RECORDING_ID text not null primary key,"; |
| 60 | migrationScript += " PLAY_COUNT integer not null,"; |
| 61 | migrationScript += " LAST_PLAYED_POSITION integer not null,"; |
| 62 | migrationScript += " LAST_SEEN integer not null"; |
| 63 | migrationScript += ")"; |
| 64 | if (!Execute(migrationScript)) { |
| 65 | return false; |
| 66 | } |
| 67 | return SetVersion(1); |
| 68 | } |
| 69 | |
| 70 | void RecordingsDB::Cleanup() { |
| 71 | time_t now; |
| 72 | time(&now); |
| 73 | |
| 74 | if (!Execute("delete from RECORDING_INFO where LAST_SEEN < " + std::to_string(now - 3600))) { |
| 75 | kodi::Log(ADDON_LOG_ERROR, "%s: Failed to clean db", m_name.c_str()); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | |
| 80 | bool RecordingsDB::Set(RecordingDBInfo& recordingDBInfo) { |
| 81 | time(&recordingDBInfo.lastSeen); |
| 82 | std::string insert = "replace into RECORDING_INFO VALUES "; |
| 83 | insert += "('" + recordingDBInfo.recordingId + "'," + std::to_string(recordingDBInfo.playCount) + "," + std::to_string(recordingDBInfo.lastPlayedPosition) + "," + std::to_string(recordingDBInfo.lastSeen) + ")"; |
| 84 | |
| 85 | if (!Execute(insert)) { |
| 86 | kodi::Log(ADDON_LOG_ERROR, "%s: Failed to insert", m_name.c_str()); |
| 87 | return false; |
| 88 | } |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | RecordingDBInfo RecordingsDB::Get(std::string recordingId) { |
| 93 | ProcessRecordingDBInfoRowCallback callback; |
| 94 | if (!Query("select * from RECORDING_INFO where RECORDING_ID = '" + recordingId + "'", callback)) { |
| 95 | kodi::Log(ADDON_LOG_ERROR, "%s: Failed to get info from db.", m_name.c_str()); |
| 96 | } |
| 97 | RecordingDBInfo ret = callback.Result(); |
| 98 | ret.recordingId = recordingId; |
| 99 | return ret; |
| 100 | } |
| 101 |