From 75e4c32bc4cdd401568dd0ecae41c9f1bd0203bb Mon Sep 17 00:00:00 2001 From: Martin Kleusberg Date: Fri, 10 Aug 2018 14:26:00 +0200 Subject: [PATCH] Don't use the SqliteTableModel class for querying row counts in export When exporting a database to an SQL file we need to query the row count of each table to calculate the progress later in the actual export. This commit replaces the SqliteTableModel class which was used for this purpose by a simple query function. This might be slightly faster here but more importantly avoids a progress window being shown for each table before finally seeing the one for the actual export. See issue #1476. --- src/sqlitedb.cpp | 36 +++++++++++++++++++++++++++++++----- src/sqlitedb.h | 4 ++++ 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/sqlitedb.cpp b/src/sqlitedb.cpp index 2b888d9a..ee5bd744 100644 --- a/src/sqlitedb.cpp +++ b/src/sqlitedb.cpp @@ -639,11 +639,8 @@ bool DBBrowserDB::dump(const QString& filePath, it.remove(); } else { // Otherwise get the number of records in this table - SqliteTableModel m(*this); - m.setQuery(QString("SELECT COUNT(*) FROM %1;") - .arg(sqlb::ObjectIdentifier("main", it.value()->name()).toString())); - if(m.completeCache()) - numRecordsTotal += m.data(m.index(0, 0)).toUInt(); + numRecordsTotal += querySingeValueFromDb(QString("SELECT COUNT(*) FROM %1;") + .arg(sqlb::ObjectIdentifier("main", it.value()->name()).toString())).toUInt(); } } @@ -955,6 +952,35 @@ bool DBBrowserDB::executeMultiSQL(const QString& statement, bool dirty, bool log return true; } +QVariant DBBrowserDB::querySingeValueFromDb(const QString& statement, bool log) +{ + waitForDbRelease(); + if(!_db) + return QVariant(); + + if(log) + logSQL(statement, kLogMsg_App); + + QByteArray utf8Query = statement.toUtf8(); + sqlite3_stmt* stmt; + if(sqlite3_prepare_v2(_db, utf8Query, utf8Query.size(), &stmt, nullptr) == SQLITE_OK) + { + if(sqlite3_step(stmt) == SQLITE_ROW && sqlite3_column_count(stmt) > 0) + { + if(sqlite3_column_type(stmt, 0) != SQLITE_NULL) + { + int bytes = sqlite3_column_bytes(stmt, 0); + if(bytes) + return QByteArray(static_cast(sqlite3_column_blob(stmt, 0)), bytes); + else + return ""; + } + } + } + + return QVariant(); +} + bool DBBrowserDB::getRow(const sqlb::ObjectIdentifier& table, const QString& rowid, QVector& rowdata) { waitForDbRelease(); diff --git a/src/sqlitedb.h b/src/sqlitedb.h index 5ec75ad3..573b1917 100644 --- a/src/sqlitedb.h +++ b/src/sqlitedb.h @@ -88,9 +88,13 @@ public: bool revertToSavepoint(const QString& pointname = "RESTOREPOINT"); bool releaseAllSavepoints(); bool revertAll(); + bool dump(const QString& filename, const QStringList& tablesToDump, bool insertColNames, bool insertNew, bool exportSchema, bool exportData, bool keepOldSchema); + bool executeSQL(QString statement, bool dirtyDB = true, bool logsql = true); bool executeMultiSQL(const QString& statement, bool dirty = true, bool log = false); + QVariant querySingeValueFromDb(const QString& statement, bool log = true); + const QString& lastError() const { return lastErrorMessage; } /**