-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdbhandler.cpp
More file actions
273 lines (247 loc) · 7.69 KB
/
dbhandler.cpp
File metadata and controls
273 lines (247 loc) · 7.69 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include <dbhandler.h>
#include <QSqlQuery>
#include <QSqlError>
#include <QSqlRecord>
#include <QDebug>
#include "dialog.h"
DbHandler::DbHandler()
{
QString path = "etaNet.db";
m_db = QSqlDatabase::addDatabase("QSQLITE");
m_db.setDatabaseName(path);
m_db.open();
if(!m_db.isOpen())
{
qDebug() << "Error: connection with database fail";
}
else
{
qDebug() << "Database: connection ok";
}
}
DbHandler::~DbHandler()
{
if (m_db.isOpen())
{
m_db.close();
}
}
bool DbHandler::isOpen() const
{
return this->m_db.isOpen();
}
bool DbHandler::createControllTable(QString message)
{
bool success = false;
const QString VALUES = " VALUES ";
const QString INSERT = "INSERT INTO ";
QSqlQuery query;
QStringList messageList2 = message.split(" (");
initControllMatrix();
QString cmatrix = INSERT + "'ControlMatrix' ('Object Name', 'Activity (on / off)', 'Prioritization', 'El. Power Limitation', "
"'Th. Power Limitation', 'Release Time', 'Operating Mode')" +
VALUES + "(" + messageList2[0] + ", '0', '0', '0', '0', '0', '0')" + ";";
success = query.prepare(cmatrix);
if (success)
{
if(!query.exec())
{
qDebug() << "Couldn't create the table: one might already exist.";
success = false;
}
else
{
qDebug() << "Table is created!";
}
}
else
{
qDebug() << "Couldn't prepare the query to create table: identical column names are not allowed for uniqueness!";
}
return success;
}
bool DbHandler::createMonitoringTable(QString message)
{
bool success = false;
const QString CREATETABLE = "CREATE TABLE IF NOT EXISTS ";
const QString VALUES = " VALUES ";
QSqlQuery query;
QStringList messageList = message.split(VALUES);
QString header = CREATETABLE + messageList[0] + ";";
success = query.prepare(header);
if (success)
{
if(!query.exec())
{
qDebug() << "Couldn't create the table: one might already exist.";
success = false;
}
else
{
qDebug() << "Table is created (Monitoring)!";
}
}
else
{
qDebug() << "Couldn't prepare the query to create table: identical column names are not allowed for uniqueness!";
}
return success;
}
bool DbHandler::insertMonitoringTuble(QString& message)
{
bool success = false;
QSqlQuery query;
if (!message.isEmpty() && !query.isActive())
{
const QString INSERT = "INSERT INTO ";
query.exec("SAVEPOINT SavepointQt1;");
if(query.exec(INSERT + message + ";"))
{
success = true;
}
else
{
qDebug() << "add tubel failed: " << query.lastError();
}
query.exec("RELEASE SavepointQt1;");
}
else
{
qDebug() << "add tubel failed: message cannot be empty";
}
return success;
}
bool DbHandler::updateLastMonitoringTuble(QString& message)
{
//DELETE FROM NameOfClient WHERE rowid = (SELECT MAX(rowid) FROM NameOfClient);
//INSERT INTO NameOfClient ( 'DBHeader 1', 'DBHeader 2', 'DBHeader 3') VALUES (125, 369, 1233)
bool success = false;
const QString header = "'";
QStringList messageList = message.split(header);
qDebug() << "splitted header: " << messageList[1];
QSqlQuery query;
int value;
if (!message.isEmpty() && !query.isActive())
{
if(query.exec("SELECT MAX(rowid) FROM '" + messageList[1] + "';"))
{
query.next();
value = query.value(0).toInt();
qDebug() << "max rowid: " << value;
if (value > 0)
{
const QString DELETE = "DELETE FROM '";
query.exec("SAVEPOINT SavepointQt2;");
if(query.exec(DELETE + messageList[1] + "' WHERE rowid = (SELECT MAX(rowid) FROM '" + messageList[1] + "');"))
{
success = true;
}
else
{
qDebug() << "add tubel failed: " << query.lastError();
success = false;
}
}
const QString INSERT = "INSERT INTO ";
if(query.exec(INSERT + message + ";"))
{
success = true;
}
else
{
qDebug() << "add tubel failed: " << query.lastError();
success = false;
}
query.exec("RELEASE SavepointQt2;");
}
else
{
qDebug() << "add tubel failed: " << query.lastError();
success = false;
}
}
else
{
qDebug() << "add tubel failed: message cannot be empty";
}
return success;
}
bool DbHandler::log(){
/*
BEGIN IMMEDIATE; -- wrap initial work in a single transaction
-- Example Log Table
CREATE TABLE IF NOT EXISTS Log ('Time', 'Text');
-- Example Trigger to autogenerate datetime
DROP TRIGGER IF EXISTS insert_Timer;
CREATE TRIGGER IF NOT EXISTS insert_Timer AFTER INSERT ON Log
BEGIN
UPDATE Log SET Time = strftime('%Y-%m-%dT%H:%M:%fZ','now') WHERE rowid = new.rowid;
-- Note: using full ISO8601 timestamp format
END;
INSERT INTO Log (Text) VALUES ('Data Logging Started');
SELECT Count(*) FROM Log; -- count of log entries
COMMIT;
*/
bool success = false;
QSqlQuery query;
if(query.exec("BEGIN IMMEDIATE;"
"CREATE TABLE IF NOT EXISTS Log ('Time', 'Text');"
"DROP TRIGGER IF EXISTS insert_Timer;"
"CREATE TRIGGER IF NOT EXISTS insert_Timer AFTER INSERT ON Log BEGIN UPDATE Log SET Time = strftime('%Y-%m-%dT%H:%M:%fZ','now') WHERE rowid = new.rowid;"
"END;"
"INSERT INTO Log (Text) VALUES ('Server was started or used');"
"SELECT Count(*) FROM Log;"
"COMMIT;"))
{
success = true;
}
else
{
qDebug() << "Cration Log-Table failed: " << query.lastError();
}
return success;
}
bool DbHandler::initControllMatrix(void)
{
bool success = false;
QSqlQuery query;
const QString CREATETABLE = "CREATE TABLE IF NOT EXISTS ";
QString header = CREATETABLE + "'ControlMatrix' ('Object Name' PRIMARY KEY, 'Activity (on / off)', "
"'Prioritization', 'El. Power Limitation', 'Th. Power Limitation', "
"'Release Time', 'Operating Mode')" + ";";
success = query.prepare(header);
if (success)
{
if(!query.exec())
{
qDebug() << "Couldn't create the table: one might already exist.";
success = false;
}
else
{
qDebug() << "Table is created (Init)!";
}
}
else
{
qDebug() << "Couldn't prepare the query to create table: identical column names are not allowed for uniqueness!";
}
return success;
}
QString DbHandler::selectControllValues(QString& name)
{
QString value = " ; ; ; ; ; ";
QSqlQuery query;
if(query.exec("SELECT * FROM 'ControlMatrix' WHERE 'ControlMatrix'.'Object Name' = " + name + ";"))
{
query.next();
value = query.value(1).toString()+ "; ";
value = value + query.value(2).toString()+ "; ";
value = value + query.value(3).toString()+ "; ";
value = value + query.value(4).toString()+ "; ";
value = value + query.value(5).toString()+ "; ";
value = value + query.value(6).toString();
}
qDebug() << value;
return value;
}