summaryrefslogtreecommitdiffstats
path: root/xbmc/pvr/epg/EpgDatabase.h
blob: 580568d23ae8073dc1b8edc50242f3ec2fbbbb46 (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
 *  Copyright (C) 2012-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#pragma once

#include "dbwrappers/Database.h"
#include "threads/CriticalSection.h"

#include <memory>
#include <vector>

class CDateTime;

namespace PVR
{
  class CPVREpg;
  class CPVREpgInfoTag;
  class CPVREpgSearchFilter;

  struct PVREpgSearchData;

  /** The EPG database */

  static constexpr int EPG_COMMIT_QUERY_COUNT_LIMIT = 10000;

  class CPVREpgDatabase : public CDatabase, public std::enable_shared_from_this<CPVREpgDatabase>
  {
  public:
    /*!
     * @brief Create a new instance of the EPG database.
     */
    CPVREpgDatabase() = default;

    /*!
     * @brief Destroy this instance.
     */
    ~CPVREpgDatabase() override = default;

    /*!
     * @brief Open the database.
     * @return True if it was opened successfully, false otherwise.
     */
    bool Open() override;

    /*!
     * @brief Close the database.
     */
    void Close() override;

    /*!
     * @brief Lock the database.
     */
    void Lock();

    /*!
     * @brief Unlock the database.
     */
    void Unlock();

    /*!
     * @brief Get the minimal database version that is required to operate correctly.
     * @return The minimal database version.
     */
    int GetSchemaVersion() const override { return 16; }

    /*!
     * @brief Get the default sqlite database filename.
     * @return The default filename.
     */
    const char* GetBaseDBName() const override { return "Epg"; }

    /*! @name EPG methods */
    //@{

    /*!
     * @brief Remove all EPG information from the database
     * @return True if the EPG information was erased, false otherwise.
     */
    bool DeleteEpg();

    /*!
     * @brief Queue deletionof an EPG table.
     * @param tag The table to queue for deletion.
     * @return True on success, false otherwise.
     */
    bool QueueDeleteEpgQuery(const CPVREpg& table);

    /*!
     * @brief Write the query to delete the given EPG tag to db query queue.
     * @param tag The EPG tag to remove.
     * @return True on success, false otherwise.
     */
    bool QueueDeleteTagQuery(const CPVREpgInfoTag& tag);

    /*!
     * @brief Get all EPG tables from the database. Does not get the EPG tables' entries.
     * @return The entries.
     */
    std::vector<std::shared_ptr<CPVREpg>> GetAll();

    /*!
     * @brief Get all tags for a given EPG id.
     * @param iEpgID The ID of the EPG.
     * @return The entries.
     */
    std::vector<std::shared_ptr<CPVREpgInfoTag>> GetAllEpgTags(int iEpgID);

    /*!
     * @brief Get all icon paths for a given EPG id.
     * @param iEpgID The ID of the EPG.
     * @return The entries.
     */
    std::vector<std::string> GetAllIconPaths(int iEpgID);

    /*!
     * @brief Check whether this EPG has any tags.
     * @param iEpgID The ID of the EPG.
     * @return True in case there are tags, false otherwise.
     */
    bool HasTags(int iEpgID);

    /*!
     * @brief Get the end time of the last tag in this EPG.
     * @param iEpgID The ID of the EPG.
     * @return The time.
     */
    CDateTime GetLastEndTime(int iEpgID);

    /*!
     * @brief Get the start and end time across all EPGs.
     * @return The times; first: start time, second: end time.
     */
    std::pair<CDateTime, CDateTime> GetFirstAndLastEPGDate();

    /*!
     * @brief Get the start time of the first tag with a start time greater than the given min time.
     * @param iEpgID The ID of the EPG.
     * @param minStart The min start time.
     * @return The time.
     */
    CDateTime GetMinStartTime(int iEpgID, const CDateTime& minStart);

    /*!
     * @brief Get the end time of the first tag with an end time less than the given max time.
     * @param iEpgID The ID of the EPG.
     * @param maxEnd The mx end time.
     * @return The time.
     */
    CDateTime GetMaxEndTime(int iEpgID, const CDateTime& maxEnd);

    /*!
     * @brief Get all EPG tags matching the given search criteria.
     * @param searchData The search criteria.
     * @return The matching tags.
     */
    std::vector<std::shared_ptr<CPVREpgInfoTag>> GetEpgTags(const PVREpgSearchData& searchData);

    /*!
     * @brief Get an EPG tag given its EPG id and unique broadcast ID.
     * @param iEpgID The ID of the EPG for the tag to get.
     * @param iUniqueBroadcastId The unique broadcast ID for the tag to get.
     * @return The tag or nullptr, if not found.
     */
    std::shared_ptr<CPVREpgInfoTag> GetEpgTagByUniqueBroadcastID(int iEpgID,
                                                                 unsigned int iUniqueBroadcastId);

    /*!
     * @brief Get an EPG tag given its EPG id and database ID.
     * @param iEpgID The ID of the EPG for the tag to get.
     * @param iDatabaseId The database ID for the tag to get.
     * @return The tag or nullptr, if not found.
     */
    std::shared_ptr<CPVREpgInfoTag> GetEpgTagByDatabaseID(int iEpgID, int iDatabaseId);

    /*!
     * @brief Get an EPG tag given its EPG ID and start time.
     * @param iEpgID The ID of the EPG for the tag to get.
     * @param startTime The start time for the tag to get.
     * @return The tag or nullptr, if not found.
     */
    std::shared_ptr<CPVREpgInfoTag> GetEpgTagByStartTime(int iEpgID, const CDateTime& startTime);

    /*!
     * @brief Get the next EPG tag matching the given EPG id and min start time.
     * @param iEpgID The ID of the EPG for the tag to get.
     * @param minStartTime The min start time for the tag to get.
     * @return The tag or nullptr, if not found.
     */
    std::shared_ptr<CPVREpgInfoTag> GetEpgTagByMinStartTime(int iEpgID,
                                                            const CDateTime& minStartTime);

    /*!
     * @brief Get the next EPG tag matching the given EPG id and max end time.
     * @param iEpgID The ID of the EPG for the tag to get.
     * @param maxEndTime The max end time for the tag to get.
     * @return The tag or nullptr, if not found.
     */
    std::shared_ptr<CPVREpgInfoTag> GetEpgTagByMaxEndTime(int iEpgID, const CDateTime& maxEndTime);

    /*!
     * @brief Get all EPG tags matching the given EPG id, min start time and max end time.
     * @param iEpgID The ID of the EPG for the tags to get.
     * @param minStartTime The min start time for the tags to get.
     * @param maxEndTime The max end time for the tags to get.
     * @return The tags or empty vector, if no tags were found.
     */
    std::vector<std::shared_ptr<CPVREpgInfoTag>> GetEpgTagsByMinStartMaxEndTime(
        int iEpgID, const CDateTime& minStartTime, const CDateTime& maxEndTime);

    /*!
     * @brief Get all EPG tags matching the given EPG id, min end time and max start time.
     * @param iEpgID The ID of the EPG for the tags to get.
     * @param minEndTime The min end time for the tags to get.
     * @param maxStartTime The max start time for the tags to get.
     * @return The tags or empty vector, if no tags were found.
     */
    std::vector<std::shared_ptr<CPVREpgInfoTag>> GetEpgTagsByMinEndMaxStartTime(
        int iEpgID, const CDateTime& minEndTime, const CDateTime& maxStartTime);

    /*!
     * @brief Write the query to delete all EPG tags in range of given EPG id, min end time and max
     * start time to db query queue. .
     * @param iEpgID The ID of the EPG for the tags to delete.
     * @param minEndTime The min end time for the tags to delete.
     * @param maxStartTime The max start time for the tags to delete.
     * @return True if it was removed or queued successfully, false otherwise.
     */
    bool QueueDeleteEpgTagsByMinEndMaxStartTimeQuery(int iEpgID,
                                                     const CDateTime& minEndTime,
                                                     const CDateTime& maxStartTime);

    /*!
     * @brief Get the last stored EPG scan time.
     * @param iEpgId The table to update the time for. Use 0 for a global value.
     * @param lastScan The last scan time or -1 if it wasn't found.
     * @return True if the time was fetched successfully, false otherwise.
     */
    bool GetLastEpgScanTime(int iEpgId, CDateTime* lastScan);

    /*!
     * @brief Write the query to update the last scan time for the given EPG to db query queue.
     * @param iEpgId The table to update the time for.
     * @param lastScanTime The time to write to the database.
     * @return True on success, false otherwise.
     */
    bool QueuePersistLastEpgScanTimeQuery(int iEpgId, const CDateTime& lastScanTime);

    /*!
     * @brief Write the query to delete the last scan time for the given EPG to db query queue.
     * @param iEpgId The table to delete the time for.
     * @return True on success, false otherwise.
     */
    bool QueueDeleteLastEpgScanTimeQuery(const CPVREpg& table);

    /*!
     * @brief Persist an EPG table. It's entries are not persisted.
     * @param epg The table to persist.
     * @param bQueueWrite If true, don't execute the query immediately but queue it.
     * @return The database ID of this entry or 0 if bQueueWrite is false and the query was queued.
     */
    int Persist(const CPVREpg& epg, bool bQueueWrite);

    /*!
     * @brief Erase all EPG tags with the given epg ID and an end time less than the given time.
     * @param iEpgId The ID of the EPG.
     * @param maxEndTime The maximum allowed end time.
     * @return True if the entries were removed successfully, false otherwise.
     */
    bool DeleteEpgTags(int iEpgId, const CDateTime& maxEndTime);

    /*!
     * @brief Erase all EPG tags with the given epg ID.
     * @param iEpgId The ID of the EPG.
     * @return True if the entries were removed successfully, false otherwise.
     */
    bool DeleteEpgTags(int iEpgId);

    /*!
     * @brief Queue the erase all EPG tags with the given epg ID.
     * @param iEpgId The ID of the EPG.
     * @return True if the entries were queued successfully, false otherwise.
     */
    bool QueueDeleteEpgTags(int iEpgId);

    /*!
     * @brief Write the query to persist the given EPG tag to db query queue.
     * @param tag The tag to persist.
     * @return True on success, false otherwise.
     */
    bool QueuePersistQuery(const CPVREpgInfoTag& tag);

    /*!
     * @return Last EPG id in the database
     */
    int GetLastEPGId();

    //@}

    /*! @name EPG searches methods */
    //@{

    /*!
     * @brief Get all saved searches from the database.
     * @param bRadio Whether to fetch saved searches for radio or TV.
     * @return The searches.
     */
    std::vector<std::shared_ptr<CPVREpgSearchFilter>> GetSavedSearches(bool bRadio);

    /*!
     * @brief Get the saved search matching the given id.
     * @param bRadio Whether to fetch a TV or radio saved search.
     * @param iId The id.
     * @return The saved search or nullptr if not found.
     */
    std::shared_ptr<CPVREpgSearchFilter> GetSavedSearchById(bool bRadio, int iId);

    /*!
     * @brief Persist a search.
     * @param epgSearch The search.
     * @return True on success, false otherwise.
     */
    bool Persist(CPVREpgSearchFilter& epgSearch);

    /*!
     * @brief Update time last executed for the given search.
     * @param epgSearch The search.
     * @return True on success, false otherwise.
     */
    bool UpdateSavedSearchLastExecuted(const CPVREpgSearchFilter& epgSearch);

    /*!
     * @brief Delete a saved search.
     * @param epgSearch The search.
     * @return True on success, false otherwise.
     */
    bool Delete(const CPVREpgSearchFilter& epgSearch);

    /*!
     * @brief Delete all saved searches.
     * @return True on success, false otherwise.
     */
    bool DeleteSavedSearches();

    //@}

  private:
    /*!
     * @brief Create the EPG database tables.
     */
    void CreateTables() override;

    /*!
     * @brief Create the EPG database analytics.
     */
    void CreateAnalytics() override;

    /*!
     * @brief Update an old version of the database.
     * @param version The version to update the database from.
     */
    void UpdateTables(int version) override;

    int GetMinSchemaVersion() const override { return 4; }

    std::shared_ptr<CPVREpgInfoTag> CreateEpgTag(const std::unique_ptr<dbiplus::Dataset>& pDS);

    std::shared_ptr<CPVREpgSearchFilter> CreateEpgSearchFilter(
        bool bRadio, const std::unique_ptr<dbiplus::Dataset>& pDS);

    CCriticalSection m_critSection;
  };
}