-
Notifications
You must be signed in to change notification settings - Fork 1
/
dboperate.cpp
358 lines (331 loc) · 11.8 KB
/
dboperate.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
#include "dboperate.h"
#include <QStandardPaths>
#include <QSqlQuery>
#include <QDir>
#include <QStringList>
#include <QString>
#include <QDebug>
#include <QtConcurrent>
#include <QSqlError>
QString DBOperate::DBFileDir = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)+"/SensorCamellia/DB";
DBOperate::DBOperate(QObject *parent):
QThread(parent),m_is_run(false)
{
m_mapDataBaseNameAndDBInfo.clear();
}
void DBOperate::setStartThread(bool run)
{
m_is_run = run;
}
void DBOperate::init_DBs(QList<TableDataOfNodeList> db_list)
{
//获取当前路径下的数据库
_getDBOfDBDir();
for(int i = 0;i < db_list.count();++i)
{
if(m_mapDataBaseNameAndDBInfo.contains(db_list.at(i).serial_number))
{
DB_Info &info = m_mapDataBaseNameAndDBInfo[db_list.at(i).serial_number];
info.addr = db_list.at(i).node_addr;
info.serial_number = db_list.at(i).serial_number;
info.version = db_list.at(i).softversion;
info.range = db_list.at(i).node_range;
QFuture<void> future = QtConcurrent::run(this,&DBOperate::_createTable,info);
while(!future.isFinished())
QCoreApplication::processEvents();
info.db.transaction(); //开启事务
}else
{
DB_Info info(db_list.at(i).serial_number);
info.addr = db_list.at(i).node_addr;
info.serial_number = db_list.at(i).serial_number;
info.version = db_list.at(i).softversion;
info.range = db_list.at(i).node_range;
info.db.setDatabaseName(QString("%1/%2").arg(DBFileDir).arg(info.serial_number));
info.db.open();
QFuture<void> future = QtConcurrent::run(this,&DBOperate::_createTable,info);
while(!future.isFinished())
QCoreApplication::processEvents();
m_mapDataBaseNameAndDBInfo.insert(info.serial_number,info);
info.db.transaction(); //开启事务
}
}
}
void DBOperate::close_DBs()
{
for(int i =0;i < m_mapDataBaseNameAndDBInfo.count();++i)
{
m_mapDataBaseNameAndDBInfo.values()[i].db.commit();
m_mapDataBaseNameAndDBInfo.values()[i].db.close();
}
m_mapDataBaseNameAndDBInfo.clear();
}
void DBOperate::addDBTableOperate(QString sn, QString op)
{
QMutexLocker locker(&m_op_mutex);
DB_OPInstruct op_instruct;
op_instruct.sn = sn;
op_instruct.query_str = op;
m_op_instructList.append(op_instruct);
}
//获取操作的数据库表
QString DBOperate::getDBTableStrFromPayload(nodePayload payload)
{
QString table_name="";
switch (payload) {
case FULL_PAYLOAD:
table_name = QString("full_payload");
break;
case FOUR_FIFTH_PAYLOAD:
table_name = QString("four_fifth_payload");
break;
case THREE_FIFTH_PAYLOAD:
table_name = QString("three_fifth_payload");
break;
case TWO_FIFTH_PAYLOAD:
table_name = QString("two_fifth_payload");
break;
case ONE_FIFTH_PAYLOAD:
table_name = QString("one_fifth_payload");
break;
case EMPTY_PAYLOAD:
table_name = QString("zero_payload");
break;
default:
table_name = QString("");
break;
}
return table_name;
}
void DBOperate::commitTransaction(bool commit)
{
m_commit_transaction = commit;
}
void DBOperate::getNodeDatasTimeRangeFromDB(QSharedPointer<NodeParam> node)
{
DB_Info &info = m_mapDataBaseNameAndDBInfo[node->getSerialNumber()];
QSqlQuery query(info.db);
uint query_time=0;
QFuture<bool> future;
QVector<nodePayload> payloadVec={FULL_PAYLOAD,FOUR_FIFTH_PAYLOAD,THREE_FIFTH_PAYLOAD,TWO_FIFTH_PAYLOAD,ONE_FIFTH_PAYLOAD,EMPTY_PAYLOAD};
node->setNodeDataMinTime(0);
node->setNodeDataMaxTime(0);
node->clearNodeDatas();
for(int i = 0;i < payloadVec.count();++i)
{
//查询最大时间
query_time = 0;
future = QtConcurrent::run(&query,&QSqlQuery::exec,QString("select max(unix_time) from `%1`;").arg(getDBTableStrFromPayload(payloadVec[i])));
while(!future.isFinished())
QCoreApplication::processEvents();
if(query.next())
{
query_time = query.value(0).toUInt();
}
if(query_time != 0)
{
if(node->getNodeDataMaxTime() == 0)
{
node->setNodeDataMaxTime(query_time);
}else
{
if(node->getNodeDataMaxTime() < query_time)
node->setNodeDataMaxTime(query_time);
}
}
query.finish();
//查询最小时间
query_time = 0;
future = QtConcurrent::run(&query,&QSqlQuery::exec,QString("select min(unix_time) from `%1`;").arg(getDBTableStrFromPayload(payloadVec[i])));
while(!future.isFinished())
QCoreApplication::processEvents();
if(query.next())
{
query_time = query.value(0).toUInt();
}
if(query_time != 0)
{
if(node->getNodeDataMinTime() == 0)
{
node->setNodeDataMinTime(query_time);
}else
{
if(node->getNodeDataMinTime() > query_time)
node->setNodeDataMinTime(query_time);
}
}
query.finish();
}
}
void DBOperate::getNodeDatasFromStartTimeToEndTime(QSharedPointer<NodeParam> node, uint start_time, uint end_time,nodeDataType data_type)
{
DB_Info &info = m_mapDataBaseNameAndDBInfo[node->getSerialNumber()];
QSqlQuery query(info.db);
node_Data data;
QVector<nodePayload> payloadVec={FULL_PAYLOAD,FOUR_FIFTH_PAYLOAD,THREE_FIFTH_PAYLOAD,TWO_FIFTH_PAYLOAD,ONE_FIFTH_PAYLOAD,EMPTY_PAYLOAD};
if(data_type == DATA_UP)
node->clearUpData();
else if(data_type == DATA_DOWN)
node->clearDownData();
else
node->clearNodeDatas();
for(int i = 0;i < payloadVec.count();++i)
{
QFuture<bool> future = QtConcurrent::run(&query,&QSqlQuery::exec,QString("select * from `%1` where unix_time >= %2 AND unix_time <= %3;").arg(getDBTableStrFromPayload(payloadVec[i]))
.arg(start_time).arg(end_time));
while(!future.isFinished())
QCoreApplication::processEvents();
while(query.next())
{
data.addr = node->getNodeAddr();
data.serial_number = node->getSerialNumber();
data.payload_type = payloadVec[i];
data.unix_time = query.value(0).toDouble();
data.temp = query.value(1).toDouble();
data.pressure = query.value(2).toDouble();
data.ref_pressure = query.value(3).toDouble();
data.div_pressure = query.value(4).toDouble();
if(data_type == DATA_UP)
node->appendUpData(payloadVec[i],data);
else if(data_type == DATA_DOWN)
node->appendDownData(payloadVec[i],data);
else
node->appendnodeData(payloadVec[i],data);
}
query.finish();
}
}
void DBOperate::run()
{
while(m_is_run)
{
if(m_op_instructList.isEmpty())
{
QThread::msleep(50);
continue;
}
DB_OPInstruct op_instruct;
{
QMutexLocker locker(&m_op_mutex);
op_instruct = m_op_instructList.first();
m_op_instructList.removeFirst();
}
QSqlDatabase &db = m_mapDataBaseNameAndDBInfo[op_instruct.sn].db;
QSqlQuery query(db);
query.exec(op_instruct.query_str);
if(m_commit_transaction)
{
m_commit_transaction = false;
for(int i =0;i < m_mapDataBaseNameAndDBInfo.count();++i)
{
m_mapDataBaseNameAndDBInfo.values()[i].db.commit();
m_mapDataBaseNameAndDBInfo.values()[i].db.transaction();
}
}
QCoreApplication::processEvents();
}
close_DBs();
}
//从数据库路径下遍历数据库
void DBOperate::_getDBOfDBDir()
{
QDir dir(DBFileDir);
dir.setFilter(QDir::Files | QDir::NoSymLinks);
if(!dir.exists(DBFileDir)) //创建目录
dir.mkdir(DBFileDir);
QFileInfoList file_list = dir.entryInfoList();
for(int i = 0;i < file_list.count();)
{
QFileInfo info = file_list.at(i);
if(info.fileName().contains("journal"))
{
QFile::remove(info.absoluteFilePath());
++i;
continue;
}
DB_Info db_info(info.fileName());
db_info.db.setDatabaseName(info.absoluteFilePath());
db_info.db.open();
if(db_info.db.tables().isEmpty())
{
db_info.db.close();
QFile::remove(info.absoluteFilePath());
++i;
continue;
}
QSqlQuery query(db_info.db);
if(query.exec("select * from `node_info`;") && query.next())
{
db_info.serial_number = query.value(0).toString();
db_info.addr = query.value(1).toUInt();
db_info.version = query.value(2).toUInt();
db_info.range = query.value(3).toUInt();
m_mapDataBaseNameAndDBInfo.insert(info.baseName(),db_info);
}
++i;
}
}
void DBOperate::_createTable(DB_Info &db_info)
{
QStringList query_list;
query_list.clear();
query_list.append("CREATE TABLE IF NOT EXISTS node_info( \
sn VARCHAR(20) UNIQUE, \
addr int, \
version int, \
range int \
);");
query_list.append("CREATE TABLE IF NOT EXISTS full_payload( \
unix_time double UNIQUE, \
temp double, \
pressure double, \
ref_pressure double, \
div_pressure double \
);");
query_list.append("CREATE TABLE IF NOT EXISTS four_fifth_payload( \
unix_time double UNIQUE, \
temp double, \
pressure double, \
ref_pressure double, \
div_pressure double \
); ");
query_list.append("CREATE TABLE IF NOT EXISTS three_fifth_payload( \
unix_time double UNIQUE, \
temp double, \
pressure double, \
ref_pressure double, \
div_pressure double \
); ");
query_list.append("CREATE TABLE IF NOT EXISTS two_fifth_payload( \
unix_time double UNIQUE, \
temp double, \
pressure double, \
ref_pressure double, \
div_pressure double \
); ");
query_list.append("CREATE TABLE IF NOT EXISTS one_fifth_payload( \
unix_time double UNIQUE, \
temp double, \
pressure double, \
ref_pressure double, \
div_pressure double \
); ");
query_list.append("CREATE TABLE IF NOT EXISTS zero_payload( \
unix_time double UNIQUE, \
temp double, \
pressure double, \
ref_pressure double, \
div_pressure double \
); ");
query_list.append(QString("insert or replace into `node_info` values('%1',%2,%3,%4);").arg(db_info.serial_number).arg(db_info.addr).arg(db_info.version).arg(db_info.range));
db_info.db.transaction(); //开启事务
QSqlQuery query(db_info.db);
for(int i = 0;i < query_list.count();++i)
{
if(!query.exec(query_list.at(i)))
{
qDebug() << "create database fail:" << db_info.db.lastError();
}
}
db_info.db.commit();
}