forked from Securepoint/openvpn-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.cpp
147 lines (122 loc) · 4.34 KB
/
database.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
#include "database.h"
#include "debug.h"
#include "appfunc.h"
#include "settings.h"
#include "message.h"
Database::Database()
: databasePath (AppFunc::getAppSavePath())
{
// Make QSqlDatabase object
if (!QSqlDatabase::database(QLatin1String("myConnection")).isValid()) {
this->db = QSqlDatabase::addDatabase("QSQLITE", QLatin1String("myConnection"));
} else {
this->db = QSqlDatabase::database(QLatin1String("myConnection"));
}
if (Settings::getInstance()->getIsPortableClient()) {
this->databasePath = qApp->applicationDirPath();
}
QString databaseFullPath (this->databasePath + QLatin1String("/vpn.sqlite"));
Debug::log(QLatin1String("Database path: ") + databaseFullPath);
bool createTables(false);
if (!QFile::exists(databaseFullPath)) {
// No Database was found
//Message::error(QLatin1String("No database found. Create new database."));
//
// Build new database
createTables = true;
}
// Set database path
this->db.setDatabaseName(databaseFullPath);
// Init database
this->db.setConnectOptions("QSQLITE_BUSY_TIMEOUT=50");
// Open Databse
if (!this->open()) {
Debug::error(QLatin1String("Can't open database!"));
//
QCoreApplication::exit(1);
}
this->execute("pragma key = '" + Settings::getInstance()->getCryptKey() + "';");
Debug::log(QLatin1String("Databse: is open"));
// Check if it is necessary to create the tables
if (createTables) {
Debug::log(QLatin1String("Create tables"));
QLatin1String ssql ("CREATE TABLE \"vpn\" (\"vpn-id\" INTEGER PRIMARY KEY NOT NULL, \"vpn-name\" VARCHAR, \"vpn-config\" VARCHAR, \"vpn-autostart\" INTEGER, \"vpn-user\" VARCHAR, \"vpn-password\" VARCHAR, \"vpn-pkcs12\" VARCHAR, \"vpn-http-user\" VARCHAR, \"vpn-http-password\" VARCHAR)");
if (!this->execute(ssql)) {
Debug::error(QLatin1String("Can't create tables!"));
//
QCoreApplication::exit(1);
}
}
}
Database::~Database()
{
this->close();
}
bool Database::open()
{
if (!this->db.open()) {
Debug::error(QLatin1String("Database: Can't open Database"));
return false;
}
return true;
}
void Database::close()
{
if (this->db.isOpen()) {
this->db.close();
}
this->db.removeDatabase(QLatin1String("myConnection"));
}
bool Database::execute(const QString &sql)
{
bool retVal (true);
QSqlQuery *query = new QSqlQuery (QSqlDatabase::database(QLatin1String("myConnection")));
query->exec(sql);
if (query->lastError().type() != QSqlError::NoError) {
Debug::error(QLatin1String("Database: SQL: ") + query->lastError().text());
Debug::log(QLatin1String("SQL for last error: ") + sql);
}
query->clear();
delete query;
query = 0;
return retVal;
}
QSqlQuery* Database::openQuery(const QString &sql)
{
//
// Open a new query and retur the recordset
//
// Build new query
// The ownership changes to the caller
QSqlQuery *query = new QSqlQuery (QSqlDatabase::database(QLatin1String("myConnection")));
// Exec query
query->exec(sql);
// Error occurred?
if (query->lastError().type() != QSqlError::NoError) {
Debug::error(QLatin1String("Database: SQL: ") + query->lastError().text());
Debug::log(QLatin1String("SQL for last error: ") + sql);
}
return query;
}
QString Database::makeCleanValue(const QString &value)
{
//
// Removes the most sql injections commands
//
return value;
}
QString Database::join(const QSqlRecord &record, const QString &separator, bool setNewline)
{
//
// Joins the record with the given separator
//
QStringList recordData; for (int fieldsCount = 0; fieldsCount < record.count(); fieldsCount++) {
// Get valure from field
QString fieldValue (record.value(fieldsCount).toByteArray().constData());
// Mark invalid separator string
fieldValue = fieldValue.replace(separator, QLatin1String("#INV-SEP#"));
// Push value to value list
recordData << fieldValue;
}
return recordData.join(separator) + (setNewline ? QLatin1String("\n") : QLatin1String(""));
}