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 <QtCore/QDir>
20 #include <QtCore/QDebug>
21 #include <QtCore/QVariant>
22 #include <QtSql/QSqlQuery>
23 #include <QtSql/QSqlError>
28 const QString SQL_DRIVER = "QSQLITE";
29 const QString SQL_DATABASE = ".jenirok.db";
30 const int DB_VERSION = 2;
33 bool DB::initialized_ = false;
40 db_ = QSqlDatabase::addDatabase(SQL_DRIVER);
41 QString path = QDir::home().path() + QDir::separator() + SQL_DATABASE;
42 db_.setDatabaseName(QDir::toNativeSeparators(path));
45 bool ret = db_.open();
49 QSqlError error = db_.lastError();
50 qDebug() << error.text();
53 if(ret && !initialized_)
55 QSqlQuery query("SELECT value FROM settings WHERE name = 'db_version'");
59 int currentVersion = query.value(0).toString().toInt();
61 if(currentVersion < DB_VERSION)
63 ret = createTables(true);
68 ret = createTables(false);
85 void DB::removeDatabase()
94 QSqlDatabase::removeDatabase(QSqlDatabase::defaultConnection);
103 QSqlDatabase& DB::instance()
108 bool DB::createTables(bool update)
114 ret = ret && query.exec("CREATE TABLE IF NOT EXISTS cache (id INTEGER PRIMARY KEY, number VARCHAR(32) NOT NULL UNIQUE, name VARCHAR(255) NOT NULL, street VARCHAR(255) NOT NULL, city VARCHAR(255) NOT NULL)");
115 ret = ret && query.exec("CREATE TABLE IF NOT EXISTS settings (name VARCHAR(255) NOT NULL PRIMARY KEY, value VARCHAR(255) NOT NULL)");
116 ret = ret && query.exec("CREATE TABLE IF NOT EXISTS log (id INTEGER PRIMARY KEY, number VARCHAR(32) NOT NULL, name VARCHAR(255), street VARCHAR(255), city VARCHAR(255), country VARCHAR(255), time INTEGER NOT NULL, missed INTEGER NOT NULL)");
120 ret = ret && query.exec("UPDATE settings SET value = '" + QString::number(DB_VERSION) + "' WHERE name = 'db_version'");
124 ret = ret && query.exec("INSERT INTO settings(name, value) VALUES('db_version', '" + QString::number(DB_VERSION) + "')");