Skip to content

Commit

Permalink
#5148 Fixed counting of tables in queries transparently attaching oth…
Browse files Browse the repository at this point in the history
…er databases
  • Loading branch information
pawelsalawa committed Dec 8, 2024
1 parent aa72498 commit e82c12f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
27 changes: 20 additions & 7 deletions SQLiteStudio3/coreSQLiteStudio/db/queryexecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <QThreadPool>
#include <QDebug>
#include <QtMath>
#include <dbattacher.h>

// TODO modify all executor steps to use rebuildTokensFromContents() method, instead of replacing tokens manually.

Expand Down Expand Up @@ -297,13 +298,6 @@ void QueryExecutor::execInternal()
simpleExecution = false;
interrupted = false;

if (resultsCountingAsyncId != 0)
{
resultsCountingAsyncId = 0;
db->interrupt();
releaseResultsAndCleanup();
}

// Reset context
delete context;
context = new Context();
Expand Down Expand Up @@ -351,6 +345,24 @@ bool QueryExecutor::countResults()
return false;
}

// Apply all transparent attaches to the counting DB
auto it = context->dbNameToAttach.iterator();
while (it.hasNext())
{
auto entry = it.next();
Db* dbToAttach = DBLIST->getByName(entry.key());
SqlQueryPtr attachRes = countingDb->exec(QString("ATTACH '%1' AS %2").arg(dbToAttach->getPath(), entry.value()));
if (attachRes->isError())
{
notifyError(tr("An error occured while executing the count(*) query, thus data paging will be disabled. Error details from the database: %1")
.arg("Failed to attach necessary databases for counting."));

qDebug() << "Error while attaching db for counting:" << attachRes->getErrorText();
countingDb->detachAll();
return false;
}
}

if (asyncMode)
{
// Start asynchronous results counting query
Expand Down Expand Up @@ -596,6 +608,7 @@ bool QueryExecutor::handleRowCountingResults(SqlQueryPtr results)
.arg(results->getErrorText()));
}

countingDb->detachAll();
return true;
}

Expand Down
9 changes: 1 addition & 8 deletions SQLiteStudio3/coreSQLiteStudio/db/queryexecutor.h
Original file line number Diff line number Diff line change
Expand Up @@ -1385,14 +1385,6 @@ class API_EXPORT QueryExecutor : public QObject, public QRunnable
*/
qint64 simpleExecutionStartTime;

/**
* @brief Asynchronous ID of counting query execution.
*
* Asynchronous ID returned from Db::asyncExec() for the counting query execution.
* See countResults() for details on counting query.
*/
quint32 resultsCountingAsyncId = 0;

/**
* @brief Flag indicating results preloading.
*
Expand Down Expand Up @@ -1496,6 +1488,7 @@ class API_EXPORT QueryExecutor : public QObject, public QRunnable
bool forceSimpleMode = false;
ChainExecutor* simpleExecutor = nullptr;
Db* countingDb = nullptr;
QStringList countingAttaches;

signals:
/**
Expand Down
2 changes: 1 addition & 1 deletion SQLiteStudio3/coreSQLiteStudio/sqlitestudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

DEFINE_SINGLETON(SQLiteStudio)

static const int sqlitestudioVersion = 30409;
static const int sqlitestudioVersion = 30410;

SQLiteStudio::SQLiteStudio()
{
Expand Down

0 comments on commit e82c12f

Please sign in to comment.