forked from CDrummond/cantata
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlibrarydb.cpp
1362 lines (1241 loc) · 41.2 KB
/
librarydb.cpp
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
/*
* Cantata
*
* Copyright (c) 2017-2022 Craig Drummond <[email protected]>
*
* ----
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "librarydb.h"
#include "support/utils.h"
#include <QCoreApplication>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QFile>
#include <QRegularExpression>
#include <QRandomGenerator>
#include <QDebug>
#include <algorithm>
static const int constSchemaVersion=5;
bool LibraryDb::dbgEnabled=false;
#define DBUG if (dbgEnabled) qWarning() << metaObject()->className() << __FUNCTION__ << (void *)this
const QLatin1String LibraryDb::constFileExt(".sql");
const QLatin1String LibraryDb::constNullGenre("-");
LibraryDb::AlbumSort LibraryDb::toAlbumSort(const QString &str)
{
for (int i=0; i<AS_Count; ++i) {
if (albumSortStr((AlbumSort)i)==str) {
return (AlbumSort)i;
}
}
return AS_AlArYr;
}
QString LibraryDb::albumSortStr(AlbumSort m)
{
switch(m) {
case AS_ArAlYr: return "artist";
default:
case AS_AlArYr: return "album";
case AS_YrAlAr: return "year";
case AS_Modified: return "modified";
// Re-added in 2.1
case AS_ArYrAl: return "aryral";
case AS_AlYrAr: return "alyral";
case AS_YrArAl: return "yraral";
}
}
static bool albumsSortAlArYr(const LibraryDb::Album &a, const LibraryDb::Album &b)
{
const QString &an=a.sort.isEmpty() ? a.name : a.sort;
const QString &bn=b.sort.isEmpty() ? b.name : b.sort;
int cmp=Utils::compare(an, bn);
if (cmp!=0) {
return cmp<0;
}
const QString &aa=a.artistSort.isEmpty() ? a.artist : a.artistSort;
const QString &ba=b.artistSort.isEmpty() ? b.artist : b.artistSort;
cmp=Utils::compare(aa, ba);
if (cmp!=0) {
return cmp<0;
}
return a.year<b.year;
}
static bool albumsSortArAlYr(const LibraryDb::Album &a, const LibraryDb::Album &b)
{
const QString &aa=a.artistSort.isEmpty() ? a.artist : a.artistSort;
const QString &ba=b.artistSort.isEmpty() ? b.artist : b.artistSort;
int cmp=Utils::compare(aa, ba);
if (cmp!=0) {
return cmp<0;
}
const QString &an=a.sort.isEmpty() ? a.name : a.sort;
const QString &bn=b.sort.isEmpty() ? b.name : b.sort;
cmp=Utils::compare(an, bn);
if (cmp!=0) {
return cmp<0;
}
return a.year<b.year;
}
static bool albumsSortAlYrAr(const LibraryDb::Album &a, const LibraryDb::Album &b)
{
const QString &an=a.sort.isEmpty() ? a.name : a.sort;
const QString &bn=b.sort.isEmpty() ? b.name : b.sort;
int cmp=Utils::compare(an, bn);
if (cmp!=0) {
return cmp<0;
}
if (a.year!=b.year) {
return a.year<b.year;
}
const QString &aa=a.artistSort.isEmpty() ? a.artist : a.artistSort;
const QString &ba=b.artistSort.isEmpty() ? b.artist : b.artistSort;
return Utils::compare(aa, ba)<0;
}
static bool albumsSortArYrAl(const LibraryDb::Album &a, const LibraryDb::Album &b)
{
const QString &aa=a.artistSort.isEmpty() ? a.artist : a.artistSort;
const QString &ba=b.artistSort.isEmpty() ? b.artist : b.artistSort;
int cmp=Utils::compare(aa, ba);
if (cmp!=0) {
return cmp<0;
}
if (a.year!=b.year) {
return a.year<b.year;
}
const QString &an=a.sort.isEmpty() ? a.name : a.sort;
const QString &bn=b.sort.isEmpty() ? b.name : b.sort;
return Utils::compare(an, bn)<0;
}
static bool albumsSortYrAlAr(const LibraryDb::Album &a, const LibraryDb::Album &b)
{
if (a.year!=b.year) {
return a.year<b.year;
}
const QString &an=a.sort.isEmpty() ? a.name : a.sort;
const QString &bn=b.sort.isEmpty() ? b.name : b.sort;
int cmp=Utils::compare(an, bn);
if (cmp!=0) {
return cmp<0;
}
const QString &aa=a.artistSort.isEmpty() ? a.artist : a.artistSort;
const QString &ba=b.artistSort.isEmpty() ? b.artist : b.artistSort;
return Utils::compare(aa, ba)<0;
}
static bool albumsSortYrArAl(const LibraryDb::Album &a, const LibraryDb::Album &b)
{
if (a.year!=b.year) {
return a.year<b.year;
}
const QString &aa=a.artistSort.isEmpty() ? a.artist : a.artistSort;
const QString &ba=b.artistSort.isEmpty() ? b.artist : b.artistSort;
int cmp=Utils::compare(aa, ba);
if (cmp!=0) {
return cmp<0;
}
const QString &an=a.sort.isEmpty() ? a.name : a.sort;
const QString &bn=b.sort.isEmpty() ? b.name : b.sort;
return Utils::compare(an, bn)<0;
}
static bool albumsSortModified(const LibraryDb::Album &a, const LibraryDb::Album &b)
{
if (a.lastModified==b.lastModified) {
return albumsSortAlArYr(a, b);
}
return a.lastModified>b.lastModified;
}
static bool songSort(const Song &a, const Song &b)
{
if (Song::SingleTracks==a.type && Song::SingleTracks==b.type) {
int cmp=Utils::compare(a.title, b.title);
if (0!=cmp) {
return cmp<0;
}
cmp=Utils::compare(a.artist, b.artist);
if (0!=cmp) {
return cmp<0;
}
}
if (a.disc!=b.disc) {
return a.disc<b.disc;
}
if (a.track!=b.track) {
return a.track<b.track;
}
if (a.displayYear()!=b.displayYear()) {
return a.displayYear()<b.displayYear();
}
int cmp=Utils::compare(a.title, b.title);
if (0!=cmp) {
return cmp<0;
}
cmp=Utils::compare(a.name(), b.name());
if (0!=cmp) {
return cmp<0;
}
cmp=Utils::compare(a.displayGenre(), b.displayGenre());
if (0!=cmp) {
return cmp<0;
}
if (a.time!=b.time) {
return a.time<b.time;
}
return a.file.compare(b.file)<0;
}
static bool songsSortAlAr(const Song &a, const Song &b)
{
const QString an=a.hasAlbumSort() ? a.albumSort() : a.album;
const QString bn=b.hasAlbumSort() ? b.albumSort() : b.album;
int cmp=Utils::compare(an, bn);
if (cmp!=0) {
return cmp<0;
}
const QString aa=a.hasArtistSort() ? a.artistSort() : a.albumArtist();
const QString ba=b.hasArtistSort() ? b.artistSort() : b.albumArtist();
cmp=Utils::compare(aa, ba);
if (cmp!=0) {
return cmp<0;
}
// if (a.displayYear()!=b.displayYear()) {
// return a.displayYear()<b.displayYear();
// }
return songSort(a, b);
}
static bool songsSortArAl(const Song &a, const Song &b)
{
const QString aa=a.hasArtistSort() ? a.artistSort() : a.albumArtist();
const QString ba=b.hasArtistSort() ? b.artistSort() : b.albumArtist();
int cmp=Utils::compare(aa, ba);
if (cmp!=0) {
return cmp<0;
}
const QString an=a.hasAlbumSort() ? a.albumSort() : a.album;
const QString bn=b.hasAlbumSort() ? b.albumSort() : b.album;
cmp=Utils::compare(an, bn);
if (cmp!=0) {
return cmp<0;
}
// if (a.displayYear()!=b.displayYear()) {
// return a.displayYear()<b.displayYear();
// }
return songSort(a, b);
}
//static bool songsSortAlYrAr(const Song &a, const Song &b)
//{
// const QString an=a.hasAlbumSort() ? a.albumSort() : a.album;
// const QString bn=b.hasAlbumSort() ? b.albumSort() : b.album;
// int cmp=Utils::compare(an, bn);
// if (cmp!=0) {
// return cmp<0;
// }
// if (a.displayYear()!=b.displayYear()) {
// return a.displayYear()<b.displayYear();
// }
// const QString aa=a.hasArtistSort() ? a.artistSort() : a.albumArtist();
// const QString ba=b.hasArtistSort() ? b.artistSort() : b.albumArtist();
// cmp=Utils::compare(aa, ba);
// if (cmp!=0) {
// return cmp<0;
// }
// return songSort(a, b);
//}
//static bool songsSortArYrAl(const Song &a, const Song &b)
//{
// const QString aa=a.hasArtistSort() ? a.artistSort() : a.albumArtist();
// const QString ba=b.hasArtistSort() ? b.artistSort() : b.albumArtist();
// int cmp=Utils::compare(aa, ba);
// if (cmp!=0) {
// return cmp<0;
// }
// if (a.displayYear()!=b.displayYear()) {
// return a.displayYear()<b.displayYear();
// }
// const QString an=a.hasAlbumSort() ? a.albumSort() : a.album;
// const QString bn=b.hasAlbumSort() ? b.albumSort() : b.album;
// cmp=Utils::compare(an, bn);
// if (cmp!=0) {
// return cmp<0;
// }
// return songSort(a, b);
//}
//static bool songsSortYrAlAr(const Song &a, const Song &b)
//{
// if (a.displayYear()!=b.displayYear()) {
// return a.displayYear()<b.displayYear();
// }
// const QString an=a.hasAlbumSort() ? a.albumSort() : a.album;
// const QString bn=b.hasAlbumSort() ? b.albumSort() : b.album;
// int cmp=Utils::compare(an, bn);
// if (cmp!=0) {
// return cmp<0;
// }
// const QString aa=a.hasArtistSort() ? a.artistSort() : a.albumArtist();
// const QString ba=b.hasArtistSort() ? b.artistSort() : b.albumArtist();
// cmp=Utils::compare(aa, ba);
// if (cmp!=0) {
// return cmp<0;
// }
// return songSort(a, b);
//}
//static bool songsSortYrArAl(const Song &a, const Song &b)
//{
// if (a.displayYear()!=b.displayYear()) {
// return a.displayYear()<b.displayYear();
// }
// const QString aa=a.hasArtistSort() ? a.artistSort() : a.albumArtist();
// const QString ba=b.hasArtistSort() ? b.artistSort() : b.albumArtist();
// int cmp=Utils::compare(aa, ba);
// if (cmp!=0) {
// return cmp<0;
// }
// const QString an=a.hasAlbumSort() ? a.albumSort() : a.album;
// const QString bn=b.hasAlbumSort() ? b.albumSort() : b.album;
// cmp=Utils::compare(an, bn);
// if (cmp!=0) {
// return cmp<0;
// }
// return songSort(a, b);
//}
//static bool songsSortModified(const Song &a, const Song &b)
//{
// if (a.lastModified==b.lastModified) {
// return songsSortAlArYr(a, b);
// }
// return a.lastModified>b.lastModified;
//}
static QString artistSort(const Song &s)
{
if (s.useComposer() && !s.composer().isEmpty()) {
return s.composer();
}
if (!s.artistSortString().isEmpty()) {
return s.artistSortString();
}
return Song::sortString(s.albumArtist());
}
static QString albumSort(const Song &s)
{
if (!s.albumSort().isEmpty()) {
return s.albumSort();
}
return Song::sortString(s.album);
}
// Code taken from Clementine's LibraryQuery
class SqlQuery
{
public:
SqlQuery(const QString &colSpec, QSqlDatabase &database)
: db(database)
, fts(false)
, columSpec(colSpec)
, limit(0)
{
}
void addWhere(const QString &column, const QVariant &value, const QString &op="=")
{
// ignore 'literal' for IN
if (!op.compare("IN", Qt::CaseInsensitive)) {
QStringList final;
for(const QString &singleValue: value.toStringList()) {
final.append("?");
boundValues << singleValue;
}
whereClauses << QString("%1 IN (" + final.join(",") + ")").arg(column);
} else {
// Do integers inline - sqlite seems to get confused when you pass integers
// to bound parameters
if (QMetaType::Int==value.typeId()) {
whereClauses << QString("%1 %2 %3").arg(column, op, value.toString());
} else if ("genre"==column) {
QString clause("(");
for (int i=0; i<Song::constNumGenres; ++i) {
clause+=QString("%1 %2 ?").arg(column+QString::number(i+1), op);
if (i<(Song::constNumGenres-1)) {
clause+=" OR ";
}
boundValues << value;
}
clause+=")";
whereClauses << clause;
} else {
whereClauses << QString("%1 %2 ?").arg(column, op);
boundValues << value;
}
}
}
void setFilter(const QString &filter, const QString yearFilter)
{
if (!filter.isEmpty()) {
whereClauses << "songs_fts match ?";
boundValues << "\'"+filter+"\'";
fts=true;
}
if (!yearFilter.isEmpty()) {
whereClauses << yearFilter;
}
}
void setOrder(const QString &o)
{
order=o;
}
void setLimit(int l)
{
limit=l;
}
bool exec()
{
QString sql=fts
? QString("SELECT %1 FROM songs INNER JOIN songs_fts AS fts ON songs.ROWID = fts.ROWID").arg(columSpec)
: QString("SELECT %1 FROM songs").arg(columSpec);
if (!whereClauses.isEmpty()) {
sql+=" WHERE " + whereClauses.join(" AND ");
}
if (!order.isEmpty()) {
sql+=" ORDER by "+order;
}
if (limit>0) {
sql+=" LIMIT "+QString::number(limit);
}
query = QSqlQuery(db);
query.prepare(sql);
for (const QVariant &value: boundValues) {
query.addBindValue(value);
}
return query.exec();
}
bool next() { return query.next(); }
QString executedQuery() const { return query.executedQuery(); }
int size() const { return query.size(); }
QVariant value(int col) const { return query.value(col); }
const QSqlQuery & realQuery() const { return query; }
private:
QSqlDatabase &db;
QSqlQuery query;
bool fts;
QString columSpec;
QStringList whereClauses;
QVariantList boundValues;
QString order;
int limit;
};
LibraryDb::LibraryDb(QObject *p, const QString &name)
: QObject(p)
, dbName(name)
, currentVersion(0)
, newVersion(0)
, db(nullptr)
, insertSongQuery(nullptr)
{
DBUG;
}
LibraryDb::~LibraryDb()
{
reset();
}
void LibraryDb::clear()
{
if (db) {
DBUG;
erase();
currentVersion=0;
init(dbFileName);
}
}
void LibraryDb::erase()
{
reset();
if (!dbFileName.isEmpty() && QFile::exists(dbFileName)) {
QFile::remove(dbFileName);
}
}
enum SongFields {
SF_file,
SF_artist ,
SF_artistId,
SF_albumArtist,
SF_artistSort,
SF_composer,
SF_album,
SF_albumId,
SF_albumSort,
SF_title,
SF_genre1,
SF_genre2,
SF_genre3,
SF_genre4,
SF_track,
SF_disc,
SF_time,
SF_year,
SF_origYear,
SF_type,
SF_lastModified
};
bool LibraryDb::init(const QString &dbFile)
{
if (dbFile!=dbFileName) {
reset();
dbFileName=dbFile;
}
if (db) {
return true;
}
DBUG << dbFile << dbName;
currentVersion=0;
db=new QSqlDatabase(QSqlDatabase::addDatabase("QSQLITE", dbName.isEmpty() ? QLatin1String(QSqlDatabase::defaultConnection) : dbName));
if (!db || !db->isValid()) {
emit error(tr("Database error - please check Qt SQLite driver is installed"));
return false;
}
db->setDatabaseName(dbFile);
DBUG << (void *)db;
if (!db->open()) {
delete db;
db=nullptr;
DBUG << "Failed to open";
return false;
}
if (!createTable("versions(collection integer, schema integer)")) {
DBUG << "Failed to create versions table";
return false;
}
QSqlQuery query("select collection, schema from versions", *db);
int schemaVersion=0;
if (query.next()) {
currentVersion=query.value(0).toUInt();
schemaVersion=query.value(1).toUInt();
}
if (schemaVersion>0 && schemaVersion!=constSchemaVersion) {
DBUG << "Scheme version changed";
currentVersion=0;
erase();
return init(dbFile);
}
if (0==currentVersion || (schemaVersion>0 && schemaVersion!=constSchemaVersion)) {
QSqlQuery(*db).exec("delete from versions");
QSqlQuery(*db).exec("insert into versions (collection, schema) values(0, "+QString::number(constSchemaVersion)+")");
}
DBUG << "Current version" << currentVersion;
// NOTE: The order here MUST match SongFields enum above!!!
if (createTable("songs ("
"file text, "
"artist text, "
"artistId text, "
"albumArtist text, "
"artistSort text, "
"composer text, "
"album text, "
"albumId text, "
"albumSort text, "
"title text, "
"genre1 text, "
"genre2 text, "
"genre3 text, "
"genre4 text, "
"track integer, "
"disc integer, "
"time integer, "
"year integer, "
"origYear integer, "
"type integer, "
"lastModified integer, "
"primary key (file))")) {
QSqlQuery fts(*db);
if (!fts.exec("create virtual table if not exists songs_fts using fts4(fts_artist, fts_artistId, fts_album, fts_albumId, fts_title, tokenize=unicode61)")) {
DBUG << "Failed to create FTS table" << fts.lastError().text() << "trying again with simple tokenizer";
if (!fts.exec("create virtual table if not exists songs_fts using fts4(fts_artist, fts_artistId, fts_album, fts_albumId, fts_title, tokenize=simple)")) {
DBUG << "Failed to create FTS table" << fts.lastError().text();
}
}
} else {
DBUG << "Failed to create songs table";
return false;
}
emit libraryUpdated();
DBUG << "Created";
return true;
}
void LibraryDb::insertSong(const Song &s)
{
if (!db) {
return;
}
if (!insertSongQuery) {
insertSongQuery=new QSqlQuery(*db);
insertSongQuery->prepare("insert into songs(file, artist, artistId, albumArtist, artistSort, composer, album, albumId, albumSort, title, genre1, genre2, genre3, genre4, track, disc, time, year, origYear, type, lastModified) "
"values(:file, :artist, :artistId, :albumArtist, :artistSort, :composer, :album, :albumId, :albumSort, :title, :genre1, :genre2, :genre3, :genre4, :track, :disc, :time, :year, :origYear, :type, :lastModified)");
}
QString albumId=s.albumId();
insertSongQuery->bindValue(":file", s.file);
insertSongQuery->bindValue(":artist", s.artist);
insertSongQuery->bindValue(":artistId", s.albumArtistOrComposer());
insertSongQuery->bindValue(":albumArtist", s.albumartist);
insertSongQuery->bindValue(":artistSort", artistSort(s));
insertSongQuery->bindValue(":composer", s.composer());
insertSongQuery->bindValue(":album", s.album==albumId ? QString() : s.album);
insertSongQuery->bindValue(":albumId", albumId);
insertSongQuery->bindValue(":albumSort", albumSort(s));
insertSongQuery->bindValue(":title", s.title);
for (int i=0; i<Song::constNumGenres; ++i) {
insertSongQuery->bindValue(":genre"+QString::number(i+1), s.genres[i].isEmpty() ? constNullGenre : s.genres[i]);
}
insertSongQuery->bindValue(":track", s.track);
insertSongQuery->bindValue(":disc", s.disc);
insertSongQuery->bindValue(":time", s.time);
insertSongQuery->bindValue(":year", s.year);
insertSongQuery->bindValue(":origYear", s.origYear);
insertSongQuery->bindValue(":type", s.type);
insertSongQuery->bindValue(":lastModified", s.lastModified);
if (!insertSongQuery->exec()) {
qWarning() << "insert failed" << insertSongQuery->lastError().text() << newVersion << s.file;
}
}
QList<LibraryDb::Genre> LibraryDb::getGenres()
{
DBUG;
QList<LibraryDb::Genre> genres;
if (!db) {
return genres;
}
QMap<QString, QSet<QString> > map;
if (0!=currentVersion && db) {
QString queryStr("distinct ");
for (int i=0; i<Song::constNumGenres; ++i) {
queryStr+="genre"+QString::number(i+1)+", ";
}
queryStr+="artistId";
SqlQuery query(queryStr, *db);
query.setFilter(filter, yearFilter);
query.exec();
DBUG << query.executedQuery();
while (query.next()) {
for (int i=0; i<Song::constNumGenres; ++i) {
QString genre=query.value(i).toString();
if (!genre.isEmpty() && genre!=constNullGenre) {
map[genre].insert(query.value(Song::constNumGenres).toString());
}
}
}
}
QMap<QString, QSet<QString> >::ConstIterator it=map.constBegin();
QMap<QString, QSet<QString> >::ConstIterator end=map.constEnd();
for (; it!=end; ++it) {
DBUG << it.key();
genres.append(Genre(it.key(), it.value().size()));
}
std::sort(genres.begin(), genres.end());
return genres;
}
QList<LibraryDb::Artist> LibraryDb::getArtists(const QString &genre)
{
DBUG << genre;
QList<LibraryDb::Artist> artists;
if (!db) {
return artists;
}
QMap<QString, QString> sortMap;
QMap<QString, int> albumMap;
if (0!=currentVersion && db) {
SqlQuery query("distinct artistId, albumId, artistSort", *db);
query.setFilter(filter, yearFilter);
if (!genre.isEmpty()) {
query.addWhere("genre", genre);
} else if (!genreFilter.isEmpty()) {
query.addWhere("genre", genreFilter);
}
query.exec();
DBUG << query.executedQuery();
while (query.next()) {
QString artist=query.value(0).toString();
albumMap[artist]++;
sortMap[artist]=query.value(2).toString();
}
}
QMap<QString, int>::ConstIterator it=albumMap.constBegin();
QMap<QString, int>::ConstIterator end=albumMap.constEnd();
for (; it!=end; ++it) {
// DBUG << it.key();
artists.append(Artist(it.key(), sortMap[it.key()], it.value()));
}
std::sort(artists.begin(), artists.end());
return artists;
}
QList<LibraryDb::Album> LibraryDb::getAlbums(const QString &artistId, const QString &genre, AlbumSort sort)
{
timer.start();
DBUG << artistId << genre;
QList<Album> albums;
if (!db) {
return albums;
}
if (0!=currentVersion && db) {
bool wantModified=AS_Modified==sort;
bool wantArtist=artistId.isEmpty();
QString queryString="album, albumId, albumSort, artist, albumArtist, composer";
for (int i=0; i<Song::constNumGenres; ++i) {
queryString+=", genre"+QString::number(i+1);
}
queryString+=", type, year, origYear, time";
if (wantModified) {
queryString+=", lastModified";
}
if (wantArtist) {
queryString+=", artistId, artistSort";
}
SqlQuery query(queryString, *db);
query.setFilter(filter, yearFilter);
if (!artistId.isEmpty()) {
query.addWhere("artistId", artistId);
}
if (!genre.isEmpty()) {
query.addWhere("genre", genre);
} else if (!genreFilter.isEmpty()) {
query.addWhere("genre", genreFilter);
}
query.exec();
int count=0;
QMap<QString, Album> entries;
QMap<QString, QSet<QString> > albumIdArtists; // Map of albumId -> albumartists/composers
while (query.next()) {
count++;
int col=0;
QString album=query.value(col++).toString();
QString albumId=query.value(col++).toString();
QString albumSort=query.value(col++).toString();
Song s;
s.artist=query.value(col++).toString();
s.albumartist=query.value(col++).toString();
s.setComposer(query.value(col++).toString());
s.album=album.isEmpty() ? albumId : album;
for (int i=0; i<Song::constNumGenres; ++i) {
QString genre=query.value(col++).toString();
if (genre!=constNullGenre) {
s.addGenre(genre);
}
}
s.type=(Song::Type)query.value(col++).toInt();
s.year=query.value(col++).toInt();
s.origYear=query.value(col++).toInt();
if (Song::SingleTracks==s.type) {
s.album=Song::singleTracks();
s.albumartist=Song::variousArtists();
s.year = s.origYear = 0;
}
album=s.albumName();
int time=query.value(col++).toInt();
int lastModified=wantModified ? query.value(col++).toInt() : 0;
QString artist=wantArtist ? query.value(col++).toString() : QString();
QString artistSort=wantArtist ? query.value(col++).toString() : QString();
// If listing albums not filtered on artist, then if we have a unqique id for the album use that.
// This will allow us to grouup albums with different composers when the composer tweak is set
// Issue #1025
bool haveUniqueId=wantArtist && !albumId.isEmpty() && !album.isEmpty() && albumId!=album;
QString key=haveUniqueId ? albumId : ('{'+albumId+"}{"+(wantArtist ? artist : artistId)+'}');
QMap<QString, Album>::iterator it=entries.find(key);
if (it==entries.end()) {
entries.insert(key, Album(album.isEmpty() ? albumId : album, albumId, albumSort, artist, artistSort, s.displayYear(), 1, time, lastModified, haveUniqueId));
} else {
Album &al=it.value();
if (wantModified) {
al.lastModified=qMax(al.lastModified, lastModified);
}
al.year=qMax(al.year, (int)s.displayYear());
al.duration+=time;
al.trackCount++;
}
if (haveUniqueId) {
QMap<QString, QSet<QString> >::iterator aIt = albumIdArtists.find(key);
if (aIt == albumIdArtists.end()) {
albumIdArtists.insert(key, QSet<QString>() << artist);
} else {
aIt.value().insert(artist);
}
}
}
QMap<QString, QSet<QString> >::ConstIterator aIt = albumIdArtists.constBegin();
QMap<QString, QSet<QString> >::ConstIterator aEnd = albumIdArtists.constEnd();
for(; aIt!=aEnd; ++aIt) {
if (aIt.value().count()>1) {
QStringList artists = aIt.value().values();
artists.sort();
Album &al = entries.find(aIt.key()).value();
al.artist = artists.join(", ");
al.artistSort = QString();
}
}
albums=entries.values();
DBUG << count << albums.count();
}
DBUG << "After select" << timer.elapsed();
switch(sort) {
case AS_AlArYr:
std::sort(albums.begin(), albums.end(), albumsSortAlArYr);
break;
case AS_AlYrAr:
std::sort(albums.begin(), albums.end(), albumsSortAlYrAr);
break;
case AS_ArAlYr:
std::sort(albums.begin(), albums.end(), albumsSortArAlYr);
break;
case AS_ArYrAl:
std::sort(albums.begin(), albums.end(), albumsSortArYrAl);
break;
case AS_YrAlAr:
std::sort(albums.begin(), albums.end(), albumsSortYrAlAr);
break;
case AS_YrArAl:
std::sort(albums.begin(), albums.end(), albumsSortYrArAl);
break;
case AS_Modified:
std::sort(albums.begin(), albums.end(), albumsSortModified);
break;
default:
break;
}
DBUG << "After sort" << timer.elapsed();
return albums;
}
QList<Song> LibraryDb::getTracks(const QString &artistId, const QString &albumId, const QString &genre, AlbumSort sort, bool useFilter, int maxTracks)
{
DBUG << artistId << albumId << genre << sort;
QList<Song> songs;
if (!db) {
return songs;
}
if (0!=currentVersion && db) {
SqlQuery query("*", *db);
if (useFilter) {
query.setFilter(filter, yearFilter);
}
if (!artistId.isEmpty()) {
query.addWhere("artistId", artistId);
}
if (!albumId.isEmpty()) {
query.addWhere("albumId", albumId);
}
if (!genre.isEmpty()) {
query.addWhere("genre", genre);
} else if (useFilter && !genreFilter.isEmpty()) {
query.addWhere("genre", genreFilter);
}
if (maxTracks != 1) {
query.setLimit(maxTracks);
}
query.exec();
DBUG << query.executedQuery();
while (query.next()) {
songs.append(getSong(query.realQuery()));
}
}
switch(sort) {
case AS_AlArYr:
std::sort(songs.begin(), songs.end(), songsSortAlAr);
break;
case AS_ArAlYr:
std::sort(songs.begin(), songs.end(), songsSortArAl);
break;
// case AS_Year:
// std::sort(songs.begin(), songs.end(), songsSortYrAlAr);
// break;
// case AS_Modified:
// std::sort(songs.begin(), songs.end(), songsSortModified);
// break;
default:
std::sort(songs.begin(), songs.end(), songSort);
break;
}
return songs;
}
QList<Song> LibraryDb::getTracks(int rowFrom, int count)
{
QList<Song> songList;
if (db) {
SqlQuery query("*", *db);
query.addWhere("rowid", rowFrom, ">");
query.addWhere("rowid", rowFrom+count, "<=");
query.addWhere("type", 0);
query.exec();
DBUG << query.executedQuery();
while (query.next()) {
songList.append(getSong(query.realQuery()));
}
}
return songList;
}
int LibraryDb::trackCount()
{
if (!db) {
return 0;
}
SqlQuery query("(count())", *db);
query.addWhere("type", 0);
query.exec();
DBUG << query.executedQuery();
int numTracks=query.next() ? query.value(0).toInt() : 0;
DBUG << numTracks;
return numTracks;
}
QList<Song> LibraryDb::songs(const QStringList &files, bool allowPlaylists) const
{
QList<Song> songList;
if (0!=currentVersion && db) {
for (const QString &f: files) {
SqlQuery query("*", *db);
query.addWhere("file", f);
query.exec();
DBUG << query.executedQuery();
if (query.next()) {
Song song=getSong(query.realQuery());
if (allowPlaylists || Song::Playlist!=song.type) {
songList.append(song);
}
}
}
}
return songList;
}