aboutsummaryrefslogtreecommitdiffstats
path: root/SQLiteStudio3/coreSQLiteStudio/services/importmanager.cpp
blob: 7b38211dd0ab5ea1acad11c283931e1baa1b6f6e (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "importmanager.h"
#include "services/pluginmanager.h"
#include "services/notifymanager.h"
#include "plugins/importplugin.h"
#include "importworker.h"
#include "db/db.h"
#include "common/unused.h"
#include <QThreadPool>
#include <QDebug>

ImportManager::ImportManager()
{
}

QStringList ImportManager::getImportDataSourceTypes() const
{
    QStringList types;
    for (ImportPlugin* plugin : PLUGINS->getLoadedPlugins<ImportPlugin>())
        types << plugin->getDataSourceTypeName();

    return types;
}

ImportPlugin* ImportManager::getPluginForDataSourceType(const QString& dataSourceType) const
{
    for (ImportPlugin* plugin : PLUGINS->getLoadedPlugins<ImportPlugin>())
    {
        if (plugin->getDataSourceTypeName() == dataSourceType)
            return plugin;
    }

    return nullptr;
}

void ImportManager::configure(const QString& dataSourceType, const ImportManager::StandardImportConfig& config)
{
    plugin = getPluginForDataSourceType(dataSourceType);
    importConfig = config;
}

void ImportManager::importToTable(Db* db, const QString& table, bool async)
{
    this->db = db;
    this->table = table;

    if (importInProgress)
    {
        emit importFailed();
        qCritical() << "Tried to import while other import was in progress.";
        return;
    }

    if (!db->isOpen())
    {
        emit importFailed();
        qCritical() << "Tried to import into closed database.";
        return;
    }

    if (!plugin)
    {
        emit importFailed();
        qCritical() << "Tried to import, while ImportPlugin was null.";
        return;
    }

    importInProgress = true;

    ImportWorker* worker = new ImportWorker(plugin, &importConfig, db, table);
    connect(worker, SIGNAL(finished(bool, int)), this, SLOT(finalizeImport(bool, int)));
    connect(worker, SIGNAL(createdTable(Db*,QString)), this, SLOT(handleTableCreated(Db*,QString)));
    connect(this, SIGNAL(orderWorkerToInterrupt()), worker, SLOT(interrupt()));

    if (async)
        QThreadPool::globalInstance()->start(worker);
    else
    {
        worker->run();
        delete worker;
    }
}

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

bool ImportManager::isAnyPluginAvailable()
{
    return PLUGINS->getLoadedPlugins<ImportPlugin>().size() > 0;
}

void ImportManager::finalizeImport(bool result, int rowCount)
{
    importInProgress = false;
    emit importFinished();
    if (result)
    {
        notifyInfo(tr("Imported data to the table '%1' successfully. Number of imported rows: %2").arg(table, QString::number(rowCount)));
        emit importSuccessful();
    }
    else
        emit importFailed();
}

void ImportManager::handleTableCreated(Db* db, const QString& table)
{
    UNUSED(table);
    emit schemaModified(db);
}