summaryrefslogtreecommitdiffstats
path: root/xbmc/pvr/PVRDatabase.h
blob: 1e9cbf5756e5fd20e64b03274a42d3df77fd2b04 (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
/*
 *  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 <map>
#include <vector>

namespace PVR
{
  class CPVRChannel;
  class CPVRChannelGroup;
  class CPVRChannelGroupMember;
  class CPVRChannelGroups;
  class CPVRProvider;
  class CPVRProviders;
  class CPVRClient;
  class CPVRTimerInfoTag;
  class CPVRTimers;

  /** The PVR database */

  static constexpr int CHANNEL_COMMIT_QUERY_COUNT_LIMIT = 10000;

  class CPVRDatabase : public CDatabase
  {
  public:
    /*!
     * @brief Create a new instance of the PVR database.
     */
    CPVRDatabase() = default;
    ~CPVRDatabase() 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 40; }

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

    /*! @name Client methods */
    //@{

    /*!
     * @brief Remove all client entries from the database.
     * @return True if all client entries were removed, false otherwise.
     */
    bool DeleteClients();

    /*!
     * @brief Add or update a client entry in the database
     * @param client The client to persist.
     * @return True when persisted, false otherwise.
     */
    bool Persist(const CPVRClient& client);

    /*!
     * @brief Remove a client entry from the database
     * @param client The client to remove.
     * @return True if the client was removed, false otherwise.
     */
    bool Delete(const CPVRClient& client);

    /*!
     * @brief Get the priority for a given client from the database.
     * @param client The client.
     * @return The priority.
     */
    int GetPriority(const CPVRClient& client);

    /*! @name Channel methods */
    //@{

    /*!
     * @brief Remove all channels from the database.
     * @return True if all channels were removed, false otherwise.
     */
    bool DeleteChannels();

    /*!
     * @brief Get channels from the database.
     * @param bRadio Whether to fetch radio or TV channels.
     * @param clients The PVR clients the channels should be loaded for. Leave empty for all clients.
     * @param results The container for the channels.
     * @return The number of channels loaded.
     */
    int Get(bool bRadio,
            const std::vector<std::shared_ptr<CPVRClient>>& clients,
            std::map<std::pair<int, int>, std::shared_ptr<CPVRChannel>>& results) const;

    /*!
     * @brief Add or update a channel entry in the database
     * @param channel The channel to persist.
     * @param bCommit queue only or queue and commit
     * @return True when persisted or queued, false otherwise.
     */
    bool Persist(CPVRChannel& channel, bool bCommit);

    /*!
     * @brief Remove a channel entry from the database
     * @param channel The channel to remove.
     * @return True if the channel was removed, false otherwise.
     */
    bool QueueDeleteQuery(const CPVRChannel& channel);

    //@}

    /*! @name Channel group member methods */
    //@{

    /*!
     * @brief Remove a channel group member entry from the database
     * @param groupMember The group member to remove.
     * @return True if the member was removed, false otherwise.
     */
    bool QueueDeleteQuery(const CPVRChannelGroupMember& groupMember);

    //@}

    /*! @name Channel provider methods */
    //@{

    /*!
     * @brief Remove all providers from the database.
     * @return True if all providers were removed, false otherwise.
     */
    bool DeleteProviders();

    /*!
     * @brief Add or update a provider entry in the database
     * @param provider The provider to persist.
     * @param updateRecord True if record to be updated, false for insert
     * @return True when persisted, false otherwise.
     */
    bool Persist(CPVRProvider& provider, bool updateRecord = false);

    /*!
     * @brief Remove a provider entry from the database
     * @param provider The provider to remove.
     * @return True if the provider was removed, false otherwise.
     */
    bool Delete(const CPVRProvider& provider);

    /*!
     * @brief Get the list of providers from the database
     * @param results The providers to store the results in.
     * @param clients The PVR clients the providers should be loaded for. Leave empty for all clients.
     * @return The amount of providers that were added.
     */
    bool Get(CPVRProviders& results, const std::vector<std::shared_ptr<CPVRClient>>& clients) const;

    /*!
     * @brief Get the maximum provider id in the database
     * @return The maximum provider id in the database
     */
    int GetMaxProviderId();

    //@}

    /*! @name Channel group methods */
    //@{

    /*!
     * @brief Remove all channel groups from the database
     * @return True if all channel groups were removed.
     */
    bool DeleteChannelGroups();

    /*!
     * @brief Delete a channel group and all its members from the database.
     * @param group The group to delete.
     * @return True if the group was deleted successfully, false otherwise.
     */
    bool Delete(const CPVRChannelGroup& group);

    /*!
     * @brief Get the channel groups.
     * @param results The container to store the results in.
     * @return The number of groups loaded.
     */
    int Get(CPVRChannelGroups& results) const;

    /*!
     * @brief Get the members of a channel group.
     * @param group The group to get the members for.
     * @param clients The PVR clients the group members should be loaded for. Leave empty for all clients.
     * @return The group members.
     */
    std::vector<std::shared_ptr<CPVRChannelGroupMember>> Get(
        const CPVRChannelGroup& group,
        const std::vector<std::shared_ptr<CPVRClient>>& clients) const;

    /*!
     * @brief Add or update a channel group entry in the database.
     * @param group The group to persist.
     * @return True if the group was persisted successfully, false otherwise.
     */
    bool Persist(CPVRChannelGroup& group);

    /*!
     * @brief Reset all epg ids to 0
     * @return True when reset, false otherwise.
     */
    bool ResetEPG();

    /*! @name Timer methods */
    //@{

    /*!
     * @brief Get the timers.
     * @param timers The container for the timers.
     * @param clients The PVR clients the timers should be loaded for. Leave empty for all clients.
     * @return The timers.
     */
    std::vector<std::shared_ptr<CPVRTimerInfoTag>> GetTimers(
        CPVRTimers& timers, const std::vector<std::shared_ptr<CPVRClient>>& clients) const;

    /*!
     * @brief Add or update a timer entry in the database
     * @param channel The timer to persist.
     * @return True if persisted, false otherwise.
     */
    bool Persist(CPVRTimerInfoTag& timer);

    /*!
     * @brief Remove a timer from the database
     * @param timer The timer to remove.
     * @return True if the timer was removed, false otherwise.
     */
    bool Delete(const CPVRTimerInfoTag& timer);

    /*!
     * @brief Remove all timer entries from the database.
     * @return True if all timer entries were removed, false otherwise.
     */
    bool DeleteTimers();
    //@}

    /*! @name Client methods */
    //@{

    /*!
     * @brief Updates the last watched timestamp for the channel
     * @param channel the channel
     * @return whether the update was successful
     */
    bool UpdateLastWatched(const CPVRChannel& channel);

    /*!
     * @brief Updates the last watched timestamp for the channel group
     * @param group the group
     * @return whether the update was successful
     */
    bool UpdateLastWatched(const CPVRChannelGroup& group);
    //@}

    /*!
     * @brief Updates the last opened timestamp for the channel group
     * @param group the group
     * @return whether the update was successful
     */
    bool UpdateLastOpened(const CPVRChannelGroup& group);
    //@}

  private:
    /*!
     * @brief Create the PVR database tables.
     */
    void CreateTables() override;
    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 11; }

    bool PersistGroupMembers(const CPVRChannelGroup& group);

    bool PersistChannels(const CPVRChannelGroup& group);

    bool RemoveChannelsFromGroup(const CPVRChannelGroup& group);

    mutable CCriticalSection m_critSection;
  };
}