summaryrefslogtreecommitdiffstats
path: root/src/sql
diff options
context:
space:
mode:
authorMark Brand <mabrand@mabrand.nl>2012-02-08 00:16:46 +0100
committerQt by Nokia <qt-info@nokia.com>2012-02-09 10:45:38 +0100
commita58e630b61c19eac8f0c0d931825b42eeb7ea16b (patch)
tree04da05c0118ab44d125c723ec56b2a0adfe4e890 /src/sql
parent40afbf3deb4a0d8823a3307286a35bd046c44e01 (diff)
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>
Diffstat (limited to 'src/sql')
-rw-r--r--src/sql/models/qsqltablemodel.cpp34
1 files changed, 22 insertions, 12 deletions
diff --git a/src/sql/models/qsqltablemodel.cpp b/src/sql/models/qsqltablemodel.cpp
index dd4629662b..9e1cfe3051 100644
--- a/src/sql/models/qsqltablemodel.cpp
+++ b/src/sql/models/qsqltablemodel.cpp
@@ -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
setRecord() internally.
+ Only fields where the generated flag is true will be included.
+
Returns true if the record could be inserted, otherwise false.
\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
- record. Returns true if all the values could be set; otherwise
- returns false.
+ record for fields where generated flag is true.
+
+ Returns true if all the values could be set; otherwise returns
+ false.
\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))
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];
if (mrow.op() == QSqlTableModelPrivate::None)
mrow = QSqlTableModelPrivate::ModifiedRow(QSqlTableModelPrivate::Update,
d->rec,
d->primaryValues(indexInQuery(createIndex(row, 0)).row()));
- bool isOk = true;
- for (int i = 0; i < record.count(); ++i) {
- int idx = d->nameToIndex(record.fieldName(i));
- if (idx == -1)
- isOk = false;
- else
- mrow.setValue(idx, record.value(i));
- }
+ Map::const_iterator i = map.constBegin();
+ const Map::const_iterator e = map.constEnd();
+ for ( ; i != e; ++i)
+ mrow.setValue(i.value(), record.value(i.key()));
if (columnCount())
emit dataChanged(createIndex(row, 0), createIndex(row, columnCount() - 1));
if (d->strategy == OnFieldChange)
return submit();
- else if (d->strategy == OnManualSubmit)
- return isOk;
return true;
}