forked from mixxxdj/mixxx
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathautodjcratesdao.cpp
More file actions
1260 lines (1161 loc) · 51.8 KB
/
Copy pathautodjcratesdao.cpp
File metadata and controls
1260 lines (1161 loc) · 51.8 KB
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
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "library/dao/autodjcratesdao.h"
#include <QRandomGenerator>
#include <QtDebug>
#include "library/dao/trackschema.h"
#include "library/queryutil.h"
#include "library/trackcollection.h"
#include "library/trackcollectionmanager.h"
#include "library/trackset/crate/crate.h"
#include "library/trackset/crate/crateschema.h"
#include "mixer/playerinfo.h"
#include "mixer/playermanager.h"
#include "moc_autodjcratesdao.cpp"
#include "track/track.h"
#if !defined(VERBOSE_DEBUG_LOG)
// set to true for verbose debug logs
#define VERBOSE_DEBUG_LOG false
#endif
#define AUTODJCRATESTABLE_TRACKID "track_id"
#define AUTODJCRATESTABLE_CRATEREFS "craterefs"
#define AUTODJCRATESTABLE_TIMESPLAYED "timesplayed"
#define AUTODJCRATESTABLE_AUTODJREFS "autodjrefs"
#define AUTODJCRATESTABLE_LASTPLAYED "lastplayed"
#define AUTODJCRATES_TABLE "temp_autodj_crates"
// A table of all tracks that are assigned to the AutoDJ via a crate
// Columns:
// INTEGER AUTODJCRATESTABLE_TRACKID -> Tracks that are member of crate assigned to AutoDj
// INTEGER AUTODJCRATESTABLE_CRATEREFS -> counts the occurrences of track in auto dj crates
// INTEGER AUTODJCRATESTABLE_TIMESPLAYED -> library played counter
// INTEGER AUTODJCRATESTABLE_AUTODJREFS -> counts the occurrences of the track in the AutoDj queue
// DATETIME AUTODJCRATESTABLE_LASTPLAYED -> from the history feature
#define AUTODJACTIVETRACKS_TABLE "temp_autodj_activetracks"
// The same table like above, but of all tracks that not already queued into Auto DJ
// ordered by TimesPlayed.
namespace {
// Percentage of most and least played tracks to ignore [0,50)
constexpr int kLeastPreferredPercent = 15;
// These consts are only used for DEBUG_ASSERTs
constexpr int kLeastPreferredPercentMin = 0;
constexpr int kLeastPreferredPercentMax = 50;
int bounded_rand(int highest) {
return QRandomGenerator::global()->bounded(highest);
}
} // anonymous namespace
AutoDJCratesDAO::AutoDJCratesDAO(
int iAutoDjPlaylistId,
TrackCollectionManager* pTrackCollectionManager,
UserSettingsPointer pConfig)
: m_iAutoDjPlaylistId(iAutoDjPlaylistId),
m_pTrackCollectionManager(pTrackCollectionManager),
m_database(pTrackCollectionManager->internalCollection()->database()),
m_pConfig(pConfig),
// The database has not been created yet.
m_bAutoDjCratesDbCreated(false),
// By default, active tracks are not tracks that haven't been played in
// a while.
m_bUseIgnoreTime(false) {
}
AutoDJCratesDAO::~AutoDJCratesDAO() {
}
// Create the temporary auto-DJ-crates table.
// Done the first time it's used, since the user might not even make
// use of this feature.
void AutoDJCratesDAO::createAndConnectAutoDjCratesDatabase() {
// If the use of tracks that haven't been played in a while has changed,
// then the active-tracks view must be recreated.
bool bUseIgnoreTime = m_pConfig->getValue(
ConfigKey("[Auto DJ]", "UseIgnoreTime"), false);
if (m_bAutoDjCratesDbCreated) {
if (m_bUseIgnoreTime != bUseIgnoreTime) {
// Do all this in a single transaction.
ScopedTransaction oTransaction(m_database);
// Get rid of the old active-tracks view.
QSqlQuery oQuery(m_database);
oQuery.exec ("DROP VIEW IF EXISTS " AUTODJACTIVETRACKS_TABLE);
if (!oQuery.exec()) {
LOG_FAILED_QUERY(oQuery);
return;
}
// Create the new active-tracks view.
if (!createActiveTracksView(bUseIgnoreTime)) {
return;
}
// Remember the new setting.
m_bUseIgnoreTime = bUseIgnoreTime;
// Commit these changes.
oTransaction.commit();
}
} else {
m_bUseIgnoreTime = bUseIgnoreTime;
}
// If this database has already been created, skip this.
if (m_bAutoDjCratesDbCreated) {
return;
}
// Do all of this in a single transaction.
ScopedTransaction oTransaction(m_database);
// The auto-DJ-crates table contains the track ID, the number of references
// to that track ID in all of the auto-DJ crates, the number of times that
// track has been played, and the number of references to the track in the
// auto-DJ playlist (or in loaded decks). It filters out tracks that have
// been deleted from the database (i.e. "hidden" tracks).
// Create an empty table.
QSqlQuery oQuery(m_database);
// CREATE TEMP TABLE temp_autodj_crates (track_id INTEGER UNIQUE, craterefs INTEGER, timesplayed INTEGER, autodjrefs INTEGER, lastplayed DATETIME);
//oQuery.exec ("DROP TABLE IF EXISTS " AUTODJCRATES_TABLE);
QString strQuery("CREATE TEMP TABLE " AUTODJCRATES_TABLE
" (" AUTODJCRATESTABLE_TRACKID " INTEGER UNIQUE, "
AUTODJCRATESTABLE_CRATEREFS " INTEGER, "
AUTODJCRATESTABLE_TIMESPLAYED " INTEGER, "
AUTODJCRATESTABLE_AUTODJREFS " INTEGER, "
AUTODJCRATESTABLE_LASTPLAYED " DATETIME)");
oQuery.prepare(strQuery);
if (!oQuery.exec()) {
LOG_FAILED_QUERY(oQuery);
return;
}
strQuery = QStringLiteral(
"INSERT INTO " AUTODJCRATES_TABLE "(" AUTODJCRATESTABLE_TRACKID
"," AUTODJCRATESTABLE_CRATEREFS "," AUTODJCRATESTABLE_TIMESPLAYED
"," AUTODJCRATESTABLE_LASTPLAYED "," AUTODJCRATESTABLE_AUTODJREFS
") SELECT " CRATE_TRACKS_TABLE
".%5," // TRACKID
"COUNT(*)," // CRATEREFS
LIBRARY_TABLE ".%2," // TIMESPLAYED
LIBRARY_TABLE
".%3," // LASTPLAYED
"0" // AUTODJREFS = default
" FROM " CRATE_TRACKS_TABLE
" INNER JOIN " LIBRARY_TABLE " ON " LIBRARY_TABLE ".%1=" CRATE_TRACKS_TABLE
".%5"
" WHERE " LIBRARY_TABLE
".%4=0"
" AND " CRATE_TRACKS_TABLE ".%6 IN (SELECT %7 FROM " CRATE_TABLE
" WHERE %8=1)"
" GROUP BY " CRATE_TRACKS_TABLE ".%5")
.arg(LIBRARYTABLE_ID, // %1
LIBRARYTABLE_TIMESPLAYED, // %2
LIBRARYTABLE_LAST_PLAYED_AT, // %3
LIBRARYTABLE_MIXXXDELETED, // %4
CRATETRACKSTABLE_TRACKID, // %5
CRATETRACKSTABLE_CRATEID, // %6
CRATETABLE_ID, // %7
CRATETABLE_AUTODJ_SOURCE); // %8
#if !defined(VERBOSE_DEBUG_LOG)
qDebug().noquote()
<< "Populating " AUTODJCRATES_TABLE " using the following SQL query:"
<< strQuery;
#endif
oQuery.prepare(strQuery);
if (!oQuery.exec()) {
LOG_FAILED_QUERY(oQuery);
return;
}
// Fill out the number of auto-DJ-playlist references.
if (!updateAutoDjPlaylistReferences()) {
return;
}
// Fill out the last-played date/time.
if (!updateLastPlayedDateTime()) {
return;
}
// Create the active-tracks view.
//oQuery.exec ("DROP VIEW IF EXISTS " AUTODJACTIVETRACKS_TABLE);
if (!createActiveTracksView (m_bUseIgnoreTime)) {
return;
}
// Make a list of the IDs of every set-log playlist.
// SELECT id FROM Playlists WHERE hidden = 2;
oQuery.prepare(QString("SELECT %1 FROM " PLAYLIST_TABLE " WHERE %2 = %3")
.arg(PLAYLISTTABLE_ID, // %1
PLAYLISTTABLE_HIDDEN, // %2
QString::number(PlaylistDAO::PLHT_SET_LOG))); // %3
if (oQuery.exec()) {
while (oQuery.next()) {
m_lstSetLogPlaylistIds.append(oQuery.value(0).toInt());
}
} else {
LOG_FAILED_QUERY(oQuery);
return;
}
// Now the auto-DJ crates database is initialized.
// Externally-driven updates to the database from now on are driven by
// signals.
oTransaction.commit();
// Be notified when a track is modified.
// We only care when the number of times it's been played changes.
connect(m_pTrackCollectionManager->internalCollection(),
&TrackCollection::trackDirty,
this,
&AutoDJCratesDAO::slotTrackDirty);
// Be notified when the status of crates changes.
connect(m_pTrackCollectionManager->internalCollection(),
&TrackCollection::crateInserted,
this,
&AutoDJCratesDAO::slotCrateInserted);
connect(m_pTrackCollectionManager->internalCollection(),
&TrackCollection::crateDeleted,
this,
&AutoDJCratesDAO::slotCrateDeleted);
connect(m_pTrackCollectionManager->internalCollection(),
&TrackCollection::crateUpdated,
this,
&AutoDJCratesDAO::slotCrateUpdated);
connect(m_pTrackCollectionManager->internalCollection(),
&TrackCollection::crateTracksChanged,
this,
&AutoDJCratesDAO::slotCrateTracksChanged);
// Be notified when playlists are added/removed.
// We only care about set-log playlists.
connect(&m_pTrackCollectionManager->internalCollection()->getPlaylistDAO(),
&PlaylistDAO::added,
this,
&AutoDJCratesDAO::slotPlaylistAdded);
connect(&m_pTrackCollectionManager->internalCollection()->getPlaylistDAO(),
&PlaylistDAO::deleted,
this,
&AutoDJCratesDAO::slotPlaylistDeleted);
// Be notified when tracks are added/removed from playlists.
// We only care about the auto-DJ playlist and the set-log playlists.
connect(&m_pTrackCollectionManager->internalCollection()->getPlaylistDAO(),
&PlaylistDAO::trackAdded,
this,
&AutoDJCratesDAO::slotPlaylistTrackAdded);
connect(&m_pTrackCollectionManager->internalCollection()->getPlaylistDAO(),
&PlaylistDAO::trackRemoved,
this,
&AutoDJCratesDAO::slotPlaylistTrackRemoved);
// Be notified when tracks are loaded to, or unloaded from, a deck.
// These count as auto-DJ references, i.e. prevent the track from being
// selected randomly.
connect(&PlayerInfo::instance(),
&PlayerInfo::trackChanged,
this,
&AutoDJCratesDAO::slotPlayerInfoTrackChanged);
// Remember that the auto-DJ-crates database has been created.
m_bAutoDjCratesDbCreated = true;
}
// Create the active-tracks view.
bool AutoDJCratesDAO::createActiveTracksView (bool a_bUseIgnoreTime) {
// Create the active-tracks view. This is a list of all tracks loaded into
// the auto-DJ-crates database, excluding all tracks already in the auto-DJ
// playlist, sorted by the number of times the track has been played, and
// limited by either the active percentage or by the number of tracks that
// have never been played, whichever is larger.
//
// At one point, I hoped that I could create this table in a single SQL
// statement, with a "limit" clause that dynamically updated the size of the
// view as the state of the system changed. Unfortunately, that limit
// clause is only evaluated once. For posterity, though, here's the monster
// SQL query that attempted to create that version:
//
// CREATE TEMP VIEW temp_autodj_activetracks AS
// SELECT * FROM temp_autodj_crates
// WHERE autodjrefs = 0
// ORDER BY timesplayed, lastplayed LIMIT (
// SELECT MAX(count)
// FROM (
// SELECT COUNT(*) AS count
// FROM temp_autodj_crates WHERE timesplayed = 0
// UNION ALL SELECT (
// count * (
// SELECT value
// FROM settings WHERE
// name="mixxx.db.model.autodjcrates.active_percentage")
// / 100) AS count
// FROM (
// SELECT COUNT(*) AS count
// FROM temp_autodj_crates)));
// CREATE TEMP VIEW temp_autodj_activetracks AS
// SELECT *
// FROM temp_autodj_crates
// WHERE autodjrefs = 0
// ORDER BY timesplayed, lastplayed;
QSqlQuery oQuery(m_database);
QString strTimesPlayed;
if (!a_bUseIgnoreTime) {
strTimesPlayed = AUTODJCRATESTABLE_TIMESPLAYED ", ";
}
oQuery.prepare(QString("CREATE TEMP VIEW " AUTODJACTIVETRACKS_TABLE
" AS SELECT * FROM " AUTODJCRATES_TABLE " WHERE "
AUTODJCRATESTABLE_AUTODJREFS " = 0 ORDER BY %1"
AUTODJCRATESTABLE_LASTPLAYED)
.arg(strTimesPlayed)); // %1
if (!oQuery.exec()) {
LOG_FAILED_QUERY(oQuery);
return false;
}
return true;
}
// Update the number of auto-DJ-playlist references to each track in the
// auto-DJ-crates database.
bool AutoDJCratesDAO::updateAutoDjPlaylistReferences() {
QSqlQuery oQuery(m_database);
// Rebuild the auto-DJ-playlist reference count.
// INSERT OR REPLACE INTO temp_autodj_crates (
// track_id, craterefs, timesplayed, autodjrefs)
// SELECT *
// FROM (
// SELECT PlaylistTracks.track_id, craterefs, timesplayed, COUNT (*) AS newautodjrefs
// FROM PlaylistTracks, temp_autodj_crates
// WHERE PlaylistTracks.playlist_id IN (
// SELECT id FROM Playlists WHERE hidden = PLHT_AUTO_DJ)
// AND PlaylistTracks.track_id = temp_autodj_crates.track_id
// GROUP BY PlaylistTracks.track_id)
// WHERE newautodjrefs > 0;
QString strHidden;
strHidden.setNum(PlaylistDAO::PLHT_AUTO_DJ);
QString strQuery(QString ("INSERT OR REPLACE INTO " AUTODJCRATES_TABLE
" (" AUTODJCRATESTABLE_TRACKID ", " AUTODJCRATESTABLE_CRATEREFS ", "
AUTODJCRATESTABLE_TIMESPLAYED ", " AUTODJCRATESTABLE_AUTODJREFS ")"
" SELECT * FROM (SELECT " PLAYLIST_TRACKS_TABLE ".%1, "
AUTODJCRATESTABLE_CRATEREFS ", " AUTODJCRATESTABLE_TIMESPLAYED
", COUNT (*) AS new" AUTODJCRATESTABLE_AUTODJREFS " FROM "
PLAYLIST_TRACKS_TABLE ", " AUTODJCRATES_TABLE " WHERE "
PLAYLIST_TRACKS_TABLE ".%2 IN (SELECT %3 FROM " PLAYLIST_TABLE
" WHERE %4 = %5) AND " PLAYLIST_TRACKS_TABLE ".%1 = "
AUTODJCRATES_TABLE "." AUTODJCRATESTABLE_TRACKID " GROUP BY "
PLAYLIST_TRACKS_TABLE ".%1) WHERE new" AUTODJCRATESTABLE_AUTODJREFS
" > 0")
.arg(PLAYLISTTRACKSTABLE_TRACKID, // %1
PLAYLISTTRACKSTABLE_PLAYLISTID, // %2
PLAYLISTTABLE_ID, // %3
PLAYLISTTABLE_HIDDEN, // %4
strHidden)); // %5
oQuery.prepare(strQuery);
if (!oQuery.exec()) {
LOG_FAILED_QUERY(oQuery);
return false;
}
// Incorporate all tracks loaded into decks.
// Each track has to be done as a separate database query, in case the same
// track is loaded into multiple decks.
int numDecks = PlayerInfo::instance().numDecks();
for (int i = 0; i < numDecks; ++i) {
QString group = PlayerManager::groupForDeck(i);
TrackPointer pTrack = PlayerInfo::instance().getTrackInfo(group);
if (pTrack) {
TrackId trackId(pTrack->getId());
// UPDATE temp_autodj_crates SET autodjrefs = autodjrefs + 1 WHERE track_id IN (:track_id);
oQuery.prepare("UPDATE " AUTODJCRATES_TABLE " SET "
AUTODJCRATESTABLE_AUTODJREFS " = " AUTODJCRATESTABLE_AUTODJREFS
" + 1 WHERE " AUTODJCRATESTABLE_TRACKID " IN (:track_id)");
oQuery.bindValue(":track_id", trackId.toVariant());
if (!oQuery.exec()) {
LOG_FAILED_QUERY(oQuery);
return false;
}
}
}
return true;
}
// Update the number of auto-DJ-playlist references to the given track in the
// auto-DJ-crates database.
bool AutoDJCratesDAO::updateAutoDjPlaylistReferencesForTrack(TrackId trackId) {
QSqlQuery oQuery(m_database);
// INSERT OR REPLACE INTO temp_autodj_crates (
// track_id, craterefs, timesplayed, autodjrefs)
// SELECT *
// FROM (
// SELECT :track_id AS new_track_id, craterefs, timesplayed, COUNT (*) AS newautodjrefs
// FROM PlaylistTracks, temp_autodj_crates
// WHERE PlaylistTracks.playlist_id IN (
// SELECT id
// FROM Playlists
// WHERE hidden = 1)
// AND PlaylistTracks.track_id = :track_id
// AND temp_autodj_crates.track_id = :track_id
// GROUP BY new_track_id)
// WHERE newautodjrefs > 0;
QString strHidden;
strHidden.setNum(PlaylistDAO::PLHT_AUTO_DJ);
oQuery.prepare(QString("INSERT OR REPLACE INTO " AUTODJCRATES_TABLE " ("
AUTODJCRATESTABLE_TRACKID ", " AUTODJCRATESTABLE_CRATEREFS ", "
AUTODJCRATESTABLE_TIMESPLAYED ", " AUTODJCRATESTABLE_AUTODJREFS
") SELECT * FROM (SELECT :track_id_1 AS new_track_id, "
AUTODJCRATESTABLE_CRATEREFS ", " AUTODJCRATESTABLE_TIMESPLAYED
", COUNT (*) AS new" AUTODJCRATESTABLE_AUTODJREFS " FROM "
PLAYLIST_TRACKS_TABLE ", " AUTODJCRATES_TABLE " WHERE "
PLAYLIST_TRACKS_TABLE ".%1 IN (SELECT %4 FROM " PLAYLIST_TABLE
" WHERE %2 = %5) AND " PLAYLIST_TRACKS_TABLE ".%3 = :track_id_2 AND "
AUTODJCRATES_TABLE "." AUTODJCRATESTABLE_TRACKID
" = :track_id_3 GROUP BY new_track_id) WHERE new"
AUTODJCRATESTABLE_AUTODJREFS " > 0")
.arg(PLAYLISTTRACKSTABLE_PLAYLISTID, // %1
PLAYLISTTABLE_HIDDEN, // %2
PLAYLISTTRACKSTABLE_TRACKID, // %3
PLAYLISTTABLE_ID, // %4
strHidden)); // %5
oQuery.bindValue(":track_id_1", trackId.toVariant());
oQuery.bindValue(":track_id_2", trackId.toVariant());
oQuery.bindValue(":track_id_3", trackId.toVariant());
if (!oQuery.exec()) {
LOG_FAILED_QUERY(oQuery);
return false;
}
// The update was successful.
return true;
}
// Update the last-played date/time for each track in the auto-DJ-crates
// database.
bool AutoDJCratesDAO::updateLastPlayedDateTime() {
QSqlQuery oQuery(m_database);
// Rebuild the auto-DJ-playlist last-played date/time.
// INSERT OR REPLACE INTO temp_autodj_crates (
// track_id, craterefs, timesplayed, autodjrefs, lastplayed)
// SELECT *
// FROM (
// SELECT PlaylistTracks.track_id, craterefs, timesplayed, autodjrefs, MAX(pl_datetime_added) AS newlastplayed
// FROM PlaylistTracks, temp_autodj_crates
// WHERE PlaylistTracks.playlist_id IN (
// SELECT id FROM Playlists WHERE hidden = PLHT_SET_LOG)
// AND PlaylistTracks.track_id = temp_autodj_crates.track_id
// GROUP BY PlaylistTracks.track_id)
// WHERE newlastplayed != "";
QString strSetLog;
strSetLog.setNum(PlaylistDAO::PLHT_SET_LOG);
QString strQuery(QString(
"INSERT OR REPLACE INTO " AUTODJCRATES_TABLE
" (" AUTODJCRATESTABLE_TRACKID ", " AUTODJCRATESTABLE_CRATEREFS
", " AUTODJCRATESTABLE_TIMESPLAYED ", " AUTODJCRATESTABLE_AUTODJREFS
", " AUTODJCRATESTABLE_LASTPLAYED
")"
" SELECT * FROM (SELECT " PLAYLIST_TRACKS_TABLE
".%1, " AUTODJCRATESTABLE_CRATEREFS
", " AUTODJCRATESTABLE_TIMESPLAYED ", " AUTODJCRATESTABLE_AUTODJREFS
", MAX(%3) AS new" AUTODJCRATESTABLE_LASTPLAYED
" FROM " PLAYLIST_TRACKS_TABLE ", " AUTODJCRATES_TABLE
" WHERE " PLAYLIST_TRACKS_TABLE
".%2 IN (SELECT %4 FROM " PLAYLIST_TABLE
" WHERE %5 = %6) AND " PLAYLIST_TRACKS_TABLE
".%1 = " AUTODJCRATES_TABLE "." AUTODJCRATESTABLE_TRACKID
" GROUP BY " PLAYLIST_TRACKS_TABLE
".%1) WHERE new" AUTODJCRATESTABLE_LASTPLAYED " != ''")
.arg(PLAYLISTTRACKSTABLE_TRACKID, // %1
PLAYLISTTRACKSTABLE_PLAYLISTID, // %2
PLAYLISTTRACKSTABLE_DATETIMEADDED, // %3
PLAYLISTTABLE_ID, // %4
PLAYLISTTABLE_HIDDEN, // %5
strSetLog)); // %6
oQuery.prepare(strQuery);
if (!oQuery.exec()) {
LOG_FAILED_QUERY(oQuery);
return false;
}
return true;
}
// Update the last-played date/time for the given track in the auto-DJ-crates
// database.
bool AutoDJCratesDAO::updateLastPlayedDateTimeForTrack(TrackId trackId) {
QSqlQuery oQuery(m_database);
// Update the last-played date/time for this track.
// INSERT OR REPLACE INTO temp_autodj_crates (
// track_id, craterefs, timesplayed, autodjrefs, lastplayed)
// SELECT *
// FROM (
// SELECT PlaylistTracks.track_id, craterefs, timesplayed, autodjrefs, MAX(pl_datetime_added) AS newlastplayed
// FROM PlaylistTracks, temp_autodj_crates
// WHERE PlaylistTracks.playlist_id IN (
// SELECT id FROM Playlists WHERE hidden = PLHT_SET_LOG)
// AND PlaylistTracks.track_id = :track_id
// AND PlaylistTracks.track_id = temp_autodj_crates.track_id
// GROUP BY PlaylistTracks.track_id)
// WHERE newlastplayed != "";
QString strSetLog;
strSetLog.setNum(PlaylistDAO::PLHT_SET_LOG);
oQuery.prepare(QString(
"INSERT OR REPLACE INTO " AUTODJCRATES_TABLE
" (" AUTODJCRATESTABLE_TRACKID ", " AUTODJCRATESTABLE_CRATEREFS
", " AUTODJCRATESTABLE_TIMESPLAYED ", " AUTODJCRATESTABLE_AUTODJREFS
", " AUTODJCRATESTABLE_LASTPLAYED
")"
" SELECT * FROM (SELECT " PLAYLIST_TRACKS_TABLE
".%1, " AUTODJCRATESTABLE_CRATEREFS
", " AUTODJCRATESTABLE_TIMESPLAYED ", " AUTODJCRATESTABLE_AUTODJREFS
", MAX(%3) AS new" AUTODJCRATESTABLE_LASTPLAYED
" FROM " PLAYLIST_TRACKS_TABLE ", " AUTODJCRATES_TABLE
" WHERE " PLAYLIST_TRACKS_TABLE
".%2 IN (SELECT %4 FROM " PLAYLIST_TABLE
" WHERE %5 = %6) AND " PLAYLIST_TRACKS_TABLE
".%1 = :track_id AND " PLAYLIST_TRACKS_TABLE
".%1 = " AUTODJCRATES_TABLE "." AUTODJCRATESTABLE_TRACKID
" GROUP BY " PLAYLIST_TRACKS_TABLE
".%1) WHERE new" AUTODJCRATESTABLE_LASTPLAYED " != ''")
.arg(PLAYLISTTRACKSTABLE_TRACKID, // %1
PLAYLISTTRACKSTABLE_PLAYLISTID, // %2
PLAYLISTTRACKSTABLE_DATETIMEADDED, // %3
PLAYLISTTABLE_ID, // %4
PLAYLISTTABLE_HIDDEN, // %5
strSetLog)); // %6
oQuery.bindValue(":track_id", trackId.toVariant());
if (!oQuery.exec()) {
LOG_FAILED_QUERY(oQuery);
return false;
}
// The update was successful.
return true;
}
// Get the ID, i.e. one that references library.id, of a random track.
// Returns an invalid track id if there was an error.
TrackId AutoDJCratesDAO::getRandomTrackId() {
// If necessary, create the temporary auto-DJ-crates database.
createAndConnectAutoDjCratesDatabase();
// Calculate the number of active-tracks that have never been played, and
// the total number of active-tracks.
QSqlQuery oQuery(m_database);
// SELECT COUNT(*) AS count
// FROM temp_autodj_activetracks
// WHERE timesplayed = 0
// UNION ALL SELECT COUNT(*) AS count
// FROM temp_autodj_activetracks;
oQuery.prepare("SELECT COUNT(*) AS count FROM " AUTODJACTIVETRACKS_TABLE
" WHERE " AUTODJCRATESTABLE_TIMESPLAYED
" = 0 UNION ALL SELECT COUNT(*) AS count FROM "
AUTODJACTIVETRACKS_TABLE);
if (!oQuery.exec()) {
LOG_FAILED_QUERY(oQuery);
DEBUG_ASSERT(!"failed query");
return TrackId();
}
int iUnplayedTracks = 0;
int iTotalTracks = 0;
if (oQuery.next()) {
iUnplayedTracks = oQuery.value(0).toInt();
if (oQuery.next()) {
iTotalTracks = oQuery.value(0).toInt();
}
}
// Get the active percentage (default 20%).
int minimumAvailablePercentage = m_pConfig->getValue(
ConfigKey("[Auto DJ]", "MinimumAvailable"), 20);
// If there are no tracks available not already in AutoDJ
// start re-adding tracks to AutoDJ
if (iTotalTracks == 0) {
return getRandomTrackIdFromAutoDj(minimumAvailablePercentage);
}
// Calculate the number of active-tracks. This is either the number of
// auto-DJ-crate tracks that have never been played, or the active
// percentage of the total number of tracks, whichever is larger.
int iMinAvailable = 0;
if (minimumAvailablePercentage) {
// if minimumAvailablePercentage not 0 % (disabled),
// have a minimum of one at least
iMinAvailable = qMax((iTotalTracks * minimumAvailablePercentage / 100), 1);
}
int iActiveTracks = qMax(iUnplayedTracks, iMinAvailable);
// The number of active-tracks might also be tracks that haven't been played
// in a while.
if (m_bUseIgnoreTime) {
// Get the current time, in UTC (since that's what sqlite uses).
QDateTime timeCurrent = QDateTime::currentDateTimeUtc();
// Subtract the replay age.
QTime timIgnoreTime = (QTime::fromString(m_pConfig->getValue(
ConfigKey("[Auto DJ]", "IgnoreTime"), "23:59"), "hh:mm"));
timeCurrent = timeCurrent.addSecs(-(timIgnoreTime.hour() * 3600
+ timIgnoreTime.minute() * 60));
// Convert the time to sqlite's format, which is similar to ISO date,
// but not quite.
QString strDateTime = timeCurrent.toString("yyyy-MM-dd hh:mm:ss");
// Count the number of tracks that haven't been played since this time.
// SELECT COUNT(*) FROM temp_autodj_activetracks WHERE lastplayed < :lastplayed;
int iIgnoreTimeTracks = 0;
oQuery.prepare("SELECT COUNT(*) FROM " AUTODJACTIVETRACKS_TABLE
" WHERE " AUTODJCRATESTABLE_LASTPLAYED " < :lastplayed");
oQuery.bindValue (":lastplayed", strDateTime);
if (oQuery.exec()) {
if (oQuery.next()) {
iIgnoreTimeTracks = oQuery.value(0).toInt();
}
} else {
LOG_FAILED_QUERY(oQuery);
return TrackId();
}
// Allow that to be a new maximum.
iActiveTracks = qMax(iActiveTracks, iIgnoreTimeTracks);
qDebug() << iActiveTracks << iIgnoreTimeTracks;
}
// If there are no tracks, let our caller know.
if (iActiveTracks == 0) {
qDebug() << "No random track available for Auto DJ";
return TrackId();
}
// Pick a random track.
// SELECT track_id
// FROM temp_autodj_activetracks LIMIT 1 OFFSET ABS (RANDOM() % :active);
oQuery.prepare("SELECT " AUTODJCRATESTABLE_TRACKID " FROM "
AUTODJACTIVETRACKS_TABLE " LIMIT 1 OFFSET ABS (RANDOM() % :active)");
oQuery.bindValue (":active", iActiveTracks);
if (!oQuery.exec()) {
LOG_FAILED_QUERY(oQuery);
DEBUG_ASSERT(!"failed query");
return TrackId();
}
if (oQuery.next()) {
// Give our caller the randomly-selected track.
return TrackId(oQuery.value(0));
} else {
DEBUG_ASSERT(false); // We should have exit earlier
qDebug() << "No random track available for Auto DJ";
return TrackId();
}
}
TrackId AutoDJCratesDAO::getRandomTrackIdFromAutoDj(int percentActive) {
// This function is called when all crate tracks are already in AutoDJ.
// So now the percentage applies to the AutoDJ tracks as well.
if (percentActive == 0 || m_bUseIgnoreTime) {
qDebug() << "All crate Tracks already added to Auto DJ";
return TrackId();
}
// Calculate the number of tracks in the AutoDJ playlist
// that are already queued up from the crates
QSqlQuery oQuery(m_database);
// SELECT COUNT(*) AS count
// FROM temp_autodj_crates
// WHERE autodjrefs > 0;
oQuery.prepare("SELECT COUNT(*) AS count FROM " AUTODJCRATES_TABLE
" WHERE " AUTODJCRATESTABLE_AUTODJREFS " > 0" );
if (!oQuery.exec()) {
LOG_FAILED_QUERY(oQuery);
DEBUG_ASSERT(!"failed query");
return TrackId();
}
VERIFY_OR_DEBUG_ASSERT(oQuery.next()) {
return TrackId();
}
int queuedTracks = oQuery.value(0).toInt();
// If there are no tracks, let our caller know.
if (queuedTracks == 0) {
qDebug() << "No tracks for re-add to Auto DJ";
return TrackId();
}
// Use the top percentage of the AutoDJ to re-add
int iActiveTracks = qMax((queuedTracks * percentActive / 100), 1);
// Pick a random track.
// SELECT DISTINCT track_id
// FROM temp_autodj_crates INNER JOIN Playlists
// ON temp_autodj_crates.track_id = PlaylistsTracks.track_id
// AND PlaylistsTracks.playlist_id = m_iAutoDjPlaylistId
// ORDER BY temp_autodj_crates.autodjrefs, PlaylistsTracks.position
// LIMIT 1 OFFSET ABS (RANDOM() % :active);
oQuery.prepare(QString("SELECT DISTINCT " AUTODJCRATES_TABLE "."
AUTODJCRATESTABLE_TRACKID " FROM "
AUTODJCRATES_TABLE " INNER JOIN " PLAYLIST_TRACKS_TABLE
" ON " AUTODJCRATES_TABLE "." AUTODJCRATESTABLE_TRACKID
" = " PLAYLIST_TRACKS_TABLE ".%1"
" AND " PLAYLIST_TRACKS_TABLE ".%2 = %3"
" ORDER BY " AUTODJCRATES_TABLE "." AUTODJCRATESTABLE_AUTODJREFS ", "
PLAYLIST_TRACKS_TABLE ".%4"
" LIMIT 1 OFFSET ABS (RANDOM() % :active)")
.arg(PLAYLISTTRACKSTABLE_TRACKID, // %1
PLAYLISTTRACKSTABLE_PLAYLISTID, // %2
QString::number(m_iAutoDjPlaylistId), // %3
PLAYLISTTABLE_POSITION)); // %4
oQuery.bindValue (":active", iActiveTracks);
if (!oQuery.exec()) {
LOG_FAILED_QUERY(oQuery);
DEBUG_ASSERT(!"failed query");
return TrackId();
}
if (oQuery.next()) {
// Give our caller the randomly-selected track.
return TrackId(oQuery.value(0));
} else {
DEBUG_ASSERT(false); // We should have exit earlier
qDebug() << "No random track available for Auto DJ";
return TrackId();
}
}
// Signaled by the track DAO when a track's information is updated.
void AutoDJCratesDAO::slotTrackDirty(TrackId trackId) {
// Update our record of the number of times played, if that changed.
TrackPointer pTrack = m_pTrackCollectionManager->getTrackById(trackId);
if (!pTrack) {
return;
}
const PlayCounter playCounter(pTrack->getPlayCounter());
if (playCounter.getTimesPlayed() == 0) {
return;
}
// Update our record of how many times this track has been played.
// UPDATE temp_autodj_crates
// SET timesplayed = :newplayed
// WHERE track_id = :track_id
// AND timesplayed = :oldplayed;
QSqlQuery oQuery(m_database);
oQuery.prepare("UPDATE " AUTODJCRATES_TABLE " SET "
AUTODJCRATESTABLE_TIMESPLAYED " = :newplayed WHERE "
AUTODJCRATESTABLE_TRACKID " = :track_id AND "
AUTODJCRATESTABLE_TIMESPLAYED " = :oldplayed");
oQuery.bindValue(":track_id", trackId.toVariant());
oQuery.bindValue(":oldplayed", playCounter.getTimesPlayed() - 1);
oQuery.bindValue(":newplayed", playCounter.getTimesPlayed());
if (!oQuery.exec()) {
LOG_FAILED_QUERY(oQuery);
return;
}
}
void AutoDJCratesDAO::slotCrateInserted(CrateId crateId) {
// If this newly-added crate is in the auto-DJ queue, add it to the list.
Crate crate;
if (m_pTrackCollectionManager->internalCollection()->crates().readCrateById(crateId, &crate)) {
if (crate.isAutoDjSource()) {
updateAutoDjCrate(crateId);
}
}
}
void AutoDJCratesDAO::slotCrateUpdated(CrateId crateId) {
Crate crate;
if (m_pTrackCollectionManager->internalCollection()->crates().readCrateById(crateId, &crate)) {
if (crate.isAutoDjSource()) {
updateAutoDjCrate(crateId);
} else {
deleteAutoDjCrate(crateId);
}
}
}
void AutoDJCratesDAO::slotCrateDeleted(CrateId crateId) {
// The crate doesn't exist any more. It might have been an AutoDJ
// crate or not. But that doesn't matter, simply try to delete it.
deleteAutoDjCrate(crateId);
}
void AutoDJCratesDAO::updateAutoDjCrate(CrateId crateId) {
// Handle a crate that's entered the auto-DJ queue differently than one that
// is leaving it. (Obviously.)
ScopedTransaction oTransaction(m_database);
// Add a crate-reference to every track in this crate, if that track is
// already in the auto-DJ-crates table.
QSqlQuery oQuery(m_database);
// UPDATE temp_autodj_crates SET craterefs = craterefs + 1
// WHERE track_id IN (
// SELECT temp_autodj_crates.track_id
// FROM crate_tracks, temp_autodj_crates
// WHERE crate_tracks.crate_id = :crate_id
// AND crate_tracks.track_id = temp_autodj_crates.track_id);
oQuery.prepare(QString ("UPDATE " AUTODJCRATES_TABLE " SET "
AUTODJCRATESTABLE_CRATEREFS " = " AUTODJCRATESTABLE_CRATEREFS
" + 1 WHERE " AUTODJCRATESTABLE_TRACKID " IN (SELECT "
AUTODJCRATES_TABLE "." AUTODJCRATESTABLE_TRACKID " FROM "
CRATE_TRACKS_TABLE ", " AUTODJCRATES_TABLE " WHERE "
CRATE_TRACKS_TABLE ".%2 = :crate_id AND " CRATE_TRACKS_TABLE
".%1 = " AUTODJCRATES_TABLE "." AUTODJCRATESTABLE_TRACKID ")")
.arg(CRATETRACKSTABLE_TRACKID, // %1
CRATETRACKSTABLE_CRATEID)); // %2
oQuery.bindValue(":crate_id", crateId.toVariant());
if (!oQuery.exec()) {
LOG_FAILED_QUERY(oQuery);
return;
}
// Create an entry for all tracks that weren't in the auto-DJ-crates
// table already.
// The number of crate references is known to be 1 for such tracks.
// The number of references to each track in the auto-DJ playlist isn't
// set yet; it defaults to zero.
// If no records were modified by this query, then there's no reason to
// update the number of auto-DJ-playlist references to each track.
// INSERT INTO temp_autodj_crates (
// track_id, craterefs, timesplayed, autodjrefs)
// SELECT crate_tracks.track_id, 1, library.timesplayed, 0
// FROM crate_tracks, library
// WHERE crate_tracks.crate_id = :crate_id
// AND crate_tracks.track_id NOT IN (
// SELECT track_id FROM temp_autodj_crates)
// AND crate_tracks.track_id = library.id
// AND library.mixxx_deleted = 0;
oQuery.prepare(QString("INSERT INTO " AUTODJCRATES_TABLE " ("
AUTODJCRATESTABLE_TRACKID ", " AUTODJCRATESTABLE_CRATEREFS ", "
AUTODJCRATESTABLE_TIMESPLAYED ", " AUTODJCRATESTABLE_AUTODJREFS
") SELECT " CRATE_TRACKS_TABLE ".%1, 1, " LIBRARY_TABLE
".timesplayed, 0 FROM " CRATE_TRACKS_TABLE ", " LIBRARY_TABLE
" WHERE " CRATE_TRACKS_TABLE ".%2 = :crate_id AND "
CRATE_TRACKS_TABLE ".%1 NOT IN (SELECT " AUTODJCRATESTABLE_TRACKID
" FROM " AUTODJCRATES_TABLE" ) AND " CRATE_TRACKS_TABLE ".%1 = "
LIBRARY_TABLE ".%3 AND " LIBRARY_TABLE ".%4 = 0")
.arg(CRATETRACKSTABLE_TRACKID, // %1
CRATETRACKSTABLE_CRATEID, // %2
LIBRARYTABLE_ID, // %3
LIBRARYTABLE_MIXXXDELETED)); // %4
oQuery.bindValue(":crate_id", crateId.toVariant());
if (!oQuery.exec()) {
LOG_FAILED_QUERY(oQuery);
return;
}
if (oQuery.numRowsAffected() == 0) {
oTransaction.commit();
return;
}
// Update the number of auto-DJ-playlist references to each track.
// There's no way to avoid updating the number of references to tracks
// that were already in the database, but the auto-DJ-playlist is likely
// to be small compared to the number of tracks in auto-DJ crates, so
// this isn't so bad.
if (!updateAutoDjPlaylistReferences()) {
return;
}
// Update the last-played date/time for each track.
// Similarly, there's no way to avoid updating the last-played date/time
// of tracks that were already in the database.
if (!updateLastPlayedDateTime()) {
return;
}
// The transaction was successful.
oTransaction.commit();
}
void AutoDJCratesDAO::deleteAutoDjCrate(CrateId crateId) {
// Handle a crate that's entered the auto-DJ queue differently than one that
// is leaving it. (Obviously.)
ScopedTransaction oTransaction(m_database);
// Remove a crate-reference from every track in this crate.
QSqlQuery oQuery(m_database);
// UPDATE temp_autodj_crates SET craterefs = craterefs - 1
// WHERE track_id IN (
// SELECT track_id
// FROM crate_tracks
// WHERE crate_tracks.crate_id = :crate_id);
oQuery.prepare(QString("UPDATE " AUTODJCRATES_TABLE " SET "
AUTODJCRATESTABLE_CRATEREFS " = " AUTODJCRATESTABLE_CRATEREFS
" - 1 WHERE " AUTODJCRATESTABLE_TRACKID " IN (SELECT %1 FROM "
CRATE_TRACKS_TABLE " WHERE " CRATE_TRACKS_TABLE ".%2 = :crate_id)")
.arg(CRATETRACKSTABLE_TRACKID, // %1
CRATETRACKSTABLE_CRATEID)); // %2
oQuery.bindValue(":crate_id", crateId.toVariant());
if (!oQuery.exec()) {
LOG_FAILED_QUERY(oQuery);
return;
}
// Remove all tracks that no longer have crate references.
//DELETE FROM temp_autodj_crates WHERE craterefs = 0;
oQuery.prepare("DELETE FROM " AUTODJCRATES_TABLE " WHERE "
AUTODJCRATESTABLE_CRATEREFS " = 0");
if (!oQuery.exec()) {
LOG_FAILED_QUERY(oQuery);
return;
}
// The transaction was successful.
oTransaction.commit();
}
void AutoDJCratesDAO::slotCrateTracksChanged(
CrateId crateId, const QList<TrackId>& addedTrackIds,
const QList<TrackId>& removedTrackIds) {
// Skip this if it's not an auto-DJ crate.
Crate crate;
if (!m_pTrackCollectionManager->internalCollection()
->crates()
.readCrateById(crateId, &crate) ||
!crate.isAutoDjSource()) {
return;
}
ScopedTransaction oTransaction(m_database);
QSqlQuery oQuery(m_database);
for (const auto& trackId: addedTrackIds) {
// Add a crate-reference to this track, if it's already in the
// auto-DJ-crates table (in which case, we're done).
// UPDATE temp_autodj_crates SET craterefs = craterefs + 1 WHERE track_id = :track_id;
QString strHidden;
strHidden.setNum(PlaylistDAO::PLHT_AUTO_DJ);
oQuery.prepare("UPDATE " AUTODJCRATES_TABLE " SET "
AUTODJCRATESTABLE_CRATEREFS " = " AUTODJCRATESTABLE_CRATEREFS
" + 1 WHERE " AUTODJCRATESTABLE_TRACKID " = :track_id");
oQuery.bindValue(":track_id", trackId.toVariant());
if (!oQuery.exec()) {
LOG_FAILED_QUERY(oQuery);
return;
}
if (oQuery.numRowsAffected() == 1) {
continue;
}
// Create an entry for the track.
// The number of crate references is known to be 1 for such tracks.
// The number of references to each track in the auto-DJ playlist isn't
// set yet; it defaults to zero.
// If no records were modified by this query (i.e. because the track's
// mixxx_deleted flag is set), then there's no reason to update the number
// of auto-DJ-playlist references to each track.
// INSERT INTO temp_autodj_crates (track_id, craterefs, timesplayed, autodjrefs) SELECT :track_id, 1, library.timesplayed, 0 FROM library WHERE :track_id = library.id AND library.mixxx_deleted = 0;
oQuery.prepare(QString("INSERT INTO " AUTODJCRATES_TABLE " ("
AUTODJCRATESTABLE_TRACKID ", " AUTODJCRATESTABLE_CRATEREFS ", "
AUTODJCRATESTABLE_TIMESPLAYED ", " AUTODJCRATESTABLE_AUTODJREFS
") SELECT :track_id_1, 1, " LIBRARY_TABLE ".%1, 0 FROM " LIBRARY_TABLE
" WHERE :track_id_2 = " LIBRARY_TABLE ".%2 AND " LIBRARY_TABLE
".%3 = 0")
.arg(LIBRARYTABLE_TIMESPLAYED, // %1
LIBRARYTABLE_ID, // %2
LIBRARYTABLE_MIXXXDELETED)); // %3
oQuery.bindValue(":track_id_1", trackId.toVariant());
oQuery.bindValue(":track_id_2", trackId.toVariant());
if (!oQuery.exec()) {
LOG_FAILED_QUERY(oQuery);
return;
}
if (oQuery.numRowsAffected() == 0) {
continue;
}
// Update the number of auto-DJ-playlist references to this track.
if (!updateAutoDjPlaylistReferencesForTrack(trackId)) {
return; // failure
}
// Update the last-played date/time for this track.
if (!updateLastPlayedDateTimeForTrack(trackId)) {
return; // failure
}
}
for (const auto& trackId: removedTrackIds) {
// UPDATE temp_autodj_crates SET craterefs = craterefs - 1 WHERE track_id = :track_id;
oQuery.prepare("UPDATE " AUTODJCRATES_TABLE " SET "
AUTODJCRATESTABLE_CRATEREFS " = " AUTODJCRATESTABLE_CRATEREFS
" - 1 WHERE " AUTODJCRATESTABLE_TRACKID " = :track_id");
oQuery.bindValue(":track_id", trackId.toVariant());
if (!oQuery.exec()) {
LOG_FAILED_QUERY(oQuery);
return;
}
// Remove the track if it no longer has a crate reference.