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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
|
/*
* 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.
*/
#include "PVRDatabase.h"
#include <utility>
#include "ServiceBroker.h"
#include "addons/PVRClient.h"
#include "dbwrappers/dataset.h"
#include "settings/AdvancedSettings.h"
#include "settings/Settings.h"
#include "utils/StringUtils.h"
#include "utils/log.h"
#include "pvr/channels/PVRChannelGroups.h"
using namespace dbiplus;
using namespace PVR;
bool CPVRDatabase::Open()
{
CSingleLock lock(m_critSection);
return CDatabase::Open(g_advancedSettings.m_databaseTV);
}
void CPVRDatabase::Close()
{
CSingleLock lock(m_critSection);
CDatabase::Close();
}
void CPVRDatabase::CreateTables()
{
CSingleLock lock(m_critSection);
CLog::LogF(LOGINFO, "Creating PVR database tables");
CLog::LogFC(LOGDEBUG, LOGPVR, "Creating table 'channels'");
m_pDS->exec(
"CREATE TABLE channels ("
"idChannel integer primary key, "
"iUniqueId integer, "
"bIsRadio bool, "
"bIsHidden bool, "
"bIsUserSetIcon bool, "
"bIsUserSetName bool, "
"bIsLocked bool, "
"sIconPath varchar(255), "
"sChannelName varchar(64), "
"bIsVirtual bool, "
"bEPGEnabled bool, "
"sEPGScraper varchar(32), "
"iLastWatched integer, "
"iClientId integer, " //! @todo use mapping table
"idEpg integer"
")"
);
CLog::LogFC(LOGDEBUG, LOGPVR, "Creating table 'channelgroups'");
m_pDS->exec(
"CREATE TABLE channelgroups ("
"idGroup integer primary key,"
"bIsRadio bool, "
"iGroupType integer, "
"sName varchar(64), "
"iLastWatched integer, "
"bIsHidden bool, "
"iPosition integer"
")"
);
CLog::LogFC(LOGDEBUG, LOGPVR, "Creating table 'map_channelgroups_channels'");
m_pDS->exec(
"CREATE TABLE map_channelgroups_channels ("
"idChannel integer, "
"idGroup integer, "
"iChannelNumber integer, "
"iSubChannelNumber integer"
")"
);
CLog::LogFC(LOGDEBUG, LOGPVR, "Creating table 'clients'");
m_pDS->exec(
"CREATE TABLE clients ("
"idClient integer primary key, "
"iPriority integer"
")"
);
}
void CPVRDatabase::CreateAnalytics()
{
CSingleLock lock(m_critSection);
CLog::LogF(LOGINFO, "Creating PVR database indices");
m_pDS->exec("CREATE INDEX idx_clients_idClient on clients(idClient);");
m_pDS->exec("CREATE UNIQUE INDEX idx_channels_iClientId_iUniqueId on channels(iClientId, iUniqueId);");
m_pDS->exec("CREATE INDEX idx_channelgroups_bIsRadio on channelgroups(bIsRadio);");
m_pDS->exec("CREATE UNIQUE INDEX idx_idGroup_idChannel on map_channelgroups_channels(idGroup, idChannel);");
}
void CPVRDatabase::UpdateTables(int iVersion)
{
CSingleLock lock(m_critSection);
if (iVersion < 13)
m_pDS->exec("ALTER TABLE channels ADD idEpg integer;");
if (iVersion < 20)
m_pDS->exec("ALTER TABLE channels ADD bIsUserSetIcon bool");
if (iVersion < 21)
m_pDS->exec("ALTER TABLE channelgroups ADD iGroupType integer");
if (iVersion < 22)
m_pDS->exec("ALTER TABLE channels ADD bIsLocked bool");
if (iVersion < 23)
m_pDS->exec("ALTER TABLE channelgroups ADD iLastWatched integer");
if (iVersion < 24)
m_pDS->exec("ALTER TABLE channels ADD bIsUserSetName bool");
if (iVersion < 25)
m_pDS->exec("DROP TABLE IF EXISTS channelsettings");
if (iVersion < 26)
{
m_pDS->exec("ALTER TABLE channels ADD iClientSubChannelNumber integer");
m_pDS->exec("UPDATE channels SET iClientSubChannelNumber = 0");
m_pDS->exec("ALTER TABLE map_channelgroups_channels ADD iSubChannelNumber integer");
m_pDS->exec("UPDATE map_channelgroups_channels SET iSubChannelNumber = 0");
}
if (iVersion < 27)
m_pDS->exec("ALTER TABLE channelgroups ADD bIsHidden bool");
if (iVersion < 28)
m_pDS->exec("DROP TABLE clients");
if (iVersion < 29)
m_pDS->exec("ALTER TABLE channelgroups ADD iPosition integer");
if (iVersion < 32)
m_pDS->exec("CREATE TABLE clients (idClient integer primary key, iPriority integer)");
}
/********** Client methods **********/
bool CPVRDatabase::DeleteClients()
{
CLog::LogFC(LOGDEBUG, LOGPVR, "Deleting all clients from the database");
CSingleLock lock(m_critSection);
return DeleteValues("clients");
}
bool CPVRDatabase::Persist(const CPVRClient &client)
{
if (client.GetID() == PVR_INVALID_CLIENT_ID)
return false;
CLog::LogFC(LOGDEBUG, LOGPVR, "Persisting client '%s' to database", client.ID().c_str());
CSingleLock lock(m_critSection);
const std::string strQuery = PrepareSQL("REPLACE INTO clients (idClient, iPriority) VALUES (%i, %i);",
client.GetID(), client.GetPriority());
return ExecuteQuery(strQuery);
}
bool CPVRDatabase::Delete(const CPVRClient &client)
{
if (client.GetID() == PVR_INVALID_CLIENT_ID)
return false;
CLog::LogFC(LOGDEBUG, LOGPVR, "Deleting client '%s' from the database", client.ID().c_str());
CSingleLock lock(m_critSection);
Filter filter;
filter.AppendWhere(PrepareSQL("idClient = '%i'", client.GetID()));
return DeleteValues("clients", filter);
}
int CPVRDatabase::GetPriority(const CPVRClient &client)
{
if (client.GetID() == PVR_INVALID_CLIENT_ID)
return 0;
CLog::LogFC(LOGDEBUG, LOGPVR, "Getting priority for client '%s' from the database", client.ID().c_str());
CSingleLock lock(m_critSection);
const std::string strWhereClause = PrepareSQL("idClient = '%i'", client.GetID());
const std::string strValue = GetSingleValue("clients", "iPriority", strWhereClause);
if (strValue.empty())
return 0;
return atoi(strValue.c_str());
}
/********** Channel methods **********/
bool CPVRDatabase::DeleteChannels(void)
{
CLog::LogFC(LOGDEBUG, LOGPVR, "Deleting all channels from the database");
CSingleLock lock(m_critSection);
return DeleteValues("channels");
}
bool CPVRDatabase::Delete(const CPVRChannel &channel)
{
/* invalid channel */
if (channel.ChannelID() <= 0)
return false;
CLog::LogFC(LOGDEBUG, LOGPVR, "Deleting channel '%s' from the database", channel.ChannelName().c_str());
Filter filter;
filter.AppendWhere(PrepareSQL("idChannel = %u", channel.ChannelID()));
CSingleLock lock(m_critSection);
return DeleteValues("channels", filter);
}
int CPVRDatabase::Get(CPVRChannelGroup &results, bool bCompressDB)
{
int iReturn(0);
bool bIgnoreEpgDB = CServiceBroker::GetSettings().GetBool(CSettings::SETTING_EPG_IGNOREDBFORCLIENT);
std::string strQuery = PrepareSQL("SELECT channels.idChannel, channels.iUniqueId, channels.bIsRadio, channels.bIsHidden, channels.bIsUserSetIcon, channels.bIsUserSetName, "
"channels.sIconPath, channels.sChannelName, channels.bIsVirtual, channels.bEPGEnabled, channels.sEPGScraper, channels.iLastWatched, channels.iClientId, channels.bIsLocked, "
"map_channelgroups_channels.iChannelNumber, map_channelgroups_channels.iSubChannelNumber, channels.idEpg "
"FROM map_channelgroups_channels "
"LEFT JOIN channels ON channels.idChannel = map_channelgroups_channels.idChannel "
"WHERE map_channelgroups_channels.idGroup = %u", results.GroupID());
CSingleLock lock(m_critSection);
if (ResultQuery(strQuery))
{
try
{
while (!m_pDS->eof())
{
CPVRChannelPtr channel = CPVRChannelPtr(new CPVRChannel());
channel->m_iChannelId = m_pDS->fv("idChannel").get_asInt();
channel->m_iUniqueId = m_pDS->fv("iUniqueId").get_asInt();
channel->m_bIsRadio = m_pDS->fv("bIsRadio").get_asBool();
channel->m_bIsHidden = m_pDS->fv("bIsHidden").get_asBool();
channel->m_bIsUserSetIcon = m_pDS->fv("bIsUserSetIcon").get_asBool();
channel->m_bIsUserSetName = m_pDS->fv("bIsUserSetName").get_asBool();
channel->m_bIsLocked = m_pDS->fv("bIsLocked").get_asBool();
channel->m_strIconPath = m_pDS->fv("sIconPath").get_asString();
channel->m_strChannelName = m_pDS->fv("sChannelName").get_asString();
channel->m_bEPGEnabled = m_pDS->fv("bEPGEnabled").get_asBool();
channel->m_strEPGScraper = m_pDS->fv("sEPGScraper").get_asString();
channel->m_iLastWatched = static_cast<time_t>(m_pDS->fv("iLastWatched").get_asInt());
channel->m_iClientId = m_pDS->fv("iClientId").get_asInt();
channel->m_iEpgId = bIgnoreEpgDB ? -1 : m_pDS->fv("idEpg").get_asInt();
channel->UpdateEncryptionName();
PVRChannelGroupMember newMember(channel,
CPVRChannelNumber(static_cast<unsigned int>(m_pDS->fv("iChannelNumber").get_asInt()),
static_cast<unsigned int>(m_pDS->fv("iSubChannelNumber").get_asInt())),
0);
results.m_sortedMembers.emplace_back(newMember);
results.m_members.insert(std::make_pair(channel->StorageId(), newMember));
m_pDS->next();
++iReturn;
}
m_pDS->close();
}
catch (...)
{
CLog::LogF(LOGERROR, "Couldn't load channels from PVR database");
}
}
else
{
CLog::LogF(LOGERROR, "PVR database query failed");
}
m_pDS->close();
if (iReturn > 0 && bCompressDB)
Compress(true);
return iReturn;
}
/********** Channel group methods **********/
bool CPVRDatabase::RemoveChannelsFromGroup(const CPVRChannelGroup &group)
{
Filter filter;
filter.AppendWhere(PrepareSQL("idGroup = %u", group.GroupID()));
CSingleLock lock(m_critSection);
return DeleteValues("map_channelgroups_channels", filter);
}
bool CPVRDatabase::GetCurrentGroupMembers(const CPVRChannelGroup &group, std::vector<int> &members)
{
bool bReturn(false);
/* invalid group id */
if (group.GroupID() <= 0)
{
CLog::LogF(LOGERROR, "Invalid channel group id: %d", group.GroupID());
return false;
}
CSingleLock lock(m_critSection);
const std::string strCurrentMembersQuery = PrepareSQL("SELECT idChannel FROM map_channelgroups_channels WHERE idGroup = %u", group.GroupID());
if (ResultQuery(strCurrentMembersQuery))
{
try
{
while (!m_pDS->eof())
{
members.emplace_back(m_pDS->fv("idChannel").get_asInt());
m_pDS->next();
}
m_pDS->close();
bReturn = true;
}
catch (...)
{
CLog::LogF(LOGERROR, "Couldn't load channels from PVR database");
}
}
else
{
CLog::LogF(LOGERROR, "PVR database query failed");
}
return bReturn;
}
bool CPVRDatabase::DeleteChannelsFromGroup(const CPVRChannelGroup &group, const std::vector<int> &channelsToDelete)
{
bool bDelete(true);
unsigned int iDeletedChannels(0);
/* invalid group id */
if (group.GroupID() <= 0)
{
CLog::LogF(LOGERROR, "Invalid channel group id: %d", group.GroupID());
return false;
}
CSingleLock lock(m_critSection);
while (iDeletedChannels < channelsToDelete.size())
{
std::string strChannelsToDelete;
for (unsigned int iChannelPtr = 0; iChannelPtr + iDeletedChannels < channelsToDelete.size() && iChannelPtr < 50; iChannelPtr++)
strChannelsToDelete += StringUtils::Format(", %d", channelsToDelete.at(iDeletedChannels + iChannelPtr));
if (!strChannelsToDelete.empty())
{
strChannelsToDelete.erase(0, 2);
Filter filter;
filter.AppendWhere(PrepareSQL("idGroup = %u", group.GroupID()));
filter.AppendWhere(PrepareSQL("idChannel IN (%s)", strChannelsToDelete.c_str()));
bDelete = DeleteValues("map_channelgroups_channels", filter) && bDelete;
}
iDeletedChannels += 50;
}
return bDelete;
}
int CPVRDatabase::GetClientIdByChannelId(int iChannelId)
{
const std::string strQuery = PrepareSQL("idChannel = %u", iChannelId);
const std::string strValue = GetSingleValue("channels", "iClientId", strQuery);
if (!strValue.empty())
return std::atoi(strValue.c_str());
return PVR_INVALID_CLIENT_ID;
}
bool CPVRDatabase::RemoveStaleChannelsFromGroup(const CPVRChannelGroup &group)
{
bool bDelete(true);
/* invalid group id */
if (group.GroupID() <= 0)
{
CLog::LogF(LOGERROR, "Invalid channel group id: %d", group.GroupID());
return false;
}
CSingleLock lock(m_critSection);
if (!group.IsInternalGroup())
{
/* First remove channels that don't exist in the main channels table */
// XXX work around for frodo: fix this up so it uses one query for all db types
// mysql doesn't support subqueries when deleting and sqlite doesn't support joins when deleting
if (StringUtils::EqualsNoCase(g_advancedSettings.m_databaseTV.type, "mysql"))
{
const std::string strQuery = PrepareSQL("DELETE m FROM map_channelgroups_channels m LEFT JOIN channels c ON (c.idChannel = m.idChannel) WHERE c.idChannel IS NULL");
bDelete = ExecuteQuery(strQuery);
}
else
{
Filter filter;
filter.AppendWhere("idChannel IN (SELECT m.idChannel FROM map_channelgroups_channels m LEFT JOIN channels on m.idChannel = channels.idChannel WHERE channels.idChannel IS NULL)");
bDelete = DeleteValues("map_channelgroups_channels", filter);
}
}
if (group.HasChannels())
{
std::vector<int> currentMembers;
if (GetCurrentGroupMembers(group, currentMembers))
{
std::vector<int> channelsToDelete;
for (int iChannelId : currentMembers)
{
if (!group.IsGroupMember(iChannelId))
{
int iClientId = GetClientIdByChannelId(iChannelId);
if (iClientId == PVR_INVALID_CLIENT_ID || !group.IsMissingChannelsFromClient(iClientId))
{
channelsToDelete.emplace_back(iChannelId);
}
}
}
if (!channelsToDelete.empty())
bDelete = DeleteChannelsFromGroup(group, channelsToDelete) && bDelete;
}
}
else
{
Filter filter;
filter.AppendWhere(PrepareSQL("idGroup = %u", group.GroupID()));
bDelete = DeleteValues("map_channelgroups_channels", filter) && bDelete;
}
return bDelete;
}
bool CPVRDatabase::DeleteChannelGroups(void)
{
CLog::LogFC(LOGDEBUG, LOGPVR, "Deleting all channel groups from the database");
CSingleLock lock(m_critSection);
return DeleteValues("channelgroups") && DeleteValues("map_channelgroups_channels");
}
bool CPVRDatabase::Delete(const CPVRChannelGroup &group)
{
/* invalid group id */
if (group.GroupID() <= 0)
{
CLog::LogF(LOGERROR, "Invalid channel group id: %d", group.GroupID());
return false;
}
CSingleLock lock(m_critSection);
bool bIgnoreChannel = false;
std::vector<int> currentMembers;
if (GetCurrentGroupMembers(group, currentMembers))
{
for (int iChannelId : currentMembers)
{
int iClientId = GetClientIdByChannelId(iChannelId);
if (iClientId != PVR_INVALID_CLIENT_ID && group.IsMissingChannelGroupMembersFromClient(iClientId))
{
bIgnoreChannel = true;
break;
}
}
}
if (!bIgnoreChannel)
{
Filter filter;
filter.AppendWhere(PrepareSQL("idGroup = %u", group.GroupID()));
filter.AppendWhere(PrepareSQL("bIsRadio = %u", group.IsRadio()));
return RemoveChannelsFromGroup(group) && DeleteValues("channelgroups", filter);
}
return true;
}
bool CPVRDatabase::Get(CPVRChannelGroups &results)
{
bool bReturn = false;
CSingleLock lock(m_critSection);
const std::string strQuery = PrepareSQL("SELECT * from channelgroups WHERE bIsRadio = %u", results.IsRadio());
if (ResultQuery(strQuery))
{
try
{
while (!m_pDS->eof())
{
CPVRChannelGroup data(m_pDS->fv("bIsRadio").get_asBool(), m_pDS->fv("idGroup").get_asInt(), m_pDS->fv("sName").get_asString());
data.SetGroupType(m_pDS->fv("iGroupType").get_asInt());
data.SetLastWatched(static_cast<time_t>(m_pDS->fv("iLastWatched").get_asInt()));
data.SetHidden(m_pDS->fv("bIsHidden").get_asBool());
data.SetPosition(m_pDS->fv("iPosition").get_asInt());
results.Update(data);
CLog::LogFC(LOGDEBUG, LOGPVR, "Group '%s' loaded from PVR database", data.GroupName().c_str());
m_pDS->next();
}
m_pDS->close();
bReturn = true;
}
catch (...)
{
CLog::LogF(LOGERROR, "Couldn't load channels from PVR database");
}
}
return bReturn;
}
int CPVRDatabase::Get(CPVRChannelGroup &group, const CPVRChannelGroup &allGroup)
{
int iReturn = -1;
/* invalid group id */
if (group.GroupID() < 0)
{
CLog::LogF(LOGERROR, "Invalid channel group id: %d", group.GroupID());
return -1;
}
CSingleLock lock(m_critSection);
const std::string strQuery = PrepareSQL("SELECT idChannel, iChannelNumber, iSubChannelNumber FROM map_channelgroups_channels "
"WHERE idGroup = %u ORDER BY iChannelNumber", group.GroupID());
if (ResultQuery(strQuery))
{
iReturn = 0;
// create a map to speedup data lookup
std::map<int, CPVRChannelPtr> allChannels;
for (const auto& groupMember : allGroup.GetMembers())
{
allChannels.insert(std::make_pair(groupMember.channel->ChannelID(), groupMember.channel));
}
try
{
while (!m_pDS->eof())
{
int iChannelId = m_pDS->fv("idChannel").get_asInt();
const auto& channel = allChannels.find(iChannelId);
if (channel != allChannels.end())
{
PVRChannelGroupMember newMember(channel->second,
CPVRChannelNumber(static_cast<unsigned int>(m_pDS->fv("iChannelNumber").get_asInt()),
static_cast<unsigned int>(m_pDS->fv("iSubChannelNumber").get_asInt())),
0);
group.m_sortedMembers.emplace_back(newMember);
group.m_members.insert(std::make_pair(channel->second->StorageId(), newMember));
++iReturn;
}
else
{
// remove the channel from the table if it doesn't exist on client (anymore)
int iClientId = GetClientIdByChannelId(iChannelId);
if (iClientId == PVR_INVALID_CLIENT_ID || !allGroup.IsMissingChannelsFromClient(iClientId))
{
Filter filter;
filter.AppendWhere(PrepareSQL("idGroup = %u", group.GroupID()));
filter.AppendWhere(PrepareSQL("idChannel = %u", iChannelId));
DeleteValues("map_channelgroups_channels", filter);
}
}
m_pDS->next();
}
m_pDS->close();
}
catch(...)
{
CLog::LogF(LOGERROR, "Failed to get channels");
}
}
if (iReturn > 0)
group.SortByChannelNumber();
return iReturn;
}
bool CPVRDatabase::PersistChannels(CPVRChannelGroup &group)
{
bool bReturn(true);
CPVRChannelPtr channel;
for (const auto& groupMember : group.m_members)
{
channel = groupMember.second.channel;
if (channel->IsChanged() || channel->IsNew())
{
if (Persist(*channel, false))
{
groupMember.second.channel->Persisted();
bReturn = true;
}
}
}
bReturn &= CommitInsertQueries();
if (bReturn)
{
std::string strQuery;
std::string strValue;
for (const auto& groupMember : group.m_members)
{
channel = groupMember.second.channel;
strQuery = PrepareSQL("iUniqueId = %u AND iClientId = %u", channel->UniqueID(), channel->ClientID());
strValue = GetSingleValue("channels", "idChannel", strQuery);
if (!strValue.empty() && StringUtils::IsInteger(strValue))
channel->SetChannelID(atoi(strValue.c_str()));
}
}
return bReturn;
}
bool CPVRDatabase::PersistGroupMembers(const CPVRChannelGroup &group)
{
bool bReturn = true;
bool bRemoveChannels = true;
std::string strQuery;
CSingleLock lock(group.m_critSection);
if (group.HasChannels())
{
for (const auto& groupMember : group.m_sortedMembers)
{
const std::string strWhereClause = PrepareSQL("idChannel = %u AND idGroup = %u AND iChannelNumber = %u AND iSubChannelNumber = %u",
groupMember.channel->ChannelID(), group.GroupID(), groupMember.channelNumber.GetChannelNumber(), groupMember.channelNumber.GetSubChannelNumber());
const std::string strValue = GetSingleValue("map_channelgroups_channels", "idChannel", strWhereClause);
if (strValue.empty())
{
strQuery = PrepareSQL("REPLACE INTO map_channelgroups_channels ("
"idGroup, idChannel, iChannelNumber, iSubChannelNumber) "
"VALUES (%i, %i, %i, %i);",
group.GroupID(), groupMember.channel->ChannelID(), groupMember.channelNumber.GetChannelNumber(), groupMember.channelNumber.GetSubChannelNumber());
QueueInsertQuery(strQuery);
}
}
bReturn = CommitInsertQueries();
bRemoveChannels = RemoveStaleChannelsFromGroup(group);
}
return bReturn && bRemoveChannels;
}
/********** Client methods **********/
bool CPVRDatabase::ResetEPG(void)
{
CSingleLock lock(m_critSection);
const std::string strQuery = PrepareSQL("UPDATE channels SET idEpg = 0");
return ExecuteQuery(strQuery);
}
bool CPVRDatabase::Persist(CPVRChannelGroup &group)
{
bool bReturn(false);
if (group.GroupName().empty())
{
CLog::LogF(LOGERROR, "Empty group name");
return bReturn;
}
std::string strQuery;
bReturn = true;
CSingleLock lock(m_critSection);
{
/* insert a new entry when this is a new group, or replace the existing one otherwise */
if (group.GroupID() <= 0)
strQuery = PrepareSQL("INSERT INTO channelgroups (bIsRadio, iGroupType, sName, iLastWatched, bIsHidden, iPosition) VALUES (%i, %i, '%s', %u, %i, %i)",
(group.IsRadio() ? 1 :0), group.GroupType(), group.GroupName().c_str(), static_cast<unsigned int>(group.LastWatched()), group.IsHidden(), group.GetPosition());
else
strQuery = PrepareSQL("REPLACE INTO channelgroups (idGroup, bIsRadio, iGroupType, sName, iLastWatched, bIsHidden, iPosition) VALUES (%i, %i, %i, '%s', %u, %i, %i)",
group.GroupID(), (group.IsRadio() ? 1 :0), group.GroupType(), group.GroupName().c_str(), static_cast<unsigned int>(group.LastWatched()), group.IsHidden(), group.GetPosition());
bReturn = ExecuteQuery(strQuery);
/* set the group id if it was <= 0 */
if (bReturn && group.GroupID() <= 0)
{
CSingleLock lock(group.m_critSection);
group.m_iGroupId = (int) m_pDS->lastinsertid();
}
}
/* only persist the channel data for the internal groups */
if (group.IsInternalGroup())
bReturn &= PersistChannels(group);
/* persist the group member entries */
if (bReturn)
bReturn = PersistGroupMembers(group);
return bReturn;
}
bool CPVRDatabase::Persist(CPVRChannel &channel, bool bCommit)
{
bool bReturn(false);
/* invalid channel */
if (channel.UniqueID() <= 0)
{
CLog::LogF(LOGERROR, "Invalid channel uid: %d", channel.UniqueID());
return bReturn;
}
CSingleLock lock(m_critSection);
// Note: Do not use channel.ChannelID value to check presence of channel in channels table. It might not yet be set correctly.
std::string strQuery = PrepareSQL("iUniqueId = %u AND iClientId = %u", channel.UniqueID(), channel.ClientID());
const std::string strValue = GetSingleValue("channels", "idChannel", strQuery);
if (strValue.empty())
{
/* new channel */
strQuery = PrepareSQL("INSERT INTO channels ("
"iUniqueId, bIsRadio, bIsHidden, bIsUserSetIcon, bIsUserSetName, bIsLocked, "
"sIconPath, sChannelName, bIsVirtual, bEPGEnabled, sEPGScraper, iLastWatched, iClientId, "
"idEpg) "
"VALUES (%i, %i, %i, %i, %i, %i, '%s', '%s', %i, %i, '%s', %u, %i, %i)",
channel.UniqueID(), (channel.IsRadio() ? 1 :0), (channel.IsHidden() ? 1 : 0), (channel.IsUserSetIcon() ? 1 : 0), (channel.IsUserSetName() ? 1 : 0), (channel.IsLocked() ? 1 : 0),
channel.IconPath().c_str(), channel.ChannelName().c_str(), 0, (channel.EPGEnabled() ? 1 : 0), channel.EPGScraper().c_str(), static_cast<unsigned int>(channel.LastWatched()), channel.ClientID(),
channel.EpgID());
}
else
{
/* update channel */
strQuery = PrepareSQL("REPLACE INTO channels ("
"iUniqueId, bIsRadio, bIsHidden, bIsUserSetIcon, bIsUserSetName, bIsLocked, "
"sIconPath, sChannelName, bIsVirtual, bEPGEnabled, sEPGScraper, iLastWatched, iClientId, "
"idChannel, idEpg) "
"VALUES (%i, %i, %i, %i, %i, %i, '%s', '%s', %i, %i, '%s', %u, %i, %s, %i)",
channel.UniqueID(), (channel.IsRadio() ? 1 :0), (channel.IsHidden() ? 1 : 0), (channel.IsUserSetIcon() ? 1 : 0), (channel.IsUserSetName() ? 1 : 0), (channel.IsLocked() ? 1 : 0),
channel.IconPath().c_str(), channel.ChannelName().c_str(), 0, (channel.EPGEnabled() ? 1 : 0), channel.EPGScraper().c_str(), static_cast<unsigned int>(channel.LastWatched()), channel.ClientID(),
strValue.c_str(),
channel.EpgID());
}
if (QueueInsertQuery(strQuery))
{
bReturn = true;
if (bCommit)
bReturn = CommitInsertQueries();
}
return bReturn;
}
bool CPVRDatabase::UpdateLastWatched(const CPVRChannel &channel)
{
CSingleLock lock(m_critSection);
const std::string strQuery = PrepareSQL("UPDATE channels SET iLastWatched = %u WHERE idChannel = %d",
static_cast<unsigned int>(channel.LastWatched()), channel.ChannelID());
return ExecuteQuery(strQuery);
}
bool CPVRDatabase::UpdateLastWatched(const CPVRChannelGroup &group)
{
CSingleLock lock(m_critSection);
const std::string strQuery = PrepareSQL("UPDATE channelgroups SET iLastWatched = %u WHERE idGroup = %d",
static_cast<unsigned int>(group.LastWatched()), group.GroupID());
return ExecuteQuery(strQuery);
}
|