QSqlRelationalTableModelPrivate: simplify field name translation

It's not appropriate to use indexInQuery() here. First of all,
the row might be an inserted row, and thus not be in the query.
The intent was probably to get the column position in the query,
but this is certainly not row dependent. Furthermore, if there
are inserted or removed columns, these are managed within
QSqlQueryModel.

Change-Id: I89668655b263747a5b849136404112e911722b3d
Reviewed-by: Yunqiao Yin <charles.yin@nokia.com>
This commit is contained in:
Mark Brand 2012-02-13 09:26:21 +01:00 committed by Qt by Nokia
parent 7bced28780
commit 76fa96b99e

View File

@ -268,7 +268,7 @@ public:
void clearCache(); void clearCache();
void revertCachedRow(int row); void revertCachedRow(int row);
void translateFieldNames(int row, QSqlRecord &values) const; void translateFieldNames(QSqlRecord &values) const;
QSqlRelationalTableModel::JoinMode joinMode; QSqlRelationalTableModel::JoinMode joinMode;
}; };
@ -744,16 +744,13 @@ void QSqlRelationalTableModel::setTable(const QString &table)
/*! \internal /*! \internal
*/ */
void QSqlRelationalTableModelPrivate::translateFieldNames(int row, QSqlRecord &values) const void QSqlRelationalTableModelPrivate::translateFieldNames(QSqlRecord &values) const
{ {
Q_Q(const QSqlRelationalTableModel);
for (int i = 0; i < values.count(); ++i) { for (int i = 0; i < values.count(); ++i) {
int realCol = q->indexInQuery(q->createIndex(row, i)).column(); if (relations.value(i).isValid()) {
if (realCol != -1 && relations.value(realCol).isValid()) {
QVariant v = values.value(i); QVariant v = values.value(i);
bool gen = values.isGenerated(i); bool gen = values.isGenerated(i);
values.replace(i, baseRec.field(realCol)); values.replace(i, baseRec.field(i));
values.setValue(i, v); values.setValue(i, v);
values.setGenerated(i, gen); values.setGenerated(i, gen);
} }
@ -768,7 +765,7 @@ bool QSqlRelationalTableModel::updateRowInTable(int row, const QSqlRecord &value
Q_D(QSqlRelationalTableModel); Q_D(QSqlRelationalTableModel);
QSqlRecord rec = values; QSqlRecord rec = values;
d->translateFieldNames(row, rec); d->translateFieldNames(rec);
return QSqlTableModel::updateRowInTable(row, rec); return QSqlTableModel::updateRowInTable(row, rec);
} }
@ -781,7 +778,7 @@ bool QSqlRelationalTableModel::insertRowIntoTable(const QSqlRecord &values)
Q_D(QSqlRelationalTableModel); Q_D(QSqlRelationalTableModel);
QSqlRecord rec = values; QSqlRecord rec = values;
d->translateFieldNames(0, rec); d->translateFieldNames(rec);
return QSqlTableModel::insertRowIntoTable(rec); return QSqlTableModel::insertRowIntoTable(rec);
} }