Commit 8cf9c9f8 authored by camilo higuita's avatar camilo higuita

add modifier to db queries, now use don albumsmodel

parent 2a3507a6
......@@ -202,7 +202,7 @@ bool CollectionDB::update(const QString &tableName, const FMH::MODEL &updateData
for (auto key : where.keys())
condition.append(key+" = '"+where[key].toString()+"'");
QString sqlQueryString = "UPDATE " + tableName + " SET " + QString(set.join(",")) + " WHERE " + QString(condition.join(",")) ;
const QString sqlQueryString = "UPDATE " + tableName + " SET " + QString(set.join(",")) + " WHERE " + QString(condition.join(",")) ;
auto query = this->getQuery(sqlQueryString);
qDebug()<<sqlQueryString;
return query.exec();
......@@ -210,11 +210,15 @@ bool CollectionDB::update(const QString &tableName, const FMH::MODEL &updateData
bool CollectionDB::update(const QString &table, const QString &column, const QVariant &newValue, const QVariant &op, const QString &id)
{
auto queryStr = QString("UPDATE %1 SET %2 = \"%3\" WHERE %4 = \"%5\"").arg(table, column, newValue.toString().replace("\"","\"\""), op.toString(), id);
const auto queryStr = QString("UPDATE %1 SET %2 = \"%3\" WHERE %4 = \"%5\"").arg(table, column, newValue.toString().replace("\"","\"\""), op.toString(), id);
auto query = this->getQuery(queryStr);
return query.exec();
}
bool CollectionDB::remove(const QString &table, const QString &column, const QVariantMap &where)
{
return false;}
bool CollectionDB::execQuery(QSqlQuery &query) const
{
if(query.exec()) return true;
......@@ -572,7 +576,7 @@ FMH::MODEL_LIST CollectionDB::getDBData(const QStringList &urls)
return mapList;
}
FMH::MODEL_LIST CollectionDB::getDBData(const QString &queryTxt)
FMH::MODEL_LIST CollectionDB::getDBData(const QString &queryTxt, std::function<void(FMH::MODEL &item)> modifier)
{
FMH::MODEL_LIST mapList;
......@@ -586,7 +590,8 @@ FMH::MODEL_LIST CollectionDB::getDBData(const QString &queryTxt)
for(auto key : FMH::MODEL_NAME.keys())
if(query.record().indexOf(FMH::MODEL_NAME[key]) > -1)
data.insert(key, query.value(FMH::MODEL_NAME[key]).toString());
if(modifier)
modifier(data);
mapList << data;
}
......@@ -1036,9 +1041,14 @@ void CollectionDB::removeMissingTracks()
}
bool CollectionDB::removeArtwork(const QString &table, const QVariantMap &item)
{
return this->update(table, {{FMH::MODEL_KEY::ARTWORK, ""}}, item);
}
bool CollectionDB::removeArtist(const QString &artist)
{
auto queryTxt = QString("DELETE FROM %1 WHERE %2 = \"%3\" ").arg(TABLEMAP[TABLE::ARTISTS],
const auto queryTxt = QString("DELETE FROM %1 WHERE %2 = \"%3\" ").arg(TABLEMAP[TABLE::ARTISTS],
FMH::MODEL_NAME[FMH::MODEL_KEY::ARTIST],artist);
auto query = this->getQuery(queryTxt);
......@@ -1048,7 +1058,7 @@ bool CollectionDB::removeArtist(const QString &artist)
bool CollectionDB::cleanArtists()
{
// delete from artists where artist in (select artist from artists except select distinct artist from tracks);
auto queryTxt=QString("DELETE FROM %1 WHERE %2 IN (SELECT %2 FROM %1 EXCEPT SELECT DISTINCT %2 FROM %3)").arg(
const auto queryTxt=QString("DELETE FROM %1 WHERE %2 IN (SELECT %2 FROM %1 EXCEPT SELECT DISTINCT %2 FROM %3)").arg(
TABLEMAP[TABLE::ARTISTS],
FMH::MODEL_NAME[FMH::MODEL_KEY::ARTIST],
TABLEMAP[TABLE::TRACKS]
......@@ -1062,7 +1072,7 @@ bool CollectionDB::cleanArtists()
bool CollectionDB::removeFolder(const QString &url)
{
auto queryTxt=QString("DELETE FROM %1 WHERE %2 LIKE \"%3%\"").arg(
const auto queryTxt=QString("DELETE FROM %1 WHERE %2 LIKE \"%3%\"").arg(
TABLEMAP[TABLE::FOLDERS],
FMH::MODEL_NAME[FMH::MODEL_KEY::URL], url);
......
......@@ -12,7 +12,7 @@
#include <QFileInfo>
#include <QDir>
#include <QVariantMap>
#include <functional>
#include "../utils/bae.h"
enum sourceTypes
......@@ -30,7 +30,7 @@ public:
bool insert(const QString &tableName, const QVariantMap &insertData);
bool update(const QString &tableName, const FMH::MODEL &updateData, const QVariantMap &where);
bool update(const QString &table, const QString &column, const QVariant &newValue, const QVariant &op, const QString &id);
bool remove();
bool remove(const QString &table, const QString &column, const QVariantMap &where);
bool execQuery(QSqlQuery &query) const;
bool execQuery(const QString &queryTxt);
......@@ -70,7 +70,7 @@ public:
bool favTrack(const QString &path, const bool &value);
FMH::MODEL_LIST getDBData(const QStringList &urls);
FMH::MODEL_LIST getDBData(const QString &queryTxt);
FMH::MODEL_LIST getDBData(const QString &queryTxt, std::function<void(FMH::MODEL &item)> modifier = nullptr);
QVariantList getDBDataQML(const QString &queryTxt);
static QStringList dataToList(const FMH::MODEL_LIST &list, const FMH::MODEL_KEY &key);
......@@ -98,6 +98,7 @@ public:
Q_INVOKABLE bool removePlaylistTrack(const QString &url, const QString &playlist);
Q_INVOKABLE bool removePlaylist(const QString &playlist);
Q_INVOKABLE void removeMissingTracks();
bool removeArtwork(const QString &table, const QVariantMap &item);
bool removeArtist(const QString &artist);
bool cleanArtists();
bool removeAlbum(const QString &album, const QString &artist);
......
......@@ -9,10 +9,7 @@ AlbumsModel::AlbumsModel(QObject *parent) : BaseList(parent)
connect(this, &AlbumsModel::queryChanged, this, &AlbumsModel::setList);
}
AlbumsModel::~AlbumsModel()
{
}
AlbumsModel::~AlbumsModel() {}
FMH::MODEL_LIST AlbumsModel::items() const
{
......@@ -25,8 +22,6 @@ void AlbumsModel::setQuery(const QUERY &query)
return;
this->query = query;
qDebug()<< "setting query"<< this->query;
emit this->queryChanged();
}
......@@ -42,9 +37,9 @@ void AlbumsModel::setSortBy(const SORTBY &sort)
this->sort = sort;
this->preListChanged();
emit this->preListChanged();
this->sortList();
this->postListChanged();
emit this->postListChanged();
emit this->sortByChanged();
}
......@@ -57,10 +52,9 @@ void AlbumsModel::sortList()
{
const auto key = static_cast<FMH::MODEL_KEY>(this->sort);
qDebug()<< "SORTING LIST BY"<< this->sort;
qSort(this->list.begin(), this->list.end(), [key](const FMH::MODEL &e1, const FMH::MODEL &e2) -> bool
std::sort(this->list.begin(), this->list.end(), [key](const FMH::MODEL &e1, const FMH::MODEL &e2) -> bool
{
auto role = key;
const auto role = key;
switch(role)
{
case FMH::MODEL_KEY::RELEASEDATE:
......@@ -72,14 +66,11 @@ void AlbumsModel::sortList()
case FMH::MODEL_KEY::ADDDATE:
{
auto currentTime = QDateTime::currentDateTime();
const auto date1 = QDateTime::fromString(e1[role], Qt::TextDate);
const auto date2 = QDateTime::fromString(e2[role], Qt::TextDate);
auto date1 = QDateTime::fromString(e1[role], Qt::TextDate);
auto date2 = QDateTime::fromString(e2[role], Qt::TextDate);
if(date1.secsTo(currentTime) < date2.secsTo(currentTime))
if(date1.secsTo(QDateTime::currentDateTime()) < date2.secsTo(QDateTime::currentDateTime()))
return true;
break;
}
......@@ -103,6 +94,8 @@ void AlbumsModel::sortList()
});
}
void AlbumsModel::setList()
{
emit this->preListChanged();
......@@ -113,10 +106,14 @@ void AlbumsModel::setList()
else if(this->query == AlbumsModel::QUERY::ARTISTS)
m_Query = "select * from artists order by artist asc";
this->list = this->db->getDBData(m_Query);
qDebug()<< "my LIST" ;
this->list = this->db->getDBData(m_Query, [=](FMH::MODEL &item)
{
if(!FMH::fileExists(item[FMH::MODEL_KEY::ARTWORK]))
{
this->db->removeArtwork(FMH::MODEL_NAME[static_cast<FMH::MODEL_KEY>(this->query)], FM::toMap(item));
item[FMH::MODEL_KEY::ARTWORK] = "";
}
});
this->sortList();
emit this->postListChanged();
}
......@@ -125,7 +122,6 @@ void AlbumsModel::fetchInformation()
{
qDebug() << "RNUNGING BRAIN EFFORRTS";
QFutureWatcher<void> *watcher = new QFutureWatcher<void>;
QObject::connect(watcher, &QFutureWatcher<void>::finished, [=]()
{
watcher->deleteLater();
......@@ -143,8 +139,9 @@ void AlbumsModel::fetchInformation()
if(BAE::artworkCache(album, FMH::MODEL_KEY::ALBUM))
{
qDebug()<< "cache artwork done" << album;
db->insertArtwork(album);
this->updateArtwork(index, album[FMH::MODEL_KEY::ARTWORK]);
emit this->updateModel(index, {FMH::MODEL_KEY::ARTWORK});
continue;
}
......
......@@ -24,7 +24,8 @@ public:
enum QUERY : uint_fast8_t
{
ARTISTS, ALBUMS
ARTISTS = FMH::MODEL_KEY::ARTIST,
ALBUMS = FMH::MODEL_KEY::ALBUM
};
Q_ENUM(QUERY)
......
......@@ -108,10 +108,7 @@ QString vvave::moodColor(const int &index)
void vvave::scanDir(const QStringList &paths)
{
this->checkCollection(paths, [=](uint size)
{
emit this->refreshTables(size);
});
this->checkCollection(paths, [=](uint size) {emit this->refreshTables(size);});
}
QStringList vvave::getSourceFolders()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment