From 60f24ec608fc784731a18b43fa59b82ad41f9478 Mon Sep 17 00:00:00 2001 From: Mark Brand Date: Tue, 5 Jul 2011 17:27:54 +0200 Subject: [PATCH] reduce cost of checking cache for data() and headerData() Assume requested row is usually not cached. Change-Id: I7727b431cc3841528aa6390400b93b1218773e65 Reviewed-by: Yunqiao Yin --- src/sql/models/qsqltablemodel.cpp | 49 +++++++++++++++++-------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/src/sql/models/qsqltablemodel.cpp b/src/sql/models/qsqltablemodel.cpp index 3e5beb33b7..ff32533440 100644 --- a/src/sql/models/qsqltablemodel.cpp +++ b/src/sql/models/qsqltablemodel.cpp @@ -382,26 +382,29 @@ QVariant QSqlTableModel::data(const QModelIndex &index, int role) const // and indexInQuery is not virtual (grrr) so any values we pass to QSQM need // to handle the insertedRows QModelIndex item = indexInQuery(index); - const QSqlTableModelPrivate::ModifiedRow row = d->cache.value(index.row()); - switch (d->strategy) { - case OnFieldChange: - case OnRowChange: - if (row.op == QSqlTableModelPrivate::Insert) { - if (item.column() < 0 || item.column() >= row.rec.count()) - return QVariant(); - return row.rec.value(item.column()); - } else if (row.op == QSqlTableModelPrivate::Update) { - if (row.rec.isGenerated(item.column())) + if (d->cache.contains(index.row())) { + const QSqlTableModelPrivate::ModifiedRow row = d->cache.value(index.row()); + + switch (d->strategy) { + case OnFieldChange: + case OnRowChange: + if (row.op == QSqlTableModelPrivate::Insert) { + if (item.column() < 0 || item.column() >= row.rec.count()) + return QVariant(); return row.rec.value(item.column()); + } else if (row.op == QSqlTableModelPrivate::Update) { + if (row.rec.isGenerated(item.column())) + return row.rec.value(item.column()); + } + break; + case OnManualSubmit: + if (row.op == QSqlTableModelPrivate::Insert + || (row.op != QSqlTableModelPrivate::None + && row.rec.isGenerated(item.column()))) + return row.rec.value(item.column()); + break; } - break; - case OnManualSubmit: - if (row.op == QSqlTableModelPrivate::Insert - || (row.op != QSqlTableModelPrivate::None - && row.rec.isGenerated(item.column()))) - return row.rec.value(item.column()); - break; } // We need to handle row mapping here, but not column mapping @@ -415,11 +418,13 @@ QVariant QSqlTableModel::headerData(int section, Qt::Orientation orientation, in { Q_D(const QSqlTableModel); if (orientation == Qt::Vertical && role == Qt::DisplayRole) { - const QSqlTableModelPrivate::Op op = d->cache.value(section).op; - if (op == QSqlTableModelPrivate::Insert) - return QLatin1String("*"); - else if (op == QSqlTableModelPrivate::Delete) - return QLatin1String("!"); + if (d->cache.contains(section)) { + const QSqlTableModelPrivate::Op op = d->cache.value(section).op; + if (op == QSqlTableModelPrivate::Insert) + return QLatin1String("*"); + else if (op == QSqlTableModelPrivate::Delete) + return QLatin1String("!"); + } } return QSqlQueryModel::headerData(section, orientation, role); }