]> Some of my projects - localmylist.git/commitdiff
A way of dynamically preparing statements only when needed + test use
authorAPTX <marek321@gmail.com>
Sat, 14 Jul 2012 01:10:22 +0000 (03:10 +0200)
committerAPTX <marek321@gmail.com>
Sat, 14 Jul 2012 01:10:22 +0000 (03:10 +0200)
localmylist/database.cpp
localmylist/database.h
localmylist/mylistnode.cpp

index 525763333eb2ce5a1e2bb65eac298465f322d048..d095f707e57280c9ab068379af0a799082eb2397 100644 (file)
@@ -127,6 +127,8 @@ struct DatabaseInternal
 {
        QSqlDatabase db;
 
+       QHash<const char *const, QSqlQuery> preparedQueries;
+
        QSqlQuery getHostInfoQuery;
        QSqlQuery isKnownFileQuery;
 
@@ -1318,6 +1320,33 @@ OpenFileData Database::readOpenFileData(QSqlQuery &q)
        return data;
 }
 
+
+QSqlQuery &Database::prepare(const char *const sql)
+{
+       auto it = d->preparedQueries.find(sql);
+
+       if (it != d->preparedQueries.end())
+               return it.value();
+
+
+       QSqlQuery query(d->db);
+
+       if (!query.prepare(sql))
+       {
+               qDebug() << "SQL error: " << query.lastError().type();
+               qDebug() << "Message:   " << query.lastError().text();
+               qDebug() << "DB Message:" << query.lastError().databaseText();
+               qDebug() << "Query:     " << query.executedQuery();
+
+               if (query.lastError().type() == QSqlError::ConnectionError)
+                       disconnect();
+       }
+
+       auto ite = d->preparedQueries.insert(sql, query);
+       return ite.value();
+}
+
+
 bool Database::exec(QSqlQuery &query)
 {
        Q_ASSERT_X(d->thread == QThread::currentThread(), "threads", "DB used from different thread");
index d56bca30fafe98947c35b949a49391a718535ae9..779e661e26d8153f34e21268cd0efc6254959e9d 100644 (file)
@@ -292,6 +292,7 @@ public:
 
        QSqlDatabase connection() const;
 
+       QSqlQuery &prepare(const char *const sql);
        bool exec(QSqlQuery &query);
        bool exec(const QString &sql);
 
index 1b36156cfffb8a9c0701258129fc73d90a040048..9f546f67c0050608cf3803fdf362c77f18c3f876 100644 (file)
@@ -96,11 +96,14 @@ bool MyListNode::canFetchMore() const
 int MyListNode::fetchMore()
 {
        qDebug() << "fetching some more for root";
-       QSqlQuery q(LocalMyList::instance()->database()->connection());
-
-       if (!q.exec("SELECT aid, title_romaji AS title, (SELECT COUNT(e.eid) FROM episode e WHERE e.aid = a.aid), rating, my_vote, (SELECT COUNT(DISTINCT f.eid) FROM episode e  JOIN file f ON (f.eid = e.eid)  WHERE e.aid = a.aid  AND f.my_watched IS NOT NULL) FROM anime a ORDER BY title ASC "
-                               "LIMIT " + QString::number(LIMIT) + " "
-                               "OFFSET " + QString::number(childCount())))
+       QSqlQuery &q = LocalMyList::instance()->database()->prepare(
+                       "SELECT aid, title_romaji AS title, (SELECT COUNT(e.eid) FROM episode e WHERE e.aid = a.aid), rating, my_vote, (SELECT COUNT(DISTINCT f.eid) FROM episode e  JOIN file f ON (f.eid = e.eid)  WHERE e.aid = a.aid  AND f.my_watched IS NOT NULL) FROM anime a ORDER BY title ASC "
+                       "LIMIT :limit "
+                       "OFFSET :offset ");
+       q.bindValue(":limit", LIMIT);
+       q.bindValue(":offset", childCount());
+
+       if (!LocalMyList::instance()->database()->exec(q))
                return 0;
 
        while (q.next())