summaryrefslogtreecommitdiffstats
path: root/tests/auto/qsqlrelationaltablemodel/tst_qsqlrelationaltablemodel.cpp
blob: 8c840cd1d535c06d33b5246f0cb09f731464a681 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/


#include <QtTest/QtTest>
#include <QtSql/QtSql>

#include "../qsqldatabase/tst_databases.h"



//TESTED_CLASS=
//TESTED_FILES=

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();

private:
    void dropTestTables( QSqlDatabase db );
};


void tst_QSqlRelationalTableModel::initTestCase_data()
{
    dbs.open();
    if (dbs.fillTestTable() == 0) {
        qWarning("NO DATABASES");
        QSKIP("No database drivers are available in this Qt configuration", SkipAll);
    }
}

void tst_QSqlRelationalTableModel::recreateTestTables(QSqlDatabase db)
{
    dropTestTables(db);

    QSqlQuery q(db);
    QVERIFY_SQL( q, exec("create table " + qTableName("reltest1") +
            " (id int not null primary key, name varchar(20), title_key int, another_title_key int)"));
    QVERIFY_SQL( q, exec("insert into " + qTableName("reltest1") + " values(1, 'harry', 1, 2)"));
    QVERIFY_SQL( q, exec("insert into " + qTableName("reltest1") + " values(2, 'trond', 2, 1)"));
    QVERIFY_SQL( q, exec("insert into " + qTableName("reltest1") + " values(3, 'vohi', 1, 2)"));
    QVERIFY_SQL( q, exec("insert into " + qTableName("reltest1") + " values(4, 'boris', 2, 2)"));

    QVERIFY_SQL( q, exec("create table " + qTableName("reltest2") + " (tid int not null primary key, title varchar(20))"));
    QVERIFY_SQL( q, exec("insert into " + qTableName("reltest2") + " values(1, 'herr')"));
    QVERIFY_SQL( q, exec("insert into " + qTableName("reltest2") + " values(2, 'mister')"));

    QVERIFY_SQL( q, exec("create table " + qTableName("reltest3") + " (id int not null primary key, name varchar(20), city_key int)"));
    QVERIFY_SQL( q, exec("insert into " + qTableName("reltest3") + " values(1, 'Gustav', 1)"));
    QVERIFY_SQL( q, exec("insert into " + qTableName("reltest3") + " values(2, 'Heidi', 2)"));

    QVERIFY_SQL( q, exec("create table " + qTableName("reltest4") + " (id int not null primary key, name varchar(20))"));
    QVERIFY_SQL( q, exec("insert into " + qTableName("reltest4") + " values(1, 'Oslo')"));
    QVERIFY_SQL( q, exec("insert into " + qTableName("reltest4") + " values(2, 'Trondheim')"));

    QVERIFY_SQL( q, exec("create table " + qTableName("reltest5") + " (title varchar(20) not null primary key, abbrev varchar(20))"));
    QVERIFY_SQL( q, exec("insert into " + qTableName("reltest5") + " values('herr', 'Hr')"));
    QVERIFY_SQL( q, exec("insert into " + qTableName("reltest5") + " values('mister', 'Mr')"));

    if (testWhiteSpaceNames(db.driverName())) {
        QString reltest6 = db.driver()->escapeIdentifier(qTableName("rel test6"), QSqlDriver::TableName);
        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)"));

        QString reltest7 = db.driver()->escapeIdentifier(qTableName("rel test7"), QSqlDriver::TableName);
        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);
        if (db.driverName().startsWith("QIBASE"))
            db.exec("SET DIALECT 3");
        else if (tst_Databases::isSqlServer(db)) {
            db.exec("SET ANSI_DEFAULTS ON");
            db.exec("SET IMPLICIT_TRANSACTIONS OFF");
        }
        else if(tst_Databases::isPostgreSQL(db))
            db.exec("set client_min_messages='warning'");
        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 << qTableName( "reltest1" )
            << qTableName( "reltest2" )
            << qTableName( "reltest3" )
            << qTableName( "reltest4" )
            << qTableName( "reltest5" )
            << qTableName( "rel test6" )
            << qTableName( "rel test7" )
            << qTableName("CASETEST1" )
            << qTableName("casetest1" );
    tst_Databases::safeDropTables( db, tableNames );

    db.exec("DROP SCHEMA "+qTableName("QTBUG_5373")+" CASCADE");
    db.exec("DROP SCHEMA "+qTableName("QTBUG_5373_s2")+" CASCADE");
}

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(qTableName("reltest1"));
    model.setRelation(2, QSqlRelation(qTableName("reltest2"), "tid", "title"));
    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"));

    //try a non-existant index
    QVERIFY2(model.data(model.index(0,4)).isValid() == false,"Invalid index returned valid QVariant");

    //check data retrieval when relational key is a non-integer type
    //in this case a string
    QSqlRelationalTableModel model2(0,db);
    model2.setTable(qTableName("reltest2"));
    model2.setRelation(1, QSqlRelation(qTableName("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);

    // set the values using OnRowChange Strategy
    {
        QSqlRelationalTableModel model(0, db);

        model.setTable(qTableName("reltest1"));
        model.setSort(0, Qt::AscendingOrder);
        model.setRelation(2, QSqlRelation(qTableName("reltest2"), "tid", "title"));
        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(qTableName("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);

        model.setRelation(2, QSqlRelation(qTableName("reltest2"), "tid", "title"));
        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(qTableName("reltest1"));
        model.setEditStrategy(QSqlTableModel::OnFieldChange);
        model.setSort(0, Qt::AscendingOrder);
        model.setRelation(2, QSqlRelation(qTableName("reltest2"), "tid", "title"));
        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(qTableName("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);

        model.setRelation(2, QSqlRelation(qTableName("reltest2"), "tid", "title"));
        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(qTableName("reltest1"));
        model.setRelation(2, QSqlRelation(qTableName("reltest2"), "tid", "title"));

        //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
        if (!db.driverName().startsWith("QTDS"))
            model.setRelation(3, QSqlRelation(qTableName("reltest2"), "tid", "title"));

        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"));
        if (!db.driverName().startsWith("QTDS"))
            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(qTableName("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);

        model.setRelation(2, QSqlRelation(qTableName("reltest2"), "tid", "title"));
        if (!db.driverName().startsWith("QTDS"))
            model.setRelation(3, QSqlRelation(qTableName("reltest2"), "tid", "title"));
        QVERIFY_SQL(model, select());
        QCOMPARE(model.data(model.index(3, 2)).toString(), QString("herr"));

        if (!db.driverName().startsWith("QTDS"))
            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(qTableName("reltest2"));
        model.setRelation(1, QSqlRelation(qTableName("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"));
    }

}

void tst_QSqlRelationalTableModel::multipleRelation()
{
    QFETCH_GLOBAL(QString, dbName);
    QSqlDatabase db = QSqlDatabase::database(dbName);
    CHECK_DATABASE(db);
    recreateTestTables(db);

    QSqlRelationalTableModel model(0, db);

    model.setTable(qTableName("reltest1"));
    model.setRelation(2, QSqlRelation(qTableName("reltest2"), "tid", "title"));
    model.setRelation(3, QSqlRelation(qTableName("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"));
}

void tst_QSqlRelationalTableModel::insertRecord()
{
    QFETCH_GLOBAL(QString, dbName);
    QSqlDatabase db = QSqlDatabase::database(dbName);
    CHECK_DATABASE(db);

    QSqlRelationalTableModel model(0, db);

    model.setTable(qTableName("reltest1"));
    model.setRelation(2, QSqlRelation(qTableName("reltest2"), "tid", "title"));
    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);

    f1.setValue(5);
    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));

    QCOMPARE(model.data(model.index(4, 0)).toInt(), 5);
    QCOMPARE(model.data(model.index(4, 1)).toString(), QString("test"));
    QCOMPARE(model.data(model.index(4, 2)).toString(), QString("herr"));
}

void tst_QSqlRelationalTableModel::setRecord()
{
    QFETCH_GLOBAL(QString, dbName);
    QSqlDatabase db = QSqlDatabase::database(dbName);
    CHECK_DATABASE(db);
    recreateTestTables(db);

    QSqlRelationalTableModel model(0, db);

    model.setTable(qTableName("reltest1"));
    model.setRelation(2, QSqlRelation(qTableName("reltest2"), "tid", "title"));
    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);

    f1.setValue(5);
    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));

    QCOMPARE(model.data(model.index(1, 0)).toInt(), 5);
    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());

    QCOMPARE(model.data(model.index(3, 0)).toInt(), 5);
    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);

    QSqlRelationalTableModel model(0, db);

    model.setTable(qTableName("reltest1"));
    model.setRelation(2, QSqlRelation(qTableName("reltest2"), "tid", "title"));
    model.setSort(0, Qt::AscendingOrder);

    if (!db.driverName().startsWith("QTDS"))
        model.setRelation(3, QSqlRelation(qTableName("reltest2"), "tid", "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"));
    if (!db.driverName().startsWith("QTDS"))
        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"));
    if (!db.driverName().startsWith("QTDS"))
        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"));
    if (!db.driverName().startsWith("QTDS"))
        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);

    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"));

    if (!db.driverName().startsWith("QTDS")) {
        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"));
    if (!db.driverName().startsWith("QTDS"))
        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"));
    if (!db.driverName().startsWith("QTDS"))
        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"));
    if (!db.driverName().startsWith("QTDS"))
        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(qTableName("reltest1"));
    model.setRelation(2, QSqlRelation(qTableName("reltest2"), "tid", "title"));
    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());

}

void tst_QSqlRelationalTableModel::filter()
{
    QFETCH_GLOBAL(QString, dbName);
    QSqlDatabase db = QSqlDatabase::database(dbName);
    CHECK_DATABASE(db);
    recreateTestTables(db);

    QSqlRelationalTableModel model(0, db);

    model.setTable(qTableName("reltest1"));
    model.setRelation(2, QSqlRelation(qTableName("reltest2"), "tid", "title"));
    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"));
}

void tst_QSqlRelationalTableModel::sort()
{
    QFETCH_GLOBAL(QString, dbName);
    QSqlDatabase db = QSqlDatabase::database(dbName);
    CHECK_DATABASE(db);

    QSqlRelationalTableModel model(0, db);

    model.setTable(qTableName("reltest1"));
    model.setRelation(2, QSqlRelation(qTableName("reltest2"), "tid", "title"));
    if (!db.driverName().startsWith("QTDS"))
        model.setRelation(3, QSqlRelation(qTableName("reltest2"), "tid", "title"));

    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());

    if (!db.driverName().startsWith("QTDS")) {
        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);
    }

}

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();
    QVERIFY(model.rowCount() == initialRowCount);

    /* 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(qTableName("reltest1"));
    model.setRelation(2, QSqlRelation(qTableName("reltest2"), "tid", "title"));
    model.setRelation(3, QSqlRelation(qTableName("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;

    /* and again with OnManualSubmit */
    model.setEditStrategy(QSqlTableModel::OnManualSubmit);
    testRevert(model);
}

