reduce cost of checking cache for data() and headerData()

Assume requested row is usually not cached.

Change-Id: I7727b431cc3841528aa6390400b93b1218773e65
Reviewed-by: Yunqiao Yin <charles.yin@nokia.com>
This commit is contained in:
Mark Brand 2011-07-05 17:27:54 +02:00 committed by Qt by Nokia
parent e5c8927fe8
commit 60f24ec608

View File

@ -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 // and indexInQuery is not virtual (grrr) so any values we pass to QSQM need
// to handle the insertedRows // to handle the insertedRows
QModelIndex item = indexInQuery(index); QModelIndex item = indexInQuery(index);
const QSqlTableModelPrivate::ModifiedRow row = d->cache.value(index.row());
switch (d->strategy) { if (d->cache.contains(index.row())) {
case OnFieldChange: const QSqlTableModelPrivate::ModifiedRow row = d->cache.value(index.row());
case OnRowChange:
if (row.op == QSqlTableModelPrivate::Insert) { switch (d->strategy) {
if (item.column() < 0 || item.column() >= row.rec.count()) case OnFieldChange:
return QVariant(); case OnRowChange:
return row.rec.value(item.column()); if (row.op == QSqlTableModelPrivate::Insert) {
} else if (row.op == QSqlTableModelPrivate::Update) { if (item.column() < 0 || item.column() >= row.rec.count())
if (row.rec.isGenerated(item.column())) return QVariant();
return row.rec.value(item.column()); 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 // 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); Q_D(const QSqlTableModel);
if (orientation == Qt::Vertical && role == Qt::DisplayRole) { if (orientation == Qt::Vertical && role == Qt::DisplayRole) {
const QSqlTableModelPrivate::Op op = d->cache.value(section).op; if (d->cache.contains(section)) {
if (op == QSqlTableModelPrivate::Insert) const QSqlTableModelPrivate::Op op = d->cache.value(section).op;
return QLatin1String("*"); if (op == QSqlTableModelPrivate::Insert)
else if (op == QSqlTableModelPrivate::Delete) return QLatin1String("*");
return QLatin1String("!"); else if (op == QSqlTableModelPrivate::Delete)
return QLatin1String("!");
}
} }
return QSqlQueryModel::headerData(section, orientation, role); return QSqlQueryModel::headerData(section, orientation, role);
} }