-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlhelper.cpp
152 lines (140 loc) · 4.7 KB
/
sqlhelper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/****************************************************************************
**
** FilePile - Remote SSH file manager
** Copyright (C) 2014 Andrej Sinicyn
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
**
****************************************************************************/
#include "sqlhelper.h"
/**
* @brief Default constructor
*
* @param parent
*/
SQLHelper::SQLHelper(QObject *parent) :
QObject(parent)
{
}
/**
* @brief Gets the current version of the database. Creates the version table if it doesn't exist yet.
*
* @param db Database that should be checked.
* @param dbMajor Reference to integer variable to receive major version part.
* @param dbMinor Reference to integer variable to receive minor version part.
* @return bool Indicates if version info could be retrieved successfully.
*/
bool SQLHelper::getDbVersion(QSqlDatabase db, int &dbMajor, int &dbMinor)
{
if (db.isValid() && db.isOpen())
{
QSqlQuery query("CREATE TABLE IF NOT EXISTS \"_dbversion\" (\"major\" INT, \"minor\" INT) WITHOUT ROWID;"
"SELECT \"major\", \"minor\" FROM \"_dbversion\";", db);
if (query.isActive() && query.first())
{
dbMajor = query.value(0).Int;
dbMinor = query.value(1).Int;
return true;
}
}
return false;
}
/**
* @brief Updates the database.
*
* @param sqlString SQL string used to update the database.
* @param db Database that should be updated.
* @param dbMajor Major part of database update version.
* @param dbMinor Minor part of database update version.
* @return bool Indicates if database update ran successfully.
*/
bool SQLHelper::updateDb(QString sqlString, QSqlDatabase db, int dbMajor, int dbMinor)
{
if (db.isValid() && db.transaction() && db.isOpen())
{
QSqlQuery query(db);
if (query.prepare("UPDATE \"_dbversion\" SET \"major\"=:major, \"minor\"=:minor"))
{
query.bindValue(":major", dbMajor);
query.bindValue(":minor", dbMinor);
if (query.exec() && query.exec(sqlString) && db.commit())
{
return true;
}
}
db.rollback();
}
return false;
}
/**
* @brief Checks presence of DB and updates it if needed.
*
* @return bool State, if DB is valid and up to date.
*/
bool SQLHelper::ensurePresenceOfCurrentDB()
{
bool result = false;
int dbMajor = 0;
int dbMinor = 0;
QFile sqlFile;
// Getting current app DB version.
QSettings settings(":/sql/dbversion.ini", QSettings::IniFormat);
settings.beginGroup("DBVersion");
int appMajor = settings.value("Major", QVariant(0)).toInt();
int appMinor = settings.value("Minor", QVariant(0)).toInt();
settings.endGroup();
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
if(db.isValid())
{
QString path;
#ifndef Q_OS_ANDROID
path = QStandardPaths::writableLocation(QStandardPaths::DataLocation).append(QDir::separator());
// Ensure that path exists
QDir qDir;
qDir.mkpath(path);
#endif
path.append("settings.db");
db.setDatabaseName(path);
db.setConnectOptions("QSQLITE_BUSY_TIMEOUT");
if (db.open() && getDbVersion(db, dbMajor, dbMinor))
{
if ((dbMajor == 0) && (dbMinor == 0))
{
// Fresh db initialization
while (true)
{
sqlFile.setFileName(QString(":/sql/%1.sql").arg(appMajor));
if (!sqlFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
appMajor--;
if (appMajor < 0)
break;
}
}
if (sqlFile.isOpen())
{
QTextStream in(&sqlFile);
if (updateDb(in.readAll(), db, appMajor, 0))
{
getDbVersion(db, dbMajor, dbMinor);
}
}
}
}
}
if (!result)
{
QSqlDatabase::removeDatabase(db.connectionName());
}
return result;
}