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 "populateworker.h"
#include "common/utils_sql.h"
#include "db/db.h"
#include "db/sqlquery.h"
#include "plugins/populateplugin.h"
#include "services/notifymanager.h"
PopulateWorker::PopulateWorker(Db* db, const QString& table, const QStringList& columns, const QList<PopulateEngine*>& engines, qint64 rows, QObject* parent) :
QObject(parent), db(db), table(table), columns(columns), engines(engines), rows(rows)
{
}
PopulateWorker::~PopulateWorker()
{
}
void PopulateWorker::run()
{
static const QString insertSql = QStringLiteral("INSERT INTO %1 (%2) VALUES (%3);");
if (!db->begin())
{
notifyError(tr("Could not start transaction in order to perform table populating. Error details: %1").arg(db->getErrorText()));
emit finished(false);
return;
}
Dialect dialect = db->getDialect();
QString wrappedTable = wrapObjIfNeeded(table, dialect);
QStringList cols;
QStringList argList;
for (const QString& column : columns)
{
cols << wrapObjIfNeeded(column, dialect);
argList << "?";
}
QString finalSql = insertSql.arg(wrappedTable, cols.join(", "), argList.join(", "));
SqlQueryPtr query = db->prepare(finalSql);
QList<QVariant> args;
bool nextValueError = false;
for (qint64 i = 0; i < rows; i++)
{
if (i == 0 && !beforePopulating())
return;
args.clear();
for (PopulateEngine* engine : engines)
{
args << engine->nextValue(nextValueError);
db->rollback();
emit finished(false);
return;
}
query->setArgs(args);
if (!query->execute())
{
notifyError(tr("Error while populating table: %1").arg(query->getErrorText()));
db->rollback();
emit finished(false);
return;
}
}
if (!db->commit())
{
notifyError(tr("Could not commit transaction after table populating. Error details: %1").arg(db->getErrorText()));
db->rollback();
emit finished(false);
return;
}
afterPopulating();
emit finished(true);
}
bool PopulateWorker::isInterrupted()
{
QMutexLocker locker(&interruptMutex);
return interrupted;
}
bool PopulateWorker::beforePopulating()
{
for (PopulateEngine* engine : engines)
{
if (!engine->beforePopulating(db, table))
{
db->rollback();
emit finished(false);
return false;
}
}
return true;
}
void PopulateWorker::afterPopulating()
{
for (PopulateEngine* engine : engines)
engine->afterPopulating();
}
void PopulateWorker::interrupt()
{
QMutexLocker locker(&interruptMutex);
interrupted = true;
}
|