QSqlTableModel: mirror database values in change cache

Between submitting and the next select, these values will be more
up-to-date than those that could be obtained from the query.

This will be useful for constructing primary values and reverting
changes made after submitting.

Change-Id: I8317617f3e7043ad0b79b333731c55fb88aef171
Reviewed-by: Honglei Zhang <honglei.zhang@nokia.com>
This commit is contained in:
Mark Brand 2012-02-14 22:50:40 +01:00 committed by Qt by Nokia
parent 50f6cf6e48
commit 463bd32fe5
2 changed files with 11 additions and 7 deletions

View File

@ -510,7 +510,7 @@ bool QSqlTableModel::setData(const QModelIndex &index, const QVariant &value, in
if (row.op() == QSqlTableModelPrivate::None)
row = QSqlTableModelPrivate::ModifiedRow(QSqlTableModelPrivate::Update,
d->rec);
record(index.row()));
row.setValue(index.column(), value);
emit dataChanged(index, index);
@ -1004,7 +1004,7 @@ bool QSqlTableModel::removeRows(int row, int count, const QModelIndex &parent)
// so fake this by adjusting row
--row;
} else {
d->cache[idx] = QSqlTableModelPrivate::ModifiedRow(QSqlTableModelPrivate::Delete);
d->cache[idx] = QSqlTableModelPrivate::ModifiedRow(QSqlTableModelPrivate::Delete, record(idx));
if (d->strategy == OnManualSubmit)
emit headerDataChanged(Qt::Vertical, idx, idx);
}
@ -1235,7 +1235,7 @@ bool QSqlTableModel::setRecord(int row, const QSqlRecord &values)
QSqlTableModelPrivate::ModifiedRow &mrow = d->cache[row];
if (mrow.op() == QSqlTableModelPrivate::None)
mrow = QSqlTableModelPrivate::ModifiedRow(QSqlTableModelPrivate::Update,
d->rec);
record(row));
Map::const_iterator i = map.constBegin();
const Map::const_iterator e = map.constEnd();

View File

@ -102,10 +102,7 @@ public:
public:
inline ModifiedRow(Op o = None, const QSqlRecord &r = QSqlRecord())
: m_op(o), m_rec(r), m_submitted(false)
{
for (int i = m_rec.count() - 1; i >= 0; --i)
m_rec.setGenerated(i, false);
}
{ init_rec(); }
inline Op op() const { return m_op; }
inline QSqlRecord rec() const { return m_rec; }
inline QSqlRecord& recRef() { return m_rec; }
@ -117,8 +114,15 @@ public:
inline bool submitted() const { return m_submitted; }
inline void setSubmitted() { m_submitted = true; }
private:
void init_rec()
{
for (int i = m_rec.count() - 1; i >= 0; --i)
m_rec.setGenerated(i, false);
m_db_values = m_rec;
}
Op m_op;
QSqlRecord m_rec;
QSqlRecord m_db_values;
bool m_submitted;
};