-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget.cpp
296 lines (263 loc) · 8.86 KB
/
widget.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
#include "widget.h"
#include "ui_widget.h"
#include <QtGui>
#include <QMessageBox>
#include <QtEndian>
#include <QFileDialog>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
QCoreApplication::setOrganizationName("hanrai");
QCoreApplication::setOrganizationDomain("hanrai.com");
QCoreApplication::setApplicationName("Exporter");
QSettings settings;
ui->dataFile->setText(settings.value("filenames/src", QString()).toString());
ui->targetFolder->setText(settings.value("filenames/dest", QString()).toString());
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_dataFileButton_clicked()
{
QString path = "E:/";
if(!ui->dataFile->text().isEmpty())
path = QDir(ui->dataFile->text()).absolutePath();
QString filename = QFileDialog::getOpenFileName(this,
tr("Open Data File"),
path,
tr("Data File(data.dat)"));
if(filename.isEmpty())
return;
ui->dataFile->setText(filename);
QSettings settings;
settings.setValue("filenames/src", filename);
}
void Widget::on_targetButton_clicked()
{
QString path = "E:/";
if(!ui->targetFolder->text().isEmpty())
path = QDir(ui->targetFolder->text()).absolutePath();
QString filename = QFileDialog::getSaveFileName(
this,
tr("Export to..."),
path,
tr("SQLite (*.sqlite)"));
if(filename.isEmpty())
return;
ui->targetFolder->setText(filename);
QSettings settings;
settings.setValue("filenames/dest", filename);
}
int decode(QByteArray &data,
QSqlQuery &query,
QString &table,
int id)
{
if(data.size() <= 17)
return 0;
Header *header = (Header*)data.data();
DecodeHeader(header);
auto count = header->count;
if(!count)
return count;
query.exec("CREATE TABLE IF NOT EXISTS t_" + table + ""
"(id INTEGER NOT NULL REFERENCES datas(id) ON DELETE CASCADE, "
"timestamp INTEGER NOT NULL, value1 REAL NOT NULL)");
query.exec("CREATE INDEX IF NOT EXISTS iid_" + table + " ON t_" + table + "(id);");
query.prepare("INSERT INTO t_" + table + " VALUES "
"(:id, :timestamp, :data)");
quint8 *cursor = (quint8*)&header->data;
for(int i=0;i<count;i++)
{
quint32 ttime = ntohl(*(int*)cursor);
cursor+=4;
double doubleValue;
quint32 *pvalue=(quint32*)&doubleValue;
*pvalue = ntohl(*(int*)cursor);
cursor+=4;
pvalue++;
*pvalue = ntohl(*(int*)cursor);
cursor+=4;
query.bindValue(":id", id);
query.bindValue(":timestamp", ttime);
query.bindValue(":data", doubleValue);
if(!query.exec())
qDebug()<<query.lastError().text();
}
return count;
}
int decode2(QByteArray &data,
QSqlQuery &query,
QString &table,
int id)
{
if(data.size()<=17)
return 0;
Header *header = (Header*)data.data();
DecodeHeader(header);
auto count = header->count;
if(!count)
return count;
quint8 *cursor = (quint8*)&header->data;
auto cloneCursor = cursor;
cloneCursor += 4;
auto colCount = ntohl(*(quint32*)cloneCursor);
QString queryString = "CREATE TABLE IF NOT EXISTS t_" + table + ""
"(id INTEGER NOT NULL REFERENCES datas(id) ON DELETE CASCADE, "
"timestamp INTEGER NOT NULL";
for(int i=0; i<colCount; i++)
queryString += QString(", value%1 REAL NOT NULL").arg(i+1);
queryString += ");";
query.exec(queryString);
query.exec("CREATE INDEX IF NOT EXISTS iid_" + table + " ON t_" + table + "(id);");
queryString = "INSERT INTO t_" + table + " VALUES "
"(:id, :timestamp";
for(int i=0; i<colCount; i++)
queryString += QString(", :data%1").arg(i+1);
queryString += ");";
query.prepare(queryString);
for(int i=0;i<count;i++)
{
quint32 ttime = ntohl(*(quint32*)cursor);
cursor+=4;
quint32 countv = ntohl(*(quint32*)cursor);
if(countv != colCount)
{
query.exec("DROP TABLE t_" + table + "");
return -1;
}
cursor+=4;
query.bindValue(":id", id);
query.bindValue(":timestamp", ttime);
for(int i=0;i<countv;i++)
{
double doubleValue;
*((quint32*)&doubleValue)=ntohl(*(quint32*)cursor);
cursor+=4;
*((quint32*)&doubleValue+1)=ntohl(*(quint32*)cursor);
cursor+=4;
query.bindValue(QString(":data%1").arg(i+1), doubleValue);
}
if(!query.exec())
qDebug()<<query.lastError().text();
}
return count;
}
void Widget::on_startButton_clicked()
{
if(ui->dataFile->text().isEmpty())
return;
if(ui->targetFolder->text().isEmpty())
return;
if(!OpenDatabase(database, "src", ui->dataFile->text()))
return;
if(!OpenDatabase(destDatabase, "dest", ui->targetFolder->text()))
{
database.close();
return;
}
QSqlQuery destQuery(destDatabase);
destQuery.exec("PRAGMA foreign_keys = 1");
destQuery.setForwardOnly(true);
destDatabase.transaction();
InitInfoTable(destQuery);
QSqlQuery query(database);
query.setForwardOnly(true);
query.exec("SELECT * FROM t_stockly_double");
Decode(query, destQuery, 1);
query.exec("SELECT * FROM t_stockly_vdouble");
Decode(query, destQuery, 2);
destDatabase.commit();
destDatabase.close();
database.close();
qDebug()<<"Done!";
ui->msg->append("Done!");
}
bool Widget::OpenDatabase(QSqlDatabase &database, QString name, QString dbName)
{
bool result = true;
database = QSqlDatabase::addDatabase("QSQLITE", name);
database.setDatabaseName(dbName);
if(!database.open())
{
result = false;
QMessageBox msgBox;
msgBox.setText("Open Database Error:" + database.lastError().text());
msgBox.exec();
}
return result;
}
void Widget::InitInfoTable(QSqlQuery &query)
{
query.exec("CREATE TABLE IF NOT EXISTS datas("
"id INTEGER PRIMARY KEY, "
"market TEXT NOT NULL, "
"product TEXT NOT NULL, "
"data TEXT NOT NULL, "
"dataid TEXT NOT NULL, "
"timestamp INTEGER NOT NULL,"
"datacount INTEGER DEFAULT 0);");
query.exec("CREATE UNIQUE INDEX IF NOT EXISTS idatas ON datas("
"market, product, data, dataid, timestamp);");
query.exec("CREATE INDEX IF NOT EXISTS idatas_t ON datas(timestamp);");
query.exec("CREATE INDEX IF NOT EXISTS idatas_count ON datas(datacount);");
query.exec("DELETE FROM datas WHERE timestamp = 0;");
}
void Widget::Decode(QSqlQuery &query, QSqlQuery &destQuery, int selector)
{
while(query.next())
{
auto name = query.value(0).toString();
auto nameList = name.split("-");
auto value = query.value(1).toByteArray();
Q_ASSERT(nameList.size()==5);
QString market = nameList.at(0).toLocal8Bit();
QString product = nameList.at(1).toLocal8Bit();
QString table = nameList.at(2).toLocal8Bit();
QString subTable = nameList.at(3).toLocal8Bit();
auto ttime = nameList.at(4).toInt();
destQuery.prepare("INSERT INTO datas "
"VALUES (NULL, :market, :product, :data, :dataid, :timestamp, 0)");
destQuery.bindValue(":market", market);
destQuery.bindValue(":product", product);
destQuery.bindValue(":data", table);
destQuery.bindValue(":dataid", subTable);
destQuery.bindValue(":timestamp", ttime);
auto result = destQuery.exec();
if(!result)
{
qDebug() << destQuery.lastError().text();
ui->msg->append(QString("%1:%2").arg(selector).arg(name));
qApp->processEvents();
continue;
}
auto id = destQuery.lastInsertId().toInt();
Q_ASSERT(id >= 1);
int rowCount = 0;
switch(selector)
{
case 1:
rowCount = decode(value, destQuery, table, id);
break;
case 2:
rowCount = decode2(value, destQuery, table, id);
break;
default:
break;
}
destQuery.prepare("UPDATE datas SET datacount = :rowCount WHERE id = :id");
destQuery.bindValue(":rowCount", rowCount);
destQuery.bindValue(":id", id);
destQuery.exec();
ui->msg->append(QString("%1:%2:%3").arg(selector).arg(name).arg(rowCount));
qApp->processEvents();
}
}
void DecodeHeader(struct Header *header)
{
header->blocks = ntohl(*(int*)&header->blocks);
header->count = ntohl(*(int*)&header->count);
}