summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMark Brand <mabrand@mabrand.nl>2012-07-11 09:52:14 +0200
committerQt by Nokia <qt-info@nokia.com>2012-07-17 08:13:30 +0200
commit76880aa2c43946103c4ad2babe45eb71e1ab4f74 (patch)
tree393b844d4222eda39a075c02a1a51bdadae8d275 /src
parent87ff2c830d32bf26f8540689dd38fba59b18d688 (diff)
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>
Diffstat (limited to 'src')
-rw-r--r--src/sql/models/qsqltablemodel.cpp30
1 files changed, 19 insertions, 11 deletions
diff --git a/src/sql/models/qsqltablemodel.cpp b/src/sql/models/qsqltablemodel.cpp
index ddec24709f..0416656113 100644
--- a/src/sql/models/qsqltablemodel.cpp
+++ b/src/sql/models/qsqltablemodel.cpp
@@ -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));