aboutsummaryrefslogtreecommitdiffstats
path: root/SQLiteStudio3/coreSQLiteStudio/services/populatemanager.cpp
blob: 237667da51649b74a727c196421fd3286b413f7c (plain) (blame)
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
#include "populatemanager.h"
#include "services/pluginmanager.h"
#include "plugins/populateplugin.h"
#include "services/notifymanager.h"
#include "populateworker.h"
#include "plugins/populatesequence.h"
#include "plugins/populaterandom.h"
#include "plugins/populaterandomtext.h"
#include "plugins/populateconstant.h"
#include "plugins/populatedictionary.h"
#include "plugins/populatescript.h"
#include <QDebug>
#include <QThreadPool>

PopulateManager::PopulateManager(QObject *parent) :
    PluginServiceBase(parent)
{
    PLUGINS->loadBuiltInPlugin(new PopulateSequence());
    PLUGINS->loadBuiltInPlugin(new PopulateRandom());
    PLUGINS->loadBuiltInPlugin(new PopulateRandomText());
    PLUGINS->loadBuiltInPlugin(new PopulateConstant());
    PLUGINS->loadBuiltInPlugin(new PopulateDictionary());
    PLUGINS->loadBuiltInPlugin(new PopulateScript());
}

void PopulateManager::populate(Db* db, const QString& table, const QHash<QString, PopulateEngine*>& engines, qint64 rows)
{
    if (workInProgress)
    {
        error();
        qCritical() << "Tried to call second populating process at the same time.";
        return;
    }

    if (!db->isOpen())
    {
        error();
        qCritical() << "Tried to populate table in closed database.";
        return;
    }

    workInProgress = true;

    columns.clear();
    engineList.clear();
    for (const QString& column : engines.keys())
    {
        columns << column;
        engineList << engines[column];
    }


    this->db = db;
    this->table = table;

    PopulateWorker* worker = new PopulateWorker(db, table, columns, engineList, rows);
    connect(worker, SIGNAL(finished(bool)), this, SLOT(finalizePopulating(bool)));
    connect(this, SIGNAL(orderWorkerToInterrupt()), worker, SLOT(interrupt()));

    QThreadPool::globalInstance()->start(worker);

}

void PopulateManager::error()
{
    emit populatingFinished();
    emit populatingFailed();
}

void PopulateManager::deleteEngines(const QList<PopulateEngine*>& engines)
{
    for (PopulateEngine* engine : engines)
        delete engine;
}

void PopulateManager::interrupt()
{
    emit orderWorkerToInterrupt();
}

void PopulateManager::finalizePopulating(bool result)
{
    workInProgress = false;

    emit populatingFinished();
    if (result)
    {
        notifyInfo(tr("Table '%1' populated successfully.").arg(table));
        emit populatingSuccessful();
    }
    else
        emit populatingFailed();
}