diff options
author | Water-Team <water@pad.test.qt.nokia.com> | 2011-07-27 23:00:13 (GMT) |
---|---|---|
committer | Water-Team <water@pad.test.qt.nokia.com> | 2011-07-27 23:00:13 (GMT) |
commit | 836ecd799937294fb18cf2d47f155e4649a88077 (patch) | |
tree | c13f2a791bf26f4c1908c57d1554f3f73aba9092 | |
parent | a92f10160c94af56bdcbc1f40784a28273563b5c (diff) | |
parent | c9f0c578bbaf73211ad4729e1496e53867a641c9 (diff) | |
download | Qt-836ecd799937294fb18cf2d47f155e4649a88077.zip Qt-836ecd799937294fb18cf2d47f155e4649a88077.tar.gz Qt-836ecd799937294fb18cf2d47f155e4649a88077.tar.bz2 |
Merge branch '4.8-upstream' into master-water
-rw-r--r-- | src/sql/models/qsqlrelationaltablemodel.cpp | 36 | ||||
-rw-r--r-- | tests/auto/qsqlrelationaltablemodel/tst_qsqlrelationaltablemodel.cpp | 23 |
2 files changed, 57 insertions, 2 deletions
diff --git a/src/sql/models/qsqlrelationaltablemodel.cpp b/src/sql/models/qsqlrelationaltablemodel.cpp index f6c4018..5b0406f 100644 --- a/src/sql/models/qsqlrelationaltablemodel.cpp +++ b/src/sql/models/qsqlrelationaltablemodel.cpp @@ -119,6 +119,8 @@ QT_BEGIN_NAMESPACE returns false. */ +class QRelatedTableModel; + struct QRelation { public: @@ -135,7 +137,7 @@ struct QRelation bool isValid(); QSqlRelation rel; - QSqlTableModel *model; + QRelatedTableModel *model; QHash<QString, QVariant> dictionary;//maps keys to display values private: @@ -143,6 +145,15 @@ struct QRelation bool m_dictInitialized; }; +class QRelatedTableModel : public QSqlTableModel +{ +public: + QRelatedTableModel(QRelation *rel, QObject *parent = 0, QSqlDatabase db = QSqlDatabase()); + bool select(); +private: + bool firstSelect; + QRelation *relation; +}; /* A QRelation must be initialized before it is considered valid. Note: population of the model and dictionary are kept separate @@ -162,7 +173,7 @@ void QRelation::populateModel() Q_ASSERT(m_parent != NULL); if (!model) { - model = new QSqlTableModel(m_parent, m_parent->database()); + model = new QRelatedTableModel(this, m_parent, m_parent->database()); model->setTable(rel.tableName()); model->select(); } @@ -219,6 +230,27 @@ bool QRelation::isValid() return (rel.isValid() && m_parent != NULL); } + + +QRelatedTableModel::QRelatedTableModel(QRelation *rel, QObject *parent, QSqlDatabase db) : + QSqlTableModel(parent, db), firstSelect(true), relation(rel) +{ +} + +bool QRelatedTableModel::select() +{ + if (firstSelect) { + firstSelect = false; + return QSqlTableModel::select(); + } + relation->clearDictionary(); + bool res = QSqlTableModel::select(); + if (res) + relation->populateDictionary(); + return res; +} + + class QSqlRelationalTableModelPrivate: public QSqlTableModelPrivate { Q_DECLARE_PUBLIC(QSqlRelationalTableModel) diff --git a/tests/auto/qsqlrelationaltablemodel/tst_qsqlrelationaltablemodel.cpp b/tests/auto/qsqlrelationaltablemodel/tst_qsqlrelationaltablemodel.cpp index cc4ab67..5f1a621 100644 --- a/tests/auto/qsqlrelationaltablemodel/tst_qsqlrelationaltablemodel.cpp +++ b/tests/auto/qsqlrelationaltablemodel/tst_qsqlrelationaltablemodel.cpp @@ -92,6 +92,7 @@ private slots: void escapedTableName(); void whiteSpaceInIdentifiers(); void psqlSchemaTest(); + void selectAfterUpdate(); private: void dropTestTables( QSqlDatabase db ); @@ -1467,5 +1468,27 @@ void tst_QSqlRelationalTableModel::psqlSchemaTest() QVERIFY_SQL(model, select()); } +void tst_QSqlRelationalTableModel::selectAfterUpdate() +{ + QFETCH_GLOBAL(QString, dbName); + QSqlDatabase db = QSqlDatabase::database(dbName); + CHECK_DATABASE(db); + + QSqlRelationalTableModel model(0, db); + model.setTable(reltest1); + model.setRelation(2, QSqlRelation(reltest2, "tid", "title")); + QVERIFY_SQL(model, select()); + QVERIFY(model.relationModel(2)->rowCount() == 2); + { + QSqlQuery q(db); + QVERIFY_SQL(q, exec("insert into " + reltest2 + " values(3, 'mrs')")); + model.relationModel(2)->select(); + } + QVERIFY(model.relationModel(2)->rowCount() == 3); + QVERIFY(model.setData(model.index(0,2), 3)); + QVERIFY(model.submitAll()); + QCOMPARE(model.data(model.index(0,2)), QVariant("mrs")); +} + QTEST_MAIN(tst_QSqlRelationalTableModel) #include "tst_qsqlrelationaltablemodel.moc" |