-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbthread.cpp
148 lines (140 loc) · 4.17 KB
/
dbthread.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
#include "dbthread.h"
/*!
* \brief DBthread::DBthread
* \param parent
*
* Constructor for building the DBthread,
* initializes the db driver.
*/
DBthread::DBthread(QObject *parent) :
QObject(parent),
result(true)
{
db = QSqlDatabase::addDatabase("QMYSQL");
qDebug()<<"Running DBthread";
}
/*!
* \brief DBthread::~DBthread
*
* Destructor for the DBthread
* removes the db driver.
*/
DBthread::~DBthread(){
QSqlDatabase::removeDatabase("QMYSQL");
}
/*!
* \brief DBthread::connectDB
* \param Host
* \param User
* \param Password
* Slot for communicating with the main thread and starting the database
*/
void DBthread::connectDB(QString Host, QString User, QString Password){
db.setHostName(Host);
db.setUserName(User);
db.setPassword(Password);
if(db.open()){
emit connected(true);
} else{
emit connected(false);
}
}
/*!
* \brief DBthread::openDB
* \param query
* Slot for getting the main's thread request of opening an existing DB
*/
void DBthread::openDB(QString query){
QSqlQuery quer(query);
if(!quer.isActive()){
emit openDBFail(false);
} else {
emit openDBFail(true);
}
}
/*!
* \brief DBthread::newDB
* \param query
* \param DBname
*
* Slot for creating a new db and all the tables needed inside it.
*/
void DBthread::newDB(QString query, QString DBname){
QSqlQuery query1(query);
if(query1.isActive())
qDebug()<<"Base de Datos Creada";
else{
if(result){
result = false;
emit querResult(result,query1.lastError().text());
qDebug()<<"Base de Datos no Creada";
}
}
QSqlQuery query2("use " + DBname);
if(query2.isActive())
qDebug()<<"Colocado en la base de datos " << DBname;
else{
if(result){
result = false;
emit querResult(result,query2.lastError().text());
qDebug() << query2.lastError();
}
}
QSqlQuery PlayTab("create table if not exists players(id TINYINT UNSIGNED NOT NULL AUTO_INCREMENT, player_label VARCHAR(10) NOT NULL, PRIMARY KEY(id))");
if(PlayTab.isActive())
qDebug()<<"Tabla de jugadores creada";
else{
if(result){
result = false;
emit querResult(result,PlayTab.lastError().text());
qDebug()<<"Tabla de jugadores no creada";
}
}
QSqlQuery FrameTab("create table if not exists data(id INT UNSIGNED NOT NULL AUTO_INCREMENT,frame BIGINT UNSIGNED NOT NULL, player_id TINYINT UNSIGNED NOT NULL, xpos DOUBLE PRECISION UNSIGNED NOT NULL, ypos DOUBLE PRECISION UNSIGNED NOT NULL, FOREIGN KEY (player_id) REFERENCES players(id) ON DELETE CASCADE, PRIMARY KEY (id))");
if(FrameTab.isActive())
qDebug()<<"Tabla de datos creada";
else{
if(result){
result = false;
emit querResult(result,FrameTab.lastError().text());
qDebug()<<"Tabla de datos no creada";
}
}
if(result){
qDebug() << result;
emit querResult(result,QString());
}
}
/*!
* \brief DBthread::storeInfor
* \param framesToInsert
*
* Slot for storing frame information into the database
*/
void DBthread::storeInfor(QVector<Frame> *framesToInsert){
qDebug()<< "Storing Info";
QVector<Frame>::const_iterator it = framesToInsert->begin();
while(it != framesToInsert->end()){
QString query = QString("INSERT INTO data (frame, player_id, xpos, ypos) Values ( %1, %2, %3, %4)").arg(it->frameNum).arg(it->player_id).arg(it->xpos).arg(it->ypos);
QSqlQuery quer(query);
if(!quer.isActive())
qDebug() << quer.lastError();
it++;
}
delete framesToInsert;
}
/*!
* \brief DBthread::storePlayers
* \param start
* \param end
* \param players
* Slot for storing player information in the database
*/
void DBthread::storePlayers(int start, int end, QVector<Player> *players){
int contador = start;
while(contador != end){
QSqlQuery quer(QString("INSERT INTO players (player_label) Values (\"%1\")").arg(players->at(contador).Tag));
qDebug() << QString("INSERT INTO players (player_label) Values (\"%1\")").arg(players->at(contador).Tag);
contador++;
}
}