2 * Copyright (C) 2012-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
11 #include "dbwrappers/Database.h"
12 #include "threads/CriticalSection.h"
20 class CPVRChannelGroup
;
21 class CPVRChannelGroupMember
;
22 class CPVRChannelGroups
;
26 class CPVRTimerInfoTag
;
29 /** The PVR database */
31 static constexpr int CHANNEL_COMMIT_QUERY_COUNT_LIMIT
= 10000;
33 class CPVRDatabase
: public CDatabase
37 * @brief Create a new instance of the PVR database.
39 CPVRDatabase() = default;
40 ~CPVRDatabase() override
= default;
43 * @brief Open the database.
44 * @return True if it was opened successfully, false otherwise.
49 * @brief Close the database.
51 void Close() override
;
54 * @brief Lock the database.
59 * @brief Unlock the database.
64 * @brief Get the minimal database version that is required to operate correctly.
65 * @return The minimal database version.
67 int GetSchemaVersion() const override
{ return 44; }
70 * @brief Get the default sqlite database filename.
71 * @return The default filename.
73 const char* GetBaseDBName() const override
{ return "TV"; }
75 /*! @name Client methods */
79 * @brief Remove all client entries from the database.
80 * @return True if all client entries were removed, false otherwise.
85 * @brief Add or update a client entry in the database
86 * @param client The client to persist.
87 * @return True when persisted, false otherwise.
89 bool Persist(const CPVRClient
& client
);
92 * @brief Remove a client entry from the database
93 * @param client The client to remove.
94 * @return True if the client was removed, false otherwise.
96 bool Delete(const CPVRClient
& client
);
99 * @brief Get the priority for a given client from the database.
100 * @param client The client.
101 * @return The priority.
103 int GetPriority(const CPVRClient
& client
) const;
105 /*! @name Channel methods */
109 * @brief Remove all channels from the database.
110 * @return True if all channels were removed, false otherwise.
112 bool DeleteChannels();
115 * @brief Get channels from the database.
116 * @param bRadio Whether to fetch radio or TV channels.
117 * @param clients The PVR clients the channels should be loaded for. Leave empty for all clients.
118 * @param results The container for the channels.
119 * @return The number of channels loaded.
122 const std::vector
<std::shared_ptr
<CPVRClient
>>& clients
,
123 std::map
<std::pair
<int, int>, std::shared_ptr
<CPVRChannel
>>& results
) const;
126 * @brief Add or update a channel entry in the database
127 * @param channel The channel to persist.
128 * @param bCommit queue only or queue and commit
129 * @return True when persisted or queued, false otherwise.
131 bool Persist(CPVRChannel
& channel
, bool bCommit
);
134 * @brief Remove a channel entry from the database
135 * @param channel The channel to remove.
136 * @return True if the channel was removed, false otherwise.
138 bool QueueDeleteQuery(const CPVRChannel
& channel
);
142 /*! @name Channel group member methods */
146 * @brief Remove a channel group member entry from the database
147 * @param groupMember The group member to remove.
148 * @return True if the member was removed, false otherwise.
150 bool QueueDeleteQuery(const CPVRChannelGroupMember
& groupMember
);
154 /*! @name Channel provider methods */
158 * @brief Remove all providers from the database.
159 * @return True if all providers were removed, false otherwise.
161 bool DeleteProviders();
164 * @brief Add or update a provider entry in the database
165 * @param provider The provider to persist.
166 * @param updateRecord True if record to be updated, false for insert
167 * @return True when persisted, false otherwise.
169 bool Persist(CPVRProvider
& provider
, bool updateRecord
= false);
172 * @brief Remove a provider entry from the database
173 * @param provider The provider to remove.
174 * @return True if the provider was removed, false otherwise.
176 bool Delete(const CPVRProvider
& provider
);
179 * @brief Get the list of providers from the database
180 * @param results The providers to store the results in.
181 * @param clients The PVR clients the providers should be loaded for. Leave empty for all clients.
182 * @return The amount of providers that were added.
184 bool Get(CPVRProviders
& results
, const std::vector
<std::shared_ptr
<CPVRClient
>>& clients
) const;
187 * @brief Get the maximum provider id in the database
188 * @return The maximum provider id in the database
190 int GetMaxProviderId() const;
194 /*! @name Channel group methods */
198 * @brief Remove all channel groups from the database
199 * @return True if all channel groups were removed.
201 bool DeleteChannelGroups();
204 * @brief Delete a channel group and all its members from the database.
205 * @param group The group to delete.
206 * @return True if the group was deleted successfully, false otherwise.
208 bool Delete(const CPVRChannelGroup
& group
);
211 * @brief Get all local channel groups.
212 * @param results The container to store the results in.
213 * @return The number of groups loaded.
215 int GetLocalGroups(CPVRChannelGroups
& results
) const;
218 * @brief Get client-supplied channel groups.
219 * @param results The container to store the results in.
220 * @param clients The PVR clients the groups should be loaded for. Leave empty for all clients.
221 * @return The number of groups loaded.
223 int Get(CPVRChannelGroups
& results
,
224 const std::vector
<std::shared_ptr
<CPVRClient
>>& clients
) const;
227 * @brief Get the members of a channel group.
228 * @param group The group to get the members for.
229 * @param clients The PVR clients the group members should be loaded for. Leave empty for all clients.
230 * @return The group members.
232 std::vector
<std::shared_ptr
<CPVRChannelGroupMember
>> Get(
233 const CPVRChannelGroup
& group
,
234 const std::vector
<std::shared_ptr
<CPVRClient
>>& clients
) const;
237 * @brief Add or update a channel group entry in the database.
238 * @param group The group to persist.
239 * @return True if the group was persisted successfully, false otherwise.
241 bool Persist(CPVRChannelGroup
& group
);
244 * @brief Reset all epg ids to 0
245 * @return True when reset, false otherwise.
249 /*! @name Timer methods */
253 * @brief Get the timers.
254 * @param timers The container for the timers.
255 * @param clients The PVR clients the timers should be loaded for. Leave empty for all clients.
256 * @return The timers.
258 std::vector
<std::shared_ptr
<CPVRTimerInfoTag
>> GetTimers(
259 CPVRTimers
& timers
, const std::vector
<std::shared_ptr
<CPVRClient
>>& clients
) const;
262 * @brief Add or update a timer entry in the database
263 * @param channel The timer to persist.
264 * @return True if persisted, false otherwise.
266 bool Persist(CPVRTimerInfoTag
& timer
);
269 * @brief Remove a timer from the database
270 * @param timer The timer to remove.
271 * @return True if the timer was removed, false otherwise.
273 bool Delete(const CPVRTimerInfoTag
& timer
);
276 * @brief Remove all timer entries from the database.
277 * @return True if all timer entries were removed, false otherwise.
282 /*! @name Client methods */
286 * @brief Updates the last watched timestamp for the channel
287 * @param channel the channel
288 * @param groupId the id of the group used to watch the channel
289 * @return whether the update was successful
291 bool UpdateLastWatched(const CPVRChannel
& channel
, int groupId
);
294 * @brief Updates the last watched timestamp for the channel group
295 * @param group the group
296 * @return whether the update was successful
298 bool UpdateLastWatched(const CPVRChannelGroup
& group
);
302 * @brief Updates the last opened timestamp for the channel group
303 * @param group the group
304 * @return whether the update was successful
306 bool UpdateLastOpened(const CPVRChannelGroup
& group
);
311 * @brief Create the PVR database tables.
313 void CreateTables() override
;
314 void CreateAnalytics() override
;
316 * @brief Update an old version of the database.
317 * @param version The version to update the database from.
319 void UpdateTables(int version
) override
;
320 int GetMinSchemaVersion() const override
{ return 11; }
322 int GetGroups(CPVRChannelGroups
& results
, const std::string
& query
) const;
324 bool PersistGroupMembers(const CPVRChannelGroup
& group
);
326 bool PersistChannels(const CPVRChannelGroup
& group
);
328 bool RemoveChannelsFromGroup(const CPVRChannelGroup
& group
);
330 mutable CCriticalSection m_critSection
;