From: APTX Date: Tue, 24 Jul 2012 00:02:17 +0000 (+0200) Subject: Move MyListModel queries to the new prepare(). Split fetchMore() into 2 parts, so... X-Git-Url: https://gitweb.tyo.aptx.org/?a=commitdiff_plain;h=53dc75d1f0da39a47f66286118b331e96e8e7647;p=localmylist.git Move MyListModel queries to the new prepare(). Split fetchMore() into 2 parts, so that it works well with beginInsertRows() --- diff --git a/localmylist/mylistmodel.cpp b/localmylist/mylistmodel.cpp index 3ab101f..330e8d7 100644 --- a/localmylist/mylistmodel.cpp +++ b/localmylist/mylistmodel.cpp @@ -118,10 +118,13 @@ void MyListModel::fetchMore(const QModelIndex &parent) else node = rootItem; - int nextFetchSize = node->nextFetchSize(); - beginInsertRows(parent, rowCount(parent), rowCount(parent) + nextFetchSize - 1); int newrows = node->fetchMore(); - Q_ASSERT(newrows == nextFetchSize); + + if (!newrows) + return; + + beginInsertRows(parent, rowCount(parent), rowCount(parent) + newrows - 1); + node->addFetched(); endInsertRows(); qDebug() << "added" << newrows << "new rows"; diff --git a/localmylist/mylistnode.cpp b/localmylist/mylistnode.cpp index 8160f95..86c5ffe 100644 --- a/localmylist/mylistnode.cpp +++ b/localmylist/mylistnode.cpp @@ -85,12 +85,6 @@ int MyListNode::totalRowCount() const return m_totalRowCount; } -int MyListNode::nextFetchSize() const -{ - int limit = LIMIT; - return qMin(limit, totalRowCount() - childCount()); -} - bool MyListNode::canFetchMore() const { if (m_type != FileLocation && childCount() < totalRowCount()) @@ -130,10 +124,18 @@ int MyListNode::fetchMore() << (q.value(3).toDouble() < 1 ? "n/a" : QString::number(q.value(3).toDouble(), 'f', 2)) << (q.value(4).toDouble() < 1 ? "n/a" : QString::number(q.value(4).toDouble(), 'f', 2)) << QObject::tr("%1 of %2").arg(q.value(5).toInt()).arg(q.value(2).toInt()); - childItems << new MyListAnimeNode(id, data, this); + newItems << new MyListAnimeNode(id, data, this); } - qDebug() << "root" << q.size(); - return q.size(); + + q.finish(); + + return newItems.count(); +} + +void MyListNode::addFetched() +{ + while (newItems.count()) + childItems << newItems.takeFirst(); } bool MyListNode::hasChildren() const @@ -209,10 +211,12 @@ int MyListAnimeNode::fetchMore() << (q.value(3).toDouble() < 1 ? "n/a" : QString::number(q.value(3).toDouble(), 'f', 2)) << (q.value(4).toDouble() < 1 ? "n/a" : QString::number(q.value(4).toDouble(), 'f', 2)) << (q.value(5).toDateTime().isValid() ? QObject::tr("Yes, on %1").arg(q.value(5).toDateTime().toString()) : QObject::tr("No")); - childItems << new MyListEpisodeNode(id, data, this); + newItems << new MyListEpisodeNode(id, data, this); } - return q.size(); + q.finish(); + + return newItems.count(); } // ---- @@ -240,16 +244,18 @@ QString MyListEpisodeNode::totalRowCountSql() const int MyListEpisodeNode::fetchMore() { qDebug() << "fetching some more for eid" << m_id; - QSqlQuery q(LocalMyList::instance()->database()->connection()); + QSqlQuery &q = LocalMyList::instance()->database()->prepare( + "SELECT fid, group_name, version, quality, my_watched " + " FROM file " + " WHERE eid = :eida " + "UNION " + "SELECT f.fid, f.group_name, f.version, f.quality, f.my_watched FROM file f " + " JOIN file_episode_rel fer ON (fer.fid = f.fid) " + " WHERE fer.eid = :eidb "); + q.bindValue(":eida", m_id); + q.bindValue(":eidb", m_id); - if (!q.exec(QString( - "SELECT fid, group_name, version, quality, my_watched " - " FROM file " - " WHERE eid = %1 " - "UNION " - "SELECT f.fid, f.group_name, f.version, f.quality, f.my_watched FROM file f " - " JOIN file_episode_rel fer ON (fer.fid = f.fid) " - " WHERE fer.eid = %1 ").arg(m_id))) + if (!LocalMyList::instance()->database()->exec(q)) return 0; while (q.next()) @@ -258,10 +264,12 @@ int MyListEpisodeNode::fetchMore() QVariantList data; data << q.value(1) << "v" + q.value(2).toString() << q.value(3) << "" << (q.value(4).toDateTime().isValid() ? QObject::tr("Yes, on %1").arg(q.value(4).toDateTime().toString()) : QObject::tr("No")); - childItems << new MyListFileNode(id, data, this); + newItems << new MyListFileNode(id, data, this); } - return q.size(); + q.finish(); + + return newItems.count(); } // --------------- @@ -274,11 +282,13 @@ MyListFileNode::MyListFileNode(int id, const QList &data, MyListNode * int MyListFileNode::fetchMore() { qDebug() << "fetching some more for fid" << m_id; - QSqlQuery q(LocalMyList::instance()->database()->connection()); + QSqlQuery &q = LocalMyList::instance()->database()->prepare( + "SELECT fl.fid, fl.host_id, h.name, fl.path, fl.renamed, fl.failed_rename FROM file_location fl " + " JOIN host h ON (fl.host_id = h.host_id) " + "WHERE fl.fid = :fid"); + q.bindValue(":fid", m_id); - if (!q.exec("SELECT fl.fid, fl.host_id, h.name, fl.path, fl.renamed, fl.failed_rename FROM file_location fl " - " JOIN host h ON (fl.host_id = h.host_id) " - "WHERE fl.fid = " + QString::number(m_id))) + if (!LocalMyList::instance()->database()->exec(q)) return 0; while (q.next()) @@ -290,10 +300,12 @@ int MyListFileNode::fetchMore() << "" << "" << (q.value(5).toBool() ? QObject::tr("Rename Failed") : q.value(4).toDateTime().isValid() ? QObject::tr("Yes, on %1").arg(q.value(4).toDateTime().toString()) : QObject::tr("Not Renamed")); - childItems << new MyListFileLocationNode(id, data, this); + newItems << new MyListFileLocationNode(id, data, this); } - return q.size(); + q.finish(); + + return newItems.count(); } QString MyListFileNode::totalRowCountSql() const diff --git a/localmylist/mylistnode.h b/localmylist/mylistnode.h index 32483f4..7d584a0 100644 --- a/localmylist/mylistnode.h +++ b/localmylist/mylistnode.h @@ -32,6 +32,7 @@ public: bool canFetchMore() const; virtual int fetchMore(); + void addFetched(); bool hasChildren() const; NodeType type() const; @@ -47,6 +48,7 @@ protected: int fetchedRowCount; QList childItems; + QList newItems; QList itemData; MyListNode *parentItem;