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();
58 bool Cache::findItem(QString const& number, Source::Result& result)
60 bool connected = DB::connected();
67 QSqlQuery query("SELECT name, street, city FROM cache WHERE number LIKE '%" + number.right(7) + "'");
69 //query.prepare("SELECT name, street, city FROM cache WHERE number = :number");
70 //query.bindValue(":number", number);
76 result.number = number;
77 result.name = query.value(0).toString();
78 result.street = query.value(1).toString();
79 result.city = query.value(2).toString();
92 bool Cache::addItem(Source::Result const& result)
94 bool connected = DB::connected();
105 query.prepare("INSERT INTO cache(number, name, street, city) VALUES(:number, :name, :street, :city)");
106 query.bindValue(":number", result.number);
107 query.bindValue(":name", result.name);
108 query.bindValue(":street", result.street);
109 query.bindValue(":city", result.city);
118 QString cacheSize = Settings::instance()->get("cache_size");
122 // Delete old entries from cache
123 if((cacheLimit = cacheSize.toInt()) > 0)
125 if(query.exec("SELECT COUNT(*) FROM cache") && query.next())
127 int itemsToDelete = query.value(0).toInt() - cacheLimit;
129 for(int i = 0; i < itemsToDelete; i++)
133 if(!query.exec("DELETE FROM cache WHERE id = (SELECT MIN(id) FROM cache)"))
135 QSqlError error = query.lastError();
136 qDebug() << "Unable to delete old cache entries: " << error.text();
143 QSqlError error = query.lastError();
144 qDebug() << "Unable to get count for cache entries: " << error.text();
159 bool Cache::logItem(Source::Result const& result, bool missed, unsigned int time)
161 bool connected = DB::connected();
172 query.prepare("INSERT INTO log(number, name, street, city, country, time, missed) VALUES(:number, :name, :street, :city, :country, :time, :missed)");
173 query.bindValue(":number", result.number);
174 query.bindValue(":name", result.name);
175 query.bindValue(":street", result.street);
176 query.bindValue(":city", result.city);
177 query.bindValue(":country", result.country);
178 query.bindValue(":time", time);
179 int misVal = missed ? 1 : 0;
181 query.bindValue(":missed", misVal);
190 // Delete old entries from cache
193 if(query.exec("SELECT COUNT(*) FROM log") && query.next())
195 int itemsToDelete = query.value(0).toInt() - LOG_MAX_SIZE;
197 for(int i = 0; i < itemsToDelete; i++)
201 if(!query.exec("DELETE FROM cache WHERE id = (SELECT MIN(id) FROM cache)"))
203 QSqlError error = query.lastError();
204 qDebug() << "Unable to delete old cache entries: " << error.text();
211 QSqlError error = query.lastError();
212 qDebug() << "Unable to get count for cache entries: " << error.text();
227 void Cache::getLogItems(QList<Cache::LogDetails>& items, int limit)
229 bool connected = DB::connected();
236 QSqlQuery query("SELECT number, name, street, city, country, time, missed FROM log ORDER BY time DESC LIMIT " + QString::number(limit));
241 details.result.number = query.value(0).toString();
242 details.result.name = query.value(1).toString();
243 details.result.street = query.value(2).toString();
244 details.result.city = query.value(3).toString();
245 details.result.country = query.value(4).toString();
246 details.time = query.value(5).toInt();
248 int missed = query.value(6).toInt();
250 details.missed = missed ? true : false;
252 items.push_back(details);
262 int Cache::clearLog()
264 bool connected = DB::connected();
274 if(query.exec("DELETE FROM log"))
276 ret = query.numRowsAffected();
287 Cache& Cache::instance()
291 instance_ = new Cache;