QSqlTableModel: derive primary values from database values

Primary values are used to map a row in the model to a row in the
database table. It is critically important between submitting a
change and the following select (which refreshes the query) to have
updated primary values. Otherwise, if the change affected the primary
values, additional changes before select will misbehave.

Change-Id: I5d08dd70ac5d3f06cd9d3186a439f4c80a037c2d
Reviewed-by: Yunqiao Yin <charles.yin@nokia.com>
This commit is contained in:
Mark Brand 2012-02-14 23:45:44 +01:00 committed by Qt by Nokia
parent 463bd32fe5
commit 698e620aad
2 changed files with 13 additions and 10 deletions

View File

@ -201,16 +201,7 @@ bool QSqlTableModelPrivate::exec(const QString &stmt, bool prepStatement,
QSqlRecord QSqlTableModelPrivate::primaryValues(int row) const
{
Q_Q(const QSqlTableModel);
if (cache.value(row).op() == Insert)
return QSqlRecord();
QSqlRecord values(primaryIndex.isEmpty() ? rec : primaryIndex);
for (int i = 0; i < values.count(); ++i)
values.setValue(i, q->QSqlQueryModel::data(createIndex(row, rec.indexOf(values.fieldName(i))), Qt::EditRole));
return values;
return cache.value(row).primaryValues(primaryIndex.isEmpty() ? rec : primaryIndex);
}
/*!

View File

@ -113,6 +113,18 @@ public:
}
inline bool submitted() const { return m_submitted; }
inline void setSubmitted() { m_submitted = true; }
inline QSqlRecord primaryValues(const QSqlRecord& pi) const
{
if (m_op == None || m_op == Insert)
return QSqlRecord();
QSqlRecord values(pi);
for (int i = values.count() - 1; i >= 0; --i)
values.setValue(i, m_db_values.value(values.fieldName(i)));
return values;
}
private:
void init_rec()
{