kodi
EpgDatabase.h
1 /*
2  * Copyright (C) 2012-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "dbwrappers/Database.h"
12 #include "threads/CriticalSection.h"
13 
14 #include <memory>
15 #include <vector>
16 
17 class CDateTime;
18 
19 namespace PVR
20 {
21  class CPVREpg;
22  class CPVREpgInfoTag;
23  class CPVREpgSearchFilter;
24 
25  struct PVREpgSearchData;
26 
29  static constexpr int EPG_COMMIT_QUERY_COUNT_LIMIT = 10000;
30 
31  class CPVREpgDatabase : public CDatabase, public std::enable_shared_from_this<CPVREpgDatabase>
32  {
33  public:
37  CPVREpgDatabase() = default;
38 
42  ~CPVREpgDatabase() override = default;
43 
48  bool Open() override;
49 
53  void Close() override;
54 
58  void Lock();
59 
63  void Unlock();
64 
69  int GetSchemaVersion() const override { return 16; }
70 
75  const char* GetBaseDBName() const override { return "Epg"; }
76 
79 
84  bool DeleteEpg();
85 
91  bool QueueDeleteEpgQuery(const CPVREpg& table);
92 
98  bool QueueDeleteTagQuery(const CPVREpgInfoTag& tag);
99 
104  std::vector<std::shared_ptr<CPVREpg>> GetAll();
105 
111  std::vector<std::shared_ptr<CPVREpgInfoTag>> GetAllEpgTags(int iEpgID) const;
112 
118  std::vector<std::string> GetAllIconPaths(int iEpgID) const;
119 
125  bool HasTags(int iEpgID) const;
126 
132  CDateTime GetLastEndTime(int iEpgID) const;
133 
138  std::pair<CDateTime, CDateTime> GetFirstAndLastEPGDate() const;
139 
146  CDateTime GetMinStartTime(int iEpgID, const CDateTime& minStart) const;
147 
154  CDateTime GetMaxEndTime(int iEpgID, const CDateTime& maxEnd) const;
155 
161  std::vector<std::shared_ptr<CPVREpgInfoTag>> GetEpgTags(
162  const PVREpgSearchData& searchData) const;
163 
170  std::shared_ptr<CPVREpgInfoTag> GetEpgTagByUniqueBroadcastID(
171  int iEpgID, unsigned int iUniqueBroadcastId) const;
172 
179  std::shared_ptr<CPVREpgInfoTag> GetEpgTagByDatabaseID(int iEpgID, int iDatabaseId) const;
180 
187  std::shared_ptr<CPVREpgInfoTag> GetEpgTagByStartTime(int iEpgID,
188  const CDateTime& startTime) const;
189 
196  std::shared_ptr<CPVREpgInfoTag> GetEpgTagByMinStartTime(int iEpgID,
197  const CDateTime& minStartTime) const;
198 
205  std::shared_ptr<CPVREpgInfoTag> GetEpgTagByMaxEndTime(int iEpgID,
206  const CDateTime& maxEndTime) const;
207 
215  std::vector<std::shared_ptr<CPVREpgInfoTag>> GetEpgTagsByMinStartMaxEndTime(
216  int iEpgID, const CDateTime& minStartTime, const CDateTime& maxEndTime) const;
217 
225  std::vector<std::shared_ptr<CPVREpgInfoTag>> GetEpgTagsByMinEndMaxStartTime(
226  int iEpgID, const CDateTime& minEndTime, const CDateTime& maxStartTime) const;
227 
237  const CDateTime& minEndTime,
238  const CDateTime& maxStartTime);
239 
246  bool GetLastEpgScanTime(int iEpgId, CDateTime* lastScan) const;
247 
254  bool QueuePersistLastEpgScanTimeQuery(int iEpgId, const CDateTime& lastScanTime);
255 
261  bool QueueDeleteLastEpgScanTimeQuery(const CPVREpg& table);
262 
269  int Persist(const CPVREpg& epg, bool bQueueWrite);
270 
277  bool DeleteEpgTags(int iEpgId, const CDateTime& maxEndTime);
278 
284  bool DeleteEpgTags(int iEpgId);
285 
291  bool QueueDeleteEpgTags(int iEpgId);
292 
298  bool QueuePersistQuery(const CPVREpgInfoTag& tag);
299 
303  int GetLastEPGId() const;
304 
306 
309 
315  std::vector<std::shared_ptr<CPVREpgSearchFilter>> GetSavedSearches(bool bRadio) const;
316 
323  std::shared_ptr<CPVREpgSearchFilter> GetSavedSearchById(bool bRadio, int iId) const;
324 
330  bool Persist(CPVREpgSearchFilter& epgSearch);
331 
337  bool UpdateSavedSearchLastExecuted(const CPVREpgSearchFilter& epgSearch);
338 
344  bool Delete(const CPVREpgSearchFilter& epgSearch);
345 
350  bool DeleteSavedSearches();
351 
353 
354  private:
358  void CreateTables() override;
359 
363  void CreateAnalytics() override;
364 
369  void UpdateTables(int version) override;
370 
371  int GetMinSchemaVersion() const override { return 4; }
372 
373  std::shared_ptr<CPVREpgInfoTag> CreateEpgTag(
374  const std::unique_ptr<dbiplus::Dataset>& pDS) const;
375 
376  std::shared_ptr<CPVREpgSearchFilter> CreateEpgSearchFilter(
377  bool bRadio, const std::unique_ptr<dbiplus::Dataset>& pDS) const;
378 
379  mutable CCriticalSection m_critSection;
380  };
381 }
CDateTime GetMaxEndTime(int iEpgID, const CDateTime &maxEnd) const
Get the end time of the first tag with an end time less than the given max time.
Definition: EpgDatabase.cpp:525
bool DeleteEpg()
Remove all EPG information from the database.
Definition: EpgDatabase.cpp:329
CPVREpgDatabase()=default
Create a new instance of the EPG database.
bool HasTags(int iEpgID) const
Check whether this EPG has any tags.
Definition: EpgDatabase.cpp:463
std::vector< std::string > GetAllIconPaths(int iEpgID) const
Get all icon paths for a given EPG id.
Definition: EpgDatabase.cpp:1064
Definition: EpgSearchFilter.h:23
~CPVREpgDatabase() override=default
Destroy this instance.
Definition: EpgDatabase.h:31
void Unlock()
Unlock the database.
Definition: EpgDatabase.cpp:47
std::shared_ptr< CPVREpgInfoTag > GetEpgTagByStartTime(int iEpgID, const CDateTime &startTime) const
Get an EPG tag given its EPG ID and start time.
Definition: EpgDatabase.cpp:842
int Persist(const CPVREpg &epg, bool bQueueWrite)
Persist an EPG table. It&#39;s entries are not persisted.
Definition: EpgDatabase.cpp:1140
Definition: ContextMenuManager.h:24
bool GetLastEpgScanTime(int iEpgId, CDateTime *lastScan) const
Get the last stored EPG scan time.
Definition: EpgDatabase.cpp:1090
std::pair< CDateTime, CDateTime > GetFirstAndLastEPGDate() const
Get the start and end time across all EPGs.
Definition: EpgDatabase.cpp:484
bool Delete(const CPVREpgSearchFilter &epgSearch)
Delete a saved search.
Definition: EpgDatabase.cpp:1474
CDateTime GetMinStartTime(int iEpgID, const CDateTime &minStart) const
Get the start time of the first tag with a start time greater than the given min time.
Definition: EpgDatabase.cpp:508
std::shared_ptr< CPVREpgInfoTag > GetEpgTagByUniqueBroadcastID(int iEpgID, unsigned int iUniqueBroadcastId) const
Get an EPG tag given its EPG id and unique broadcast ID.
Definition: EpgDatabase.cpp:788
std::vector< std::shared_ptr< CPVREpgInfoTag > > GetEpgTagsByMinEndMaxStartTime(int iEpgID, const CDateTime &minEndTime, const CDateTime &maxStartTime) const
Get all EPG tags matching the given EPG id, min end time and max start time.
Definition: EpgDatabase.cpp:974
std::vector< std::shared_ptr< CPVREpgSearchFilter > > GetSavedSearches(bool bRadio) const
Get all saved searches from the database.
Definition: EpgDatabase.cpp:1332
std::shared_ptr< CPVREpgInfoTag > GetEpgTagByMaxEndTime(int iEpgID, const CDateTime &maxEndTime) const
Get the next EPG tag matching the given EPG id and max end time.
Definition: EpgDatabase.cpp:903
bool QueueDeleteTagQuery(const CPVREpgInfoTag &tag)
Write the query to delete the given EPG tag to db query queue.
Definition: EpgDatabase.cpp:364
std::shared_ptr< CPVREpgSearchFilter > GetSavedSearchById(bool bRadio, int iId) const
Get the saved search matching the given id.
Definition: EpgDatabase.cpp:1359
int GetLastEPGId() const
Definition: EpgDatabase.cpp:1273
bool QueueDeleteEpgTags(int iEpgId)
Queue the erase all EPG tags with the given epg ID.
Definition: EpgDatabase.cpp:1191
CDateTime GetLastEndTime(int iEpgID) const
Get the end time of the last tag in this EPG.
Definition: EpgDatabase.cpp:472
bool QueueDeleteEpgQuery(const CPVREpg &table)
Queue deletionof an EPG table.
Definition: EpgDatabase.cpp:343
Definition: EpgInfoTag.h:28
bool QueueDeleteLastEpgScanTimeQuery(const CPVREpg &table)
Write the query to delete the last scan time for the given EPG to db query queue. ...
Definition: EpgDatabase.cpp:1120
std::vector< std::shared_ptr< CPVREpg > > GetAll()
Get all EPG tables from the database. Does not get the EPG tables&#39; entries.
Definition: EpgDatabase.cpp:380
bool UpdateSavedSearchLastExecuted(const CPVREpgSearchFilter &epgSearch)
Update time last executed for the given search.
Definition: EpgDatabase.cpp:1461
std::shared_ptr< CPVREpgInfoTag > GetEpgTagByMinStartTime(int iEpgID, const CDateTime &minStartTime) const
Get the next EPG tag matching the given EPG id and min start time.
Definition: EpgDatabase.cpp:872
Definition: Database.h:26
std::vector< std::shared_ptr< CPVREpgInfoTag > > GetEpgTagsByMinStartMaxEndTime(int iEpgID, const CDateTime &minStartTime, const CDateTime &maxEndTime) const
Get all EPG tags matching the given EPG id, min start time and max end time.
Definition: EpgDatabase.cpp:934
bool QueuePersistQuery(const CPVREpgInfoTag &tag)
Write the query to persist the given EPG tag to db query queue.
Definition: EpgDatabase.cpp:1203
bool QueuePersistLastEpgScanTimeQuery(int iEpgId, const CDateTime &lastScanTime)
Write the query to update the last scan time for the given EPG to db query queue. ...
Definition: EpgDatabase.cpp:1111
const char * GetBaseDBName() const override
Get the default sqlite database filename.
Definition: EpgDatabase.h:75
int GetSchemaVersion() const override
Get the minimal database version that is required to operate correctly.
Definition: EpgDatabase.h:69
DateTime class, which uses FileTime as it&#39;s base.
Definition: XBDateTime.h:63
std::vector< std::shared_ptr< CPVREpgInfoTag > > GetEpgTags(const PVREpgSearchData &searchData) const
Get all EPG tags matching the given search criteria.
Definition: EpgDatabase.cpp:667
std::shared_ptr< CPVREpgInfoTag > GetEpgTagByDatabaseID(int iEpgID, int iDatabaseId) const
Get an EPG tag given its EPG id and database ID.
Definition: EpgDatabase.cpp:815
bool Open() override
Open the database.
Definition: EpgDatabase.cpp:30
Definition: EpgSearchData.h:20
Definition: Epg.h:31
bool DeleteEpgTags(int iEpgId, const CDateTime &maxEndTime)
Erase all EPG tags with the given epg ID and an end time less than the given time.
Definition: EpgDatabase.cpp:1169
bool QueueDeleteEpgTagsByMinEndMaxStartTimeQuery(int iEpgID, const CDateTime &minEndTime, const CDateTime &maxStartTime)
Write the query to delete all EPG tags in range of given EPG id, min end time and max start time to d...
Definition: EpgDatabase.cpp:1014
bool DeleteSavedSearches()
Delete all saved searches.
Definition: EpgDatabase.cpp:1490
void Close() override
Close the database.
Definition: EpgDatabase.cpp:36
void Lock()
Lock the database.
Definition: EpgDatabase.cpp:42
std::vector< std::shared_ptr< CPVREpgInfoTag > > GetAllEpgTags(int iEpgID) const
Get all tags for a given EPG id.
Definition: EpgDatabase.cpp:1038