-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsqlinterface.cpp
364 lines (311 loc) · 9.96 KB
/
sqlinterface.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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include "sqlinterface.h"
#include <QSqlQuery>
#include "parameter.h"
#include <QDebug>
#include <QSqlError>
#include <limits>
SQLInterface::SQLInterface()
{
db_ = QSqlDatabase::addDatabase("QSQLITE");
}
SQLInterface::~SQLInterface()
{
if(dbIsSet_ && db_.open())
{
db_.close();
}
}
bool SQLInterface::setDatabase(QString& path)
{
db_.setDatabaseName(path);
dbIsSet_ = true;
return true;
}
bool SQLInterface::getParameterStructure(QVector<TreeData> &structuredata)
{
if(!db_.open())
{
return false;
}
const char *command = "SELECT parent.ID as parentID, child.ID, child.name, child.unit, child.description "
"FROM ParameterStructure as parent, ParameterStructure as child "
"WHERE child.lft > parent.lft "
"AND child.rgt < parent.rgt "
"AND child.dpt = parent.dpt + 1 "
"UNION "
"SELECT 0 as parentID, child.ID, child.Name, child.unit, child.description "
"FROM ParameterStructure as child "
"WHERE child.dpt = 0 "
"ORDER BY child.ID";
QSqlQuery query;
if(!query.prepare(command))
{
qDebug() << query.lastError();
}
if(!query.exec())
{
// emit logError(query.lastError());
qDebug() << query.lastError();
db_.close();
return false;
}
while(query.next())
{
TreeData item;
item.parentID = query.value(0).toInt();
item.ID = query.value(1).toInt();
item.name = query.value(2).toString();
item.unit = query.value(3).toString();
item.description = query.value(4).toString();
structuredata.push_back(item);
}
db_.close();
return true;
}
bool SQLInterface::getParameterValuesMinMax(std::map<uint32_t, parameter_min_max_val_serial_entry>& IDtoParam)
{
if(!db_.open())
{
return false;
}
const char *value_tables[4] =
{
"bool",
"double",
"int",
"ptime",
};
parameter_type types[4] =
{
parametertype_bool,
parametertype_double,
parametertype_uint,
parametertype_ptime,
};
for(int i = 0; i < 4; ++i)
{
char commandbuf[512];
sprintf(commandbuf,
"SELECT "
"ParameterStructure.ID, ParameterStructure.type, ParameterValues_%s.minimum, ParameterValues_%s.maximum, ParameterValues_%s.value "
"FROM ParameterStructure INNER JOIN ParameterValues_%s "
"ON ParameterStructure.ID = ParameterValues_%s.ID; ",
value_tables[i], value_tables[i], value_tables[i], value_tables[i], value_tables[i]);
QSqlQuery query;
query.prepare(commandbuf);
if(!query.exec())
{
// emit logError(query.lastError());
db_.close();
return false;
}
while(query.next())
{
parameter_min_max_val_serial_entry entry;
entry.ID = query.value(0).toInt();
QString typetxt = query.value(1).toString();
entry.type = Parameter::parseType(typetxt);
if(entry.type != types[i])
{
//TODO:
// emit logError("Database: Parameter with ID %d is registered as having type %s, but is in the table for %s.\n")
// entry.ID, typetxt, value_tables[i]);
db_.close();
return false;
}
switch(types[i])
{
case parametertype_bool :
{
entry.min.val_bool = (bool)query.value(2).toInt();
entry.max.val_bool = (bool)query.value(3).toInt();
entry.value.val_bool = (bool)query.value(4).toInt();
} break;
case parametertype_double :
{
entry.min.val_double = query.value(2).toDouble();
entry.max.val_double = query.value(3).toDouble();
entry.value.val_double = query.value(4).toDouble();
} break;
case parametertype_uint :
{
entry.min.val_uint = query.value(2).toULongLong();
entry.max.val_uint = query.value(3).toULongLong();
entry.value.val_uint = query.value(4).toULongLong();
} break;
case parametertype_ptime :
{
entry.min.val_ptime = query.value(2).toLongLong();
entry.max.val_ptime = query.value(3).toLongLong();
entry.value.val_ptime = query.value(4).toLongLong();
} break;
}
IDtoParam[entry.ID] = entry;
}
}
db_.close();
return true;
}
bool SQLInterface::writeParameterValues(QVector<parameter_serial_entry>& writedata)
{
if(!db_.open())
{
return false;
}
//WARNING: Volatile! This depends on the order of the enums in parameter_type not being changed.
const char *sqlcommand[4] =
{
"UPDATE ParameterValues_bool SET value = (:value) WHERE ID = (:id);",
"UPDATE ParameterValues_double SET value =(:value) WHERE ID = (:id);",
"UPDATE ParameterValues_int SET value = (:value) WHERE ID = (:id);",
"UPDATE ParameterValues_ptime SET value = (:value) WHERE ID = (:id);",
};
size_t numparameters = writedata.size();
QSqlDatabase::database().transaction();
for(int i = 0; i < numparameters; ++i)
{
parameter_serial_entry &entry = writedata[i];
QSqlQuery query;
query.prepare(sqlcommand[entry.type]);
query.bindValue(":id", entry.ID);
switch(entry.type)
{
case parametertype_double:
{
query.bindValue(":value", entry.value.val_double);
} break;
case parametertype_bool:
{
query.bindValue(":value", (int)entry.value.val_bool);
} break;
case parametertype_uint:
{
query.bindValue(":value", qulonglong(entry.value.val_uint));
} break;
case parametertype_ptime:
{
query.bindValue(":value", qlonglong( entry.value.val_ptime));
} break;
default:
{
//TODO: invalid type, log error
db_.close();
return false;
} break;
}
if(!query.exec())
{
qDebug() << query.lastError();
// emit logError(query.lastError());
db_.close();
return false;
}
}
QSqlDatabase::database().commit();
db_.close();
return true;
}
bool SQLInterface::getResultOrInputStructure(QVector<TreeData> &structuredata, const char *table)
{
if(!db_.open())
{
db_.close();
}
char sqlcommand[512];
sprintf(sqlcommand,
"SELECT parent.ID AS parentID, child.ID, child.name, child.unit "
"FROM %s AS parent, %s AS child "
"WHERE child.lft > parent.lft "
"AND child.rgt < parent.rgt "
"AND child.dpt = parent.dpt + 1 "
"UNION "
"SELECT 0 as parentID, child.ID, child.name, child.unit "
"FROM %s as child "
"WHERE child.dpt = 0 "
"ORDER BY child.ID",
table, table, table
);
QSqlQuery query;
query.prepare(sqlcommand);
if(!query.exec())
{
// emit logError(query.lastError());
return false;
}
while(query.next())
{
TreeData entry;
entry.parentID = query.value(0).toInt();
entry.ID = query.value(1).toInt();
entry.name = query.value(2).toString();
entry.unit = query.value(3).toString();
structuredata.push_back(entry);
}
db_.close();
return true;
}
bool SQLInterface::getResultOrInputValues(const char *table, const QVector<int>& IDs, QVector<QVector<double>> &seriesout, QVector<int64_t> &startdatesout)
{
if(!db_.open())
{
return false;
}
char sqlcommand[512];
//NOTE: For now we only handle cases where the timestep is one day. Otherwise we would also have to read the timestep from somewhere or read out the entire series of time values.
for(int ID : IDs)
{
sprintf(sqlcommand, "SELECT date, value FROM %s WHERE ID=%d;", table, ID);
QSqlQuery query;
query.prepare(sqlcommand);
if(!query.exec())
{
// emit logError(query.lastError());
db_.close();
return false;
}
QVector<double> series;
series.reserve(100); // We don't know how large it is, but this tends to speed things up.
int64_t startDate;
bool first = true;
while(query.next())
{
if(first)
{
startDate = query.value(0).toLongLong();
first = false;
}
if(query.value(1).isNull())
{
series.push_back(std::numeric_limits<double>::quiet_NaN());
}
else
{
series.push_back(query.value(1).toDouble());
}
}
seriesout.push_back(series);
startdatesout.push_back(startDate);
}
db_.close();
return true;
}
bool SQLInterface::getExenameFromParameterInfo(QString& exename)
{
if(!db_.open())
{
return false;
}
QSqlQuery query;
query.prepare("SELECT Exename FROM Info");
if(!query.exec())
{
// emit logError(query.lastError());
qDebug() << query.lastError();
db_.close();
return false;
}
query.next();
exename = query.value(0).toString();
db_.close();
return true;
}