File: startsql.cpp

package info (click to toggle)
tipp10 3.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,984 kB
  • sloc: cpp: 8,343; xml: 70; ansic: 60; makefile: 11
file content (533 lines) | stat: -rw-r--r-- 18,319 bytes parent folder | download | duplicates (2)
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
/*
Copyright (c) 2006-2009, Tom Thielicke IT Solutions

SPDX-License-Identifier: GPL-2.0-only
*/

/****************************************************************
**
** Implementation of the StartSql class
** File name: startsql.cpp
**
****************************************************************/

#include <QApplication>
#include <QChar>
#include <QDateTime>
#include <QEventLoop>
#include <QHash>
#include <QHashIterator>
#include <QIcon>
#include <QListWidgetItem>
#include <QSqlQuery>
#include <QVariant>

#include "startsql.h"

// Konstruktor
StartSql::StartSql() { }

int StartSql::fillLessonList(QListWidget* listLesson,
    QList<QString>* arrayTraining, QString languageLesson)
{
    // Clear the list first because this function is called not only once in
    // the contructor but also after a successful online update
    listLesson->clear();
    arrayTraining->clear();
    // Fill lesson list from SQLite lesson table
    QSqlQuery query;
    QIcon lessonIcon;
    int lessonCounter = 0;
    // SQL: all lessons sorted by id and a left joint to the number of
    // lessons done by the user
    QString sqlString
        = "SELECT lesson_list.lesson_id, "
          "lesson_list.lesson_name, lesson_list.lesson_description, "
          "COUNT(user_lesson_list.user_lesson_lesson) FROM lesson_list "
          "LEFT JOIN user_lesson_list ON lesson_list.lesson_id = "
          "user_lesson_list.user_lesson_lesson AND "
          "user_lesson_list.user_lesson_type = 0 "
          "WHERE lesson_list.lesson_language = '"
        + languageLesson + "' ";
    sqlString = sqlString.append("GROUP BY lesson_list.lesson_id;");
    if (!query.exec(sqlString)) {
        return -1;
    }
    // Read all datasets to list items
    while (query.next()) {
        // ID of the lesson
        QString lessonId = query.value(0).toString();
        // Name of the lesson
        QString lessonName = query.value(1).toString();
        // Maybe a additional description
        QString lessonDescription = query.value(2).toString();
        // Maybe a additional description -> show it in brackets
        if (query.value(2).toString() != "") {
            lessonName.append(" (" + lessonDescription + ")");
        }
        // Show different icons depending on number of lessons done
        if (query.value(3).toInt() > 2) {
            // Lesson done multiple
            lessonIcon = QIcon(":/img/lesson_done_three.png");
        } else {
            if (query.value(3).toInt() == 2) {
                // Lesson done multiple
                lessonIcon = QIcon(":/img/lesson_done_two.png");
            } else {
                if (query.value(3).toInt() == 1) {
                    // Lesson done once
                    lessonIcon = QIcon(":/img/lesson_done_one.png");
                } else {
                    // Lesson never done
                    lessonIcon = QIcon(":/img/lesson_done_none.png");
                }
            }
        }
        // Add Item to the list
        auto* currentItem
            = new QListWidgetItem(lessonIcon, lessonName, listLesson);
        currentItem->setToolTip(lessonDescription);
        arrayTraining->append(lessonId);
        lessonCounter++;
    }
    return lessonCounter;
}

