From 61f385abbafa690b68e627217d0454348ce8238b Mon Sep 17 00:00:00 2001 From: Martin Kleusberg Date: Wed, 21 Jun 2017 23:25:53 +0200 Subject: [PATCH] Don't crash when pasting data in a view When pasting data into a cell of a view the application would have crashed because it treats the view as a table (which it isn't). This is fixed by this. --- src/sqlitedb.cpp | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/sqlitedb.cpp b/src/sqlitedb.cpp index 47cd4a51..f58e225a 100644 --- a/src/sqlitedb.cpp +++ b/src/sqlitedb.cpp @@ -959,10 +959,28 @@ bool DBBrowserDB::updateRecord(const QString& table, const QString& column, cons { if (!isOpen()) return false; + // Get primary key of the object to edit. For views we support 'pseudo' primary keys which must be specified manually. + // If no pseudo pk is specified we'll take the rowid column of the table. If this isn't a table, however, we'll just assume + // it's a view that hasn't been configured for editing and thus abort here. + QString pk; + if(pseudo_pk.isEmpty()) + { + sqlb::TablePtr tbl = getObjectByName(table).dynamicCast(); + if(tbl) + { + pk = tbl->rowidColumn(); + } else { + lastErrorMessage = tr("Cannot set data on this object"); + return false; + } + } else { + pk = pseudo_pk; + } + QString sql = QString("UPDATE %1 SET %2=? WHERE %3='%4';") .arg(sqlb::escapeIdentifier(table)) .arg(sqlb::escapeIdentifier(column)) - .arg(sqlb::escapeIdentifier(pseudo_pk.isEmpty() ? getObjectByName(table).dynamicCast()->rowidColumn() : pseudo_pk)) + .arg(pk) .arg(rowid); logSQL(sql, kLogMsg_App);