Skip to content

Commit

Permalink
Fix hanging when deleting large number of records (#856) (#870)
Browse files Browse the repository at this point in the history
  • Loading branch information
prutz1311 authored and justinclift committed Nov 25, 2016
1 parent 11366c3 commit 5d0c1d9
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 15 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ before_install:
- sudo add-apt-repository ppa:likemartinma/devel -y
- sudo add-apt-repository --yes ppa:beineri/opt-qt57-trusty
- sudo apt-get update -qq
- sudo apt-get --force-yes install -qq qt57-meta-full
- sudo apt-get --force-yes install -qq libsqlite3-dev libsqlcipher-dev libantlr-dev
- sudo apt-get --force-yes install qt57-meta-full
- sudo apt-get --force-yes install libsqlite3-dev libsqlcipher-dev libantlr-dev
- QT_ENV_SCRIPT=$(find /opt -name 'qt*-env.sh')
- source $QT_ENV_SCRIPT

Expand Down
7 changes: 5 additions & 2 deletions src/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -585,8 +585,11 @@ void MainWindow::deleteRecord()

int old_row = ui->dataTable->currentIndex().row();
while(ui->dataTable->selectionModel()->hasSelection())
{
if(!m_browseTableModel->removeRow(ui->dataTable->selectionModel()->selectedIndexes().first().row()))
{
int first_selected_row = ui->dataTable->selectionModel()->selectedIndexes().first().row();
int last_selected_row = ui->dataTable->selectionModel()->selectedIndexes().last().row();
int selected_rows_count = last_selected_row - first_selected_row + 1;
if(!m_browseTableModel->removeRows(first_selected_row, selected_rows_count))
{
QMessageBox::warning(this, QApplication::applicationName(), tr("Error deleting record:\n%1").arg(db.lastErrorMessage));
break;
Expand Down
11 changes: 8 additions & 3 deletions src/sqlitedb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -865,15 +865,20 @@ QString DBBrowserDB::addRecord(const QString& sTableName)
}
}

bool DBBrowserDB::deleteRecord(const QString& table, const QString& rowid)
bool DBBrowserDB::deleteRecords(const QString& table, const QStringList& rowids)
{
if (!isOpen()) return false;
bool ok = false;

QString statement = QString("DELETE FROM %1 WHERE %2='%3';")
QStringList quoted_rowids;
foreach(QString rowid, rowids)
{
quoted_rowids.append("'" + rowid + "'");
}
QString statement = QString("DELETE FROM %1 WHERE %2 IN (%3);")
.arg(sqlb::escapeIdentifier(table))
.arg(sqlb::escapeIdentifier(getObjectByName(table).table.rowidColumn()))
.arg(rowid);
.arg(quoted_rowids.join(", "));
if(executeSQL(statement))
ok = true;
else
Expand Down
2 changes: 1 addition & 1 deletion src/sqlitedb.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class DBBrowserDB : public QObject
* @return An sqlite conform INSERT INTO statement with empty values. (NULL,'',0)
*/
QString emptyInsertStmt(const sqlb::Table& t, const QString& pk_value = QString()) const;
bool deleteRecord(const QString& table, const QString& rowid);
bool deleteRecords(const QString& table, const QStringList& rowids);
bool updateRecord(const QString& table, const QString& column, const QString& rowid, const QByteArray& value, bool itsBlob);

bool createTable(const QString& name, const sqlb::FieldVector& structure);
Expand Down
15 changes: 8 additions & 7 deletions src/sqlitetablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,15 +410,16 @@ bool SqliteTableModel::removeRows(int row, int count, const QModelIndex& parent)

bool ok = true;

QStringList rowids;
for(int i=count-1;i>=0;i--)
{
if(m_db->deleteRecord(m_sTable, m_data.at(row + i).at(0)))
{
m_data.removeAt(row + i);
--m_rowCount;
} else {
ok = false;
}
rowids.append(m_data.at(row + i).at(0));
m_data.removeAt(row + i);
--m_rowCount;
}
if(!m_db->deleteRecords(m_sTable, rowids))
{
ok = false;
}

endRemoveRows();
Expand Down

0 comments on commit 5d0c1d9

Please sign in to comment.