void tst_QSqlRelationalTableModel::clearDisplayValuesCache()
{
    QFETCH_GLOBAL(QString, dbName);
    QSqlDatabase db = QSqlDatabase::database(dbName);
    CHECK_DATABASE(db);

    QSqlRelationalTableModel model(0, db);

    model.setTable(qTableName("reltest1"));
    model.setRelation(2, QSqlRelation(qTableName("reltest2"), "tid", "title"));

    if (!db.driverName().startsWith("QTDS"))
        model.setRelation(3, QSqlRelation(qTableName("reltest2"), "tid", "title"));
    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"));
    if (!db.driverName().startsWith("QTDS"))
        QCOMPARE(model.data(model.index(3, 3)).toString(), QString("mister"));
    else
        QCOMPARE(model.data(model.index(3, 3)).toInt(), 2 );

    model.insertRow(model.rowCount());
    QVERIFY(model.setData(model.index(4, 0), 5, Qt::EditRole));
    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();

    QCOMPARE(model.data(model.index(0, 0)).toInt(), 5);
    QCOMPARE(model.data(model.index(0, 1)).toString(), QString("anders"));
    QCOMPARE(model.data(model.index(0, 2)).toString(), QString("herr"));
    if (!db.driverName().startsWith("QTDS"))
        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"));
    if (!db.driverName().startsWith("QTDS"))
        QCOMPARE(model.data(model.index(4, 3)).toString(), QString("mister"));
    else
        QCOMPARE(model.data(model.index(4, 3)).toInt(), 2);
}

