summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMark Brand <mabrand@mabrand.nl>2012-09-17 14:22:29 +0200
committerThe Qt Project <gerrit-noreply@qt-project.org>2012-09-24 21:27:51 +0200
commit7389c096379d63d956a023835646af1179ac6ea4 (patch)
tree2032bebc9aa51b69fe0025e295c0168eb59be52b /src
parentcb2d87e8b862c99dcbd12c399da3ac5ec9c5dcad (diff)
QSqlTableModel::selectRow(): fix failure on uncached rows
This method was originally intended for refreshing rows after submitting changes. It should also work for refreshing rows that are unchanged (i.e., not cached), but did not because constructing the primary values depended on the cache. As a consequence, the WHERE clause for the query was not created. Fixed by deriving primary values for uncached rows from the query record. Note that the cache is still authoritative for rows it holds. This is important because the prmary values there may differ from the original query record due to changes to columns of the primary key. Includes new test. Change-Id: I41cca2cbf26019d4b495ffa6d876e2b55ec57803 Reviewed-by: Andy Shaw <andy.shaw@digia.com> Reviewed-by: Mark Brand <mabrand@mabrand.nl>
Diffstat (limited to 'src')
-rw-r--r--src/sql/models/qsqltablemodel.cpp20
-rw-r--r--src/sql/models/qsqltablemodel_p.h8
2 files changed, 21 insertions, 7 deletions
diff --git a/src/sql/models/qsqltablemodel.cpp b/src/sql/models/qsqltablemodel.cpp
index ce830ce740..fbd9207741 100644
--- a/src/sql/models/qsqltablemodel.cpp
+++ b/src/sql/models/qsqltablemodel.cpp
@@ -195,9 +195,27 @@ bool QSqlTableModelPrivate::exec(const QString &stmt, bool prepStatement,
return true;
}
+QSqlRecord QSqlTableModelPrivate::primaryValues(const QSqlRecord &rec, const QSqlRecord &pIndex)
+{
+ QSqlRecord pValues(pIndex);
+
+ for (int i = pValues.count() - 1; i >= 0; --i)
+ pValues.setValue(i, rec.value(pValues.fieldName(i)));
+
+ return pValues;
+}
+
QSqlRecord QSqlTableModelPrivate::primaryValues(int row) const
{
- return cache.value(row).primaryValues(primaryIndex.isEmpty() ? rec : primaryIndex);
+ Q_Q(const QSqlTableModel);
+
+ const QSqlRecord &pIndex = primaryIndex.isEmpty() ? rec : primaryIndex;
+
+ ModifiedRow mr = cache.value(row);
+ if (mr.op() != None)
+ return mr.primaryValues(pIndex);
+ else
+ return primaryValues(q->QSqlQueryModel::record(row), pIndex);
}
/*!
diff --git a/src/sql/models/qsqltablemodel_p.h b/src/sql/models/qsqltablemodel_p.h
index ccdc1243a5..462ff96093 100644
--- a/src/sql/models/qsqltablemodel_p.h
+++ b/src/sql/models/qsqltablemodel_p.h
@@ -70,6 +70,7 @@ public:
busyInsertingRows(false)
{}
void clear();
+ static QSqlRecord primaryValues(const QSqlRecord &rec, const QSqlRecord &pIndex);
QSqlRecord primaryValues(int index) const;
virtual void clearCache();
QSqlRecord record(const QVector<QVariant> &values) const;
@@ -167,12 +168,7 @@ public:
if (m_op == None || m_op == Insert)
return QSqlRecord();
- QSqlRecord values(pi);
-
- for (int i = values.count() - 1; i >= 0; --i)
- values.setValue(i, m_db_values.value(values.fieldName(i)));
-
- return values;
+ return QSqlTableModelPrivate::primaryValues(m_db_values, pi);
}
private:
inline static void setGenerated(QSqlRecord& r, bool g)