Permalink
Browse files

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.
  • Loading branch information...
mgrojo committed Jul 29, 2018
1 parent 2701223 commit 241372e6eb33e6522e9a4a7dd4869df2fff066e3
Showing with 15 additions and 4 deletions.
  1. +14 −3 src/MainWindow.cpp
  2. +1 −1 src/sqlitedb.cpp
@@ -2923,19 +2923,30 @@ void MainWindow::unlockViewEditing(bool unlock, QString pk)
enableEditing(true);
return;
}
sqlb::ViewPtr obj = db.getObjectByName(currentTable).dynamicCast<sqlb::View>();

// 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))
@@ -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);

0 comments on commit 241372e

Please sign in to comment.