// For task 140782 and 176374: If the main table and the the related tables uses the same
// 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);

    QSqlRelationalTableModel model(0, db);
    model.setTable(qTableName("reltest3"));
    model.setEditStrategy(QSqlTableModel::OnManualSubmit);
    model.setSort(0, Qt::AscendingOrder);

    // Duplication of "name", used in both reltest3 and reltest4.
    model.setRelation(2, QSqlRelation(qTableName("reltest4"), "id", "name"));
    QVERIFY_SQL(model, select());

    if (db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2")) {
        QCOMPARE(model.record(1).value(qTableName("reltest4").append(QLatin1String("_name_2")).toUpper()).toString(),
            QString("Trondheim"));
    } else {
        QCOMPARE(model.record(1).value(qTableName("reltest4").append(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

    if (db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2")) {
        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.
    if(db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2"))
        QCOMPARE(rec.fieldName(2), (qTableName("reltest4").append(QLatin1String("_name_2"))).toUpper());
    else
        QCOMPARE(rec.fieldName(2), qTableName("reltest4").append(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(qTableName("reltest1"));
    model.setRelation(2, QSqlRelation(qTableName("reltest2"), "tid", "title"));
    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
    QVERIFY(model.setData(model.index(0,10),5) == false);
}

void tst_QSqlRelationalTableModel::relationModel()
{
    QFETCH_GLOBAL(QString, dbName);
    QSqlDatabase db = QSqlDatabase::database(dbName);
    CHECK_DATABASE(db);

    QSqlRelationalTableModel model(0, db);
    model.setTable(qTableName("reltest1"));
    model.setRelation(2, QSqlRelation(qTableName("reltest2"), "tid", "title"));
    QVERIFY_SQL(model, select());

    QVERIFY(model.relationModel(0) == NULL);
    QVERIFY(model.relationModel(1) == NULL);
    QVERIFY(model.relationModel(2) != NULL);
    QVERIFY(model.relationModel(3) == NULL);
    QVERIFY(model.relationModel(4) == NULL);

    model.setRelation(3, QSqlRelation(qTableName("reltest4"), "id", "name"));
    QVERIFY_SQL(model, select());

    QVERIFY(model.relationModel(0) == NULL);
    QVERIFY(model.relationModel(1) == NULL);
    QVERIFY(model.relationModel(2) != NULL);
    QVERIFY(model.relationModel(3) != NULL);
    QVERIFY(model.relationModel(4) == NULL);

    QSqlTableModel *rel_model = model.relationModel(2);
    QCOMPARE(rel_model->data(rel_model->index(0,1)).toString(), QString("herr"));
}

void tst_QSqlRelationalTableModel::casing()
{
    QFETCH_GLOBAL(QString, dbName);
    QSqlDatabase db = QSqlDatabase::database(dbName);
    CHECK_DATABASE(db);

    if (db.driverName().startsWith("QSQLITE") || db.driverName().startsWith("QIBASE") || tst_Databases::isSqlServer(db))
        QSKIP("The casing test for this database is irrelevant since this database does not treat different cases as separate entities", SkipAll);

    QSqlQuery q(db);
    QVERIFY_SQL( q, exec("create table " + qTableName("CASETEST1", db.driver()).toUpper()  +
                " (id int not null primary key, name varchar(20), title_key int, another_title_key int)"));

    if( !q.exec("create table " + qTableName("casetest1", db.driver())  +
                " (ident int not null primary key, name varchar(20), title_key int)"))
        QSKIP("The casing test for this database is irrelevant since this database does not treat different cases as separate entities", SkipAll);

    QVERIFY_SQL( q, exec("insert into " + qTableName("CASETEST1", db.driver()).toUpper() + " values(1, 'harry', 1, 2)"));
    QVERIFY_SQL( q, exec("insert into " + qTableName("CASETEST1", db.driver()).toUpper() + " values(2, 'trond', 2, 1)"));
    QVERIFY_SQL( q, exec("insert into " + qTableName("CASETEST1", db.driver()).toUpper() + " values(3, 'vohi', 1, 2)"));
    QVERIFY_SQL( q, exec("insert into " + qTableName("CASETEST1", db.driver()).toUpper() + " values(4, 'boris', 2, 2)"));
    QVERIFY_SQL( q, exec("insert into " + qTableName("casetest1", db.driver()) + " values(1, 'jerry', 1)"));
    QVERIFY_SQL( q, exec("insert into " + qTableName("casetest1", db.driver()) + " values(2, 'george', 2)"));
    QVERIFY_SQL( q, exec("insert into " + qTableName("casetest1", db.driver()) + " values(4, 'kramer', 2)"));

    if (db.driverName().startsWith("QOCI")) {
        //try an owner that doesn't exist
        QSqlRecord rec = db.driver()->record("doug." + qTableName("CASETEST1", db.driver()).toUpper());
        QCOMPARE( rec.count(), 0);

        //try an owner that does exist
        rec = db.driver()->record(db.userName() + "." +  qTableName("CASETEST1", db.driver()).toUpper());
        QCOMPARE( rec.count(), 4);
    }
    QSqlRecord rec = db.driver()->record(qTableName("CASETEST1", db.driver()).toUpper());
    QCOMPARE( rec.count(), 4);

    rec = db.driver()->record(qTableName("casetest1", db.driver()));
    QCOMPARE( rec.count(), 3);

    QSqlTableModel upperCaseModel(0, db);
    upperCaseModel.setTable(qTableName("CASETEST1", db.driver()).toUpper());

    QCOMPARE(upperCaseModel.tableName(),qTableName("CASETEST1",db.driver()).toUpper());

    QVERIFY_SQL(upperCaseModel, select());

    QCOMPARE(upperCaseModel.rowCount(), 4);

    QSqlTableModel lowerCaseModel(0, db);
    lowerCaseModel.setTable(qTableName("casetest1", db.driver()));
    QCOMPARE(lowerCaseModel.tableName(), qTableName("casetest1",db.driver()));
    QVERIFY_SQL(lowerCaseModel, select());

    QCOMPARE(lowerCaseModel.rowCount(), 3);

    QSqlRelationalTableModel model(0, db);
    model.setTable(qTableName("CASETEST1", db.driver()).toUpper());
    model.setRelation(2, QSqlRelation(qTableName("reltest2"), "tid", "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"));
}

void tst_QSqlRelationalTableModel::escapedRelations()
{
    QFETCH_GLOBAL(QString, dbName);
    QSqlDatabase db = QSqlDatabase::database(dbName);
    CHECK_DATABASE(db);
    recreateTestTables(db);

    QSqlRelationalTableModel model(0, db);
    model.setTable(qTableName("reltest1"));

    //try with relation table name quoted
    if (db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2")) {
        model.setRelation(2, QSqlRelation(db.driver()->escapeIdentifier(qTableName("reltest2").toUpper(),QSqlDriver::TableName),
                            "tid",
                            "title"));
    } else {
        model.setRelation(2, QSqlRelation(db.driver()->escapeIdentifier(qTableName("reltest2"),QSqlDriver::TableName),
                            "tid",
                            "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"));

    //try with index column quoted
    if (db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2")) {
        model.setRelation(2, QSqlRelation(qTableName("reltest2"),
                            db.driver()->escapeIdentifier("tid", QSqlDriver::FieldName).toUpper(),
                            "title"));
    } else {
        model.setRelation(2, QSqlRelation(qTableName("reltest2"),
                            db.driver()->escapeIdentifier("tid", QSqlDriver::FieldName),
                            "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"));

    //try with display column quoted

    if (db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2")) {

        model.setRelation(2, QSqlRelation(qTableName("reltest2"),
                            "tid",
                            db.driver()->escapeIdentifier("title", QSqlDriver::FieldName).toUpper()));
    } else {
        model.setRelation(2, QSqlRelation(qTableName("reltest2"),
                            "tid",
                            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"));

    //try with tablename and index and display columns quoted in the relation

    if (db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2")) {
        model.setRelation(2, QSqlRelation(qTableName("reltest2"),
                            "tid",
                            db.driver()->escapeIdentifier("title", QSqlDriver::FieldName).toUpper()));
    } else {
        model.setRelation(2, QSqlRelation(qTableName("reltest2"),
                            "tid",
                            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"));
}

void tst_QSqlRelationalTableModel::escapedTableName()
{
    QFETCH_GLOBAL(QString, dbName);
    QSqlDatabase db = QSqlDatabase::database(dbName);
    CHECK_DATABASE(db);

    // set the values using OnRowChange Strategy with an escaped tablename
    {
        QSqlRelationalTableModel model(0, db);

        if (db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2")) {
            model.setTable(db.driver()->escapeIdentifier(qTableName("reltest1").toUpper(), QSqlDriver::TableName));
        } else {
            model.setTable(db.driver()->escapeIdentifier(qTableName("reltest1"), QSqlDriver::TableName));
        }
        model.setSort(0, Qt::AscendingOrder);
        model.setRelation(2, QSqlRelation(qTableName("reltest2"), "tid", "title"));
        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(qTableName("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);

        model.setRelation(2, QSqlRelation(qTableName("reltest2"), "tid", "title"));
        QVERIFY_SQL(model, select());
        QCOMPARE(model.data(model.index(0, 2)).toString(), QString("mister"));
        QCOMPARE(model.data(model.index(3,2)).toString(), QString("herr"));

    }
}

void tst_QSqlRelationalTableModel::whiteSpaceInIdentifiers()
{
    QFETCH_GLOBAL(QString, dbName);
    QSqlDatabase db = QSqlDatabase::database(dbName);
    CHECK_DATABASE(db);

    if (!testWhiteSpaceNames(db.driverName()))
        QSKIP("White space test irrelevant for driver", SkipAll);
    QSqlRelationalTableModel model(0, db);
    model.setTable(db.driver()->escapeIdentifier(qTableName("rel test6"), QSqlDriver::TableName));
    model.setSort(0, Qt::DescendingOrder);
    model.setRelation(1, QSqlRelation(db.driver()->escapeIdentifier(qTableName("rel test7"), QSqlDriver::TableName),
                        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"));

    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();

    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);

    //TODO: For some reson setting a record using manual submit fails
    //model.setEditStrategy(QSqlTableModel::OnManualSubmit);

    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);

    if(!tst_Databases::isPostgreSQL(db)) {
        QSKIP("Postgresql specific test", SkipSingle);
        return;
    }
    QSqlRelationalTableModel model(0, db);
    QSqlQuery q(db);
    QVERIFY_SQL(q, exec("create schema "+qTableName("QTBUG_5373")));
    QVERIFY_SQL(q, exec("create schema "+qTableName("QTBUG_5373_s2")));
    QVERIFY_SQL(q, exec("create table "+qTableName("QTBUG_5373")+"."+qTableName("user")+"(userid int primary key, relatingid int)"));
    QVERIFY_SQL(q, exec("create table "+qTableName("QTBUG_5373_s2")+"."+qTableName("user2")+"(userid2 int primary key, username2 char(40))"));
    model.setTable(qTableName("QTBUG_5373")+"."+qTableName("user"));
    model.setRelation(1, QSqlRelation(qTableName("QTBUG_5373_s2")+"."+qTableName("user2"), "userid2", "username2"));
    QVERIFY_SQL(model, select());
}

QTEST_MAIN(tst_QSqlRelationalTableModel)
#include "tst_qsqlrelationaltablemodel.moc"