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);
119 QString cacheSize = Settings::instance()->get("cache_size");
123 // Delete old entries from cache
124 if((cacheLimit = cacheSize.toInt()) > 0)
126 if(query.exec("SELECT COUNT(*) FROM cache") && query.next())
128 int itemsToDelete = query.value(0).toInt() - cacheLimit;
130 for(int i = 0; i < itemsToDelete; i++)
134 if(!query.exec("DELETE FROM cache WHERE id = (SELECT MIN(id) FROM cache)"))
136 QSqlError error = query.lastError();
137 qDebug() << "Unable to delete old cache entries: " << error.text();
144 QSqlError error = query.lastError();
145 qDebug() << "Unable to get count for cache entries: " << error.text();
160 bool Cache::logItem(Source::Result const& result, bool missed, unsigned int time)
162 bool connected = DB::connected();
173 query.prepare("INSERT INTO log(number, name, street, city, country, time, missed) VALUES(:number, :name, :street, :city, :country, :time, :missed)");
174 query.bindValue(":number", result.number);
175 query.bindValue(":name", result.name);
176 query.bindValue(":street", result.street);
177 query.bindValue(":city", result.city);
178 query.bindValue(":country", result.country);
179 query.bindValue(":time", time);
180 int misVal = missed ? 1 : 0;
182 query.bindValue(":missed", misVal);
191 // Delete old entries from cache
194 if(query.exec("SELECT COUNT(*) FROM log") && query.next())
196 int itemsToDelete = query.value(0).toInt() - LOG_MAX_SIZE;
198 for(int i = 0; i < itemsToDelete; i++)
202 if(!query.exec("DELETE FROM cache WHERE id = (SELECT MIN(id) FROM cache)"))
204 QSqlError error = query.lastError();
205 qDebug() << "Unable to delete old cache entries: " << error.text();
212 QSqlError error = query.lastError();
213 qDebug() << "Unable to get count for cache entries: " << error.text();
228 void Cache::getLogItems(QList<Cache::LogDetails>& items, int limit)
230 bool connected = DB::connected();
237 QSqlQuery query("SELECT number, name, street, city, country, time, missed FROM log ORDER BY time DESC LIMIT " + QString::number(limit));
242 details.result.number = query.value(0).toString();
243 details.result.name = query.value(1).toString();
244 details.result.street = query.value(2).toString();
245 details.result.city = query.value(3).toString();
246 details.result.country = query.value(4).toString();
247 details.time = query.value(5).toInt();
249 int missed = query.value(6).toInt();
251 details.missed = missed ? true : false;
253 items.push_back(details);
263 int Cache::clearLog()
265 bool connected = DB::connected();
275 if(query.exec("DELETE FROM log"))
277 ret = query.numRowsAffected();
288 Cache& Cache::instance()
292 instance_ = new Cache;