// TODO: decide what to do with unused variable languageLesson
int StartSql::fillOpenList(QListWidget* listOpen, QList<QString>* arrayOpen,
    QString themeId, [[maybe_unused]] QString languageLesson)
{
    // Clear the list first because this function is called not only once in
    // the contructor but also after a successful online update
    listOpen->clear();
    arrayOpen->clear();
    // Fill lesson list from SQLite lesson table
    QSqlQuery query;
    QIcon openIcon;
    int lessonCounter = 0;
    QString themeAll = "WHERE open_list.open_theme = " + themeId + " ";
    if (themeId == "0" || themeId == "") {
        themeAll = "";
    }
    // SQL: all lessons sorted by id and a left joint to the number of
    // lessons done by the user
    if (!query.exec("SELECT open_list.open_id, open_list.open_name, "
                    "open_list.open_description, "
                    "COUNT(user_lesson_list.user_lesson_lesson) FROM open_list "
                    "LEFT JOIN user_lesson_list ON open_list.open_id = "
                    "user_lesson_list.user_lesson_lesson AND "
                    "user_lesson_list.user_lesson_type = 1 "
            + themeAll + "GROUP BY open_list.open_name;")) {
        return -1;
    }
    // Read all datasets to list items
    while (query.next()) {
        // ID of the lesson
        QString lessonId = query.value(0).toString();
        // Name of the lesson
        QString lessonName = query.value(1).toString();
        // Maybe a additional description
        QString lessonDescription = query.value(2).toString();
        // Show different icons depending on number of lessons done
        if (query.value(3).toInt() > 2) {
            // Lesson done multiple
            openIcon = QIcon(":/img/lesson_done_three.png");
        } else {
            if (query.value(3).toInt() == 2) {
                // Lesson done multiple
                openIcon = QIcon(":/img/lesson_done_two.png");
            } else {
                if (query.value(3).toInt() == 1) {
                    // Lesson done once
                    openIcon = QIcon(":/img/lesson_done_one.png");
                } else {
                    // Lesson never done
                    openIcon = QIcon(":/img/lesson_done_none.png");
                }
            }
        }
        // Add Item to the list
        auto* currentItem = new QListWidgetItem(openIcon, lessonName, listOpen);
        currentItem->setToolTip(lessonDescription);
        arrayOpen->append(lessonId);
        lessonCounter++;
    }
    return lessonCounter;
}

int StartSql::fillOwnList(QListWidget* listOwn, QList<QString>* arrayOwn)
{
    // Clear the list first because this function is called not only once in
    // the contructor but also after a successful online update
    listOwn->clear();
    arrayOwn->clear();
    // Fill lesson list from SQLite lesson table
    QSqlQuery query;
    QIcon ownIcon;
    int lessonCounter = 0;
    // SQL: all lessons sorted by id and a left joint to the number of
    // lessons done by the user
    if (!query.exec("SELECT own_list.own_id, own_list.own_name, "
                    "own_list.own_description, "
                    "COUNT(user_lesson_list.user_lesson_lesson) FROM own_list "
                    "LEFT JOIN user_lesson_list ON own_list.own_id = "
                    "user_lesson_list.user_lesson_lesson AND "
                    "user_lesson_list.user_lesson_type = 2 "
                    "GROUP BY own_list.own_name;")) {
        return -1;
    }
    // Read all datasets to list items
    while (query.next()) {
        // ID of the lesson
        QString lessonId = query.value(0).toString();
        // Name of the lesson
        QString lessonName = query.value(1).toString();
        // Maybe a additional description
        QString lessonDescription = query.value(2).toString();
        // Show different icons depending on number of lessons done
        if (query.value(3).toInt() > 2) {
            // Lesson done multiple
            ownIcon = QIcon(":/img/lesson_done_three.png");
        } else {
            if (query.value(3).toInt() == 2) {
                // Lesson done multiple
                ownIcon = QIcon(":/img/lesson_done_two.png");
            } else {
                if (query.value(3).toInt() == 1) {
                    // Lesson done once
                    ownIcon = QIcon(":/img/lesson_done_one.png");
                } else {
                    // Lesson never done
                    ownIcon = QIcon(":/img/lesson_done_none.png");
                }
            }
        }
        // Add Item to the list
        auto* currentItem = new QListWidgetItem(ownIcon, lessonName, listOwn);
        currentItem->setToolTip(lessonDescription);
        arrayOwn->append(lessonId);
        lessonCounter++;
    }
    return lessonCounter;
}

// TODO: decide what to do with unused variable languageLesson
int StartSql::fillThemes(QComboBox* comboTheme, QList<QString>* arrayThemes,
    [[maybe_unused]] QString languageLesson, QString textAll)
{
    // Clear the list first because this function is called not only once in
    // the contructor but also after a successful online update
    comboTheme->clear();
    arrayThemes->clear();
    // Fill lesson list from SQLite lesson table
    QSqlQuery query;
    QString themeName;
    QString themeId;
    int themeCounter = 1;
    // SQL: all lessons sorted by id and a left joint to the number of
    // lessons done by the user
    if (!query.exec("SELECT open_themes.theme_id, open_themes.theme_name, "
                    "open_themes.theme_description FROM open_themes "
                    "GROUP BY open_themes.theme_id;")) {
        return -1;
    }
    comboTheme->insertItem(0, textAll);
    arrayThemes->append(nullptr);
    // Read all datasets to list items
    while (query.next()) {
        // ID of the lesson
        themeId = query.value(0).toString();
        // Name of the lesson
        themeName = query.value(1).toString();
        // Add Item to the list
        comboTheme->insertItem(themeCounter, themeName);
        arrayThemes->append(themeId);
        themeCounter++;
    }
    return themeCounter;
}

