QSqlTableModel::setRecord(): improve handling of field mapping

-Only use fields where generated flag is set to true.

-Require all fields to map correctly. If fields don't map, that is a
sign of a programming or user error.

Change-Id: Ie8474393005de6c9926b4e46985d62b194eafde2
Reviewed-by: Yunqiao Yin <charles.yin@nokia.com>
This commit is contained in:
Mark Brand 2012-02-08 00:16:46 +01:00 committed by Qt by Nokia
parent 40afbf3deb
commit a58e630b61
2 changed files with 27 additions and 12 deletions

5
dist/changes-5.0.0 vendored
View File

@ -350,6 +350,11 @@ model methods setData() or setRecord().
before doing anything. Previously, it would remove what it could and before doing anything. Previously, it would remove what it could and
ignore the rest of the range. ignore the rest of the range.
* setRecord() and insertRecord()
-Only use fields where generated flag is set to true. This is
is consistent with the meaning of the flag.
-Require all fields to map correctly. Previously fields that didn't
map were simply ignored.
**************************************************************************** ****************************************************************************
* Database Drivers * * Database Drivers *

View File

@ -1066,6 +1066,8 @@ bool QSqlTableModel::insertRows(int row, int count, const QModelIndex &parent)
the record will be appended to the end. Calls insertRows() and the record will be appended to the end. Calls insertRows() and
setRecord() internally. setRecord() internally.
Only fields where the generated flag is true will be included.
Returns true if the record could be inserted, otherwise false. Returns true if the record could be inserted, otherwise false.
\sa insertRows(), removeRows() \sa insertRows(), removeRows()
@ -1183,8 +1185,10 @@ Qt::ItemFlags QSqlTableModel::flags(const QModelIndex &index) const
/*! /*!
Sets the values at the specified \a row to the values of \a Sets the values at the specified \a row to the values of \a
record. Returns true if all the values could be set; otherwise record for fields where generated flag is true.
returns false.
Returns true if all the values could be set; otherwise returns
false.
\sa record() \sa record()
*/ */
@ -1203,28 +1207,34 @@ bool QSqlTableModel::setRecord(int row, const QSqlRecord &record)
else if (d->strategy == OnRowChange && !d->cache.isEmpty() && !d->cache.contains(row)) else if (d->strategy == OnRowChange && !d->cache.isEmpty() && !d->cache.contains(row))
submit(); submit();
// Check field names and remember mapping
typedef QMap<int, int> Map;
Map map;
for (int i = 0; i < record.count(); ++i) {
if (record.isGenerated(i)) {
int idx = d->nameToIndex(record.fieldName(i));
if (idx == -1)
return false;
map[i] = idx;
}
}
QSqlTableModelPrivate::ModifiedRow &mrow = d->cache[row]; QSqlTableModelPrivate::ModifiedRow &mrow = d->cache[row];
if (mrow.op() == QSqlTableModelPrivate::None) if (mrow.op() == QSqlTableModelPrivate::None)
mrow = QSqlTableModelPrivate::ModifiedRow(QSqlTableModelPrivate::Update, mrow = QSqlTableModelPrivate::ModifiedRow(QSqlTableModelPrivate::Update,
d->rec, d->rec,
d->primaryValues(indexInQuery(createIndex(row, 0)).row())); d->primaryValues(indexInQuery(createIndex(row, 0)).row()));
bool isOk = true; Map::const_iterator i = map.constBegin();
for (int i = 0; i < record.count(); ++i) { const Map::const_iterator e = map.constEnd();
int idx = d->nameToIndex(record.fieldName(i)); for ( ; i != e; ++i)
if (idx == -1) mrow.setValue(i.value(), record.value(i.key()));
isOk = false;
else
mrow.setValue(idx, record.value(i));
}
if (columnCount()) if (columnCount())
emit dataChanged(createIndex(row, 0), createIndex(row, columnCount() - 1)); emit dataChanged(createIndex(row, 0), createIndex(row, columnCount() - 1));
if (d->strategy == OnFieldChange) if (d->strategy == OnFieldChange)
return submit(); return submit();
else if (d->strategy == OnManualSubmit)
return isOk;
return true; return true;
} }