Skip to content

Commit

Permalink
Avoid unnecessary queries when populating filtered table
Browse files Browse the repository at this point in the history
When a table is filtered and the browsed tabled is changed and then we
return to this filtered table, the table view shows several incorrect
disabled rows. The same may happen when refreshing this table. This seems
to be produced by a (yet unsolved) concurrency problem triggered by
several consecutive queries, that are signalled by setting the text of
the stored filters in the table header filter.

These unnecessary queries are avoided by setting the stored filter values
at the same time as the table in the SQL table model, making sure that
no intermediate queries are executed while the filters are set.

This seems to work around the concurrency problem and the symptom of the
invalid rows does not appear in those situations.
  • Loading branch information
mgrojo authored and MKleusberg committed Oct 3, 2018
1 parent dd4a71e commit c78c03b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
7 changes: 5 additions & 2 deletions src/MainWindow.cpp
Expand Up @@ -649,9 +649,9 @@ void MainWindow::populateTable()
}
}
if(only_defaults)
m_browseTableModel->setTable(tablename, storedData.sortOrderIndex, storedData.sortOrderMode);
m_browseTableModel->setTable(tablename, storedData.sortOrderIndex, storedData.sortOrderMode, storedData.filterValues);
else
m_browseTableModel->setTable(tablename, storedData.sortOrderIndex, storedData.sortOrderMode, v);
m_browseTableModel->setTable(tablename, storedData.sortOrderIndex, storedData.sortOrderMode, storedData.filterValues, v);

// There is information stored for this table, so extract it and apply it
applyBrowseTableSettings(storedData);
Expand Down Expand Up @@ -708,9 +708,12 @@ void MainWindow::applyBrowseTableSettings(BrowseDataTableSettings storedData, bo
// Filters
if(!skipFilters)
{
// Set filters blocking signals, since the filter is already applied to the browse table model
FilterTableHeader* filterHeader = qobject_cast<FilterTableHeader*>(ui->dataTable->horizontalHeader());
bool oldState = filterHeader->blockSignals(true);
for(auto filterIt=storedData.filterValues.constBegin();filterIt!=storedData.filterValues.constEnd();++filterIt)
filterHeader->setFilter(filterIt.key(), filterIt.value());
filterHeader->blockSignals(oldState);
}

// Encoding
Expand Down
10 changes: 7 additions & 3 deletions src/sqlitetablemodel.cpp
Expand Up @@ -116,7 +116,7 @@ void SqliteTableModel::setChunkSize(size_t chunksize)
m_chunkSize = chunksize;
}

void SqliteTableModel::setTable(const sqlb::ObjectIdentifier& table, int sortColumn, Qt::SortOrder sortOrder, const QVector<QString>& display_format)
void SqliteTableModel::setTable(const sqlb::ObjectIdentifier& table, int sortColumn, Qt::SortOrder sortOrder, const QMap<int, QString> filterValues, const QVector<QString>& display_format)
{
// Unset all previous settings. When setting a table all information on the previously browsed data set is removed first.
reset();
Expand All @@ -125,6 +125,9 @@ void SqliteTableModel::setTable(const sqlb::ObjectIdentifier& table, int sortCol
m_sTable = table;
m_vDisplayFormat = display_format;

for(auto filterIt=filterValues.constBegin(); filterIt!=filterValues.constEnd(); ++filterIt)
updateFilter(filterIt.key(), filterIt.value(), false);

// The first column is the rowid column and therefore is always of type integer
m_vDataTypes.push_back(SQLITE_INTEGER);

Expand Down Expand Up @@ -704,7 +707,7 @@ QStringList SqliteTableModel::getColumns(std::shared_ptr<sqlite3> pDb, const QSt
return listColumns;
}

void SqliteTableModel::updateFilter(int column, const QString& value)
void SqliteTableModel::updateFilter(int column, const QString& value, bool applyQuery)
{
// Check for any special comparison operators at the beginning of the value string. If there are none default to LIKE.
QString op = "LIKE";
Expand Down Expand Up @@ -806,7 +809,8 @@ void SqliteTableModel::updateFilter(int column, const QString& value)
}

// Build the new query
buildQuery();
if (applyQuery)
buildQuery();
}

void SqliteTableModel::clearCache()
Expand Down
4 changes: 2 additions & 2 deletions src/sqlitetablemodel.h
Expand Up @@ -82,7 +82,7 @@ class SqliteTableModel : public QAbstractTableModel
QString customQuery(bool withRowid);

/// configure for browsing specified table
void setTable(const sqlb::ObjectIdentifier& table, int sortColumn = 0, Qt::SortOrder sortOrder = Qt::AscendingOrder, const QVector<QString> &display_format = QVector<QString>());
void setTable(const sqlb::ObjectIdentifier& table, int sortColumn = 0, Qt::SortOrder sortOrder = Qt::AscendingOrder, const QMap<int, QString> filterValues = QMap<int, QString>(), const QVector<QString> &display_format = QVector<QString>());

void setChunkSize(size_t chunksize);
void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override;
Expand Down Expand Up @@ -110,7 +110,7 @@ class SqliteTableModel : public QAbstractTableModel
static void removeCommentsFromQuery(QString& query);

public slots:
void updateFilter(int column, const QString& value);
void updateFilter(int column, const QString& value, bool applyQuery = true);

signals:
void finishedFetch(int fetched_row_begin, int fetched_row_end);
Expand Down

0 comments on commit c78c03b

Please sign in to comment.