QSqlTM: use generated flag more correctly in setRecord()/insertRecord()

The generated flag should affect the generation of SQL commands rather
than how the fields of the source record are applied to the model before
submitting. This correction allows setRecord() to be used to change TRUE
generated flags to FALSE.

Clarified documentation on this point and updated change log.

Change-Id: I7ee124930822561ed8beee6c6259970b3e929c9b
Reviewed-by: Michael Goddard <michael.goddard@nokia.com>
This commit is contained in:
Mark Brand 2012-07-11 09:52:14 +02:00 committed by Qt by Nokia
parent 87ff2c830d
commit 76880aa2c4
2 changed files with 22 additions and 13 deletions

5
dist/changes-5.0.0 vendored
View File

@ -513,8 +513,9 @@ ignore the rest of the range.
removed and only if there are no other changed rows.
* setRecord() and insertRecord()
-Only use fields where generated flag is set to true. This is
is consistent with the meaning of the flag.
-The generated flags from the source record are preserved in the model
and determine which fields are included when changes are applied to
the database.
-Require all fields to map correctly. Previously fields that didn't
map were simply ignored.
-For OnManualSubmit, insertRecord() no longer leaves behind an empty

View File

@ -1120,8 +1120,6 @@ bool QSqlTableModel::insertRows(int row, int count, const QModelIndex &parent)
the record will be appended to the end. Calls insertRows() and
setRecord() internally.
Only fields where the generated flag is true will be included.
Returns true if the record could be inserted, otherwise false.
Changes are submitted immediately for OnFieldChange and
@ -1253,8 +1251,16 @@ Qt::ItemFlags QSqlTableModel::flags(const QModelIndex &index) const
}
/*!
Sets the values at the specified \a row to the values of \a
record for fields where generated flag is true.
Applies \a values to the \a row in the model. The source and
target fields are mapped by field name, not by position in
the record.
Note that the generated flags in \a values are preserved
and determine whether the corresponding fields are used when
changes are submitted to the database. The caller should
remember to set the generated flag to FALSE for fields
where the database is meant to supply the value, such as an
automatically incremented ID.
For edit strategies OnFieldChange and OnRowChange, a row may
receive a change only if no other row has a cached change.
@ -1286,12 +1292,10 @@ bool QSqlTableModel::setRecord(int row, const QSqlRecord &values)
typedef QMap<int, int> Map;
Map map;
for (int i = 0; i < values.count(); ++i) {
if (values.isGenerated(i)) {
int idx = d->nameToIndex(values.fieldName(i));
if (idx == -1)
return false;
map[i] = idx;
}
int idx = d->nameToIndex(values.fieldName(i));
if (idx == -1)
return false;
map[i] = idx;
}
QSqlTableModelPrivate::ModifiedRow &mrow = d->cache[row];
@ -1301,8 +1305,12 @@ bool QSqlTableModel::setRecord(int row, const QSqlRecord &values)
Map::const_iterator i = map.constBegin();
const Map::const_iterator e = map.constEnd();
for ( ; i != e; ++i)
for ( ; i != e; ++i) {
mrow.setValue(i.value(), values.value(i.key()));
// mrow.setValue() sets generated to TRUE, but source record should prevail.
if (!values.isGenerated(i.key()))
mrow.recRef().setGenerated(i.value(), false);
}
if (columnCount())
emit dataChanged(createIndex(row, 0), createIndex(row, columnCount() - 1));