2011-04-27 10:05:43 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2016-01-15 12:36:27 +00:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** This file is part of the test suite of the Qt Toolkit.
|
|
|
|
**
|
2016-01-15 12:36:27 +00:00
|
|
|
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
|
2012-09-19 12:28:29 +00:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2015-01-28 08:44:43 +00:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
2016-01-15 12:36:27 +00:00
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2012-09-19 12:28:29 +00:00
|
|
|
**
|
2016-01-15 12:36:27 +00:00
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
#include <QtTest/QtTest>
|
|
|
|
#include <QtSql/QtSql>
|
|
|
|
|
2011-09-02 10:08:47 +00:00
|
|
|
#include "../../kernel/qsqldatabase/tst_databases.h"
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2013-03-21 10:23:31 +00:00
|
|
|
const QString reltest1(qTableName("reltest1", __FILE__, QSqlDatabase())),
|
|
|
|
reltest2(qTableName("reltest2", __FILE__, QSqlDatabase())),
|
|
|
|
reltest3(qTableName("reltest3", __FILE__, QSqlDatabase())),
|
|
|
|
reltest4(qTableName("reltest4", __FILE__, QSqlDatabase())),
|
|
|
|
reltest5(qTableName("reltest5", __FILE__, QSqlDatabase()));
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
class tst_QSqlRelationalTableModel : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
void recreateTestTables(QSqlDatabase);
|
|
|
|
|
|
|
|
tst_Databases dbs;
|
|
|
|
|
|
|
|
public slots:
|
|
|
|
void initTestCase_data();
|
|
|
|
void initTestCase();
|
|
|
|
void cleanupTestCase();
|
|
|
|
void init();
|
|
|
|
void cleanup();
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
void data();
|
|
|
|
void setData();
|
|
|
|
void multipleRelation();
|
|
|
|
void insertRecord();
|
|
|
|
void setRecord();
|
|
|
|
void insertWithStrategies();
|
|
|
|
void removeColumn();
|
|
|
|
void filter();
|
|
|
|
void sort();
|
|
|
|
void revert();
|
|
|
|
|
|
|
|
void clearDisplayValuesCache();
|
|
|
|
void insertRecordDuplicateFieldNames();
|
|
|
|
void invalidData();
|
|
|
|
void relationModel();
|
|
|
|
void casing();
|
|
|
|
void escapedRelations();
|
|
|
|
void escapedTableName();
|
|
|
|
void whiteSpaceInIdentifiers();
|
|
|
|
void psqlSchemaTest();
|
2011-06-03 06:17:40 +00:00
|
|
|
void selectAfterUpdate();
|
2011-12-01 07:57:40 +00:00
|
|
|
void relationOnFirstColumn();
|
2017-12-01 15:00:06 +00:00
|
|
|
void setRelation();
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void dropTestTables( QSqlDatabase db );
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void tst_QSqlRelationalTableModel::initTestCase_data()
|
|
|
|
{
|
2014-04-02 12:08:29 +00:00
|
|
|
QVERIFY(dbs.open());
|
2011-10-07 01:42:28 +00:00
|
|
|
if (dbs.fillTestTable() == 0)
|
2011-10-19 02:53:13 +00:00
|
|
|
QSKIP("No database drivers are available in this Qt configuration");
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QSqlRelationalTableModel::recreateTestTables(QSqlDatabase db)
|
|
|
|
{
|
|
|
|
dropTestTables(db);
|
|
|
|
|
|
|
|
QSqlQuery q(db);
|
|
|
|
QVERIFY_SQL( q, exec("create table " + reltest1 +
|
|
|
|
" (id int not null primary key, name varchar(20), title_key int, another_title_key int)"));
|
|
|
|
QVERIFY_SQL( q, exec("insert into " + reltest1 + " values(1, 'harry', 1, 2)"));
|
|
|
|
QVERIFY_SQL( q, exec("insert into " + reltest1 + " values(2, 'trond', 2, 1)"));
|
|
|
|
QVERIFY_SQL( q, exec("insert into " + reltest1 + " values(3, 'vohi', 1, 2)"));
|
|
|
|
QVERIFY_SQL( q, exec("insert into " + reltest1 + " values(4, 'boris', 2, 2)"));
|
2011-07-19 04:57:07 +00:00
|
|
|
QVERIFY_SQL( q, exec("insert into " + reltest1 + " values(5, 'nat', NULL, NULL)"));
|
|
|
|
QVERIFY_SQL( q, exec("insert into " + reltest1 + " values(6, 'ale', NULL, 2)"));
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2016-02-16 06:19:03 +00:00
|
|
|
QVERIFY_SQL( q, exec("create table " + reltest2 + " (id int not null primary key, title varchar(20))"));
|
2011-04-27 10:05:43 +00:00
|
|
|
QVERIFY_SQL( q, exec("insert into " + reltest2 + " values(1, 'herr')"));
|
|
|
|
QVERIFY_SQL( q, exec("insert into " + reltest2 + " values(2, 'mister')"));
|
|
|
|
|
|
|
|
QVERIFY_SQL( q, exec("create table " + reltest3 + " (id int not null primary key, name varchar(20), city_key int)"));
|
|
|
|
QVERIFY_SQL( q, exec("insert into " + reltest3 + " values(1, 'Gustav', 1)"));
|
|
|
|
QVERIFY_SQL( q, exec("insert into " + reltest3 + " values(2, 'Heidi', 2)"));
|
|
|
|
|
|
|
|
QVERIFY_SQL( q, exec("create table " + reltest4 + " (id int not null primary key, name varchar(20))"));
|
|
|
|
QVERIFY_SQL( q, exec("insert into " + reltest4 + " values(1, 'Oslo')"));
|
|
|
|
QVERIFY_SQL( q, exec("insert into " + reltest4 + " values(2, 'Trondheim')"));
|
|
|
|
|
|
|
|
QVERIFY_SQL( q, exec("create table " + reltest5 + " (title varchar(20) not null primary key, abbrev varchar(20))"));
|
|
|
|
QVERIFY_SQL( q, exec("insert into " + reltest5 + " values('herr', 'Hr')"));
|
|
|
|
QVERIFY_SQL( q, exec("insert into " + reltest5 + " values('mister', 'Mr')"));
|
|
|
|
|
|
|
|
if (testWhiteSpaceNames(db.driverName())) {
|
2013-03-21 10:23:31 +00:00
|
|
|
QString reltest6 = db.driver()->escapeIdentifier(qTableName("rel", __FILE__, db) + " test6", QSqlDriver::TableName);
|
2011-04-27 10:05:43 +00:00
|
|
|
QVERIFY_SQL( q, exec("create table " + reltest6 + " (id int not null primary key, " + db.driver()->escapeIdentifier("city key", QSqlDriver::FieldName) +
|
|
|
|
" int, " + db.driver()->escapeIdentifier("extra field", QSqlDriver::FieldName) + " int)"));
|
|
|
|
QVERIFY_SQL( q, exec("insert into " + reltest6 + " values(1, 1,9)"));
|
|
|
|
QVERIFY_SQL( q, exec("insert into " + reltest6 + " values(2, 2,8)"));
|
|
|
|
|
2013-03-21 10:23:31 +00:00
|
|
|
QString reltest7 = db.driver()->escapeIdentifier(qTableName("rel", __FILE__, db) + " test7", QSqlDriver::TableName);
|
2011-04-27 10:05:43 +00:00
|
|
|
QVERIFY_SQL( q, exec("create table " + reltest7 + " (" + db.driver()->escapeIdentifier("city id", QSqlDriver::TableName) + " int not null primary key, " + db.driver()->escapeIdentifier("city name", QSqlDriver::FieldName) + " varchar(20))"));
|
|
|
|
QVERIFY_SQL( q, exec("insert into " + reltest7 + " values(1, 'New York')"));
|
|
|
|
QVERIFY_SQL( q, exec("insert into " + reltest7 + " values(2, 'Washington')"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QSqlRelationalTableModel::initTestCase()
|
|
|
|
{
|
|
|
|
foreach (const QString &dbname, dbs.dbNames) {
|
|
|
|
QSqlDatabase db=QSqlDatabase::database(dbname);
|
2014-11-20 11:55:56 +00:00
|
|
|
QSqlDriver::DbmsType dbType = tst_Databases::getDatabaseType(db);
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType == QSqlDriver::Interbase) {
|
2011-04-27 10:05:43 +00:00
|
|
|
db.exec("SET DIALECT 3");
|
2014-09-16 14:03:14 +00:00
|
|
|
} else if (dbType == QSqlDriver::MSSqlServer) {
|
2011-04-27 10:05:43 +00:00
|
|
|
db.exec("SET ANSI_DEFAULTS ON");
|
|
|
|
db.exec("SET IMPLICIT_TRANSACTIONS OFF");
|
2014-09-16 14:03:14 +00:00
|
|
|
} else if (dbType == QSqlDriver::PostgreSQL) {
|
2011-04-27 10:05:43 +00:00
|
|
|
db.exec("set client_min_messages='warning'");
|
2013-04-04 06:45:50 +00:00
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
recreateTestTables(db);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QSqlRelationalTableModel::cleanupTestCase()
|
|
|
|
{
|
|
|
|
foreach (const QString &dbName, dbs.dbNames) {
|
|
|
|
QSqlDatabase db = QSqlDatabase::database(dbName);
|
|
|
|
CHECK_DATABASE( db );
|
|
|
|
dropTestTables( QSqlDatabase::database(dbName) );
|
|
|
|
}
|
|
|
|
dbs.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QSqlRelationalTableModel::dropTestTables( QSqlDatabase db )
|
|
|
|
{
|
|
|
|
QStringList tableNames;
|
|
|
|
tableNames << reltest1
|
|
|
|
<< reltest2
|
|
|
|
<< reltest3
|
|
|
|
<< reltest4
|
|
|
|
<< reltest5
|
2013-03-21 10:23:31 +00:00
|
|
|
<< (qTableName("rel", __FILE__, db) + " test6")
|
|
|
|
<< (qTableName( "rel", __FILE__, db) + " test7")
|
|
|
|
<< qTableName("CASETEST1", db)
|
|
|
|
<< qTableName("casetest1", db);
|
2011-04-27 10:05:43 +00:00
|
|
|
tst_Databases::safeDropTables( db, tableNames );
|
|
|
|
|
2013-03-21 10:23:31 +00:00
|
|
|
db.exec("DROP SCHEMA " + qTableName("QTBUG_5373", __FILE__, db) + " CASCADE");
|
|
|
|
db.exec("DROP SCHEMA " + qTableName("QTBUG_5373_s2", __FILE__, db) + " CASCADE");
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QSqlRelationalTableModel::init()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QSqlRelationalTableModel::cleanup()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QSqlRelationalTableModel::data()
|
|
|
|
{
|
|
|
|
QFETCH_GLOBAL(QString, dbName);
|
|
|
|
QSqlDatabase db = QSqlDatabase::database(dbName);
|
|
|
|
CHECK_DATABASE(db);
|
|
|
|
|
|
|
|
QSqlRelationalTableModel model(0, db);
|
|
|
|
|
|
|
|
model.setTable(reltest1);
|
2016-02-16 06:19:03 +00:00
|
|
|
model.setRelation(2, QSqlRelation(reltest2, "id", "title"));
|
2011-04-27 10:05:43 +00:00
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
|
|
|
QCOMPARE(model.columnCount(), 4);
|
|
|
|
QCOMPARE(model.data(model.index(0, 0)).toInt(), 1);
|
|
|
|
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry"));
|
|
|
|
QCOMPARE(model.data(model.index(0, 2)).toString(), QString("herr"));
|
|
|
|
|
2011-07-04 08:37:56 +00:00
|
|
|
//try a non-existent index
|
2011-04-27 10:05:43 +00:00
|
|
|
QVERIFY2(model.data(model.index(0,4)).isValid() == false,"Invalid index returned valid QVariant");
|
|
|
|
|
2011-07-19 04:57:07 +00:00
|
|
|
// check row with null relation: they are reported only in LeftJoin mode
|
|
|
|
QCOMPARE(model.rowCount(), 4);
|
|
|
|
|
|
|
|
model.setJoinMode(QSqlRelationalTableModel::LeftJoin);
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(4, 0)).toInt(), 5);
|
|
|
|
QCOMPARE(model.data(model.index(4, 1)).toString(), QString("nat"));
|
|
|
|
QVERIFY2(model.data(model.index(4, 2)).isValid() == true, "NULL relation reported with invalid QVariant");
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
//check data retrieval when relational key is a non-integer type
|
|
|
|
//in this case a string
|
|
|
|
QSqlRelationalTableModel model2(0,db);
|
|
|
|
model2.setTable(reltest2);
|
|
|
|
model2.setRelation(1, QSqlRelation(reltest5,"title","abbrev"));
|
|
|
|
QVERIFY_SQL(model2, select());
|
|
|
|
|
|
|
|
QCOMPARE(model2.data(model2.index(0, 1)).toString(), QString("Hr"));
|
|
|
|
QCOMPARE(model2.data(model2.index(1, 1)).toString(), QString("Mr"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QSqlRelationalTableModel::setData()
|
|
|
|
{
|
|
|
|
QFETCH_GLOBAL(QString, dbName);
|
|
|
|
QSqlDatabase db = QSqlDatabase::database(dbName);
|
|
|
|
CHECK_DATABASE(db);
|
2014-11-20 11:55:56 +00:00
|
|
|
const QSqlDriver::DbmsType dbType = tst_Databases::getDatabaseType(db);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
// set the values using OnRowChange Strategy
|
|
|
|
{
|
|
|
|
QSqlRelationalTableModel model(0, db);
|
|
|
|
|
|
|
|
model.setTable(reltest1);
|
|
|
|
model.setSort(0, Qt::AscendingOrder);
|
2016-02-16 06:19:03 +00:00
|
|
|
model.setRelation(2, QSqlRelation(reltest2, "id", "title"));
|
2011-04-27 10:05:43 +00:00
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
|
|
|
QVERIFY(model.setData(model.index(0, 1), QString("harry2")));
|
|
|
|
QVERIFY(model.setData(model.index(0, 2), 2));
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry2"));
|
|
|
|
QCOMPARE(model.data(model.index(0, 2)).toString(), QString("mister"));
|
|
|
|
|
|
|
|
model.submit();
|
|
|
|
|
|
|
|
QVERIFY(model.setData(model.index(3,1), QString("boris2")));
|
|
|
|
QVERIFY(model.setData(model.index(3, 2), 1));
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(3,1)).toString(), QString("boris2"));
|
|
|
|
QCOMPARE(model.data(model.index(3, 2)).toString(), QString("herr"));
|
|
|
|
|
|
|
|
model.submit();
|
|
|
|
}
|
|
|
|
{ //verify values
|
|
|
|
QSqlRelationalTableModel model(0, db);
|
|
|
|
model.setTable(reltest1);
|
|
|
|
model.setSort(0, Qt::AscendingOrder);
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry2"));
|
|
|
|
QCOMPARE(model.data(model.index(0, 2)).toInt(), 2);
|
|
|
|
QCOMPARE(model.data(model.index(3, 1)).toString(), QString("boris2"));
|
|
|
|
QCOMPARE(model.data(model.index(3, 2)).toInt(), 1);
|
|
|
|
|
2016-02-16 06:19:03 +00:00
|
|
|
model.setRelation(2, QSqlRelation(reltest2, "id", "title"));
|
2011-04-27 10:05:43 +00:00
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
QCOMPARE(model.data(model.index(0, 2)).toString(), QString("mister"));
|
|
|
|
QCOMPARE(model.data(model.index(3,2)).toString(), QString("herr"));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//set the values using OnFieldChange strategy
|
|
|
|
{
|
|
|
|
QSqlRelationalTableModel model(0, db);
|
|
|
|
model.setTable(reltest1);
|
|
|
|
model.setEditStrategy(QSqlTableModel::OnFieldChange);
|
|
|
|
model.setSort(0, Qt::AscendingOrder);
|
2016-02-16 06:19:03 +00:00
|
|
|
model.setRelation(2, QSqlRelation(reltest2, "id", "title"));
|
2011-04-27 10:05:43 +00:00
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
|
|
|
QVERIFY(model.setData(model.index(1,1), QString("trond2")));
|
|
|
|
QVERIFY(model.setData(model.index(2,2), 2));
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(1,1)).toString(), QString("trond2"));
|
|
|
|
QCOMPARE(model.data(model.index(2,2)).toString(), QString("mister"));
|
|
|
|
}
|
|
|
|
{ //verify values
|
|
|
|
QSqlRelationalTableModel model(0, db);
|
|
|
|
model.setTable(reltest1);
|
|
|
|
model.setSort(0, Qt::AscendingOrder);
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(1, 1)).toString(), QString("trond2"));
|
|
|
|
QCOMPARE(model.data(model.index(2, 2)).toInt(), 2);
|
|
|
|
|
2016-02-16 06:19:03 +00:00
|
|
|
model.setRelation(2, QSqlRelation(reltest2, "id", "title"));
|
2011-04-27 10:05:43 +00:00
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
QCOMPARE(model.data(model.index(2, 2)).toString(), QString("mister"));
|
|
|
|
}
|
|
|
|
|
|
|
|
//set values using OnManualSubmit strategy
|
|
|
|
{
|
|
|
|
QSqlRelationalTableModel model(0, db);
|
|
|
|
|
|
|
|
model.setTable(reltest1);
|
2016-02-16 06:19:03 +00:00
|
|
|
model.setRelation(2, QSqlRelation(reltest2, "id", "title"));
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
//sybase doesn't allow tables with the same alias used twice as col names
|
|
|
|
//so don't set up an identical relation when using the tds driver
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType != QSqlDriver::Sybase)
|
2016-02-16 06:19:03 +00:00
|
|
|
model.setRelation(3, QSqlRelation(reltest2, "id", "title"));
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
model.setEditStrategy(QSqlTableModel::OnManualSubmit);
|
|
|
|
model.setSort(0, Qt::AscendingOrder);
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
|
|
|
QVERIFY(model.setData(model.index(2, 1), QString("vohi2")));
|
|
|
|
QVERIFY(model.setData(model.index(3, 2), 1));
|
|
|
|
QVERIFY(model.setData(model.index(0, 3), 1));
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(2, 1)).toString(), QString("vohi2"));
|
|
|
|
QCOMPARE(model.data(model.index(3, 2)).toString(), QString("herr"));
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType != QSqlDriver::Sybase)
|
2011-04-27 10:05:43 +00:00
|
|
|
QCOMPARE(model.data(model.index(0, 3)).toString(), QString("herr"));
|
|
|
|
else
|
|
|
|
QCOMPARE(model.data(model.index(0, 3)).toInt(), 1);
|
|
|
|
|
|
|
|
QVERIFY_SQL(model, submitAll());
|
|
|
|
}
|
|
|
|
{ //verify values
|
|
|
|
QSqlRelationalTableModel model(0, db);
|
|
|
|
model.setTable(reltest1);
|
|
|
|
model.setSort(0, Qt::AscendingOrder);
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(2, 1)).toString(), QString("vohi2"));
|
|
|
|
QCOMPARE(model.data(model.index(3, 2)).toInt(), 1);
|
|
|
|
QCOMPARE(model.data(model.index(0, 3)).toInt(), 1);
|
|
|
|
|
2016-02-16 06:19:03 +00:00
|
|
|
model.setRelation(2, QSqlRelation(reltest2, "id", "title"));
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType != QSqlDriver::Sybase)
|
2016-02-16 06:19:03 +00:00
|
|
|
model.setRelation(3, QSqlRelation(reltest2, "id", "title"));
|
2011-04-27 10:05:43 +00:00
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
QCOMPARE(model.data(model.index(3, 2)).toString(), QString("herr"));
|
|
|
|
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType != QSqlDriver::Sybase)
|
2011-04-27 10:05:43 +00:00
|
|
|
QCOMPARE(model.data(model.index(0, 3)).toString(), QString("herr"));
|
|
|
|
else
|
|
|
|
QCOMPARE(model.data(model.index(0, 3)).toInt(), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
//check setting of data when the relational key is a non-integer type
|
|
|
|
//in this case a string.
|
|
|
|
{
|
|
|
|
QSqlRelationalTableModel model(0, db);
|
|
|
|
|
|
|
|
model.setTable(reltest2);
|
|
|
|
model.setRelation(1, QSqlRelation(reltest5, "title", "abbrev"));
|
|
|
|
model.setEditStrategy(QSqlTableModel::OnManualSubmit);
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(0,1)).toString(), QString("Hr"));
|
|
|
|
QVERIFY(model.setData(model.index(0,1), QString("mister")));
|
|
|
|
QCOMPARE(model.data(model.index(0,1)).toString(), QString("Mr"));
|
|
|
|
QVERIFY_SQL(model, submitAll());
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(0,1)).toString(), QString("Mr"));
|
|
|
|
}
|
|
|
|
|
2011-07-19 04:57:07 +00:00
|
|
|
// Redo same tests, with a LeftJoin
|
|
|
|
{
|
|
|
|
QSqlRelationalTableModel model(0, db);
|
|
|
|
|
|
|
|
model.setTable(reltest2);
|
|
|
|
model.setRelation(1, QSqlRelation(reltest5, "title", "abbrev"));
|
|
|
|
model.setEditStrategy(QSqlTableModel::OnManualSubmit);
|
|
|
|
model.setJoinMode(QSqlRelationalTableModel::LeftJoin);
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(0,1)).toString(), QString("Mr"));
|
|
|
|
QVERIFY(model.setData(model.index(0,1), QString("herr")));
|
|
|
|
QCOMPARE(model.data(model.index(0,1)).toString(), QString("Hr"));
|
|
|
|
QVERIFY_SQL(model, submitAll());
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(0,1)).toString(), QString("Hr"));
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QSqlRelationalTableModel::multipleRelation()
|
|
|
|
{
|
|
|
|
QFETCH_GLOBAL(QString, dbName);
|
|
|
|
QSqlDatabase db = QSqlDatabase::database(dbName);
|
|
|
|
CHECK_DATABASE(db);
|
|
|
|
recreateTestTables(db);
|
|
|
|
|
|
|
|
QSqlRelationalTableModel model(0, db);
|
|
|
|
|
|
|
|
model.setTable(reltest1);
|
2016-02-16 06:19:03 +00:00
|
|
|
model.setRelation(2, QSqlRelation(reltest2, "id", "title"));
|
2011-04-27 10:05:43 +00:00
|
|
|
model.setRelation(3, QSqlRelation(reltest4, "id", "name"));
|
|
|
|
model.setSort(0, Qt::AscendingOrder);
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(2, 0)).toInt(), 3);
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(0, 0)).toInt(), 1);
|
|
|
|
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry"));
|
|
|
|
QCOMPARE(model.data(model.index(0, 2)).toString(), QString("herr"));
|
|
|
|
QCOMPARE(model.data(model.index(0, 3)).toString(), QString("Trondheim"));
|
2011-07-19 04:57:07 +00:00
|
|
|
|
|
|
|
// Redo same test in the LeftJoin mode
|
|
|
|
model.setTable(reltest1);
|
2016-02-16 06:19:03 +00:00
|
|
|
model.setRelation(2, QSqlRelation(reltest2, "id", "title"));
|
2011-07-19 04:57:07 +00:00
|
|
|
model.setRelation(3, QSqlRelation(reltest4, "id", "name"));
|
|
|
|
model.setSort(0, Qt::AscendingOrder);
|
|
|
|
model.setJoinMode(QSqlRelationalTableModel::LeftJoin);
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(2, 0)).toInt(), 3);
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(0, 0)).toInt(), 1);
|
|
|
|
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry"));
|
|
|
|
QCOMPARE(model.data(model.index(0, 2)).toString(), QString("herr"));
|
|
|
|
QCOMPARE(model.data(model.index(0, 3)).toString(), QString("Trondheim"));
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QSqlRelationalTableModel::insertRecord()
|
|
|
|
{
|
|
|
|
QFETCH_GLOBAL(QString, dbName);
|
|
|
|
QSqlDatabase db = QSqlDatabase::database(dbName);
|
|
|
|
CHECK_DATABASE(db);
|
|
|
|
|
|
|
|
QSqlRelationalTableModel model(0, db);
|
|
|
|
|
|
|
|
model.setTable(reltest1);
|
2016-02-16 06:19:03 +00:00
|
|
|
model.setRelation(2, QSqlRelation(reltest2, "id", "title"));
|
2011-04-27 10:05:43 +00:00
|
|
|
model.setSort(0, Qt::AscendingOrder);
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
|
|
|
QSqlRecord rec;
|
|
|
|
QSqlField f1("id", QVariant::Int);
|
|
|
|
QSqlField f2("name", QVariant::String);
|
|
|
|
QSqlField f3("title_key", QVariant::Int);
|
|
|
|
QSqlField f4("another_title_key", QVariant::Int);
|
|
|
|
|
2011-07-19 04:57:07 +00:00
|
|
|
f1.setValue(7);
|
2011-04-27 10:05:43 +00:00
|
|
|
f2.setValue("test");
|
|
|
|
f3.setValue(1);
|
|
|
|
f4.setValue(2);
|
|
|
|
|
|
|
|
f1.setGenerated(true);
|
|
|
|
f2.setGenerated(true);
|
|
|
|
f3.setGenerated(true);
|
|
|
|
f4.setGenerated(true);
|
|
|
|
|
|
|
|
rec.append(f1);
|
|
|
|
rec.append(f2);
|
|
|
|
rec.append(f3);
|
|
|
|
rec.append(f4);
|
|
|
|
|
|
|
|
QVERIFY_SQL(model, insertRecord(-1, rec));
|
|
|
|
|
2011-07-19 04:57:07 +00:00
|
|
|
QCOMPARE(model.data(model.index(4, 0)).toInt(), 7);
|
2011-04-27 10:05:43 +00:00
|
|
|
QCOMPARE(model.data(model.index(4, 1)).toString(), QString("test"));
|
|
|
|
QCOMPARE(model.data(model.index(4, 2)).toString(), QString("herr"));
|
2011-07-19 04:57:07 +00:00
|
|
|
|
|
|
|
// In LeftJoin mode, two additional rows are fetched
|
|
|
|
model.setJoinMode(QSqlRelationalTableModel::LeftJoin);
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(6, 0)).toInt(), 7);
|
|
|
|
QCOMPARE(model.data(model.index(6, 1)).toString(), QString("test"));
|
|
|
|
QCOMPARE(model.data(model.index(6, 2)).toString(), QString("herr"));
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QSqlRelationalTableModel::setRecord()
|
|
|
|
{
|
|
|
|
QFETCH_GLOBAL(QString, dbName);
|
|
|
|
QSqlDatabase db = QSqlDatabase::database(dbName);
|
|
|
|
CHECK_DATABASE(db);
|
|
|
|
recreateTestTables(db);
|
|
|
|
|
|
|
|
QSqlRelationalTableModel model(0, db);
|
|
|
|
|
|
|
|
model.setTable(reltest1);
|
2016-02-16 06:19:03 +00:00
|
|
|
model.setRelation(2, QSqlRelation(reltest2, "id", "title"));
|
2011-04-27 10:05:43 +00:00
|
|
|
model.setSort(0, Qt::AscendingOrder);
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
|
|
|
QSqlRecord rec;
|
|
|
|
QSqlField f1("id", QVariant::Int);
|
|
|
|
QSqlField f2("name", QVariant::String);
|
|
|
|
QSqlField f3("title_key", QVariant::Int);
|
|
|
|
QSqlField f4("another_title_key", QVariant::Int);
|
|
|
|
|
2011-07-19 04:57:07 +00:00
|
|
|
f1.setValue(7);
|
2011-04-27 10:05:43 +00:00
|
|
|
f2.setValue("tester");
|
|
|
|
f3.setValue(1);
|
|
|
|
f4.setValue(2);
|
|
|
|
|
|
|
|
f1.setGenerated(true);
|
|
|
|
f2.setGenerated(true);
|
|
|
|
f3.setGenerated(true);
|
|
|
|
f4.setGenerated(true);
|
|
|
|
|
|
|
|
rec.append(f1);
|
|
|
|
rec.append(f2);
|
|
|
|
rec.append(f3);
|
|
|
|
rec.append(f4);
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(1, 0)).toInt(), 2);
|
|
|
|
QCOMPARE(model.data(model.index(1, 1)).toString(), QString("trond"));
|
|
|
|
QCOMPARE(model.data(model.index(1, 2)).toString(), QString("mister"));
|
|
|
|
|
|
|
|
QVERIFY_SQL(model, setRecord(1, rec));
|
|
|
|
|
2011-07-19 04:57:07 +00:00
|
|
|
QCOMPARE(model.data(model.index(1, 0)).toInt(), 7);
|
2011-04-27 10:05:43 +00:00
|
|
|
QCOMPARE(model.data(model.index(1, 1)).toString(), QString("tester"));
|
|
|
|
QCOMPARE(model.data(model.index(1, 2)).toString(), QString("herr"));
|
|
|
|
|
|
|
|
model.setSort(0, Qt::AscendingOrder);
|
|
|
|
QVERIFY_SQL(model, submit());
|
|
|
|
|
2012-02-21 08:22:26 +00:00
|
|
|
if (model.editStrategy() != QSqlTableModel::OnManualSubmit) {
|
|
|
|
QCOMPARE(model.data(model.index(1, 0)).toInt(), 7);
|
|
|
|
QCOMPARE(model.data(model.index(1, 1)).toString(), QString("tester"));
|
|
|
|
QCOMPARE(model.data(model.index(1, 2)).toString(), QString("herr"));
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
}
|
|
|
|
|
2011-07-19 04:57:07 +00:00
|
|
|
QCOMPARE(model.data(model.index(3, 0)).toInt(), 7);
|
2011-04-27 10:05:43 +00:00
|
|
|
QCOMPARE(model.data(model.index(3, 1)).toString(), QString("tester"));
|
|
|
|
QCOMPARE(model.data(model.index(3, 2)).toString(), QString("herr"));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QSqlRelationalTableModel::insertWithStrategies()
|
|
|
|
{
|
|
|
|
QFETCH_GLOBAL(QString, dbName);
|
|
|
|
QSqlDatabase db = QSqlDatabase::database(dbName);
|
|
|
|
CHECK_DATABASE(db);
|
2014-11-20 11:55:56 +00:00
|
|
|
const QSqlDriver::DbmsType dbType = tst_Databases::getDatabaseType(db);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QSqlRelationalTableModel model(0, db);
|
|
|
|
|
|
|
|
model.setTable(reltest1);
|
2016-02-16 06:19:03 +00:00
|
|
|
model.setRelation(2, QSqlRelation(reltest2, "id", "title"));
|
2011-04-27 10:05:43 +00:00
|
|
|
model.setSort(0, Qt::AscendingOrder);
|
|
|
|
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType != QSqlDriver::Sybase)
|
2016-02-16 06:19:03 +00:00
|
|
|
model.setRelation(3, QSqlRelation(reltest2, "id", "title"));
|
2011-04-27 10:05:43 +00:00
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(0,0)).toInt(), 1);
|
|
|
|
QCOMPARE(model.data(model.index(0,1)).toString(), QString("harry"));
|
|
|
|
QCOMPARE(model.data(model.index(0,2)).toString(), QString("herr"));
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType != QSqlDriver::Sybase)
|
2011-04-27 10:05:43 +00:00
|
|
|
QCOMPARE(model.data(model.index(0,3)).toString(), QString("mister"));
|
|
|
|
else
|
|
|
|
QCOMPARE(model.data(model.index(0,3)).toInt(), 2);
|
|
|
|
|
|
|
|
model.insertRows(0, 1);
|
|
|
|
model.setData(model.index(0, 0), 1011);
|
|
|
|
model.setData(model.index(0, 1), "test");
|
|
|
|
model.setData(model.index(0, 2), 2);
|
|
|
|
model.setData(model.index(0, 3), 1);
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(0,0)).toInt(), 1011);
|
|
|
|
QCOMPARE(model.data(model.index(0,1)).toString(), QString("test"));
|
|
|
|
QCOMPARE(model.data(model.index(0,2)).toString(), QString("mister"));
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType != QSqlDriver::Sybase)
|
2011-04-27 10:05:43 +00:00
|
|
|
QCOMPARE(model.data(model.index(0,3)).toString(), QString("herr"));
|
|
|
|
else
|
|
|
|
QCOMPARE(model.data(model.index(0,3)).toInt(), 1);
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(1,0)).toInt(), 1);
|
|
|
|
QCOMPARE(model.data(model.index(1,1)).toString(), QString("harry"));
|
|
|
|
QCOMPARE(model.data(model.index(1,2)).toString(), QString("herr"));
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType != QSqlDriver::Sybase)
|
2011-04-27 10:05:43 +00:00
|
|
|
QCOMPARE(model.data(model.index(1,3)).toString(), QString("mister"));
|
|
|
|
else
|
|
|
|
QCOMPARE(model.data(model.index(1,3)).toInt(), 2);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QVERIFY_SQL(model, submitAll());
|
|
|
|
|
|
|
|
model.setEditStrategy(QSqlTableModel::OnManualSubmit);
|
2012-02-21 08:22:26 +00:00
|
|
|
// The changes were submitted, but there was no automatic select to resort
|
|
|
|
QVERIFY_SQL(model, select());
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(0,0)).toInt(), 1);
|
|
|
|
QCOMPARE(model.data(model.index(0,1)).toString(), QString("harry"));
|
|
|
|
QCOMPARE(model.data(model.index(0,2)).toString(), QString("herr"));
|
|
|
|
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType != QSqlDriver::Sybase) {
|
2011-04-27 10:05:43 +00:00
|
|
|
QCOMPARE(model.data(model.index(0,3)).toString(), QString("mister"));
|
|
|
|
model.setData(model.index(0,3),1);
|
|
|
|
QCOMPARE(model.data(model.index(0,3)).toString(), QString("herr"));
|
|
|
|
} else {
|
|
|
|
QCOMPARE(model.data(model.index(0,3)).toInt(), 2);
|
|
|
|
model.setData(model.index(0,3),1);
|
|
|
|
QCOMPARE(model.data(model.index(0,3)).toInt(), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
model.insertRows(0, 2);
|
|
|
|
model.setData(model.index(0, 0), 1012);
|
|
|
|
model.setData(model.index(0, 1), "george");
|
|
|
|
model.setData(model.index(0, 2), 2);
|
|
|
|
model.setData(model.index(0, 3), 2);
|
|
|
|
|
|
|
|
model.setData(model.index(1, 0), 1013);
|
|
|
|
model.setData(model.index(1, 1), "kramer");
|
|
|
|
model.setData(model.index(1, 2), 2);
|
|
|
|
model.setData(model.index(1, 3), 1);
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(0,0)).toInt(),1012);
|
|
|
|
QCOMPARE(model.data(model.index(0,1)).toString(), QString("george"));
|
|
|
|
QCOMPARE(model.data(model.index(0,2)).toString(), QString("mister"));
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType != QSqlDriver::Sybase)
|
2011-04-27 10:05:43 +00:00
|
|
|
QCOMPARE(model.data(model.index(0,3)).toString(), QString("mister"));
|
|
|
|
else
|
|
|
|
QCOMPARE(model.data(model.index(0,3)).toInt(), 2);
|
|
|
|
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(1,0)).toInt(),1013);
|
|
|
|
QCOMPARE(model.data(model.index(1,1)).toString(), QString("kramer"));
|
|
|
|
QCOMPARE(model.data(model.index(1,2)).toString(), QString("mister"));
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType != QSqlDriver::Sybase)
|
2011-04-27 10:05:43 +00:00
|
|
|
QCOMPARE(model.data(model.index(1,3)).toString(), QString("herr"));
|
|
|
|
else
|
|
|
|
QCOMPARE(model.data(model.index(1,3)).toInt(), 1);
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(2,0)).toInt(), 1);
|
|
|
|
QCOMPARE(model.data(model.index(2,1)).toString(), QString("harry"));
|
|
|
|
QCOMPARE(model.data(model.index(2,2)).toString(), QString("herr"));
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType != QSqlDriver::Sybase)
|
2011-04-27 10:05:43 +00:00
|
|
|
QCOMPARE(model.data(model.index(2,3)).toString(), QString("herr"));
|
|
|
|
else
|
|
|
|
QCOMPARE(model.data(model.index(2,3)).toInt(), 1);
|
|
|
|
|
|
|
|
QVERIFY_SQL(model, submitAll());
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QSqlRelationalTableModel::removeColumn()
|
|
|
|
{
|
|
|
|
QFETCH_GLOBAL(QString, dbName);
|
|
|
|
QSqlDatabase db = QSqlDatabase::database(dbName);
|
|
|
|
CHECK_DATABASE(db);
|
|
|
|
recreateTestTables(db);
|
|
|
|
|
|
|
|
QSqlRelationalTableModel model(0, db);
|
|
|
|
|
|
|
|
model.setTable(reltest1);
|
2016-02-16 06:19:03 +00:00
|
|
|
model.setRelation(2, QSqlRelation(reltest2, "id", "title"));
|
2011-04-27 10:05:43 +00:00
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
|
|
|
QVERIFY_SQL(model, removeColumn(3));
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
|
|
|
QCOMPARE(model.columnCount(), 3);
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(0, 0)).toInt(), 1);
|
|
|
|
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry"));
|
|
|
|
QCOMPARE(model.data(model.index(0, 2)).toString(), QString("herr"));
|
|
|
|
QCOMPARE(model.data(model.index(0, 3)), QVariant());
|
|
|
|
|
|
|
|
// try removing more than one column
|
|
|
|
QVERIFY_SQL(model, removeColumns(1, 2));
|
|
|
|
QCOMPARE(model.columnCount(), 1);
|
|
|
|
QCOMPARE(model.data(model.index(0, 0)).toInt(), 1);
|
|
|
|
QCOMPARE(model.data(model.index(0, 1)), QVariant());
|
|
|
|
|
2011-07-19 04:57:07 +00:00
|
|
|
// try in LeftJoin mode the same tests
|
|
|
|
CHECK_DATABASE(db);
|
|
|
|
recreateTestTables(db);
|
|
|
|
|
|
|
|
QSqlRelationalTableModel lmodel(0, db);
|
|
|
|
|
|
|
|
lmodel.setTable(reltest1);
|
2016-02-16 06:19:03 +00:00
|
|
|
lmodel.setRelation(2, QSqlRelation(reltest2, "id", "title"));
|
2011-07-19 04:57:07 +00:00
|
|
|
lmodel.setJoinMode(QSqlRelationalTableModel::LeftJoin);
|
|
|
|
QVERIFY_SQL(lmodel, select());
|
|
|
|
|
|
|
|
QVERIFY_SQL(lmodel, removeColumn(3));
|
|
|
|
QVERIFY_SQL(lmodel, select());
|
|
|
|
|
|
|
|
QCOMPARE(lmodel.columnCount(), 3);
|
|
|
|
|
|
|
|
QCOMPARE(lmodel.data(lmodel.index(0, 0)).toInt(), 1);
|
|
|
|
QCOMPARE(lmodel.data(lmodel.index(0, 1)).toString(), QString("harry"));
|
|
|
|
QCOMPARE(lmodel.data(lmodel.index(0, 2)).toString(), QString("herr"));
|
|
|
|
QCOMPARE(lmodel.data(lmodel.index(0, 3)), QVariant());
|
|
|
|
|
|
|
|
// try removing more than one column
|
|
|
|
QVERIFY_SQL(lmodel, removeColumns(1, 2));
|
|
|
|
QCOMPARE(lmodel.columnCount(), 1);
|
|
|
|
QCOMPARE(lmodel.data(lmodel.index(0, 0)).toInt(), 1);
|
|
|
|
QCOMPARE(lmodel.data(lmodel.index(0, 1)), QVariant());
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QSqlRelationalTableModel::filter()
|
|
|
|
{
|
|
|
|
QFETCH_GLOBAL(QString, dbName);
|
|
|
|
QSqlDatabase db = QSqlDatabase::database(dbName);
|
|
|
|
CHECK_DATABASE(db);
|
|
|
|
recreateTestTables(db);
|
|
|
|
|
|
|
|
QSqlRelationalTableModel model(0, db);
|
|
|
|
|
|
|
|
model.setTable(reltest1);
|
2016-02-16 06:19:03 +00:00
|
|
|
model.setRelation(2, QSqlRelation(reltest2, "id", "title"));
|
2011-04-27 10:05:43 +00:00
|
|
|
model.setFilter("title = 'herr'");
|
|
|
|
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
QCOMPARE(model.rowCount(), 2);
|
|
|
|
QCOMPARE(model.data(model.index(0, 2)).toString(), QString("herr"));
|
|
|
|
QCOMPARE(model.data(model.index(1, 2)).toString(), QString("herr"));
|
2011-07-19 04:57:07 +00:00
|
|
|
|
|
|
|
// Redo same filter test in LeftJoin mode
|
|
|
|
model.setJoinMode(QSqlRelationalTableModel::LeftJoin);
|
|
|
|
QVERIFY_SQL(model,select());
|
|
|
|
|
|
|
|
QCOMPARE(model.rowCount(), 2);
|
|
|
|
QCOMPARE(model.data(model.index(0, 2)).toString(), QString("herr"));
|
|
|
|
QCOMPARE(model.data(model.index(1, 2)).toString(), QString("herr"));
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QSqlRelationalTableModel::sort()
|
|
|
|
{
|
|
|
|
QFETCH_GLOBAL(QString, dbName);
|
|
|
|
QSqlDatabase db = QSqlDatabase::database(dbName);
|
|
|
|
CHECK_DATABASE(db);
|
2014-11-20 11:55:56 +00:00
|
|
|
const QSqlDriver::DbmsType dbType = tst_Databases::getDatabaseType(db);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QSqlRelationalTableModel model(0, db);
|
|
|
|
|
|
|
|
model.setTable(reltest1);
|
2016-02-16 06:19:03 +00:00
|
|
|
model.setRelation(2, QSqlRelation(reltest2, "id", "title"));
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType != QSqlDriver::Sybase)
|
2016-02-16 06:19:03 +00:00
|
|
|
model.setRelation(3, QSqlRelation(reltest2, "id", "title"));
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
model.setSort(2, Qt::DescendingOrder);
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
|
|
|
QCOMPARE(model.rowCount(), 4);
|
|
|
|
QCOMPARE(model.data(model.index(0, 2)).toString(), QString("mister"));
|
|
|
|
QCOMPARE(model.data(model.index(1, 2)).toString(), QString("mister"));
|
|
|
|
QCOMPARE(model.data(model.index(2, 2)).toString(), QString("herr"));
|
|
|
|
QCOMPARE(model.data(model.index(3, 2)).toString(), QString("herr"));
|
|
|
|
|
|
|
|
|
|
|
|
model.setSort(3, Qt::AscendingOrder);
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType != QSqlDriver::Sybase) {
|
2011-04-27 10:05:43 +00:00
|
|
|
QCOMPARE(model.rowCount(), 4);
|
|
|
|
QCOMPARE(model.data(model.index(0, 3)).toString(), QString("herr"));
|
|
|
|
QCOMPARE(model.data(model.index(1, 3)).toString(), QString("mister"));
|
|
|
|
QCOMPARE(model.data(model.index(2, 3)).toString(), QString("mister"));
|
|
|
|
QCOMPARE(model.data(model.index(3, 3)).toString(), QString("mister"));
|
|
|
|
} else {
|
|
|
|
QCOMPARE(model.data(model.index(0, 3)).toInt(), 1);
|
|
|
|
QCOMPARE(model.data(model.index(1, 3)).toInt(), 2);
|
|
|
|
QCOMPARE(model.data(model.index(2, 3)).toInt(), 2);
|
|
|
|
QCOMPARE(model.data(model.index(3, 3)).toInt(), 2);
|
|
|
|
}
|
|
|
|
|
2011-07-19 04:57:07 +00:00
|
|
|
// redo same test in LeftJoin mode
|
|
|
|
model.setJoinMode(QSqlRelationalTableModel::LeftJoin);
|
|
|
|
model.setSort(2, Qt::DescendingOrder);
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
|
|
|
QCOMPARE(model.rowCount(), 6);
|
|
|
|
QCOMPARE(model.data(model.index(0, 2)).toString(), QString("mister"));
|
|
|
|
QCOMPARE(model.data(model.index(1, 2)).toString(), QString("mister"));
|
|
|
|
QCOMPARE(model.data(model.index(2, 2)).toString(), QString("herr"));
|
|
|
|
QCOMPARE(model.data(model.index(3, 2)).toString(), QString("herr"));
|
|
|
|
QCOMPARE(model.data(model.index(4, 2)).toString(), QString(""));
|
|
|
|
QCOMPARE(model.data(model.index(5, 2)).toString(), QString(""));
|
|
|
|
|
|
|
|
model.setSort(3, Qt::AscendingOrder);
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType != QSqlDriver::Sybase) {
|
2011-07-19 04:57:07 +00:00
|
|
|
QCOMPARE(model.rowCount(), 6);
|
|
|
|
QCOMPARE(model.data(model.index(0, 3)).toString(), QString(""));
|
|
|
|
QCOMPARE(model.data(model.index(1, 3)).toString(), QString("herr"));
|
|
|
|
QCOMPARE(model.data(model.index(2, 3)).toString(), QString("mister"));
|
|
|
|
QCOMPARE(model.data(model.index(3, 3)).toString(), QString("mister"));
|
|
|
|
QCOMPARE(model.data(model.index(4, 3)).toString(), QString("mister"));
|
|
|
|
QCOMPARE(model.data(model.index(5, 3)).toString(), QString("mister"));
|
|
|
|
} else {
|
|
|
|
QCOMPARE(model.data(model.index(0, 3)).toInt(), 1);
|
|
|
|
QCOMPARE(model.data(model.index(1, 3)).toInt(), 2);
|
|
|
|
QCOMPARE(model.data(model.index(2, 3)).toInt(), 2);
|
|
|
|
QCOMPARE(model.data(model.index(3, 3)).toInt(), 2);
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void testRevert(QSqlRelationalTableModel &model)
|
|
|
|
{
|
|
|
|
/* revert single row */
|
|
|
|
QCOMPARE(model.data(model.index(0, 2)).toString(), QString("herr"));
|
|
|
|
QVERIFY(model.setData(model.index(0, 2), 2, Qt::EditRole));
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(0, 2)).toString(), QString("mister"));
|
|
|
|
model.revertRow(0);
|
|
|
|
QCOMPARE(model.data(model.index(0, 2)).toString(), QString("herr"));
|
|
|
|
|
|
|
|
/* revert all */
|
|
|
|
QVERIFY(model.setData(model.index(0, 2), 2, Qt::EditRole));
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(0, 2)).toString(), QString("mister"));
|
|
|
|
model.revertAll();
|
|
|
|
QCOMPARE(model.data(model.index(0, 2)).toString(), QString("herr"));
|
|
|
|
|
|
|
|
// the following only works for OnManualSubmit
|
|
|
|
if (model.editStrategy() != QSqlTableModel::OnManualSubmit)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* revert inserted rows */
|
|
|
|
int initialRowCount = model.rowCount();
|
|
|
|
QVERIFY(model.insertRows(4, 4));
|
|
|
|
QVERIFY(model.rowCount() == (initialRowCount + 4));
|
|
|
|
|
|
|
|
/* make sure the new rows are initialized to nothing */
|
|
|
|
QVERIFY(model.data(model.index(4, 2)).toString().isEmpty());
|
|
|
|
QVERIFY(model.data(model.index(5, 2)).toString().isEmpty());
|
|
|
|
QVERIFY(model.data(model.index(6, 2)).toString().isEmpty());
|
|
|
|
QVERIFY(model.data(model.index(7, 2)).toString().isEmpty());
|
|
|
|
|
|
|
|
/* Set some values */
|
|
|
|
QVERIFY(model.setData(model.index(4, 0), 42, Qt::EditRole));
|
|
|
|
QVERIFY(model.setData(model.index(5, 0), 43, Qt::EditRole));
|
|
|
|
QVERIFY(model.setData(model.index(6, 0), 44, Qt::EditRole));
|
|
|
|
QVERIFY(model.setData(model.index(7, 0), 45, Qt::EditRole));
|
|
|
|
|
|
|
|
QVERIFY(model.setData(model.index(4, 2), 2, Qt::EditRole));
|
|
|
|
QVERIFY(model.setData(model.index(5, 2), 2, Qt::EditRole));
|
|
|
|
QVERIFY(model.setData(model.index(6, 2), 1, Qt::EditRole));
|
|
|
|
QVERIFY(model.setData(model.index(7, 2), 2, Qt::EditRole));
|
|
|
|
|
|
|
|
/* Now revert the newly inserted rows */
|
|
|
|
model.revertAll();
|
2015-07-24 13:48:21 +00:00
|
|
|
QCOMPARE(model.rowCount(), initialRowCount);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
/* Insert rows again */
|
|
|
|
QVERIFY(model.insertRows(4, 4));
|
|
|
|
|
|
|
|
/* make sure the new rows are initialized to nothing */
|
|
|
|
QVERIFY(model.data(model.index(4, 2)).toString().isEmpty());
|
|
|
|
QVERIFY(model.data(model.index(5, 2)).toString().isEmpty());
|
|
|
|
QVERIFY(model.data(model.index(6, 2)).toString().isEmpty());
|
|
|
|
QVERIFY(model.data(model.index(7, 2)).toString().isEmpty());
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QSqlRelationalTableModel::revert()
|
|
|
|
{
|
|
|
|
QFETCH_GLOBAL(QString, dbName);
|
|
|
|
QSqlDatabase db = QSqlDatabase::database(dbName);
|
|
|
|
CHECK_DATABASE(db);
|
|
|
|
|
|
|
|
QSqlRelationalTableModel model(0, db);
|
|
|
|
|
|
|
|
model.setTable(reltest1);
|
2016-02-16 06:19:03 +00:00
|
|
|
model.setRelation(2, QSqlRelation(reltest2, "id", "title"));
|
2011-04-27 10:05:43 +00:00
|
|
|
model.setRelation(3, QSqlRelation(reltest4, "id", "name"));
|
|
|
|
|
|
|
|
model.setSort(0, Qt::AscendingOrder);
|
|
|
|
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
QCOMPARE(model.data(model.index(0, 0)).toString(), QString("1"));
|
|
|
|
|
|
|
|
testRevert(model);
|
|
|
|
if (QTest::currentTestFailed())
|
|
|
|
return;
|
|
|
|
|
2011-07-19 04:57:07 +00:00
|
|
|
model.setJoinMode(QSqlRelationalTableModel::LeftJoin);
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
testRevert(model);
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
/* and again with OnManualSubmit */
|
2011-07-19 04:57:07 +00:00
|
|
|
model.setJoinMode(QSqlRelationalTableModel::InnerJoin);
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
model.setEditStrategy(QSqlTableModel::OnManualSubmit);
|
|
|
|
testRevert(model);
|
|
|
|
|
|
|
|
model.setJoinMode(QSqlRelationalTableModel::LeftJoin);
|
|
|
|
QVERIFY_SQL(model, select());
|
2011-04-27 10:05:43 +00:00
|
|
|
model.setEditStrategy(QSqlTableModel::OnManualSubmit);
|
|
|
|
testRevert(model);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QSqlRelationalTableModel::clearDisplayValuesCache()
|
|
|
|
{
|
|
|
|
QFETCH_GLOBAL(QString, dbName);
|
|
|
|
QSqlDatabase db = QSqlDatabase::database(dbName);
|
|
|
|
CHECK_DATABASE(db);
|
2014-11-20 11:55:56 +00:00
|
|
|
const QSqlDriver::DbmsType dbType = tst_Databases::getDatabaseType(db);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QSqlRelationalTableModel model(0, db);
|
|
|
|
|
|
|
|
model.setTable(reltest1);
|
2016-02-16 06:19:03 +00:00
|
|
|
model.setRelation(2, QSqlRelation(reltest2, "id", "title"));
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType != QSqlDriver::Sybase)
|
2016-02-16 06:19:03 +00:00
|
|
|
model.setRelation(3, QSqlRelation(reltest2, "id", "title"));
|
2011-04-27 10:05:43 +00:00
|
|
|
model.setSort(1, Qt::AscendingOrder);
|
|
|
|
model.setEditStrategy(QSqlTableModel::OnManualSubmit);
|
|
|
|
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(3, 0)).toInt(), 3);
|
|
|
|
QCOMPARE(model.data(model.index(3, 1)).toString(), QString("vohi"));
|
|
|
|
QCOMPARE(model.data(model.index(3, 2)).toString(), QString("herr"));
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType != QSqlDriver::Sybase)
|
2011-04-27 10:05:43 +00:00
|
|
|
QCOMPARE(model.data(model.index(3, 3)).toString(), QString("mister"));
|
|
|
|
else
|
|
|
|
QCOMPARE(model.data(model.index(3, 3)).toInt(), 2 );
|
|
|
|
|
|
|
|
model.insertRow(model.rowCount());
|
2011-07-19 04:57:07 +00:00
|
|
|
QVERIFY(model.setData(model.index(4, 0), 7, Qt::EditRole));
|
2011-04-27 10:05:43 +00:00
|
|
|
QVERIFY(model.setData(model.index(4, 1), "anders", Qt::EditRole));
|
|
|
|
QVERIFY(model.setData(model.index(4, 2), 1, Qt::EditRole));
|
|
|
|
QVERIFY(model.setData(model.index(4, 3), 1, Qt::EditRole));
|
|
|
|
model.submitAll();
|
|
|
|
|
2011-07-19 04:57:07 +00:00
|
|
|
QCOMPARE(model.data(model.index(0, 0)).toInt(), 7);
|
2011-04-27 10:05:43 +00:00
|
|
|
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("anders"));
|
|
|
|
QCOMPARE(model.data(model.index(0, 2)).toString(), QString("herr"));
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType != QSqlDriver::Sybase)
|
2011-04-27 10:05:43 +00:00
|
|
|
QCOMPARE(model.data(model.index(0, 3)).toString(), QString("herr"));
|
|
|
|
else
|
|
|
|
QCOMPARE(model.data(model.index(0, 3)).toInt(), 1);
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(4, 0)).toInt(), 3);
|
|
|
|
QCOMPARE(model.data(model.index(4, 1)).toString(), QString("vohi"));
|
|
|
|
QCOMPARE(model.data(model.index(4, 2)).toString(), QString("herr"));
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType != QSqlDriver::Sybase)
|
2011-04-27 10:05:43 +00:00
|
|
|
QCOMPARE(model.data(model.index(4, 3)).toString(), QString("mister"));
|
|
|
|
else
|
|
|
|
QCOMPARE(model.data(model.index(4, 3)).toInt(), 2);
|
|
|
|
}
|
|
|
|
|
2012-09-24 16:12:43 +00:00
|
|
|
// For task 140782 and 176374: If the main table and the related tables uses the same
|
2011-04-27 10:05:43 +00:00
|
|
|
// name for a column or display column then insertRecord() would return true though it
|
|
|
|
// actually failed.
|
|
|
|
void tst_QSqlRelationalTableModel::insertRecordDuplicateFieldNames()
|
|
|
|
{
|
|
|
|
QFETCH_GLOBAL(QString, dbName);
|
|
|
|
QSqlDatabase db = QSqlDatabase::database(dbName);
|
|
|
|
CHECK_DATABASE(db);
|
2014-11-20 11:55:56 +00:00
|
|
|
const QSqlDriver::DbmsType dbType = tst_Databases::getDatabaseType(db);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QSqlRelationalTableModel model(0, db);
|
|
|
|
model.setTable(reltest3);
|
|
|
|
model.setEditStrategy(QSqlTableModel::OnManualSubmit);
|
|
|
|
model.setSort(0, Qt::AscendingOrder);
|
|
|
|
|
|
|
|
// Duplication of "name", used in both reltest3 and reltest4.
|
|
|
|
model.setRelation(2, QSqlRelation(reltest4, "id", "name"));
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType == QSqlDriver::Interbase || dbType == QSqlDriver::Oracle || dbType == QSqlDriver::DB2) {
|
2011-04-27 10:05:43 +00:00
|
|
|
QCOMPARE(model.record(1).value((reltest4+QLatin1String("_name_2")).toUpper()).toString(),
|
|
|
|
QString("Trondheim"));
|
|
|
|
} else {
|
|
|
|
QCOMPARE(model.record(1).value((reltest4+QLatin1String("_name_2"))).toString(),
|
|
|
|
QString("Trondheim"));
|
|
|
|
}
|
|
|
|
|
|
|
|
QSqlRecord rec = model.record();
|
|
|
|
rec.setValue(0, 3);
|
|
|
|
rec.setValue(1, "Berge");
|
|
|
|
rec.setValue(2, 1); // Must insert the key value
|
|
|
|
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType == QSqlDriver::Interbase || dbType == QSqlDriver::Oracle || dbType == QSqlDriver::DB2) {
|
2011-04-27 10:05:43 +00:00
|
|
|
QCOMPARE(rec.fieldName(0), QLatin1String("ID"));
|
|
|
|
QCOMPARE(rec.fieldName(1), QLatin1String("NAME")); // This comes from main table
|
|
|
|
} else {
|
|
|
|
QCOMPARE(rec.fieldName(0), QLatin1String("id"));
|
|
|
|
QCOMPARE(rec.fieldName(1), QLatin1String("name"));
|
|
|
|
}
|
|
|
|
|
|
|
|
// The duplicate field names is aliased because it's comes from the relation's display column.
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType == QSqlDriver::Interbase || dbType == QSqlDriver::Oracle || dbType == QSqlDriver::DB2)
|
2011-04-27 10:05:43 +00:00
|
|
|
QCOMPARE(rec.fieldName(2), (reltest4+QLatin1String("_name_2")).toUpper());
|
|
|
|
else
|
|
|
|
QCOMPARE(rec.fieldName(2), reltest4+QLatin1String("_name_2"));
|
|
|
|
|
|
|
|
QVERIFY(model.insertRecord(-1, rec));
|
|
|
|
QCOMPARE(model.data(model.index(2, 2)).toString(), QString("Oslo"));
|
|
|
|
QVERIFY(model.submitAll());
|
|
|
|
QCOMPARE(model.data(model.index(2, 2)).toString(), QString("Oslo"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QSqlRelationalTableModel::invalidData()
|
|
|
|
{
|
|
|
|
QFETCH_GLOBAL(QString, dbName);
|
|
|
|
QSqlDatabase db = QSqlDatabase::database(dbName);
|
|
|
|
CHECK_DATABASE(db);
|
|
|
|
|
|
|
|
QSqlRelationalTableModel model(0, db);
|
|
|
|
model.setTable(reltest1);
|
2016-02-16 06:19:03 +00:00
|
|
|
model.setRelation(2, QSqlRelation(reltest2, "id", "title"));
|
2011-04-27 10:05:43 +00:00
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
|
|
|
//try set a non-existent relational key
|
|
|
|
QVERIFY(model.setData(model.index(0, 2), 3) == false);
|
|
|
|
QCOMPARE(model.data(model.index(0, 2)).toString(), QString("herr"));
|
|
|
|
|
|
|
|
//try to set data in non valid index
|
2015-07-23 11:26:28 +00:00
|
|
|
QVERIFY(!model.setData(model.index(0,10),5));
|
2011-07-19 04:57:07 +00:00
|
|
|
|
|
|
|
//same test with LeftJoin mode
|
|
|
|
model.setJoinMode(QSqlRelationalTableModel::LeftJoin);
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
|
|
|
//try set a non-existent relational key
|
|
|
|
QVERIFY(model.setData(model.index(0, 2), 3) == false);
|
|
|
|
QCOMPARE(model.data(model.index(0, 2)).toString(), QString("herr"));
|
|
|
|
|
|
|
|
//try to set data in non valid index
|
2015-07-23 11:26:28 +00:00
|
|
|
QVERIFY(!model.setData(model.index(0,10),5));
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QSqlRelationalTableModel::relationModel()
|
|
|
|
{
|
|
|
|
QFETCH_GLOBAL(QString, dbName);
|
|
|
|
QSqlDatabase db = QSqlDatabase::database(dbName);
|
|
|
|
CHECK_DATABASE(db);
|
|
|
|
|
|
|
|
QSqlRelationalTableModel model(0, db);
|
|
|
|
model.setTable(reltest1);
|
2016-02-16 06:19:03 +00:00
|
|
|
model.setRelation(2, QSqlRelation(reltest2, "id", "title"));
|
2011-04-27 10:05:43 +00:00
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
2015-07-24 13:48:21 +00:00
|
|
|
QVERIFY(!model.relationModel(0));
|
|
|
|
QVERIFY(!model.relationModel(1));
|
2011-04-27 10:05:43 +00:00
|
|
|
QVERIFY(model.relationModel(2) != NULL);
|
2015-07-24 13:48:21 +00:00
|
|
|
QVERIFY(!model.relationModel(3));
|
|
|
|
QVERIFY(!model.relationModel(4));
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
model.setRelation(3, QSqlRelation(reltest4, "id", "name"));
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
2015-07-24 13:48:21 +00:00
|
|
|
QVERIFY(!model.relationModel(0));
|
|
|
|
QVERIFY(!model.relationModel(1));
|
2011-04-27 10:05:43 +00:00
|
|
|
QVERIFY(model.relationModel(2) != NULL);
|
|
|
|
QVERIFY(model.relationModel(3) != NULL);
|
2015-07-24 13:48:21 +00:00
|
|
|
QVERIFY(!model.relationModel(4));
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QSqlTableModel *rel_model = model.relationModel(2);
|
|
|
|
QCOMPARE(rel_model->data(rel_model->index(0,1)).toString(), QString("herr"));
|
2011-07-19 04:57:07 +00:00
|
|
|
|
|
|
|
//same test in JoinMode
|
|
|
|
model.setJoinMode(QSqlRelationalTableModel::LeftJoin);
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
2015-07-24 13:48:21 +00:00
|
|
|
QVERIFY(!model.relationModel(0));
|
|
|
|
QVERIFY(!model.relationModel(1));
|
2011-07-19 04:57:07 +00:00
|
|
|
QVERIFY(model.relationModel(2) != NULL);
|
|
|
|
QVERIFY(model.relationModel(3) != NULL);
|
2015-07-24 13:48:21 +00:00
|
|
|
QVERIFY(!model.relationModel(4));
|
2011-07-19 04:57:07 +00:00
|
|
|
|
|
|
|
QSqlTableModel *rel_model2 = model.relationModel(2);
|
|
|
|
QCOMPARE(rel_model2->data(rel_model->index(0,1)).toString(), QString("herr"));
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QSqlRelationalTableModel::casing()
|
|
|
|
{
|
|
|
|
QFETCH_GLOBAL(QString, dbName);
|
|
|
|
QSqlDatabase db = QSqlDatabase::database(dbName);
|
|
|
|
CHECK_DATABASE(db);
|
2014-11-20 11:55:56 +00:00
|
|
|
const QSqlDriver::DbmsType dbType = tst_Databases::getDatabaseType(db);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType == QSqlDriver::Interbase || dbType == QSqlDriver::SQLite || dbType == QSqlDriver::MSSqlServer)
|
2011-10-19 02:53:13 +00:00
|
|
|
QSKIP("The casing test for this database is irrelevant since this database does not treat different cases as separate entities");
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QSqlQuery q(db);
|
2013-03-21 10:23:31 +00:00
|
|
|
QVERIFY_SQL( q, exec("create table " + qTableName("CASETEST1", db).toUpper() +
|
2011-04-27 10:05:43 +00:00
|
|
|
" (id int not null primary key, name varchar(20), title_key int, another_title_key int)"));
|
|
|
|
|
2013-03-21 10:23:31 +00:00
|
|
|
if (!q.exec("create table " + qTableName("casetest1", db) +
|
2011-04-27 10:05:43 +00:00
|
|
|
" (ident int not null primary key, name varchar(20), title_key int)"))
|
2011-10-19 02:53:13 +00:00
|
|
|
QSKIP("The casing test for this database is irrelevant since this database does not treat different cases as separate entities");
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2013-03-21 10:23:31 +00:00
|
|
|
QVERIFY_SQL( q, exec("insert into " + qTableName("CASETEST1", db).toUpper() + " values(1, 'harry', 1, 2)"));
|
|
|
|
QVERIFY_SQL( q, exec("insert into " + qTableName("CASETEST1", db).toUpper() + " values(2, 'trond', 2, 1)"));
|
|
|
|
QVERIFY_SQL( q, exec("insert into " + qTableName("CASETEST1", db).toUpper() + " values(3, 'vohi', 1, 2)"));
|
|
|
|
QVERIFY_SQL( q, exec("insert into " + qTableName("CASETEST1", db).toUpper() + " values(4, 'boris', 2, 2)"));
|
|
|
|
QVERIFY_SQL( q, exec("insert into " + qTableName("casetest1", db) + " values(1, 'jerry', 1)"));
|
|
|
|
QVERIFY_SQL( q, exec("insert into " + qTableName("casetest1", db) + " values(2, 'george', 2)"));
|
|
|
|
QVERIFY_SQL( q, exec("insert into " + qTableName("casetest1", db) + " values(4, 'kramer', 2)"));
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType == QSqlDriver::Oracle) {
|
2011-04-27 10:05:43 +00:00
|
|
|
//try an owner that doesn't exist
|
2013-03-21 10:23:31 +00:00
|
|
|
QSqlRecord rec = db.driver()->record("doug." + qTableName("CASETEST1", db).toUpper());
|
2011-04-27 10:05:43 +00:00
|
|
|
QCOMPARE( rec.count(), 0);
|
|
|
|
|
|
|
|
//try an owner that does exist
|
2015-10-13 07:46:56 +00:00
|
|
|
rec = db.driver()->record(db.userName() + QLatin1Char('.') + qTableName("CASETEST1", db).toUpper());
|
2011-04-27 10:05:43 +00:00
|
|
|
QCOMPARE( rec.count(), 4);
|
|
|
|
}
|
2013-03-21 10:23:31 +00:00
|
|
|
QSqlRecord rec = db.driver()->record(qTableName("CASETEST1", db).toUpper());
|
2011-04-27 10:05:43 +00:00
|
|
|
QCOMPARE( rec.count(), 4);
|
|
|
|
|
2013-03-21 10:23:31 +00:00
|
|
|
rec = db.driver()->record(qTableName("casetest1", db));
|
2011-04-27 10:05:43 +00:00
|
|
|
QCOMPARE( rec.count(), 3);
|
|
|
|
|
|
|
|
QSqlTableModel upperCaseModel(0, db);
|
2013-03-21 10:23:31 +00:00
|
|
|
upperCaseModel.setTable(qTableName("CASETEST1", db).toUpper());
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2013-03-21 10:23:31 +00:00
|
|
|
QCOMPARE(upperCaseModel.tableName(), qTableName("CASETEST1", db).toUpper());
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QVERIFY_SQL(upperCaseModel, select());
|
|
|
|
|
|
|
|
QCOMPARE(upperCaseModel.rowCount(), 4);
|
|
|
|
|
|
|
|
QSqlTableModel lowerCaseModel(0, db);
|
2013-03-21 10:23:31 +00:00
|
|
|
lowerCaseModel.setTable(qTableName("casetest1", db));
|
|
|
|
QCOMPARE(lowerCaseModel.tableName(), qTableName("casetest1", db));
|
2011-04-27 10:05:43 +00:00
|
|
|
QVERIFY_SQL(lowerCaseModel, select());
|
|
|
|
|
|
|
|
QCOMPARE(lowerCaseModel.rowCount(), 3);
|
|
|
|
|
|
|
|
QSqlRelationalTableModel model(0, db);
|
2013-03-21 10:23:31 +00:00
|
|
|
model.setTable(qTableName("CASETEST1", db).toUpper());
|
2016-02-16 06:19:03 +00:00
|
|
|
model.setRelation(2, QSqlRelation(reltest2, "id", "title"));
|
2011-04-27 10:05:43 +00:00
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(0, 0)).toInt(), 1);
|
|
|
|
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry"));
|
|
|
|
QCOMPARE(model.data(model.index(0, 2)).toString(), QString("herr"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QSqlRelationalTableModel::escapedRelations()
|
|
|
|
{
|
|
|
|
QFETCH_GLOBAL(QString, dbName);
|
|
|
|
QSqlDatabase db = QSqlDatabase::database(dbName);
|
|
|
|
CHECK_DATABASE(db);
|
2014-11-20 11:55:56 +00:00
|
|
|
const QSqlDriver::DbmsType dbType = tst_Databases::getDatabaseType(db);
|
2013-04-04 06:45:50 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
recreateTestTables(db);
|
|
|
|
|
|
|
|
QSqlRelationalTableModel model(0, db);
|
|
|
|
model.setTable(reltest1);
|
|
|
|
|
|
|
|
//try with relation table name quoted
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType == QSqlDriver::Interbase || dbType == QSqlDriver::Oracle || dbType == QSqlDriver::DB2) {
|
2011-04-27 10:05:43 +00:00
|
|
|
model.setRelation(2, QSqlRelation(db.driver()->escapeIdentifier(reltest2.toUpper(),QSqlDriver::TableName),
|
2016-02-16 06:19:03 +00:00
|
|
|
"id",
|
2011-04-27 10:05:43 +00:00
|
|
|
"title"));
|
|
|
|
} else {
|
|
|
|
model.setRelation(2, QSqlRelation(db.driver()->escapeIdentifier(reltest2,QSqlDriver::TableName),
|
2016-02-16 06:19:03 +00:00
|
|
|
"id",
|
2011-04-27 10:05:43 +00:00
|
|
|
"title"));
|
|
|
|
|
|
|
|
}
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(0, 0)).toInt(), 1);
|
|
|
|
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry"));
|
|
|
|
QCOMPARE(model.data(model.index(0, 2)).toString(), QString("herr"));
|
|
|
|
|
2011-07-19 04:57:07 +00:00
|
|
|
model.setJoinMode(QSqlRelationalTableModel::LeftJoin);
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(0, 0)).toInt(), 1);
|
|
|
|
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry"));
|
|
|
|
QCOMPARE(model.data(model.index(0, 2)).toString(), QString("herr"));
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
//try with index column quoted
|
2011-07-19 04:57:07 +00:00
|
|
|
model.setJoinMode(QSqlRelationalTableModel::InnerJoin);
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType == QSqlDriver::Interbase || dbType == QSqlDriver::Oracle || dbType == QSqlDriver::DB2) {
|
2011-04-27 10:05:43 +00:00
|
|
|
model.setRelation(2, QSqlRelation(reltest2,
|
2016-02-16 06:19:03 +00:00
|
|
|
db.driver()->escapeIdentifier("id", QSqlDriver::FieldName).toUpper(),
|
2011-04-27 10:05:43 +00:00
|
|
|
"title"));
|
|
|
|
} else {
|
|
|
|
model.setRelation(2, QSqlRelation(reltest2,
|
2016-02-16 06:19:03 +00:00
|
|
|
db.driver()->escapeIdentifier("id", QSqlDriver::FieldName),
|
2011-04-27 10:05:43 +00:00
|
|
|
"title"));
|
|
|
|
}
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(0, 0)).toInt(), 1);
|
|
|
|
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry"));
|
|
|
|
QCOMPARE(model.data(model.index(0, 2)).toString(), QString("herr"));
|
|
|
|
|
2011-07-19 04:57:07 +00:00
|
|
|
model.setJoinMode(QSqlRelationalTableModel::LeftJoin);
|
|
|
|
QVERIFY_SQL(model, select());
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2011-07-19 04:57:07 +00:00
|
|
|
QCOMPARE(model.data(model.index(0, 0)).toInt(), 1);
|
|
|
|
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry"));
|
|
|
|
QCOMPARE(model.data(model.index(0, 2)).toString(), QString("herr"));
|
|
|
|
|
|
|
|
//try with display column quoted
|
|
|
|
model.setJoinMode(QSqlRelationalTableModel::InnerJoin);
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType == QSqlDriver::Interbase || dbType == QSqlDriver::Oracle || dbType == QSqlDriver::DB2) {
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
model.setRelation(2, QSqlRelation(reltest2,
|
2016-02-16 06:19:03 +00:00
|
|
|
"id",
|
2011-04-27 10:05:43 +00:00
|
|
|
db.driver()->escapeIdentifier("title", QSqlDriver::FieldName).toUpper()));
|
|
|
|
} else {
|
|
|
|
model.setRelation(2, QSqlRelation(reltest2,
|
2016-02-16 06:19:03 +00:00
|
|
|
"id",
|
2011-04-27 10:05:43 +00:00
|
|
|
db.driver()->escapeIdentifier("title", QSqlDriver::FieldName)));
|
|
|
|
}
|
|
|
|
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(0, 0)).toInt(), 1);
|
|
|
|
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry"));
|
|
|
|
QCOMPARE(model.data(model.index(0, 2)).toString(), QString("herr"));
|
|
|
|
|
2011-07-19 04:57:07 +00:00
|
|
|
model.setJoinMode(QSqlRelationalTableModel::LeftJoin);
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(0, 0)).toInt(), 1);
|
|
|
|
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry"));
|
|
|
|
QCOMPARE(model.data(model.index(0, 2)).toString(), QString("herr"));
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2011-07-19 04:57:07 +00:00
|
|
|
//try with tablename and index and display columns quoted in the relation
|
|
|
|
model.setJoinMode(QSqlRelationalTableModel::InnerJoin);
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType == QSqlDriver::Interbase || dbType == QSqlDriver::Oracle || dbType == QSqlDriver::DB2) {
|
2011-04-27 10:05:43 +00:00
|
|
|
model.setRelation(2, QSqlRelation(reltest2,
|
2016-02-16 06:19:03 +00:00
|
|
|
"id",
|
2011-04-27 10:05:43 +00:00
|
|
|
db.driver()->escapeIdentifier("title", QSqlDriver::FieldName).toUpper()));
|
|
|
|
} else {
|
|
|
|
model.setRelation(2, QSqlRelation(reltest2,
|
2016-02-16 06:19:03 +00:00
|
|
|
"id",
|
2011-04-27 10:05:43 +00:00
|
|
|
db.driver()->escapeIdentifier("title", QSqlDriver::FieldName)));
|
|
|
|
}
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(0, 0)).toInt(), 1);
|
|
|
|
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry"));
|
|
|
|
QCOMPARE(model.data(model.index(0, 2)).toString(), QString("herr"));
|
2011-07-19 04:57:07 +00:00
|
|
|
|
|
|
|
model.setJoinMode(QSqlRelationalTableModel::LeftJoin);
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(0, 0)).toInt(), 1);
|
|
|
|
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry"));
|
|
|
|
QCOMPARE(model.data(model.index(0, 2)).toString(), QString("herr"));
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QSqlRelationalTableModel::escapedTableName()
|
|
|
|
{
|
|
|
|
QFETCH_GLOBAL(QString, dbName);
|
|
|
|
QSqlDatabase db = QSqlDatabase::database(dbName);
|
|
|
|
CHECK_DATABASE(db);
|
2014-11-20 11:55:56 +00:00
|
|
|
const QSqlDriver::DbmsType dbType = tst_Databases::getDatabaseType(db);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
// set the values using OnRowChange Strategy with an escaped tablename
|
|
|
|
{
|
|
|
|
QSqlRelationalTableModel model(0, db);
|
|
|
|
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType == QSqlDriver::Interbase || dbType == QSqlDriver::Oracle || dbType == QSqlDriver::DB2) {
|
2011-04-27 10:05:43 +00:00
|
|
|
model.setTable(db.driver()->escapeIdentifier(reltest1.toUpper(), QSqlDriver::TableName));
|
|
|
|
} else {
|
|
|
|
model.setTable(db.driver()->escapeIdentifier(reltest1, QSqlDriver::TableName));
|
|
|
|
}
|
|
|
|
model.setSort(0, Qt::AscendingOrder);
|
2016-02-16 06:19:03 +00:00
|
|
|
model.setRelation(2, QSqlRelation(reltest2, "id", "title"));
|
2011-04-27 10:05:43 +00:00
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
|
|
|
QVERIFY(model.setData(model.index(0, 1), QString("harry2")));
|
|
|
|
QVERIFY(model.setData(model.index(0, 2), 2));
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry2"));
|
|
|
|
QCOMPARE(model.data(model.index(0, 2)).toString(), QString("mister"));
|
|
|
|
|
|
|
|
model.submit();
|
|
|
|
|
|
|
|
QVERIFY(model.setData(model.index(3,1), QString("boris2")));
|
|
|
|
QVERIFY(model.setData(model.index(3, 2), 1));
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(3,1)).toString(), QString("boris2"));
|
|
|
|
QCOMPARE(model.data(model.index(3, 2)).toString(), QString("herr"));
|
|
|
|
|
|
|
|
model.submit();
|
|
|
|
}
|
|
|
|
{ //verify values
|
|
|
|
QSqlRelationalTableModel model(0, db);
|
|
|
|
model.setTable(reltest1);
|
|
|
|
model.setSort(0, Qt::AscendingOrder);
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry2"));
|
|
|
|
QCOMPARE(model.data(model.index(0, 2)).toInt(), 2);
|
|
|
|
QCOMPARE(model.data(model.index(3, 1)).toString(), QString("boris2"));
|
|
|
|
QCOMPARE(model.data(model.index(3, 2)).toInt(), 1);
|
|
|
|
|
2016-02-16 06:19:03 +00:00
|
|
|
model.setRelation(2, QSqlRelation(reltest2, "id", "title"));
|
2011-04-27 10:05:43 +00:00
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
QCOMPARE(model.data(model.index(0, 2)).toString(), QString("mister"));
|
|
|
|
QCOMPARE(model.data(model.index(3,2)).toString(), QString("herr"));
|
|
|
|
|
|
|
|
}
|
2011-07-19 04:57:07 +00:00
|
|
|
|
|
|
|
//ok, now do same test with LeftJoin
|
|
|
|
{
|
|
|
|
QSqlRelationalTableModel model(0, db);
|
|
|
|
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType == QSqlDriver::Interbase || dbType == QSqlDriver::Oracle || dbType == QSqlDriver::DB2) {
|
2011-07-19 04:57:07 +00:00
|
|
|
model.setTable(db.driver()->escapeIdentifier(reltest1.toUpper(), QSqlDriver::TableName));
|
|
|
|
} else {
|
|
|
|
model.setTable(db.driver()->escapeIdentifier(reltest1, QSqlDriver::TableName));
|
|
|
|
}
|
|
|
|
model.setSort(0, Qt::AscendingOrder);
|
2016-02-16 06:19:03 +00:00
|
|
|
model.setRelation(2, QSqlRelation(reltest2, "id", "title"));
|
2011-07-19 04:57:07 +00:00
|
|
|
model.setJoinMode(QSqlRelationalTableModel::LeftJoin);
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
|
|
|
QVERIFY(model.setData(model.index(0, 1), QString("harry2")));
|
|
|
|
QVERIFY(model.setData(model.index(0, 2), 2));
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry2"));
|
|
|
|
QCOMPARE(model.data(model.index(0, 2)).toString(), QString("mister"));
|
|
|
|
|
|
|
|
model.submit();
|
|
|
|
|
|
|
|
QVERIFY(model.setData(model.index(3,1), QString("boris2")));
|
|
|
|
QVERIFY(model.setData(model.index(3, 2), 1));
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(3,1)).toString(), QString("boris2"));
|
|
|
|
QCOMPARE(model.data(model.index(3, 2)).toString(), QString("herr"));
|
|
|
|
|
|
|
|
model.submit();
|
|
|
|
}
|
|
|
|
{ //verify values
|
|
|
|
QSqlRelationalTableModel model(0, db);
|
|
|
|
model.setTable(reltest1);
|
|
|
|
model.setSort(0, Qt::AscendingOrder);
|
|
|
|
model.setJoinMode(QSqlRelationalTableModel::LeftJoin);
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry2"));
|
|
|
|
QCOMPARE(model.data(model.index(0, 2)).toInt(), 2);
|
|
|
|
QCOMPARE(model.data(model.index(3, 1)).toString(), QString("boris2"));
|
|
|
|
QCOMPARE(model.data(model.index(3, 2)).toInt(), 1);
|
|
|
|
|
2016-02-16 06:19:03 +00:00
|
|
|
model.setRelation(2, QSqlRelation(reltest2, "id", "title"));
|
2011-07-19 04:57:07 +00:00
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
QCOMPARE(model.data(model.index(0, 2)).toString(), QString("mister"));
|
|
|
|
QCOMPARE(model.data(model.index(3,2)).toString(), QString("herr"));
|
|
|
|
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QSqlRelationalTableModel::whiteSpaceInIdentifiers()
|
|
|
|
{
|
|
|
|
QFETCH_GLOBAL(QString, dbName);
|
|
|
|
QSqlDatabase db = QSqlDatabase::database(dbName);
|
|
|
|
CHECK_DATABASE(db);
|
|
|
|
|
|
|
|
if (!testWhiteSpaceNames(db.driverName()))
|
2011-10-19 02:53:13 +00:00
|
|
|
QSKIP("White space test irrelevant for driver");
|
2011-04-27 10:05:43 +00:00
|
|
|
QSqlRelationalTableModel model(0, db);
|
2013-03-21 10:23:31 +00:00
|
|
|
model.setTable(db.driver()->escapeIdentifier(qTableName("rel", __FILE__, db) + " test6", QSqlDriver::TableName));
|
2011-04-27 10:05:43 +00:00
|
|
|
model.setSort(0, Qt::DescendingOrder);
|
2013-03-21 10:23:31 +00:00
|
|
|
model.setRelation(1, QSqlRelation(db.driver()->escapeIdentifier(qTableName("rel", __FILE__, db) + " test7", QSqlDriver::TableName),
|
2011-04-27 10:05:43 +00:00
|
|
|
db.driver()->escapeIdentifier("city id", QSqlDriver::FieldName),
|
|
|
|
db.driver()->escapeIdentifier("city name", QSqlDriver::FieldName)));
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(0,1)).toString(), QString("Washington"));
|
|
|
|
QCOMPARE(model.data(model.index(1,1)).toString(), QString("New York"));
|
|
|
|
|
2011-07-19 04:57:07 +00:00
|
|
|
model.setJoinMode(QSqlRelationalTableModel::LeftJoin);
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(0,1)).toString(), QString("Washington"));
|
|
|
|
QCOMPARE(model.data(model.index(1,1)).toString(), QString("New York"));
|
|
|
|
|
|
|
|
model.setJoinMode(QSqlRelationalTableModel::InnerJoin);
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
QSqlRecord rec;
|
|
|
|
QSqlField f1("id", QVariant::Int);
|
|
|
|
QSqlField f2(db.driver()->escapeIdentifier("city key", QSqlDriver::FieldName), QVariant::Int);
|
|
|
|
QSqlField f3(db.driver()->escapeIdentifier("extra field", QSqlDriver::FieldName), QVariant::Int);
|
|
|
|
|
|
|
|
f1.setValue(3);
|
|
|
|
f2.setValue(2);
|
|
|
|
f3.setValue(7);
|
|
|
|
|
|
|
|
f1.setGenerated(true);
|
|
|
|
f2.setGenerated(true);
|
|
|
|
f3.setGenerated(true);
|
|
|
|
|
|
|
|
rec.append(f1);
|
|
|
|
rec.append(f2);
|
|
|
|
rec.append(f3);
|
|
|
|
|
|
|
|
QVERIFY_SQL(model, insertRecord(-1, rec));
|
|
|
|
model.submitAll();
|
2012-02-21 08:22:26 +00:00
|
|
|
if (model.editStrategy() != QSqlTableModel::OnManualSubmit)
|
|
|
|
QVERIFY_SQL(model, select());
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(0, 0)).toInt(), 3);
|
|
|
|
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("Washington"));
|
|
|
|
QCOMPARE(model.data(model.index(0, 2)).toInt(), 7);
|
|
|
|
|
2012-02-16 12:25:41 +00:00
|
|
|
model.setEditStrategy(QSqlTableModel::OnManualSubmit);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QSqlRecord recNew;
|
|
|
|
QSqlField f1New("id", QVariant::Int);
|
|
|
|
QSqlField f2New(db.driver()->escapeIdentifier("city key", QSqlDriver::FieldName), QVariant::Int);
|
|
|
|
QSqlField f3New(db.driver()->escapeIdentifier("extra field", QSqlDriver::FieldName), QVariant::Int);
|
|
|
|
|
|
|
|
f1New.setValue(4);
|
|
|
|
f2New.setValue(1);
|
|
|
|
f3New.setValue(6);
|
|
|
|
|
|
|
|
f1New.setGenerated(true);
|
|
|
|
f2New.setGenerated(true);
|
|
|
|
f3New.setGenerated(true);
|
|
|
|
|
|
|
|
recNew.append(f1New);
|
|
|
|
recNew.append(f2New);
|
|
|
|
recNew.append(f3New);
|
|
|
|
|
|
|
|
QVERIFY_SQL(model, setRecord(0, recNew));
|
|
|
|
|
|
|
|
QCOMPARE(model.data(model.index(0, 0)).toInt(), 4);
|
|
|
|
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("New York"));
|
|
|
|
QCOMPARE(model.data(model.index(0, 2)).toInt(), 6);
|
|
|
|
|
|
|
|
QVERIFY_SQL(model, submitAll());
|
|
|
|
QCOMPARE(model.data(model.index(0, 0)).toInt(), 4);
|
|
|
|
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("New York"));
|
|
|
|
QCOMPARE(model.data(model.index(0, 2)).toInt(), 6);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QSqlRelationalTableModel::psqlSchemaTest()
|
|
|
|
{
|
|
|
|
QFETCH_GLOBAL(QString, dbName);
|
|
|
|
QSqlDatabase db = QSqlDatabase::database(dbName);
|
|
|
|
CHECK_DATABASE(db);
|
2014-11-20 11:55:56 +00:00
|
|
|
QSqlDriver::DbmsType dbType = tst_Databases::getDatabaseType(db);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType != QSqlDriver::PostgreSQL)
|
2011-10-19 02:53:13 +00:00
|
|
|
QSKIP("Postgresql specific test");
|
2011-10-07 00:55:06 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
QSqlRelationalTableModel model(0, db);
|
|
|
|
QSqlQuery q(db);
|
2013-03-21 10:23:31 +00:00
|
|
|
QVERIFY_SQL(q, exec("create schema " + qTableName("QTBUG_5373", __FILE__, db)));
|
|
|
|
QVERIFY_SQL(q, exec("create schema " + qTableName("QTBUG_5373_s2", __FILE__, db)));
|
2015-10-13 07:46:56 +00:00
|
|
|
QVERIFY_SQL(q, exec("create table " + qTableName("QTBUG_5373", __FILE__, db) + QLatin1Char('.') + qTableName("document", __FILE__, db) +
|
2013-03-21 10:23:31 +00:00
|
|
|
"(document_id int primary key, relatingid int, userid int)"));
|
2015-10-13 07:46:56 +00:00
|
|
|
QVERIFY_SQL(q, exec("create table " + qTableName("QTBUG_5373_s2", __FILE__, db) + QLatin1Char('.') + qTableName("user", __FILE__, db) +
|
2013-03-21 10:23:31 +00:00
|
|
|
"(userid int primary key, username char(40))"));
|
2015-10-13 07:46:56 +00:00
|
|
|
model.setTable(qTableName("QTBUG_5373", __FILE__, db) + QLatin1Char('.') + qTableName("document", __FILE__, db));
|
|
|
|
model.setRelation(1, QSqlRelation(qTableName("QTBUG_5373_s2", __FILE__, db) + QLatin1Char('.') + qTableName("user", __FILE__, db), "userid", "username"));
|
|
|
|
model.setRelation(2, QSqlRelation(qTableName("QTBUG_5373_s2", __FILE__, db) + QLatin1Char('.') + qTableName("user", __FILE__, db), "userid", "username"));
|
2011-04-27 10:05:43 +00:00
|
|
|
QVERIFY_SQL(model, select());
|
2011-07-19 04:57:07 +00:00
|
|
|
|
|
|
|
model.setJoinMode(QSqlRelationalTableModel::LeftJoin);
|
|
|
|
QVERIFY_SQL(model, select());
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2011-06-03 06:17:40 +00:00
|
|
|
void tst_QSqlRelationalTableModel::selectAfterUpdate()
|
|
|
|
{
|
|
|
|
QFETCH_GLOBAL(QString, dbName);
|
|
|
|
QSqlDatabase db = QSqlDatabase::database(dbName);
|
|
|
|
CHECK_DATABASE(db);
|
|
|
|
|
|
|
|
QSqlRelationalTableModel model(0, db);
|
|
|
|
model.setTable(reltest1);
|
2016-02-16 06:19:03 +00:00
|
|
|
model.setRelation(2, QSqlRelation(reltest2, "id", "title"));
|
2011-06-03 06:17:40 +00:00
|
|
|
QVERIFY_SQL(model, select());
|
2015-07-24 13:48:21 +00:00
|
|
|
QCOMPARE(model.relationModel(2)->rowCount(), 2);
|
2011-06-03 06:17:40 +00:00
|
|
|
{
|
|
|
|
QSqlQuery q(db);
|
|
|
|
QVERIFY_SQL(q, exec("insert into " + reltest2 + " values(3, 'mrs')"));
|
|
|
|
model.relationModel(2)->select();
|
|
|
|
}
|
2015-07-24 13:48:21 +00:00
|
|
|
QCOMPARE(model.relationModel(2)->rowCount(), 3);
|
2011-06-03 06:17:40 +00:00
|
|
|
QVERIFY(model.setData(model.index(0,2), 3));
|
|
|
|
QVERIFY(model.submitAll());
|
|
|
|
QCOMPARE(model.data(model.index(0,2)), QVariant("mrs"));
|
|
|
|
}
|
|
|
|
|
2011-12-01 07:57:40 +00:00
|
|
|
/**
|
|
|
|
This test case verifies bug fix for QTBUG-20038.
|
|
|
|
*/
|
|
|
|
void tst_QSqlRelationalTableModel::relationOnFirstColumn()
|
|
|
|
{
|
|
|
|
QFETCH_GLOBAL(QString, dbName);
|
|
|
|
QSqlDatabase db = QSqlDatabase::database(dbName);
|
|
|
|
CHECK_DATABASE(db);
|
|
|
|
|
2013-03-21 10:23:31 +00:00
|
|
|
QString testTable1 = qTableName("QTBUG_20038_test1", __FILE__, db);
|
|
|
|
QString testTable2 = qTableName("QTBUG_20038_test2", __FILE__, db);
|
2011-12-01 07:57:40 +00:00
|
|
|
tst_Databases::safeDropTables(db, QStringList() << testTable1 << testTable2);
|
|
|
|
|
|
|
|
//prepare test1 table
|
|
|
|
QSqlQuery q(db);
|
|
|
|
QVERIFY_SQL(q, exec("CREATE TABLE " + testTable1 + " (val1 INTEGER, id1 INTEGER PRIMARY KEY);"));
|
2015-10-13 07:46:56 +00:00
|
|
|
QVERIFY_SQL(q, exec("DELETE FROM " + testTable1 + QLatin1Char(';')));
|
2011-12-01 07:57:40 +00:00
|
|
|
QVERIFY_SQL(q, exec("INSERT INTO " + testTable1 + " (id1, val1) VALUES(1, 10);"));
|
|
|
|
QVERIFY_SQL(q, exec("INSERT INTO " + testTable1 + " (id1, val1) VALUES(2, 20);"));
|
|
|
|
QVERIFY_SQL(q, exec("INSERT INTO " + testTable1 + " (id1, val1) VALUES(3, 30);"));
|
|
|
|
|
|
|
|
//prepare test2 table
|
|
|
|
QVERIFY_SQL(q, exec("CREATE TABLE " + testTable2 + " (id INTEGER PRIMARY KEY, name TEXT);"));
|
2015-10-13 07:46:56 +00:00
|
|
|
QVERIFY_SQL(q, exec("DELETE FROM " + testTable2 + QLatin1Char(';')));
|
2011-12-01 07:57:40 +00:00
|
|
|
QVERIFY_SQL(q, exec("INSERT INTO " + testTable2 + " (id, name) VALUES (10, 'Hervanta');"));
|
|
|
|
QVERIFY_SQL(q, exec("INSERT INTO " + testTable2 + " (id, name) VALUES (20, 'Keskusta');"));
|
|
|
|
QVERIFY_SQL(q, exec("INSERT INTO " + testTable2 + " (id, name) VALUES (30, 'Annala');"));
|
|
|
|
QVERIFY_SQL(q, exec("INSERT INTO " + testTable2 + " (id, name) VALUES (40, 'Tammela');"));
|
|
|
|
QVERIFY_SQL(q, exec("INSERT INTO " + testTable2 + " (id, name) VALUES (50, 'Amuri');"));
|
|
|
|
|
|
|
|
//set test model
|
|
|
|
QSqlRelationalTableModel model(NULL, db);
|
|
|
|
model.setTable(testTable1);
|
|
|
|
model.setRelation(0, QSqlRelation(testTable2, "id", "name"));
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
|
|
|
|
//verify the data
|
|
|
|
QCOMPARE(model.data(model.index(0, 0)), QVariant("Hervanta"));
|
|
|
|
QCOMPARE(model.data(model.index(1, 0)), QVariant("Keskusta"));
|
|
|
|
QCOMPARE(model.data(model.index(2, 0)), QVariant("Annala"));
|
|
|
|
|
|
|
|
//modify the model data
|
|
|
|
QVERIFY_SQL(model, setData(model.index(0, 0), 40));
|
2012-03-13 00:28:35 +00:00
|
|
|
QVERIFY_SQL(model, submit());
|
2011-12-01 07:57:40 +00:00
|
|
|
QVERIFY_SQL(model, setData(model.index(1, 0), 50));
|
2012-03-13 00:28:35 +00:00
|
|
|
QVERIFY_SQL(model, submit());
|
2011-12-01 07:57:40 +00:00
|
|
|
QVERIFY_SQL(model, setData(model.index(2, 0), 30));
|
|
|
|
|
|
|
|
//verify the data after modificaiton
|
|
|
|
QCOMPARE(model.data(model.index(0, 0)), QVariant("Tammela"));
|
|
|
|
QCOMPARE(model.data(model.index(1, 0)), QVariant("Amuri"));
|
|
|
|
QCOMPARE(model.data(model.index(2, 0)), QVariant("Annala"));
|
|
|
|
|
|
|
|
tst_Databases::safeDropTables(db, QStringList() << testTable1 << testTable2);
|
|
|
|
}
|
|
|
|
|
2017-12-01 15:00:06 +00:00
|
|
|
void tst_QSqlRelationalTableModel::setRelation()
|
|
|
|
{
|
|
|
|
QFETCH_GLOBAL(QString, dbName);
|
|
|
|
QSqlDatabase db = QSqlDatabase::database(dbName);
|
|
|
|
CHECK_DATABASE(db);
|
|
|
|
recreateTestTables(db);
|
|
|
|
|
|
|
|
QSqlRelationalTableModel model(0, db);
|
|
|
|
model.setTable(reltest1);
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
QCOMPARE(model.data(model.index(0, 2)), QVariant(1));
|
|
|
|
|
|
|
|
model.setRelation(2, QSqlRelation(reltest2, "id", "title"));
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
QCOMPARE(model.data(model.index(0, 2)), QVariant("herr"));
|
|
|
|
|
|
|
|
// Check that setting an invalid QSqlRelation() clears the relation
|
|
|
|
model.setRelation(2, QSqlRelation());
|
|
|
|
QVERIFY_SQL(model, select());
|
|
|
|
QCOMPARE(model.data(model.index(0, 2)), QVariant(1));
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
QTEST_MAIN(tst_QSqlRelationalTableModel)
|
|
|
|
#include "tst_qsqlrelationaltablemodel.moc"
|