bool StartSql::deleteUserLessonList()
{
    QSqlQuery query;
    return query.exec("DELETE FROM user_lesson_list;");
}

bool StartSql::deleteUserChars()
{
    QSqlQuery query;
    return query.exec("DELETE FROM user_chars;");
}

bool StartSql::deleteOwnLesson(QString lessonnumber)
{
    QSqlQuery query;
    if (!query.exec("DELETE FROM user_lesson_list WHERE user_lesson_lesson = "
            + lessonnumber + " AND user_lesson_type = 2;")) {
        return false;
    }
    if (!query.exec("DELETE FROM own_content WHERE content_lesson = "
            + lessonnumber + ";")) {
        return false;
    }
    if (!query.exec(
            "DELETE FROM own_list WHERE own_id = " + lessonnumber + ";")) {
        return false;
    }
    return true;
}

bool StartSql::updateOwnLesson(QString lessonnumber, QString lessonname,
    QString description, QStringList content, int unit)
{
    QVariant lastLessonId;
    QSqlQuery query;
    QString lessonid = "0";
    QString simplifiedContent = "";

    if (content.empty())
        return true;

    if (lessonnumber != "-1" && lessonnumber != "-2") {
        // Update existing lesson
        // ----------------------
        // First, delete all content of the lesson
        if (!query.exec("DELETE FROM own_content WHERE content_lesson = "
                + lessonnumber + ";")) {
            return false;
        }
        lessonid = lessonnumber;
        // Insert content name in the database
        if (!query.exec("UPDATE own_list SET own_name = '"
                + lessonname.replace(QChar(0x27), "''", Qt::CaseSensitive)
                + "', "
                  "own_description = '"
                + description.replace(QChar(0x27), "''", Qt::CaseSensitive)
                + "', "
                  "own_unit = "
                + QString::number(unit) + " WHERE own_id = " + lessonnumber
                + ";")) {
            return false;
        }
    } else {
        // Create new lesson
        // -----------------
        // Insert content name in the database
        if (!query.exec("INSERT INTO own_list VALUES(NULL,'"
                + lessonname.replace(QChar(0x27), "''", Qt::CaseSensitive)
                + "','"
                + description.replace(QChar(0x27), "''", Qt::CaseSensitive)
                + "', " + QString::number(unit) + ");")) {
            return false;
        }
        lastLessonId = query.lastInsertId();
        lessonid = QString::number(lastLessonId.toInt());
    }
    // Write every line of lesson content to database
    for (int i = 0; i < content.size(); i++) {
        // simplifiedContent = QString::QString(
        //	content.at(i)).replace(QChar(0x27), "''",
        // Qt::CaseSensitive).simplified();
        simplifiedContent = QString(content.at(i))
                                .replace(QChar(0x27), "''", Qt::CaseSensitive)
                                .trimmed();

        if (!query.exec("INSERT INTO own_content VALUES(NULL,'"
                + simplifiedContent + "'," + lessonid + ");")) {
            return false;
        }
    }

    return true;
}

bool StartSql::ownLessonExist(QString lessonname)
{
    QSqlQuery query;
    if (!query.exec("SELECT own_name "
                    "FROM own_list "
                    "WHERE own_name = '"
            + lessonname + "';")) {
        return false;
    }
    return query.first();
}

bool StartSql::getOwnLesson(QString lessonnumber, QLineEdit* lineLessonName,
    QLineEdit* lineLessonDescription, QTextEdit* lineLessonContent,
    QRadioButton* radioUnitSentence, QRadioButton* radioUnitWord)
{

    QSqlQuery query;
    QString lessonName = "";
    QString lessonDescription = "";
    QString lessonContent = "";
    int lessonUnit = 0;

    if (!query.exec("SELECT own_name, own_description, own_unit "
                    "FROM own_list "
                    "WHERE own_id = "
            + lessonnumber + ";")) {
        return false;
    }
    if (query.first()) {
        lessonName = query.value(0).toString();
        lessonDescription = query.value(1).toString();
        lessonUnit = query.value(2).toInt();
    }

    if (!query.exec("SELECT content_text "
                    "FROM own_content "
                    "WHERE content_lesson = "
            + lessonnumber
            + " "
              "ORDER BY content_id;")) {
        return false;
    }
    // Read all datasets to list items
    while (query.next()) {
        // ID of the lesson
        lessonContent.append(query.value(0).toString() + "\n");
    }

    // Fill out text lines
    lineLessonName->setText(lessonName);
    lineLessonDescription->setText(lessonDescription);
    lineLessonContent->setText(lessonContent);
    if (lessonUnit == 0) {
        radioUnitSentence->setChecked(true);
        radioUnitWord->setChecked(false);
    } else {
        radioUnitSentence->setChecked(false);
        radioUnitWord->setChecked(true);
    }
    return true;
}

