Merge pull request #26302 from ksooo/pvr-fix-recordings-playback
[xbmc.git] / xbmc / music / MusicDatabase.h
bloba2e1eb7b4405cd10b8bfe6dd7f701fa969a921dc
1 /*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #pragma once
11 /*!
12 \file MusicDatabase.h
13 \brief
16 #include "Album.h"
17 #include "MediaSource.h"
18 #include "addons/Scraper.h"
19 #include "dbwrappers/Database.h"
20 #include "settings/LibExportSettings.h"
21 #include "utils/SortUtils.h"
23 #include <utility>
24 #include <vector>
26 class CArtist;
27 class CFileItem;
28 class CMusicDbUrl;
29 class TiXmlNode;
31 namespace dbiplus
33 class field_value;
34 typedef std::vector<field_value> sql_record;
35 } // namespace dbiplus
37 #include <set>
38 #include <string>
40 // return codes of Cleaning up the Database
41 // numbers are strings from strings.po
42 #define ERROR_OK 317
43 #define ERROR_CANCEL 0
44 #define ERROR_DATABASE 315
45 #define ERROR_REORG_SONGS 319
46 #define ERROR_REORG_ARTIST 321
47 #define ERROR_REORG_OTHER 323
48 #define ERROR_REORG_PATH 325
49 #define ERROR_REORG_ALBUM 327
50 #define ERROR_WRITING_CHANGES 329
51 #define ERROR_COMPRESSING 332
53 #define NUM_SONGS_BEFORE_COMMIT 500
55 /*!
56 \ingroup music
57 \brief A set of std::string objects, used for CMusicDatabase
58 \sa ISETPATHS, CMusicDatabase
60 typedef std::set<std::string> SETPATHS;
62 /*!
63 \ingroup music
64 \brief The SETPATHS iterator
65 \sa SETPATHS, CMusicDatabase
67 typedef std::set<std::string>::iterator ISETPATHS;
69 /*!
70 \ingroup music
71 \brief A structure used for fetching music art data
72 \sa CMusicDatabase::GetArtForItem()
75 typedef struct
77 std::string mediaType;
78 std::string artType;
79 std::string prefix;
80 std::string url;
81 } ArtForThumbLoader;
83 class CGUIDialogProgress;
84 class CFileItemList;
86 /*!
87 \ingroup music
88 \brief Class to store and read tag information
90 CMusicDatabase can be used to read and store
91 tag information for faster access. It is based on
92 sqlite (http://www.sqlite.org).
94 Here is the database layout:
95 \image html musicdatabase.png
97 \sa CAlbum, CSong, VECSONGS, CMapSong, VECARTISTS, VECALBUMS, VECGENRES
99 class CMusicDatabase : public CDatabase
101 friend class DatabaseUtils;
102 friend class TestDatabaseUtilsHelper;
104 public:
105 CMusicDatabase(void);
106 ~CMusicDatabase(void) override;
108 bool Open() override;
109 bool CommitTransaction() override;
110 void EmptyCache();
111 void Clean();
112 int Cleanup(CGUIDialogProgress* progressDialog = nullptr);
113 bool LookupCDDBInfo(bool bRequery = false);
114 void DeleteCDDBInfo();
116 /////////////////////////////////////////////////
117 // Song CRUD
118 /////////////////////////////////////////////////
119 /*! \brief Add a song to the database
120 \param idSong [in] the original database ID of the song to reuse (-1 when new)
121 \param dtDateNew [in] the datetime the original ID was new
122 \param idAlbum [in] the database ID of the album for the song
123 \param strTitle [in] the title of the song (required to be non-empty)
124 \param strMusicBrainzTrackID [in] the MusicBrainz track ID of the song
125 \param strPathAndFileName [in] the path and filename to the song
126 \param strComment [in] the ids of the added songs
127 \param strMood [in] the mood of the added song
128 \param strThumb [in] the ids of the added songs
129 \param artistDisp [in] the assembled artist name(s) display string
130 \param artistSort [in] the artist name(s) sort string
131 \param genres [in] a vector of genres to which this song belongs
132 \param iTrack [in] the track number and disc number of the song
133 \param iDuration [in] the duration of the song
134 \param strReleaseDate [in] the release date of the song ISO8601 format
135 \param strOrigReleaseDate [in] the original release date of the song ISO8601 format
136 \param strDiscSubtitle [in] subtitle of a disc
137 \param iTimesPlayed [in] the number of times the song has been played
138 \param iStartOffset [in] the start offset of the song (when using a single audio file with a .cue)
139 \param iEndOffset [in] the end offset of the song (when using a single audio file with .cue)
140 \param dtLastPlayed [in] the time the song was last played
141 \param rating [in] a rating for the song
142 \param userrating [in] a userrating (my rating) for the song
143 \param votes [in] a vote counter for the song rating
144 \param songVideoURL [in] url to video of the song
145 \param replayGain [in] album and track replaygain and peak values
146 \return the id of the song
148 int AddSong(const int idSong,
149 const CDateTime& dtDateNew,
150 const int idAlbum,
151 const std::string& strTitle,
152 const std::string& strMusicBrainzTrackID,
153 const std::string& strPathAndFileName,
154 const std::string& strComment,
155 const std::string& strMood,
156 const std::string& strThumb,
157 const std::string& artistDisp,
158 const std::string& artistSort,
159 const std::vector<std::string>& genres,
160 int iTrack,
161 int iDuration,
162 const std::string& strReleaseDate,
163 const std::string& strOrigReleaseDate,
164 std::string& strDiscSubtitle,
165 const int iTimesPlayed,
166 int iStartOffset,
167 int iEndOffset,
168 const CDateTime& dtLastPlayed,
169 float rating,
170 int userrating,
171 int votes,
172 int iBPM,
173 int iBitRate,
174 int iSampleRate,
175 int iChannels,
176 const std::string& songVideoURL,
177 const ReplayGain& replayGain);
178 bool GetSong(int idSong, CSong& song);
180 /*! \brief Update a song and all its nested entities (genres, artists, contributors)
181 \param song [in/out] the song to update, artist ids are returned in artist credits
182 \param bArtists to update artist credits and contributors, default is true
183 \param bArtists to check and log if artist links have changed, default is true
184 \return true if successful
186 bool UpdateSong(CSong& song, bool bArtists = true, bool bArtistLinks = true);
188 /*! \brief Update a song in the database
189 \param idSong [in] the database ID of the song to update
190 \param strTitle [in] the title of the song (required to be non-empty)
191 \param strMusicBrainzTrackID [in] the MusicBrainz track ID of the song
192 \param strPathAndFileName [in] the path and filename to the song
193 \param strComment [in] the ids of the added songs
194 \param strMood [in] the mood of the added song
195 \param strThumb [in] the ids of the added songs
196 \param artistDisp [in] the artist name(s) display string
197 \param artistSort [in] the artist name(s) sort string
198 \param genres [in] a vector of genres to which this song belongs
199 \param iTrack [in] the track number and disc number of the song
200 \param iDuration [in] the duration of the song
201 \param strReleaseDate [in] the release date of the song ISO8601 format
202 \param strOrigReleaseDate [in] the original release date of the song ISO8601 format
203 \param strDiscSubtitle [in] subtitle of a disc
204 \param iTimesPlayed [in] the number of times the song has been played
205 \param iStartOffset [in] the start offset of the song (when using a single audio file with a .cue)
206 \param iEndOffset [in] the end offset of the song (when using a single audio file with .cue)
207 \param dtLastPlayed [in] the time the song was last played
208 \param rating [in] a rating for the song
209 \param userrating [in] a userrating (my rating) for the song
210 \param votes [in] a vote counter for the song rating
211 \param replayGain [in] album and track replaygain and peak values
212 \param iBPM [in] the beats per minute of a song
213 \param iBitRate [in] the bitrate of the song file
214 \param iSampleRate [in] the sample rate of the song file
215 \param iChannels [in] the number of audio channels in the song file
216 \param songVideoURL [in] url link to a video of the song
217 \return the id of the song
219 int UpdateSong(int idSong,
220 const std::string& strTitle,
221 const std::string& strMusicBrainzTrackID,
222 const std::string& strPathAndFileName,
223 const std::string& strComment,
224 const std::string& strMood,
225 const std::string& strThumb,
226 const std::string& artistDisp,
227 const std::string& artistSort,
228 const std::vector<std::string>& genres,
229 int iTrack,
230 int iDuration,
231 const std::string& strReleaseDate,
232 const std::string& strOrigReleaseDate,
233 const std::string& strDiscSubtitle,
234 int iTimesPlayed,
235 int iStartOffset,
236 int iEndOffset,
237 const CDateTime& dtLastPlayed,
238 float rating,
239 int userrating,
240 int votes,
241 const ReplayGain& replayGain,
242 int iBPM,
243 int iBitRate,
244 int iSampleRate,
245 int iChannels,
246 const std::string& songVideoURL);
248 //// Misc Song
249 bool GetSongByFileName(const std::string& strFileName, CSong& song, int64_t startOffset = 0);
250 bool GetSongsByPath(const std::string& strPath, MAPSONGS& songmap, bool bAppendToMap = false);
251 bool Search(const std::string& search, CFileItemList& items);
252 bool RemoveSongsFromPath(const std::string& path, MAPSONGS& songmap, bool exact = true);
253 void CheckArtistLinksChanged();
254 bool SetSongUserrating(const std::string& filePath, int userrating);
255 bool SetSongUserrating(int idSong, int userrating);
256 bool SetSongVotes(const std::string& filePath, int votes);
257 int GetSongByArtistAndAlbumAndTitle(const std::string& strArtist,
258 const std::string& strAlbum,
259 const std::string& strTitle);
261 /////////////////////////////////////////////////
262 // Album
263 /////////////////////////////////////////////////
264 /*! \brief Add an album and all its songs to the database
265 \param album the album to add
266 \param idSource the music source id
267 \return the id of the album
269 bool AddAlbum(CAlbum& album, int idSource);
271 /*! \brief Update an album and all its nested entities (artists, songs etc)
272 \param album the album to update
273 \return true or false
275 bool UpdateAlbum(CAlbum& album);
277 /*! \brief Add an album to the database
278 \param strAlbum the album title
279 \param strMusicBrainzAlbumID the Musicbrainz Id
280 \param strArtist the album artist name(s) display string
281 \param strArtistSort the album artist name(s) sort string
282 \param strGenre the album genre(s)
283 \param strReleaseDate [in] the release date of the album ISO8601 format
284 \param strOrigReleaseDate [in] the original release date of the album ISO8601 format
285 \param bBoxedSet if the album is a boxset
286 \param strRecordLabel the recording label
287 \param strType album type (Musicbrainz release type e.g. "Broadcast, Soundtrack, live"),
288 \param strReleaseStatus (see https://musicbrainz.org/doc/Release#Status)
289 \param bCompilation if the album is a compilation
290 \param releaseType "album" or "single"
291 \return the id of the album
293 int AddAlbum(const std::string& strAlbum,
294 const std::string& strMusicBrainzAlbumID,
295 const std::string& strReleaseGroupMBID,
296 const std::string& strArtist,
297 const std::string& strArtistSort,
298 const std::string& strGenre,
299 const std::string& strReleaseDate,
300 const std::string& strOrigReleaseDate,
301 bool bBoxedSet,
302 const std::string& strRecordLabel,
303 const std::string& strType,
304 const std::string& strReleaseStatus,
305 bool bCompilation,
306 CAlbum::ReleaseType releaseType);
308 /*! \brief retrieve an album, optionally with all songs.
309 \param idAlbum the database id of the album.
310 \param album [out] the album to fill.
311 \param getSongs whether or not to retrieve songs, defaults to true.
312 \return true if the album is retrieved, false otherwise.
314 bool GetAlbum(int idAlbum, CAlbum& album, bool getSongs = true);
315 int UpdateAlbum(int idAlbum,
316 const std::string& strAlbum,
317 const std::string& strMusicBrainzAlbumID,
318 const std::string& strReleaseGroupMBID,
319 const std::string& strArtist,
320 const std::string& strArtistSort,
321 const std::string& strGenre,
322 const std::string& strMoods,
323 const std::string& strStyles,
324 const std::string& strThemes,
325 const std::string& strReview,
326 const std::string& strImage,
327 const std::string& strLabel,
328 const std::string& strType,
329 const std::string& strReleaseStatus,
330 float fRating,
331 int iUserrating,
332 int iVotes,
333 const std::string& strReleaseDate,
334 const std::string& strOrigReleaseDate,
335 bool bBoxedSet,
336 bool bCompilation,
337 CAlbum::ReleaseType releaseType,
338 bool bScrapedMBID);
339 bool ClearAlbumLastScrapedTime(int idAlbum);
340 bool HasAlbumBeenScraped(int idAlbum);
342 /////////////////////////////////////////////////
343 // Audiobook
344 /////////////////////////////////////////////////
345 bool AddAudioBook(const CFileItem& item);
346 bool SetResumeBookmarkForAudioBook(const CFileItem& item, int bookmark);
347 bool GetResumeBookmarkForAudioBook(const CFileItem& item, int& bookmark);
349 /*! \brief Checks if the given path is inside a folder that has already been scanned into the library
350 \param path the path we want to check
352 bool InsideScannedPath(const std::string& path);
354 //// Misc Album
355 int GetAlbumIdByPath(const std::string& path);
356 bool GetAlbumFromSong(int idSong, CAlbum& album);
357 int GetAlbumByName(const std::string& strAlbum, const std::string& strArtist = "");
358 int GetAlbumByName(const std::string& strAlbum, const std::vector<std::string>& artist);
359 bool GetMatchingMusicVideoAlbum(const std::string& strAlbum,
360 const std::string& strArtist,
361 int& idAlbum,
362 std::string& strReview);
363 bool SearchAlbumsByArtistName(const std::string& strArtist, CFileItemList& items);
364 int GetAlbumByMatch(const CAlbum& album);
365 std::string GetAlbumById(int id);
366 std::string GetAlbumDiscTitle(int idAlbum, int idDisc);
367 bool SetAlbumUserrating(const int idAlbum, int userrating);
368 int GetAlbumDiscsCount(int idAlbum);
370 /////////////////////////////////////////////////
371 // Artist CRUD
372 /////////////////////////////////////////////////
373 bool UpdateArtist(const CArtist& artist);
375 int AddArtist(const std::string& strArtist,
376 const std::string& strMusicBrainzArtistID,
377 const std::string& strSortName,
378 bool bScrapedMBID = false);
379 int AddArtist(const std::string& strArtist,
380 const std::string& strMusicBrainzArtistID,
381 bool bScrapedMBID = false);
382 bool GetArtist(int idArtist, CArtist& artist, bool fetchAll = false);
383 bool GetArtistExists(int idArtist);
384 int GetLastArtist();
385 int GetArtistFromMBID(const std::string& strMusicBrainzArtistID, std::string& artistname);
386 int UpdateArtist(int idArtist,
387 const std::string& strArtist,
388 const std::string& strSortName,
389 const std::string& strMusicBrainzArtistID,
390 bool bScrapedMBID,
391 const std::string& strType,
392 const std::string& strGender,
393 const std::string& strDisambiguation,
394 const std::string& strBorn,
395 const std::string& strFormed,
396 const std::string& strGenres,
397 const std::string& strMoods,
398 const std::string& strStyles,
399 const std::string& strInstruments,
400 const std::string& strBiography,
401 const std::string& strDied,
402 const std::string& strDisbanded,
403 const std::string& strYearsActive,
404 const std::string& strImage);
405 bool UpdateArtistScrapedMBID(int idArtist, const std::string& strMusicBrainzArtistID);
406 bool GetTranslateBlankArtist() { return m_translateBlankArtist; }
407 void SetTranslateBlankArtist(bool translate) { m_translateBlankArtist = translate; }
408 bool HasArtistBeenScraped(int idArtist);
409 bool ClearArtistLastScrapedTime(int idArtist);
410 int AddArtistDiscography(int idArtist, const CDiscoAlbum& discoAlbum);
411 bool DeleteArtistDiscography(int idArtist);
412 bool GetArtistDiscography(int idArtist, CFileItemList& items);
413 bool AddArtistVideoLinks(const CArtist& artist);
414 bool DeleteArtistVideoLinks(const int idArtist);
416 std::string GetArtistById(int id);
417 int GetArtistByName(const std::string& strArtist);
418 int GetArtistByMatch(const CArtist& artist);
419 bool GetArtistFromSong(int idSong, CArtist& artist);
420 bool IsSongArtist(int idSong, int idArtist);
421 bool IsSongAlbumArtist(int idSong, int idArtist);
422 std::string GetRoleById(int id);
424 /*! \brief Propagate artist sort name into the concatenated artist sort name strings
425 held for songs and albums
426 \param int idArtist to propagate sort name for, -1 means all artists
428 bool UpdateArtistSortNames(int idArtist = -1);
430 /////////////////////////////////////////////////
431 // Paths
432 /////////////////////////////////////////////////
433 int AddPath(const std::string& strPath);
435 bool GetPaths(std::set<std::string>& paths);
436 bool SetPathHash(const std::string& path, const std::string& hash);
437 bool GetPathHash(const std::string& path, std::string& hash);
438 bool GetAlbumPaths(int idAlbum, std::vector<std::pair<std::string, int>>& paths);
439 bool GetAlbumPath(int idAlbum, std::string& basePath);
440 int GetDiscnumberForPathID(int idPath);
441 bool GetOldArtistPath(int idArtist, std::string& path);
442 bool GetArtistPath(const CArtist& artist, std::string& path);
443 bool GetAlbumFolder(const CAlbum& album, const std::string& strAlbumPath, std::string& strFolder);
444 bool GetArtistFolderName(const CArtist& artist, std::string& strFolder);
445 bool GetArtistFolderName(const std::string& strArtist,
446 const std::string& strMusicBrainzArtistID,
447 std::string& strFolder);
449 /////////////////////////////////////////////////
450 // Sources
451 /////////////////////////////////////////////////
452 bool UpdateSources();
453 int AddSource(const std::string& strName,
454 const std::string& strMultipath,
455 const std::vector<std::string>& vecPaths,
456 int id = -1);
457 int UpdateSource(const std::string& strOldName,
458 const std::string& strName,
459 const std::string& strMultipath,
460 const std::vector<std::string>& vecPaths);
461 bool RemoveSource(const std::string& strName);
462 int GetSourceFromPath(const std::string& strPath);
463 bool AddAlbumSource(int idAlbum, int idSource);
464 bool AddAlbumSources(int idAlbum, const std::string& strPath);
465 bool DeleteAlbumSources(int idAlbum);
466 bool GetSources(CFileItemList& items);
468 bool GetSourcesByArtist(int idArtist, CFileItem* item);
469 bool GetSourcesByAlbum(int idAlbum, CFileItem* item);
470 bool GetSourcesBySong(int idSong, const std::string& strPath, CFileItem* item);
471 int GetSourceByName(const std::string& strSource);
472 std::string GetSourceById(int id);
474 /////////////////////////////////////////////////
475 // Genres
476 /////////////////////////////////////////////////
477 int AddGenre(std::string& strGenre);
478 std::string GetGenreById(int id);
479 int GetGenreByName(const std::string& strGenre);
481 /////////////////////////////////////////////////
482 // Link tables
483 /////////////////////////////////////////////////
484 bool AddAlbumArtist(int idArtist, int idAlbum, const std::string& strArtist, int iOrder);
485 bool GetAlbumsByArtist(int idArtist, std::vector<int>& albums);
486 bool GetArtistsByAlbum(int idAlbum, CFileItem* item);
487 bool GetArtistsByAlbum(int idAlbum, std::vector<std::string>& artistIDs);
488 bool DeleteAlbumArtistsByAlbum(int idAlbum);
490 int AddRole(const std::string& strRole);
491 bool AddSongArtist(int idArtist,
492 int idSong,
493 const std::string& strRole,
494 const std::string& strArtist,
495 int iOrder);
496 bool AddSongArtist(
497 int idArtist, int idSong, int idRole, const std::string& strArtist, int iOrder);
498 int AddSongContributor(int idSong,
499 const std::string& strRole,
500 const std::string& strArtist,
501 const std::string& strSort);
502 void AddSongContributors(int idSong,
503 const VECMUSICROLES& contributors,
504 const std::string& strSort);
505 int GetRoleByName(const std::string& strRole);
506 bool GetRolesByArtist(int idArtist, CFileItem* item);
507 bool GetSongsByArtist(int idArtist, std::vector<int>& songs);
508 bool GetArtistsBySong(int idSong, std::vector<int>& artists);
509 bool DeleteSongArtistsBySong(int idSong);
511 bool AddSongGenres(int idSong, const std::vector<std::string>& genres);
512 bool GetGenresBySong(int idSong, std::vector<int>& genres);
514 bool GetGenresByAlbum(int idAlbum, CFileItem* item);
516 bool GetGenresByArtist(int idArtist, CFileItem* item);
517 bool GetIsAlbumArtist(int idArtist, CFileItem* item);
519 /////////////////////////////////////////////////
520 // Top 100
521 /////////////////////////////////////////////////
522 bool GetTop100(const std::string& strBaseDir, CFileItemList& items);
523 bool GetTop100Albums(VECALBUMS& albums);
524 bool GetTop100AlbumSongs(const std::string& strBaseDir, CFileItemList& item);
526 /////////////////////////////////////////////////
527 // Recently added
528 /////////////////////////////////////////////////
529 bool GetRecentlyAddedAlbums(VECALBUMS& albums, unsigned int limit = 0);
530 bool GetRecentlyAddedAlbumSongs(const std::string& strBaseDir,
531 CFileItemList& item,
532 unsigned int limit = 0);
533 bool GetRecentlyPlayedAlbums(VECALBUMS& albums);
534 bool GetRecentlyPlayedAlbumSongs(const std::string& strBaseDir, CFileItemList& item);
536 /////////////////////////////////////////////////
537 // Compilations
538 /////////////////////////////////////////////////
539 int GetCompilationAlbumsCount();
541 ////////////////////////////////////////////////
542 // Boxsets
543 ////////////////////////////////////////////////
544 bool IsAlbumBoxset(int idAlbum);
545 int GetBoxsetsCount();
547 int GetSinglesCount();
549 int GetArtistCountForRole(int role);
550 int GetArtistCountForRole(const std::string& strRole);
552 /*! \brief Increment the playcount of an item
553 Increments the playcount and updates the last played date
554 \param item CFileItem to increment the playcount for
556 void IncrementPlayCount(const CFileItem& item);
557 bool CleanupOrphanedItems();
559 /////////////////////////////////////////////////
560 // VIEWS
561 /////////////////////////////////////////////////
562 bool GetGenresNav(const std::string& strBaseDir,
563 CFileItemList& items,
564 const Filter& filter = Filter(),
565 bool countOnly = false);
566 bool GetSourcesNav(const std::string& strBaseDir,
567 CFileItemList& items,
568 const Filter& filter = Filter(),
569 bool countOnly = false);
570 bool GetYearsNav(const std::string& strBaseDir,
571 CFileItemList& items,
572 const Filter& filter = Filter());
573 bool GetRolesNav(const std::string& strBaseDir,
574 CFileItemList& items,
575 const Filter& filter = Filter());
576 bool GetArtistsNav(const std::string& strBaseDir,
577 CFileItemList& items,
578 bool albumArtistsOnly = false,
579 int idGenre = -1,
580 int idAlbum = -1,
581 int idSong = -1,
582 const Filter& filter = Filter(),
583 const SortDescription& sortDescription = SortDescription(),
584 bool countOnly = false);
585 bool GetCommonNav(const std::string& strBaseDir,
586 const std::string& table,
587 const std::string& labelField,
588 CFileItemList& items,
589 const Filter& filter /* = Filter() */,
590 bool countOnly /* = false */);
591 bool GetAlbumTypesNav(const std::string& strBaseDir,
592 CFileItemList& items,
593 const Filter& filter = Filter(),
594 bool countOnly = false);
595 bool GetMusicLabelsNav(const std::string& strBaseDir,
596 CFileItemList& items,
597 const Filter& filter = Filter(),
598 bool countOnly = false);
599 bool GetAlbumsNav(const std::string& strBaseDir,
600 CFileItemList& items,
601 int idGenre = -1,
602 int idArtist = -1,
603 const Filter& filter = Filter(),
604 const SortDescription& sortDescription = SortDescription(),
605 bool countOnly = false);
606 bool GetDiscsNav(const std::string& strBaseDir,
607 CFileItemList& items,
608 int idAlbum,
609 const Filter& filter = Filter(),
610 const SortDescription& sortDescription = SortDescription(),
611 bool countOnly = false);
612 bool GetAlbumsByYear(const std::string& strBaseDir, CFileItemList& items, int year);
613 bool GetSongsNav(const std::string& strBaseDir,
614 CFileItemList& items,
615 int idGenre,
616 int idArtist,
617 int idAlbum,
618 const SortDescription& sortDescription = SortDescription());
619 bool GetSongsByYear(const std::string& baseDir, CFileItemList& items, int year);
620 bool GetSongsByWhere(const std::string& baseDir,
621 const Filter& filter,
622 CFileItemList& items,
623 const SortDescription& sortDescription = SortDescription());
624 bool GetSongsFullByWhere(const std::string& baseDir,
625 const Filter& filter,
626 CFileItemList& items,
627 const SortDescription& sortDescription = SortDescription(),
628 bool artistData = false);
629 bool GetAlbumsByWhere(const std::string& baseDir,
630 const Filter& filter,
631 CFileItemList& items,
632 const SortDescription& sortDescription = SortDescription(),
633 bool countOnly = false);
634 bool GetDiscsByWhere(const std::string& baseDir,
635 const Filter& filter,
636 CFileItemList& items,
637 const SortDescription& sortDescription = SortDescription(),
638 bool countOnly = false);
639 bool GetDiscsByWhere(CMusicDbUrl& musicUrl,
640 const Filter& filter,
641 CFileItemList& items,
642 const SortDescription& sortDescription = SortDescription(),
643 bool countOnly = false);
644 bool GetArtistsByWhere(const std::string& strBaseDir,
645 const Filter& filter,
646 CFileItemList& items,
647 const SortDescription& sortDescription = SortDescription(),
648 bool countOnly = false);
649 int GetDiscsCount(const std::string& baseDir, const Filter& filter = Filter());
650 int GetSongsCount(const Filter& filter = Filter());
651 bool GetFilter(CDbUrl& musicUrl, Filter& filter, SortDescription& sorting) override;
652 int GetOrderFilter(const std::string& type, const SortDescription& sorting, Filter& filter);
654 /////////////////////////////////////////////////
655 // Party Mode
656 /////////////////////////////////////////////////
657 /*! \brief Gets song IDs in random order that match the filter criteria
658 \param filter the criteria to apply in the query
659 \param songIDs a vector of <1, id> pairs suited to party mode use
660 \return count of song ids found.
662 unsigned int GetRandomSongIDs(const Filter& filter, std::vector<std::pair<int, int>>& songIDs);
664 /////////////////////////////////////////////////
665 // JSON-RPC
666 /////////////////////////////////////////////////
667 bool GetGenresJSON(CFileItemList& items, bool bSources = false);
668 bool GetArtistsByWhereJSON(const std::set<std::string>& fields,
669 const std::string& baseDir,
670 CVariant& result,
671 int& total,
672 const SortDescription& sortDescription = SortDescription());
673 bool GetAlbumsByWhereJSON(const std::set<std::string>& fields,
674 const std::string& baseDir,
675 CVariant& result,
676 int& total,
677 const SortDescription& sortDescription = SortDescription());
678 bool GetSongsByWhereJSON(const std::set<std::string>& fields,
679 const std::string& baseDir,
680 CVariant& result,
681 int& total,
682 const SortDescription& sortDescription = SortDescription());
684 /////////////////////////////////////////////////
685 // Scraper
686 /////////////////////////////////////////////////
687 bool SetScraper(int id, const CONTENT_TYPE& content, const ADDON::ScraperPtr& scraper);
688 bool SetScraperAll(const std::string& strBaseDir, const ADDON::ScraperPtr& scraper);
689 bool GetScraper(int id, const CONTENT_TYPE& content, ADDON::ScraperPtr& scraper);
691 /*! \brief Check whether a given scraper is in use.
692 \param scraperID the scraper to check for.
693 \return true if the scraper is in use, false otherwise.
695 bool ScraperInUse(const std::string& scraperID) const;
697 /////////////////////////////////////////////////
698 // Filters
699 /////////////////////////////////////////////////
700 bool GetItems(const std::string& strBaseDir,
701 CFileItemList& items,
702 const Filter& filter = Filter(),
703 const SortDescription& sortDescription = SortDescription());
704 bool GetItems(const std::string& strBaseDir,
705 const std::string& itemType,
706 CFileItemList& items,
707 const Filter& filter = Filter(),
708 const SortDescription& sortDescription = SortDescription());
709 std::string GetItemById(const std::string& itemType, int id);
711 /////////////////////////////////////////////////
712 // XML
713 /////////////////////////////////////////////////
714 void ExportToXML(const CLibExportSettings& settings,
715 CGUIDialogProgress* progressDialog = nullptr);
716 bool ExportSongHistory(TiXmlNode* pNode, CGUIDialogProgress* progressDialog = nullptr);
717 void ImportFromXML(const std::string& xmlFile, CGUIDialogProgress* progressDialog = nullptr);
718 bool ImportSongHistory(const std::string& xmlFile,
719 const int total,
720 CGUIDialogProgress* progressDialog = nullptr);
722 /////////////////////////////////////////////////
723 // Properties
724 /////////////////////////////////////////////////
725 void SetPropertiesForFileItem(CFileItem& item);
726 static void SetPropertiesFromArtist(CFileItem& item, const CArtist& artist);
727 static void SetPropertiesFromAlbum(CFileItem& item, const CAlbum& album);
728 void SetItemUpdated(int mediaId, const std::string& mediaType);
730 /////////////////////////////////////////////////
731 // Art
732 /////////////////////////////////////////////////
733 /*! \brief Sets art for a database item.
734 Sets a single piece of art for a database item.
735 \param mediaId the id in the media (song/artist/album) table.
736 \param mediaType the type of media, which corresponds to the table the item resides in (song/artist/album).
737 \param artType the type of art to set, e.g. "thumb", "fanart"
738 \param url the url to the art (this is the original url, not a cached url).
739 \sa GetArtForItem
741 void SetArtForItem(int mediaId,
742 const std::string& mediaType,
743 const std::string& artType,
744 const std::string& url);
746 /*! \brief Sets art for a database item.
747 Sets multiple pieces of art for a database item.
748 \param mediaId the id in the media (song/artist/album) table.
749 \param mediaType the type of media, which corresponds to the table the item resides in (song/artist/album).
750 \param art a map of <type, url> where type is "thumb", "fanart", etc. and url is the original url of the art.
751 \sa GetArtForItem
753 void SetArtForItem(int mediaId,
754 const std::string& mediaType,
755 const std::map<std::string, std::string>& art);
758 /*! \brief Fetch all related art for a database item.
759 Fetches multiple pieces of art for a database item including that for related media types
760 Given song id art for the related album, artist(s) and albumartist(s) will also be fetched, looking up the
761 album and artist when ids are not provided.
762 Given album id (and not song id) art for the related artist(s) will also be fetched, looking up the
763 artist(s) when id are not provided.
764 \param songId the id in the song table, -1 when song art not being fetched
765 \param albumId the id in the album table, -1 when album art not being fetched
766 \param artistId the id in the artist table, -1 when artist not known
767 \param bPrimaryArtist true if art from only the first song artist or album artist is to be fetched
768 \param art [out] a vector, each element having media type e.g. "artist", "album" or "song",
769 artType e.g. "thumb", "fanart", etc., prefix of "", "artist" or "albumartist" etc. giving the kind of artist
770 relationship, and the original url of the art.
772 \return true if art is retrieved, false if no art is found.
773 \sa SetArtForItem
775 bool GetArtForItem(int songId,
776 int albumId,
777 int artistId,
778 bool bPrimaryArtist,
779 std::vector<ArtForThumbLoader>& art);
781 /*! \brief Fetch art for a database item.
782 Fetches multiple pieces of art for a database item.
783 \param mediaId the id in the media (song/artist/album) table.
784 \param mediaType the type of media, which corresponds to the table the item resides in (song/artist/album).
785 \param art [out] a map of <type, url> where type is "thumb", "fanart", etc. and url is the original url of the art.
786 \return true if art is retrieved, false if no art is found.
787 \sa SetArtForItem
789 bool GetArtForItem(int mediaId,
790 const std::string& mediaType,
791 std::map<std::string, std::string>& art);
793 /*! \brief Fetch art for a database item.
794 Fetches a single piece of art for a database item.
795 \param mediaId the id in the media (song/artist/album) table.
796 \param mediaType the type of media, which corresponds to the table the item resides in (song/artist/album).
797 \param artType the type of art to retrieve, eg "thumb", "fanart".
798 \return the original URL to the piece of art, if available.
799 \sa SetArtForItem
801 std::string GetArtForItem(int mediaId, const std::string& mediaType, const std::string& artType);
803 /*! \brief Remove art for a database item.
804 Removes a single piece of art for a database item.
805 \param mediaId the id in the media (song/artist/album) table.
806 \param mediaType the type of media, which corresponds to the table the item resides in (song/artist/album).
807 \param artType the type of art to remove, eg "thumb", "fanart".
808 \return true if art is removed, false if no art is found.
809 \sa RemoveArtForItem
811 bool RemoveArtForItem(int mediaId, const MediaType& mediaType, const std::string& artType);
813 /*! \brief Remove art for a database item.
814 Removes multiple pieces of art for a database item.
815 \param mediaId the id in the media (song/artist/album) table.
816 \param mediaType the type of media, which corresponds to the table the item resides in (song/artist/album).
817 \param arttypes a set of types, e.g. "thumb", "fanart", etc. to be removed.
818 \return true if art is removed, false if no art is found.
819 \sa RemoveArtForItem
821 bool RemoveArtForItem(int mediaId,
822 const MediaType& mediaType,
823 const std::set<std::string>& artTypes);
825 /*! \brief Fetch the distinct types of art held in the database for a type of media.
826 \param mediaType the type of media, which corresponds to the table the item resides in (song/artist/album).
827 \param artTypes [out] the types of art e.g. "thumb", "fanart", etc.
828 \return true if art is found, false if no art is found.
830 bool GetArtTypes(const MediaType& mediaType, std::vector<std::string>& artTypes);
832 /*! \brief Fetch the distinct types of available-but-unassigned art held in the
833 database for a specific media item.
834 \param mediaId the id in the media (artist/album) table.
835 \param mediaType the type of media, which corresponds to the table the item resides in (artist/album).
836 \return the types of art e.g. "thumb", "fanart", etc.
838 std::vector<std::string> GetAvailableArtTypesForItem(int mediaId, const MediaType& mediaType);
840 /*! \brief Fetch the list of available-but-unassigned art URLs held in the
841 database for a specific media item and art type.
842 \param mediaId the id in the media (artist/album) table.
843 \param mediaType corresponds to the table the item resides in (artist/album).
844 \param artType e.g. "thumb", "fanart", etc.
845 \return list of URLs
847 std::vector<CScraperUrl::SUrlEntry> GetAvailableArtForItem(int mediaId,
848 const MediaType& mediaType,
849 const std::string& artType);
851 /////////////////////////////////////////////////
852 // Tag Scan Version
853 /////////////////////////////////////////////////
854 /*! \brief Check if music files need all tags rescanning regardless of file being unchanged
855 because the tag processing has changed (which may happen without db version changes) since they
856 where last scanned.
857 \return -1 if an error occurred, 0 if no scan is needed, or the version number of tags if not the same as current.
859 virtual int GetMusicNeedsTagScan();
861 /*! \brief Set minimum version number of db needed when tag data scanned from music files
862 \param version the version number of db
864 void SetMusicNeedsTagScan(int version);
866 /*! \brief Set the version number of tag data
867 \param version the version number of db when tags last scanned, 0 (default) means current db version
869 void SetMusicTagScanVersion(int version = 0);
871 std::string GetLibraryLastUpdated();
872 void SetLibraryLastUpdated();
873 std::string GetLibraryLastCleaned();
874 void SetLibraryLastCleaned();
875 std::string GetArtistLinksUpdated();
876 void SetArtistLinksUpdated();
877 std::string GetGenresLastAdded();
878 std::string GetSongsLastAdded();
879 std::string GetAlbumsLastAdded();
880 std::string GetArtistsLastAdded();
881 std::string GetSongsLastModified();
882 std::string GetAlbumsLastModified();
883 std::string GetArtistsLastModified();
886 * @brief Check the passed in list of images if used in this database. Used to clean the image cache.
887 * @param imagesToCheck
888 * @return a list of the passed in images used by this database.
890 std::vector<std::string> GetUsedImages(const std::vector<std::string>& imagesToCheck) const;
892 protected:
893 std::map<std::string, int> m_genreCache;
894 std::map<std::string, int> m_pathCache;
896 void CreateTables() override;
897 void CreateAnalytics() override;
898 int GetMinSchemaVersion() const override { return 32; }
899 int GetSchemaVersion() const override;
901 const char* GetBaseDBName() const override { return "MyMusic"; }
903 private:
904 /*! \brief (Re)Create the generic database views for songs and albums
906 virtual void CreateViews();
907 void CreateNativeDBFunctions();
908 void CreateRemovedLinkTriggers();
910 void SplitPath(const std::string& strFileNameAndPath,
911 std::string& strPath,
912 std::string& strFileName);
914 CSong GetSongFromDataset();
915 CSong GetSongFromDataset(const dbiplus::sql_record* const record, int offset = 0);
916 CArtist GetArtistFromDataset(dbiplus::Dataset* pDS, int offset = 0, bool needThumb = true);
917 CArtist GetArtistFromDataset(const dbiplus::sql_record* const record,
918 int offset = 0,
919 bool needThumb = true);
920 CAlbum GetAlbumFromDataset(dbiplus::Dataset* pDS, int offset = 0, bool imageURL = false);
921 CAlbum GetAlbumFromDataset(const dbiplus::sql_record* const record,
922 int offset = 0,
923 bool imageURL = false);
924 CArtistCredit GetArtistCreditFromDataset(const dbiplus::sql_record* const record, int offset = 0);
925 CMusicRole GetArtistRoleFromDataset(const dbiplus::sql_record* const record, int offset = 0);
926 std::string GetMediaDateFromFile(const std::string& strFileNameAndPath);
927 void GetFileItemFromDataset(CFileItem* item, const CMusicDbUrl& baseUrl);
928 void GetFileItemFromDataset(const dbiplus::sql_record* const record,
929 CFileItem* item,
930 const CMusicDbUrl& baseUrl);
931 void GetFileItemFromArtistCredits(VECARTISTCREDITS& artistCredits, CFileItem* item);
933 bool DeleteRemovedLinks();
935 bool CleanupSongs(CGUIDialogProgress* progressDialog = nullptr);
936 bool CleanupSongsByIds(const std::string& strSongIds);
937 bool CleanupPaths();
938 bool CleanupAlbums();
939 bool CleanupArtists();
940 bool CleanupGenres();
941 bool CleanupInfoSettings();
942 bool CleanupRoles();
943 void UpdateTables(int version) override;
944 bool SearchArtists(const std::string& search, CFileItemList& artists);
945 bool SearchAlbums(const std::string& search, CFileItemList& albums);
946 bool SearchSongs(const std::string& strSearch, CFileItemList& songs);
947 int GetSongIDFromPath(const std::string& filePath);
948 void NormaliseSongDates(std::string& strRelease, std::string& strOriginal);
949 bool TrimImageURLs(std::string& strImage, const size_t space);
951 /*! \brief Build SQL for sort subquery from ignore article token list
952 \param strField original name or title field that articles could be removed from
953 \return SQL string e.g. WHEN strField LIKE 'the_' ESCAPE '_' THEN SUBSTR(strArtist, 5)
955 std::string GetIgnoreArticleSQL(const std::string& strField);
957 /*! \brief Build SQL for sort name scalar subquery from sort attributes and ignore article list.
958 \param strAlias alias name of scalar subquery field
959 \param sortAttributes the sort attributes e.g. SortAttributeIgnoreArticle
960 \param strField original name or title field that articles could be removed from
961 \param strSortField sort name or title field to be used instead of original (when data not null)
962 \return SQL string e.g.
963 CASE WHEN strArtistSort IS NOT NULL THEN strArtistSort
964 WHEN strField LIKE 'the ' OR strField LIKE 'the_' ESCAPE '_' THEN SUBSTR(strArtist, 5)
965 ELSE strField
966 END AS strAlias
968 std::string SortnameBuildSQL(const std::string& strAlias,
969 const SortAttribute& sortAttributes,
970 const std::string& strField,
971 const std::string& strSortField);
973 /*! \brief Build SQL for sorting field naturally and case-insensitively (in SQLite).
974 \param strField field name
975 \param sortOrder the sort order
976 \return SQL string e.g.
977 CASE WHEN CAST(strTitle AS INTEGER) = 0 THEN 100000000
978 ELSE CAST(strTitle AS INTEGER) END DESC, strTitle COLLATE NOCASE DESC
980 std::string AlphanumericSortSQL(const std::string& strField, const SortOrder& sortOrder);
982 /*! \brief Checks that source table matches sources.xml
983 returns true when they do
985 bool CheckSources(std::vector<CMediaSource>& sources);
987 /*! \brief Initially fills source table from sources.xml for use only at
988 migration of db from an earlier version than 72
989 returns true when successfully done
991 bool MigrateSources();
993 bool m_translateBlankArtist;
995 // Fields should be ordered as they
996 // appear in the songview
997 static enum _SongFields {
998 song_idSong = 0,
999 song_strArtists,
1000 song_strArtistSort,
1001 song_strGenres,
1002 song_strTitle,
1003 song_iTrack,
1004 song_iDuration,
1005 song_strReleaseDate,
1006 song_strOrigReleaseDate,
1007 song_strDiscSubtitle,
1008 song_strFileName,
1009 song_strMusicBrainzTrackID,
1010 song_iTimesPlayed,
1011 song_iStartOffset,
1012 song_iEndOffset,
1013 song_lastplayed,
1014 song_rating,
1015 song_userrating,
1016 song_votes,
1017 song_comment,
1018 song_idAlbum,
1019 song_strAlbum,
1020 song_strPath,
1021 song_strReleaseStatus,
1022 song_bCompilation,
1023 song_bBoxedSet,
1024 song_strAlbumArtists,
1025 song_strAlbumArtistSort,
1026 song_strAlbumReleaseType,
1027 song_mood,
1028 song_strReplayGain,
1029 song_iBPM,
1030 song_iBitRate,
1031 song_iSampleRate,
1032 song_iChannels,
1033 song_songVideoURL,
1034 song_iAlbumDuration,
1035 song_iDiscTotal,
1036 song_dateAdded,
1037 song_dateNew,
1038 song_dateModified,
1039 song_enumCount // end of the enum, do not add past here
1040 } SongFields;
1042 // Fields should be ordered as they
1043 // appear in the albumview
1044 static enum _AlbumFields {
1045 album_idAlbum = 0,
1046 album_strAlbum,
1047 album_strMusicBrainzAlbumID,
1048 album_strReleaseGroupMBID,
1049 album_strArtists,
1050 album_strArtistSort,
1051 album_strGenres,
1052 album_strReleaseDate,
1053 album_strOrigReleaseDate,
1054 album_bBoxedSet,
1055 album_strMoods,
1056 album_strStyles,
1057 album_strThemes,
1058 album_strReview,
1059 album_strLabel,
1060 album_strType,
1061 album_strReleaseStatus,
1062 album_strThumbURL,
1063 album_fRating,
1064 album_iUserrating,
1065 album_iVotes,
1066 album_bCompilation,
1067 album_bScrapedMBID,
1068 album_lastScraped,
1069 album_dateAdded,
1070 album_dateNew,
1071 album_dateModified,
1072 album_iTimesPlayed,
1073 album_strReleaseType,
1074 album_iTotalDiscs,
1075 album_dtLastPlayed,
1076 album_iAlbumDuration,
1077 album_enumCount // end of the enum, do not add past here
1078 } AlbumFields;
1080 // Fields should be ordered as they
1081 // appear in the songartistview/albumartistview
1082 static enum _ArtistCreditFields {
1083 // used for GetAlbum to get the cascaded album/song artist credits
1084 artistCredit_idEntity = 0, // can be idSong or idAlbum depending on context
1085 artistCredit_idArtist,
1086 artistCredit_idRole,
1087 artistCredit_strRole,
1088 artistCredit_strArtist,
1089 artistCredit_strSortName,
1090 artistCredit_strMusicBrainzArtistID,
1091 artistCredit_iOrder,
1092 artistCredit_enumCount
1093 } ArtistCreditFields;
1095 // Fields should be ordered as they
1096 // appear in the artistview
1097 static enum _ArtistFields {
1098 artist_idArtist = 0,
1099 artist_strArtist,
1100 artist_strSortName,
1101 artist_strMusicBrainzArtistID,
1102 artist_strType,
1103 artist_strGender,
1104 artist_strDisambiguation,
1105 artist_strBorn,
1106 artist_strFormed,
1107 artist_strGenres,
1108 artist_strMoods,
1109 artist_strStyles,
1110 artist_strInstruments,
1111 artist_strBiography,
1112 artist_strDied,
1113 artist_strDisbanded,
1114 artist_strYearsActive,
1115 artist_strImage,
1116 artist_bScrapedMBID,
1117 artist_lastScraped,
1118 artist_dateAdded,
1119 artist_dateNew,
1120 artist_dateModified,
1121 artist_enumCount // end of the enum, do not add past here
1122 } ArtistFields;
1124 // Fields fetched by GetArtistsByWhereJSON, order same as in JSONtoDBArtist
1125 static enum _JoinToArtistFields {
1126 joinToArtist_isSong = 0,
1127 joinToArtist_idSourceAlbum,
1128 joinToArtist_idSourceSong,
1129 joinToArtist_idSongGenreAlbum,
1130 joinToArtist_idSongGenreSong,
1131 joinToArtist_strSongGenreAlbum,
1132 joinToArtist_strSongGenreSong,
1133 joinToArtist_idArt,
1134 joinToArtist_artType,
1135 joinToArtist_artURL,
1136 joinToArtist_idRole,
1137 joinToArtist_strRole,
1138 joinToArtist_iOrderRole,
1139 joinToArtist_isalbumartist,
1140 joinToArtist_thumbnail,
1141 joinToArtist_fanart,
1142 joinToArtist_enumCount // end of the enum, do not add past here
1143 } JoinToArtistFields;
1145 // Fields fetched by GetAlbumsByWhereJSON, order same as in JSONtoDBAlbum
1146 static enum _JoinToAlbumFields {
1147 joinToAlbum_idArtist = 0,
1148 joinToAlbum_strArtist,
1149 joinToAlbum_strArtistMBID,
1150 joinToAlbum_enumCount // end of the enum, do not add past here
1151 } JoinToAlbumFields;
1153 // Fields fetched by GetSongsByWhereJSON, order same as in JSONtoDBSong
1154 static enum _JoinToSongFields {
1155 // Used by GetSongsByWhereJSON
1156 joinToSongs_idAlbumArtist = 0,
1157 joinToSongs_strAlbumArtist,
1158 joinToSongs_strAlbumArtistMBID,
1159 joinToSongs_iOrderAlbumArtist,
1160 joinToSongs_idArtist,
1161 joinToSongs_strArtist,
1162 joinToSongs_strArtistMBID,
1163 joinToSongs_iOrderArtist,
1164 joinToSongs_idRole,
1165 joinToSongs_strRole,
1166 joinToSongs_iOrderRole,
1167 joinToSongs_idGenre,
1168 joinToSongs_iOrderGenre,
1169 joinToSongs_enumCount // end of the enum, do not add past here
1170 } JoinToSongFields;