File indexing completed on 2024-05-19 04:48:25

0001 #pragma once
0002 
0003 #include <QList>
0004 #include <QString>
0005 #include <QStringList>
0006 
0007 #include <QSqlDatabase>
0008 #include <QSqlDriver>
0009 #include <QSqlError>
0010 #include <QSqlQuery>
0011 #include <QSqlRecord>
0012 
0013 #include <QDebug>
0014 
0015 #include <QVariantMap>
0016 #include <functional>
0017 
0018 #include "../utils/bae.h"
0019 
0020 enum sourceTypes { LOCAL, ONLINE, DEVICE };
0021 
0022 class CollectionDB : public QObject
0023 {
0024     Q_OBJECT
0025 
0026 public:
0027     static CollectionDB *getInstance()
0028     {
0029         static CollectionDB db;
0030         return &db;
0031     }
0032 
0033     CollectionDB(const CollectionDB &) = delete;
0034     CollectionDB &operator=(const CollectionDB &) = delete;
0035     CollectionDB(CollectionDB &&) = delete;
0036     CollectionDB &operator=(CollectionDB &&) = delete;
0037 
0038     bool insert(const QString &tableName, const QVariantMap &insertData);
0039     bool update(const QString &tableName, const FMH::MODEL &updateData, const QVariantMap &where);
0040     bool update(const QString &table, const QString &column, const QVariant &newValue, const QVariant &op, const QString &id);
0041     bool remove(const QString &table, const QString &column, const QVariantMap &where);
0042 
0043     bool execQuery(QSqlQuery &query) const;
0044     bool execQuery(const QString &queryTxt);
0045 
0046     /*basic public actions*/
0047     bool check_existance(const QString &tableName, const QString &searchId, const QString &search);
0048 
0049     /* usefull actions */
0050     bool addTrack(const FMH::MODEL &track);
0051     bool updateTrack(const FMH::MODEL &track);
0052     Q_INVOKABLE bool rateTrack(const QString &path, const int &value);
0053 
0054     bool lyricsTrack(const FMH::MODEL &track, const QString &value);
0055     Q_INVOKABLE bool playedTrack(const QString &url);
0056 
0057     bool albumTrack(const FMH::MODEL &track, const QString &value);
0058     bool trackTrack(const FMH::MODEL &track, const QString &value);
0059 
0060     FMH::MODEL_LIST getDBData(const QStringList &urls);
0061     FMH::MODEL_LIST getDBData(const QString &queryTxt, std::function<bool(FMH::MODEL &item)> modifier = nullptr);
0062     QVariantList getDBDataQML(const QString &queryTxt);
0063     static QStringList dataToList(const FMH::MODEL_LIST &list, const FMH::MODEL_KEY &key);
0064 
0065     FMH::MODEL_LIST getAlbumTracks(const QString &album, const QString &artist, const FMH::MODEL_KEY &orderBy = FMH::MODEL_KEY::TRACK, const BAE::W &order = BAE::W::ASC);
0066     FMH::MODEL_LIST getArtistTracks(const QString &artist, const FMH::MODEL_KEY &orderBy = FMH::MODEL_KEY::ALBUM, const BAE::W &order = BAE::W::ASC);
0067     FMH::MODEL_LIST getSearchedTracks(const FMH::MODEL_KEY &where, const QString &search);
0068     FMH::MODEL_LIST getMostPlayedTracks(const int &greaterThan = 1, const int &limit = 50, const FMH::MODEL_KEY &orderBy = FMH::MODEL_KEY::COUNT, const BAE::W &order = BAE::W::DESC);
0069     FMH::MODEL_LIST getRecentTracks(const int &limit = 50, const FMH::MODEL_KEY &orderBy = FMH::MODEL_KEY::ADDDATE, const BAE::W &order = BAE::W::DESC);
0070 
0071     int getTrackStars(const QString &path);
0072     QStringList getArtistAlbums(const QString &artist);
0073 
0074     void removeMissingTracks();
0075 
0076     bool removeArtist(const QString &artist);
0077     bool cleanArtists();
0078     bool removeAlbum(const QString &album, const QString &artist);
0079     bool cleanAlbums();
0080 
0081     bool removeSource(const QString &url);
0082     bool removeTrack(const QString &path);
0083     QSqlQuery getQuery(const QString &queryTxt) const;
0084     QSqlQuery getQuery() const;
0085 
0086     /*useful tools*/
0087     sourceTypes sourceType(const QString &url);
0088     void openDB(const QString &name);
0089 
0090 private:
0091     explicit CollectionDB(QObject *parent = nullptr);
0092     void prepareCollectionDB();
0093 
0094     QString name;
0095     QSqlDatabase m_db;
0096 
0097 Q_SIGNALS:
0098     void trackInserted(QVariantMap item);
0099 
0100     void albumInserted(QVariantMap item);
0101     void albumsInserted(QVariantList items);
0102 
0103     void artistInserted(QVariantMap item);
0104     void artistsInserted(QVariantList items);
0105 
0106     void sourceInserted(QVariantMap item);
0107     void sourcesInserted(QVariantList items);
0108 
0109     void albumsCleaned(const int &amount);
0110     void artistsCleaned(const int &amount);
0111 };