-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbmanager.cpp
241 lines (207 loc) · 6.62 KB
/
dbmanager.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
#include "dbmanager.h"
#include <iostream>
dbmanager::dbmanager(const QString& path)
{
m_db = QSqlDatabase::addDatabase("QSQLITE","SQLITE");
m_db.setDatabaseName(path);
if (!m_db.open())
{
qDebug() << "Error: connection with database fail";
}
else
{
qDebug() << "Database: connection established";
}
}
dbmanager::~dbmanager()
{
if (m_db.isOpen())
m_db.close();
}
bool dbmanager::isOpen() const
{
return m_db.isOpen();
}
bool dbmanager::createTable()
{
bool success = false;
QSqlQuery query(m_db);
query.prepare(table);
bool result = query.exec();
if (!result)
{
success = false;
}
else
{
success = true;
}
return success;
}
bool dbmanager::addrecord(record_params* p)
{
bool success = false;
// you should check if args are ok first...
QSqlQuery query(m_db);
query.prepare("INSERT INTO qso (callsign, freq, mode, date, time, rst_s, rst_r, exchange, moja_exchange) " \
"VALUES (:callsign, :freq, :mode, :date, :time, :rst_s, :rst_r, :exchange, :moja_exchange);");
query.bindValue(":callsign", p->callsign);
query.bindValue(":freq", p->freq);
query.bindValue(":mode", p->mode);
query.bindValue(":date", p->date);
query.bindValue(":time", p->time);
query.bindValue(":rst_s", p->rst_s);
query.bindValue(":rst_r", p->rst_r);
query.bindValue(":exchange", p->exchange);
query.bindValue(":moja_exchange", p->moja_exchange);
if(query.exec())
{
success = true;
}
else
{
qDebug() << "add new record error: " << query.lastError();
}
return success;
}
void dbmanager::selectall()
{
QSqlQuery query("SELECT * FROM qso", m_db);
int idcallsign = query.record().indexOf("callsign");
while (query.next())
{
QString callsign = query.value(idcallsign).toString();
}
}
QString dbmanager::printAllRecords() const
{
QString result = "<table width=100%><tbody><tr><td>Call</td>"
"<td>FREQ</td><td>MODE</td><td>DATE</td><td>TIME</td>"
"<td>RST_S</td><td>RST_R</td><td>EXCHANGE</td></tr>";
QSqlQuery query("SELECT * FROM qso", m_db);
query.exec();
while (query.next())
{
int idcall = query.record().indexOf("callsign");
int idfreq = query.record().indexOf("freq");
int idmode = query.record().indexOf("mode");
int iddate = query.record().indexOf("date");
int idtime = query.record().indexOf("time");
int idrst_s = query.record().indexOf("rst_s");
int idrst_r = query.record().indexOf("rst_r");
int idech = query.record().indexOf("exchange");
QString qso = query.value(idcall).toString();
double freq = query.value(idfreq).toDouble();
QString mode = query.value(idmode).toString();
QString date = query.value(iddate).toString();
QString time = query.value(idtime).toString();
QString rst_s = query.value(idrst_s).toString();
QString rst_r = query.value(idrst_r).toString();
QString exch = query.value(idech).toString();
result += "<tr><td>" + qso + "</td><td>" + QString::number(freq) +
"</td><td>" + mode + "</td><td>" + date + "</td><td>" +
time + "</td><td>" + rst_s + "</td><td>" + rst_r + "</td><td>" + exch + "</td></tr>";
}
return result + "</tbody><table>";
}
QString dbmanager::mapModulation(QString *mode) const
{
if (QString().compare(*mode, QString("SSB"), Qt::CaseInsensitive) != 1)
{
return "PH";
}
else if (QString().compare(*mode, QString("CW"), Qt::CaseInsensitive) != 1)
{
return "CW";
}
else if (QString().compare(*mode, QString("RTTY"), Qt::CaseInsensitive) != 1)
{
return "RY";
}
else if (QString().compare(*mode, QString("FM"), Qt::CaseInsensitive) != 1)
{
return "FM";
}
else if (QString().compare(*mode, QString("DIGI"), Qt::CaseInsensitive) != 1)
{
return "DG";
}
return "MIXED";
}
QString dbmanager::printToADIF() const
{
QString result;
QSqlQuery query("SELECT * FROM qso", m_db);
query.exec();
while (query.next())
{
int idcall = query.record().indexOf("callsign");
int idfreq = query.record().indexOf("freq");
int idmode = query.record().indexOf("mode");
int iddate = query.record().indexOf("date");
int idtime = query.record().indexOf("time");
int idrst_s = query.record().indexOf("rst_s");
int idrst_r = query.record().indexOf("rst_r");
int idech = query.record().indexOf("exchange");
int idmyech = query.record().indexOf("moja_exchange");
QString qso = query.value(idcall).toString();
double freq = query.value(idfreq).toDouble();
QString mode = query.value(idmode).toString();
QString date = query.value(iddate).toString();
QString time = query.value(idtime).toString();
QString rst_s = query.value(idrst_s).toString();
QString rst_r = query.value(idrst_r).toString();
QString exch = query.value(idech).toString();
QString myexch = query.value(idmyech).toString();
config cfg;
params par;
comcfg com_par;
QString f = "contest_logger.conf";
cfg.load_settings(&par, &com_par, f);
result += "QSO: \t" + QString::number(freq) + "\t" + mapModulation(&mode) + "\t" + date + "\t" + time + "\t" + par.callsign + "\t" + rst_s + "\t" + myexch
+ "\t" + qso + "\t" + rst_r + "\t" + exch + "\t" + "0" + "\n";
}
return result;
}
bool dbmanager::callsignExists(const QString& callsign) const
{
bool exists = false;
QSqlQuery checkQuery;
checkQuery.prepare("SELECT name FROM qso WHERE callsign = (:callsign)");
checkQuery.bindValue(":callsign", callsign);
if (checkQuery.exec())
{
if (checkQuery.next())
{
exists = true;
}
}
else
{
qDebug() << "callsign exists failed: " << checkQuery.lastError();
}
return exists;
}
QString dbmanager::getlastRcvExchange() const
{
QSqlQuery query("SELECT exchange FROM qso DESC LIMIT 1", m_db);
query.exec();
QString rcv_exch = "";
while (query.next())
{
rcv_exch = query.record().indexOf("exchange");
}
std::cout << "#### " << rcv_exch.toStdString() << std::endl;
return rcv_exch;
}
QString dbmanager::getlastSentExchange() const
{
QSqlQuery query("SELECT moja_exchange FROM qso DESC LIMIT 1", m_db);
query.exec();
QString sent_wym = "0";
while (query.next())
{
sent_wym = query.record().indexOf("moja_exchange");
}
return sent_wym;
}