-
Notifications
You must be signed in to change notification settings - Fork 4
/
sqlwork.cpp
91 lines (80 loc) · 1.87 KB
/
sqlwork.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
#include "sqlwork.h"
void SqlWork::working()
{
run = true;
while(run)
{
while(!isStop)
{
if(!dbStatus)
isPaused = false;
if(!dbStatus || !testDB.isOpen())
testDB.open();
testDbConnection = new QSqlQuery(testDB);
dbStatus = testDbConnection->exec("select version();");
delete testDbConnection;
if(isPaused)
{
QThread::sleep(5);
continue;
}
if(!dbStatus)
status = false;
else
{
if(dbName != "mainDB" && !DB.isOpen())
DB.open();
status = true;
isPaused = true; //连接成功后暂停线程
if(cnt++ == 1)
emit firstFinished();
}
emit newStatus(status);
QThread::sleep(5);
}
}
}
SqlWork::SqlWork(QString dbName)
{
this->dbName = dbName;
service dbService;
if(dbName == "mainDB")
dbService.addDatabase(testDB, "test_" + this->dbName); //如果是主窗口,则该类的DB弃用,DB由各个工作对象自行添加并连接
else
{
dbService.addDatabase(DB, this->dbName);
dbService.addDatabase(testDB, "test_" + this->dbName);
}
moveToThread(this->thread());
}
SqlWork::~SqlWork()
{
}
void SqlWork::beginThread() //完成数据库查询后调用此函数继续对数据库心跳验证
{
isStop = false;
}
void SqlWork::stopThread() //在对数据库查询时请调用此函数暂停该线程,避免冲突
{
isStop = true;
}
void SqlWork::quit()
{
run = false;
}
bool SqlWork::getisPaused()
{
return isPaused;
}
QSqlDatabase SqlWork::getDb()
{
return DB;
}
QSqlDatabase SqlWork::getTestDb()
{
return testDB;
}
QString SqlWork::getDbName()
{
return dbName;
}