aboutsummaryrefslogtreecommitdiffstats
path: root/SQLiteStudio3/coreSQLiteStudio/populateworker.cpp
diff options
context:
space:
mode:
authorLibravatarUnit 193 <unit193@ubuntu.com>2014-12-06 17:33:25 -0500
committerLibravatarUnit 193 <unit193@ubuntu.com>2014-12-06 17:33:25 -0500
commit7167ce41b61d2ba2cdb526777a4233eb84a3b66a (patch)
treea35c14143716e1f2c98f808c81f89426045a946f /SQLiteStudio3/coreSQLiteStudio/populateworker.cpp
Imported Upstream version 2.99.6upstream/2.99.6
Diffstat (limited to 'SQLiteStudio3/coreSQLiteStudio/populateworker.cpp')
-rw-r--r--SQLiteStudio3/coreSQLiteStudio/populateworker.cpp110
1 files changed, 110 insertions, 0 deletions
diff --git a/SQLiteStudio3/coreSQLiteStudio/populateworker.cpp b/SQLiteStudio3/coreSQLiteStudio/populateworker.cpp
new file mode 100644
index 0000000..71ab9a4
--- /dev/null
+++ b/SQLiteStudio3/coreSQLiteStudio/populateworker.cpp
@@ -0,0 +1,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;
+}