-
Notifications
You must be signed in to change notification settings - Fork 2
/
learnbase.cpp
654 lines (560 loc) · 19.2 KB
/
learnbase.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
#include "learnbase.h"
#include "QMessageBox"
#include "QDateTime"
#include "QDebug"
LearnBase::LearnBase(const QString &dbpath, const QString &connName)
:MySQLite(dbpath, connName)
{
if(!isTableExist("StateTable"))
{
createStateTable();
}
if(!isTableExist("CurrTimeStateTable"))
{
createCurrTimeStateTable();
}
}
LearnBase::~LearnBase()
{
}
void LearnBase::createStateTable()
{
db->transaction();
exec("CREATE TABLE [StateTable] (\
[id] INTEGER NOT NULL ON CONFLICT FAIL PRIMARY KEY AUTOINCREMENT,\
[wid] INTEGER,\
[bookpath] TEXT,\
[word] TEXT,\
[mean] TEXT,\
[connection] TEXT,\
[lektion] INTEGER,\
[Grade] INTEGER,\
[EF] DOUBLE,\
[Repetition] INTEGER,\
[Interval] INTEGER,\
[LastTime] DATETIME NOT NULL ON CONFLICT FAIL,\
[TimeDiff] INTEGER);");
db->commit();
}
void LearnBase::createCurrTimeStateTable()
{
db->transaction();
exec("CREATE TABLE [CurrTimeStateTable] (\
[id] INTEGER NOT NULL ON CONFLICT FAIL PRIMARY KEY AUTOINCREMENT,\
[wid] INTEGER,\
[bookpath] TEXT,\
[word] TEXT,\
[mean] TEXT,\
[connection] TEXT,\
[lektion] INTEGER,\
[Grade] INTEGER,\
[EF] DOUBLE,\
[Repetition] INTEGER,\
[Interval] INTEGER,\
[LastTime] DATETIME NOT NULL ON CONFLICT FAIL,\
[TimeDiff] INTEGER);");
db->commit();
}
QSqlDatabase *LearnBase::getLearnStatedb()
{
return getdb();
}
bool LearnBase::addNewWordState(const QString &tableName, const QString &bookPath,
const int &wid, const QString &word, const QString &mean,
const QString &connection, const int &lektion,
const double &EF,const int &Grade,const int &Repetition,const int &Interval)
{
QString currentTime = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
QString sql = QString("insert into %1 "
"(wid, bookpath, word, mean, connection, lektion, Grade, EF, Repetition, Interval, LastTime, TimeDiff) "
"values ('%2', '%3', '%4', '%5', '%6', '%7', '%8', '%9', '%10', '%11', '%12', '%13')")
.arg(tableName)
.arg(wid)
.arg(bookPath)
.arg(word)
.arg(mean)
.arg(connection)
.arg(lektion)
.arg(Grade)
.arg(EF)
.arg(Repetition)
.arg(Interval)
.arg(currentTime)
.arg(0);
return exec(sql);
}
int LearnBase::isExist(const QString &tableName, const QString &bookPath, const int &wid)
{
int id;
QString sql = QString("select id from %1 where bookpath = '%2' and wid = '%3'")
.arg(tableName)
.arg(bookPath)
.arg(wid);
QSqlQuery query(sql, *db);
query.next();
id = query.record().value("id").toInt();
return id;
}
int LearnBase::isExist(const QString &tableName, const QString &bookPath)
{
int id;
QString sql = QString("select id from %1 where bookpath = '%2'")
.arg(tableName)
.arg(bookPath);
QSqlQuery query(sql, *db);
query.next();
id = query.record().value("id").toInt();
return id;
}
void LearnBase::deleteTable(const QString &tableName)
{
//////////////////////////////////////////////////
//清空表内容
exec(QString("delete from %1").arg(tableName));
//////////////////////////////////////////////////
//清空自增记录
exec(QString("delete from sqlite_sequence where name = '%1'")
.arg(tableName));
}
bool LearnBase::deleteRecord(const QString &tableName, const int &id)
{
QString sql = QString("delete from %1 where id = '%2'")
.arg(tableName)
.arg(id);
return exec(sql);
}
bool LearnBase::deleteRecord(const QString &tableName, const QString &bookPath)
{
QString sql = QString("delete from %1 where bookpath = '%2'")
.arg(tableName)
.arg(bookPath);
return exec(sql);
}
QList<int> LearnBase::getReviewWordIDByLektion(const QString &tableName, const QString &bookPath,
const int &lektion)
{
QList<int> TimeDiff;
if(tableName == "StateTable")
{
// QString sql = QString("select id from StateTable where "
// "((julianday(datetime('now','localtime')) "
// "- julianday(datetime(LastTime))) >= Interval) "
// "and lektion = '%1' and bookpath = '%2'")
// .arg(lektion)
// .arg(bookPath);
QString sql = QString("select id from StateTable where "
"((julianday('now') "
"- julianday(date(LastTime))) >= Interval) "
"and lektion = '%1' and bookpath = '%2'")
.arg(lektion)
.arg(bookPath);
QSqlQuery query(sql, *db);
while(query.next())
{
TimeDiff << query.record().value(0).toInt();
}
return TimeDiff;
}
else if(tableName == "CurrTimeStateTable")
{
QList<int> TimeDiff;
QString sql = QString("select id from CurrTimeStateTable where "
"((julianday(datetime('now','localtime'))*86400 "
"- julianday(datetime(LastTime))*86400) >= Interval) "
"and lektion = '%1' and bookpath = '%2'")
.arg(lektion)
.arg(bookPath);
QSqlQuery query(sql, *db);
while(query.next())
{
TimeDiff << query.record().value(0).toInt();
}
return TimeDiff;
}
else
{
TimeDiff << -1;
return TimeDiff;
}
}
QList<int> LearnBase::getReviewWordID(const QString &tableName, const QString &bookPath)
{
QList<int> TimeDiff;
if(tableName == "StateTable")
{
// QString sql = QString("select id from StateTable where "
// "((julianday(datetime('now','localtime')) "
// "- julianday(datetime(LastTime))) >= Interval) "
// "and bookpath = '%1'").arg(bookPath);
QString sql = QString("select id from StateTable where "
"((julianday('now') "
"- julianday(date(LastTime))) >= Interval) "
"and bookpath = '%1'").arg(bookPath);
QSqlQuery query(sql, *db);
while(query.next())
{
TimeDiff << query.record().value(0).toInt();
}
return TimeDiff;
}
else if(tableName == "CurrTimeStateTable")
{
QList<int> TimeDiff;
QString sql = QString("select id from CurrTimeStateTable where "
"((julianday(datetime('now','localtime'))*86400 "
"- julianday(datetime(LastTime))*86400) >= Interval) "
"and bookpath = '%1'").arg(bookPath);
QSqlQuery query(sql, *db);
while(query.next())
{
TimeDiff << query.record().value(0).toInt();
}
return TimeDiff;
}
else
{
TimeDiff << -1;
return TimeDiff;
}
}
QStringList LearnBase::getImportantInfo(const QString &tableName, const int &id)
{
QStringList importantInfo;
QString sql = QString("select wid, bookpath from %1 where id = '%2'")
.arg(tableName).arg(id);
QSqlQuery query(sql, *db);
query.next();
importantInfo << query.record().value("wid").toString()
<< query.record().value("bookpath").toString();
return importantInfo;
}
int LearnBase::getRecordCount(const QString &tableName)
{
QString sql = QString("select count(*) from %1").arg(tableName);
QSqlQuery query(sql, *db);
query.next();
return query.value(0).toInt();
}
QString LearnBase::getLastTime(const QString &tableName, const int &id)
{
QString LastTime;
QString sql = QString("select LastTime from %1 where id = '%2'")
.arg(tableName).arg(id);
QSqlQuery query(sql, *db);
query.next();
LastTime = query.record().value("LastTime").toString();
return LastTime;
}
QString LearnBase::getWord(const QString &tableName, const int &id)
{
QString Word;
QString sql = QString("select word from %1 where id = '%2'")
.arg(tableName)
.arg(id);
QSqlQuery query(sql, *db);
query.next();
Word = query.record().value("word").toString();
return Word;
}
QString LearnBase::getMean(const QString &tableName, const int &id)
{
QString Mean;
QString sql = QString("select mean from %1 where id = '%2'")
.arg(tableName).arg(id);
QSqlQuery query(sql, *db);
query.next();
Mean = query.record().value("mean").toString();
return Mean;
}
QString LearnBase::getConnection(const QString &tableName, const int &id)
{
QString Connection;
QString sql = QString("select connection from %1 where id = '%2'")
.arg(tableName)
.arg(id);
QSqlQuery query(sql, *db);
query.next();
Connection = query.record().value("connection").toString();
return Connection;
}
int LearnBase::getLektion(const QString &tableName, const int &id)
{
int lektion;
QString sql = QString("select lektion from %1 where id = '%2'")
.arg(tableName)
.arg(id);
QSqlQuery query(sql, *db);
query.next();
lektion = query.record().value("lektion").toInt();
return lektion;
}
QStringList LearnBase::getWordAll(const QString &tableName, const int &id)
{
QStringList WordAll;
QString sql = QString("select word, mean, connection, lektion from %1 where id = '%2'")
.arg(tableName)
.arg(id);
QSqlQuery query(sql, *db);
query.next();
WordAll << query.record().value("word").toString()
<< query.record().value("mean").toString()
<< query.record().value("connection").toString()
<< query.record().value("lektion").toString();
return WordAll;
}
QList<QString> LearnBase::getWordState(const QString &tableName, const int &id)
{
QList<QString> wordStateList;
QString sql = QString("select Grade, EF, Repetition, Interval from %1 where id = '%2'")
.arg(tableName)
.arg(id);
QSqlQuery query(sql, *db);
query.next();
wordStateList << query.record().value("Grade").toString()
<< query.record().value("EF").toString()
<< query.record().value("Repetition").toString()
<< query.record().value("Interval").toString();
return wordStateList;
}
int LearnBase::getGrade(const QString &tableName, const int &id)
{
int Grade;
QString sql = QString("select Grade from %1 where id = '%2'")
.arg(tableName)
.arg(id);
QSqlQuery query(sql, *db);
query.next();
Grade = query.record().value("Grade").toInt();
return Grade;
}
double LearnBase::getEF(const QString &tableName, const int &id)
{
double EF;
QString sql = QString("select EF from %1 where id = '%2'")
.arg(tableName)
.arg(id);
QSqlQuery query(sql, *db);
query.next();
EF = query.record().value("EF").toDouble();
return EF;
}
int LearnBase::getRepetition(const QString &tableName, const int &id)
{
int Repetition;
QString sql = QString("select Repetition from %1 where id = '%2'")
.arg(tableName)
.arg(id);
QSqlQuery query(sql, *db);
query.next();
Repetition = query.record().value("Repetition").toInt();
return Repetition;
}
int LearnBase::getInterval(const QString &tableName, const int &id)
{
int Interval;
QString sql = QString("select Interval from %1 where id = '%2'")
.arg(tableName)
.arg(id);
QSqlQuery query(sql, *db);
query.next();
Interval = query.record().value("Interval").toInt();
return Interval;
}
int LearnBase::getTimeDiff(const QString &tableName, const int &id)
{
int TimeDiff;
QString sql = QString("select TimeDiff from %1 where id = '%2'")
.arg(tableName)
.arg(id);
QSqlQuery query(sql, *db);
query.next();
TimeDiff = query.record().value("TimeDiff").toInt();
return TimeDiff;
}
bool LearnBase::setWordAll(const QString &tableName, const int &wid,
const QString &bookPath, const QStringList &wordall)
{
QString word = wordall.at(0);
QString mean = wordall.at(1);
QString connection = wordall.at(2);
int lektion = wordall.at(3).toInt();
QString sql = QString("update '%1' set word = '%2', mean = '%3', connection = '%4', "
"lektion = '%5' where wid = '%6' and bookpath = '%7'")
.arg(tableName)
.arg(word)
.arg(mean)
.arg(connection)
.arg(lektion)
.arg(wid)
.arg(bookPath);
return exec(sql);
}
bool LearnBase::setWord(const QString &tableName, const int &wid, const QString &bookPath,
const QString &word)
{
QString sql = QString("update '%1' set word = '%2' where wid = '%3' and bookpath = '%4'")
.arg(tableName)
.arg(word)
.arg(wid)
.arg(bookPath);
return exec(sql);
}
bool LearnBase::setMean(const QString &tableName, const int &wid, const QString &bookPath,
const QString &mean)
{
QString sql = QString("update '%1' set mean = '%2' where wid = '%3' and bookPath = '%4'")
.arg(tableName)
.arg(mean)
.arg(wid)
.arg(bookPath);
return exec(sql);
}
bool LearnBase::setConnection(const QString &tableName, const int &wid, const QString &bookPath,
const QString &connection)
{
QString sql = QString("update '%1' set connection = '%2' where wid = '%3' and bookPath = '%4'")
.arg(tableName)
.arg(connection)
.arg(wid)
.arg(bookPath);
return exec(sql);
}
bool LearnBase::setLektion(const QString &tableName, const int &wid,
const QString &bookPath, const int &lektion)
{
QString sql = QString("update '%1' set lektion = '%2' where wid = '%3' and bookPath = '%4'")
.arg(tableName)
.arg(lektion)
.arg(wid)
.arg(bookPath);
return exec(sql);
}
bool LearnBase::setWordState(const QString &tableName, const int &id, const int &Grade,
const double &EF, const int &Repetition, const int &Interval)
{
QString currentTime = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
QString sql = QString("update '%1' set Grade = '%2', EF = '%3', Repetition = '%4',"
" Interval = '%5', LastTime = '%6' where id = '%7'")
.arg(tableName)
.arg(Grade)
.arg(EF)
.arg(Repetition)
.arg(Interval)
.arg(currentTime)
.arg(id);
return exec(sql);
}
bool LearnBase::setGrade(const QString &tableName, const int &id, const int &Grade)
{
QString sql = QString("update '%1' set Grade = '%2' where id = '%3'")
.arg(tableName)
.arg(Grade)
.arg(id);
return exec(sql);
}
bool LearnBase::setEF(const QString &tableName, const int &id, const double &EF)
{
QString sql = QString("update '%1' set EF = '%2' where id = '%3'")
.arg(tableName)
.arg(EF)
.arg(id);
return exec(sql);
}
bool LearnBase::setRepetition(const QString &tableName, const int &id, const int &Repetition)
{
QString sql = QString("update '%1' set Repetition = '%2' where id = '%3'")
.arg(tableName)
.arg(Repetition)
.arg(id);
return exec(sql);
}
bool LearnBase::setInterval(const QString &tableName, const int &id, const int &Interval)
{
QString sql = QString("update '%1' set Interval = '%2' where id = '%3'")
.arg(tableName)
.arg(Interval)
.arg(id);
return exec(sql);
}
bool LearnBase::setTimeDiff(const QString &tableName, const int &id, const int &TimeDiff)
{
QString sql = QString("update '%1' set TimeDiff = '%2' where id = '%3'")
.arg(tableName)
.arg(TimeDiff)
.arg(id);
return exec(sql);
}
int LearnBase::getHaveLearnedWordCountByLektion(const QString &bookPath, const int &lektion)
{
QString sql = QString("select count(*) from StateTable where bookpath = '%1' and lektion = '%2'")
.arg(bookPath)
.arg(lektion);
QSqlQuery query(sql, *db);
query.next();
return query.value(0).toInt();
}
int LearnBase::getHaveLearnedWordCount(const QString &bookPath)
{
QString sql = QString("select count(*) from StateTable where bookpath = '%1'")
.arg(bookPath);
QSqlQuery query(sql, *db);
query.next();
return query.value(0).toInt();
}
int LearnBase::getReviewCountInfo(const QString &bookPath, const int &Interval)
{
QString sql = QString("select count(*) from StateTable "
"where bookpath = '%1' and Interval = '%2'")
.arg(bookPath).arg(Interval);
// QString sql = QString("select count(*) from StateTable "
// "where bookpath = '%1' and ((julianday('now') "
// "- julianday(date(LastTime))) <= %2)")
// .arg(bookPath).arg(Interval);
QSqlQuery query(sql, *db);
query.next();
return query.value(0).toInt();
}
int LearnBase::getWordCountG5(const QString &bookPath)
{
QString sql = QString("select count(*) from StateTable "
"where bookpath = '%1' and Grade = 5")
.arg(bookPath);
QSqlQuery query(sql, *db);
query.next();
return query.value(0).toInt();
}
int LearnBase::getWordCountG4A3(const QString &bookPath)
{
QString sqlG4 = QString("select count(*) from StateTable "
"where bookpath = '%1'and Grade = 4")
.arg(bookPath);
QSqlQuery queryG4(sqlG4, *db);
queryG4.next();
int G4 = queryG4.value(0).toInt();
QString sqlG3 = QString("select count(*) from StateTable "
"where bookpath = '%1' and Grade = 3")
.arg(bookPath);
QSqlQuery queryG3(sqlG3, *db);
queryG3.next();
int G3 = queryG3.value(0).toInt();
return (G3+G4);
}
int LearnBase::getWordCountGU3(const QString &bookPath)
{
QString sql = QString("select count(*) from StateTable "
"where bookpath = '%1' and Grade < 3")
.arg(bookPath);
QSqlQuery query(sql, *db);
query.next();
return query.value(0).toInt();
}
QSqlTableModel *LearnBase::studyStateDBModel(QWidget *parent)
{
QSqlTableModel *studyStateModel;
studyStateModel = new QSqlTableModel(parent,*db);
QString tableName = QString("StateTable");
studyStateModel->setTable(tableName);
studyStateModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
studyStateModel->select(); //选取整个表的所有行
return studyStateModel;
}