2 * This file is part of Jenirok.
4 * Jenirok is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Jenirok is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Jenirok. If not, see <http://www.gnu.org/licenses/>.
19 #include <QtSql/QSqlQuery>
20 #include <QtSql/QSqlError>
21 #include <QtCore/QVariant>
22 #include <QtCore/QDebug>
27 Cache* Cache::instance_ = 0;
35 bool connected = DB::connected();
45 if(query.exec("DELETE FROM cache"))
47 ret = query.numRowsAffected();
57 bool Cache::findItem(QString const& number, Source::Result& result)
59 bool connected = DB::connected();
67 query.prepare("SELECT name, street, city FROM cache WHERE number = :number");
68 query.bindValue(":number", number);
72 if(query.exec() && query.next())
74 result.number = number;
75 result.name = query.value(0).toString();
76 result.street = query.value(1).toString();
77 result.city = query.value(2).toString();
90 bool Cache::addItem(Source::Result const& result)
92 bool connected = DB::connected();
103 query.prepare("INSERT INTO cache(number, name, street, city) VALUES(:number, :name, :street, :city)");
104 query.bindValue(":number", result.number);
105 query.bindValue(":name", result.name);
106 query.bindValue(":street", result.street);
107 query.bindValue(":city", result.city);
117 QString cacheSize = Settings::instance()->get("cache_size");
121 // Delete old entries from cache
122 if((cacheLimit = cacheSize.toInt()) > 0)
124 if(query.exec("SELECT COUNT(*) FROM cache") && query.next())
126 int itemsToDelete = query.value(0).toInt() - cacheLimit;
128 for(int i = 0; i < itemsToDelete; i++)
132 if(!query.exec("DELETE FROM cache WHERE id = (SELECT MIN(id) FROM cache)"))
134 QSqlError error = query.lastError();
135 qDebug() << "Unable to delete old cache entries: " << error.text();
142 QSqlError error = query.lastError();
143 qDebug() << "Unable to get count for cache entries: " << error.text();
158 Cache& Cache::instance()
162 instance_ = new Cache;