From 76880aa2c43946103c4ad2babe45eb71e1ab4f74 Mon Sep 17 00:00:00 2001 From: Mark Brand Date: Wed, 11 Jul 2012 09:52:14 +0200 Subject: [PATCH] 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 --- dist/changes-5.0.0 | 5 +++-- src/sql/models/qsqltablemodel.cpp | 30 +++++++++++++++++++----------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/dist/changes-5.0.0 b/dist/changes-5.0.0 index 1dfbeaa..4388858 100644 --- a/dist/changes-5.0.0 +++ b/dist/changes-5.0.0 @@ -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 diff --git a/src/sql/models/qsqltablemodel.cpp b/src/sql/models/qsqltablemodel.cpp index ddec247..0416656 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 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)); -- 2.7.4