bool StartSql::analyzeOwnLessons() { return analyzeLessons("own"); }

bool StartSql::analyzeLessons(QString lessonType)
{
    QSqlQuery mainQuery;
    QSqlQuery subQuery;
    QHash<int, int> unicodeErrorHash;
    QHash<int, int> unicodeErrorHashClean;

    if (!mainQuery.exec("SELECT char_unicode FROM lesson_chars;")) {
        // Error message
        return false;
    }

    while (mainQuery.next()) {
        unicodeErrorHashClean[mainQuery.value(0).toInt()] = 0;
    }

    // 1. Delete current lessonanalysis table
    // (no error message, table could not exist)
    mainQuery.exec("DROP TABLE " + lessonType + "_analysis;");

    // 2a. Create new analysis table query with new error definitions as columns
    QString queryString = "CREATE TABLE " + lessonType + "_analysis "
        + "(analysis_content INTEGER primary key";
    QString indexString = "CREATE INDEX " + lessonType + "_analysis_index ON "
        + lessonType + "_analysis (analysis_content";

    QHashIterator<int, int> hashIteratorClean(unicodeErrorHashClean);
    while (hashIteratorClean.hasNext()) {
        hashIteratorClean.next();
        queryString += ", analysis_char_"
            + QString::number(hashIteratorClean.key()) + " INTEGER";
        indexString
            += ", analysis_char_" + QString::number(hashIteratorClean.key());
        // cout << i.key() << ": " << i.value() << endl;
    }

    queryString += ");";
    indexString += ");";

    // 2b. Execute new analysis table query
    if (!mainQuery.exec(queryString)) {
        return false;
    }

    // 2c. Execute new analysis table query
    if (!mainQuery.exec(indexString)) {
        return false;
    }

    // 3. Count number of lesson text
    if (!mainQuery.exec(
            "SELECT COUNT(content_id) FROM " + lessonType + "_content;")) {
        return false;
    }
    mainQuery.first();
    mainQuery.value(0).toInt();

    // 4. Query all lesson text and analyze it
    if (!mainQuery.exec("SELECT content_id, content_text FROM " + lessonType
            + "_content ORDER BY content_id;")) {
        return false;
    }

    while (mainQuery.next()) {
        unicodeErrorHash = unicodeErrorHashClean;
        QString lessonString = mainQuery.value(1).toString();
        auto lessonLen = static_cast<double>(lessonString.length());

        // Now count all error chars and put them into the hash table
        for (const auto& character : lessonString) {
            if (unicodeErrorHash.contains(character.unicode())) {
                unicodeErrorHash[character.unicode()]++;
            }
        }

        queryString = "INSERT INTO " + lessonType + "_analysis VALUES(";
        queryString += mainQuery.value(0).toString();
        QHashIterator<int, int> hashIterator(unicodeErrorHash);
        // hashIterator.toFront();
        while (hashIterator.hasNext()) {
            hashIterator.next();
            // Weighted number of chars (char ratio) to SQL string
            queryString += ", "
                + QString::number(
                    (static_cast<double>(hashIterator.value()) / lessonLen)
                        * 100.0,
                    'f', 2);
        }
        queryString += ");";

        // Execute new analysis table query
        if (!subQuery.exec(queryString)) {
            return false;
        }
    }

    return true;
}

void StartSql::fillLanguage(QComboBox* combo, QString table, QString field)
{

    combo->clear();

    QSqlQuery query;
    if (!query.exec("SELECT " + table + "." + field
            + "_key, "
              ""
            + table + "." + field
            + "_label "
              "FROM "
            + table
            + " "
              "GROUP BY "
            + table + "." + field + "_id;")) {
        return;
    }

    int counter = 0;
    while (query.next()) {
        combo->insertItem(counter, query.value(1).toString(),
            QVariant(query.value(0).toString()));
        counter++;
    }
}