From 241372e6eb33e6522e9a4a7dd4869df2fff066e3 Mon Sep 17 00:00:00 2001 From: mgrojo Date: Sun, 29 Jul 2018 18:18:19 +0200 Subject: [PATCH] Improvements for the "Unlock view editing" feature Provide a combo box for selecting the field from the list of fields in the view. Uncheck the menu option when the user cancels the action. Escape the primary key in the UPDATE statements since it is not always rowid (views, without rowid tables) and consequently the name might needed it. --- src/MainWindow.cpp | 17 ++++++++++++++--- src/sqlitedb.cpp | 2 +- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 6307bfea..1601ac2b 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -2923,19 +2923,30 @@ void MainWindow::unlockViewEditing(bool unlock, QString pk) enableEditing(true); return; } + sqlb::ViewPtr obj = db.getObjectByName(currentTable).dynamicCast(); // If the view gets unlocked for editing and we don't have a 'primary key' for this view yet, then ask for one if(unlock && pk.isEmpty()) { while(true) { + bool ok; + // Ask for a PK - pk = QInputDialog::getText(this, qApp->applicationName(), tr("Please enter a pseudo-primary key in order to enable editing on this view. " - "This should be the name of a unique column in the view.")); + pk = QInputDialog::getItem(this, + qApp->applicationName(), + tr("Please enter a pseudo-primary key in order to enable editing on this view. " + "This should be the name of a unique column in the view."), + obj->fieldNames(), + 0, + false, + &ok); // Cancelled? - if(pk.isEmpty()) + if(!ok || pk.isEmpty()) { + ui->actionUnlockViewEditing->setChecked(false); return; + } // Do some basic testing of the input and if the input appears to be good, go on if(db.executeSQL(QString("SELECT %1 FROM %2 LIMIT 1;").arg(sqlb::escapeIdentifier(pk)).arg(currentTable.toString()), false, true)) diff --git a/src/sqlitedb.cpp b/src/sqlitedb.cpp index 2dbe5112..92e5d269 100644 --- a/src/sqlitedb.cpp +++ b/src/sqlitedb.cpp @@ -1144,7 +1144,7 @@ bool DBBrowserDB::updateRecord(const sqlb::ObjectIdentifier& table, const QStrin QString sql = QString("UPDATE %1 SET %2=? WHERE %3='%4';") .arg(table.toString()) .arg(sqlb::escapeIdentifier(column)) - .arg(pk) + .arg(sqlb::escapeIdentifier(pk)) .arg(QString(rowid).replace("'", "''")); logSQL(sql, kLogMsg_App);