Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 241372e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
17 changes: 14 additions & 3 deletions src/MainWindow.cpp
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion src/sqlitedb.cpp
Expand Up @@ -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);
Expand Down

0 comments on commit 241372e

Please sign in to comment.