summaryrefslogtreecommitdiffstats
path: root/tests/auto/q3sqlcursor/tst_q3sqlcursor.cpp
blob: 6f6c26807b1ce99a8f5ffa473dcfa2eb750a62e2 (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
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** 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 either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** 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.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
** $QT_END_LICENSE$
**
****************************************************************************/


#include <QtTest/QtTest>
#include <q3sqlcursor.h>
#include <qsqlfield.h>
#include <qsqldriver.h>


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


//TESTED_FILES=

QT_FORWARD_DECLARE_CLASS(QSqlDatabase)

class tst_Q3SqlCursor : public QObject
{
Q_OBJECT

public:
    tst_Q3SqlCursor();
    virtual ~tst_Q3SqlCursor();


public slots:
    void initTestCase();
    void cleanupTestCase();
    void init();
    void cleanup();
private slots:
    void copyConstructor_data() { generic_data(); }
    void copyConstructor();

    void value_data() { generic_data(); }
    void value();
    void primaryIndex_data() { generic_data(); }
    void primaryIndex();
    void insert_data() { generic_data(); }
    void insert();
    void select_data() { generic_data(); }
    void select();
    void setFilter_data() { generic_data(); }
    void setFilter();
    void setName_data() { generic_data(); }
    void setName();

    // problem specific tests
    void unicode_data() { generic_data(); }
    void unicode();
    void precision_data() { generic_data(); }
    void precision();
    void insertORA_data() { generic_data("QOCI"); }
    void insertORA();
    void batchInsert_data() { generic_data(); }
    void batchInsert();
    void insertSpecial_data() { generic_data(); }
    void insertSpecial();
    void updateNoPK_data() { generic_data(); }
    void updateNoPK();
    void insertFieldNameContainsWS_data() { generic_data(); }
    void insertFieldNameContainsWS(); // For task 117996

private:
    void generic_data(const QString &engine=QString());
    void createTestTables( QSqlDatabase db );
    void dropTestTables( QSqlDatabase db );
    void populateTestTables( QSqlDatabase db );

    tst_Databases dbs;
};

tst_Q3SqlCursor::tst_Q3SqlCursor()
{
}

tst_Q3SqlCursor::~tst_Q3SqlCursor()
{
}

void tst_Q3SqlCursor::generic_data(const QString &engine)
{
    if ( dbs.fillTestTable(engine) == 0 ) {
        if(engine.isEmpty())
	   QSKIP( "No database drivers are available in this Qt configuration", SkipAll );
        else
           QSKIP( (QString("No database drivers of type %1 are available in this Qt configuration").arg(engine)).toLocal8Bit(), SkipAll );
    }
}

void tst_Q3SqlCursor::createTestTables( QSqlDatabase db )
{
    if ( !db.isValid() )
	return;
    QSqlQuery q( db );
    if (tst_Databases::isSqlServer(db)) {
        QVERIFY_SQL(q, exec("SET ANSI_DEFAULTS ON"));
        QVERIFY_SQL(q, exec("SET IMPLICIT_TRANSACTIONS OFF"));
    }
    // please never ever change this table; otherwise fix all tests ;)
    if ( tst_Databases::isMSAccess( db ) ) {
	QVERIFY_SQL(q, exec( "create table " + qTableName( "qtest" ) + " ( id int not null, t_varchar varchar(40) not null,"
			 "t_char char(40), t_numeric number, primary key (id, t_varchar) )" ));
    } else {
	QVERIFY_SQL(q, exec( "create table " + qTableName( "qtest" ) + " ( id int not null, t_varchar varchar(40) not null,"
			 "t_char char(40), t_numeric numeric(6, 3), primary key (id, t_varchar) )" ));
    }

    if ( tst_Databases::isSqlServer( db ) ) {
	//workaround for SQL SERVER since he can store unicode only in nvarchar fields
	QVERIFY_SQL(q, exec("create table " + qTableName("qtest_unicode") + " (id int not null, "
		       "t_varchar nvarchar(40) not null, t_char nchar(40) )" ));
    } else {
	QVERIFY_SQL(q, exec("create table " + qTableName("qtest_unicode") + " (id int not null, "
		       "t_varchar varchar(40) not null," "t_char char(40))" ));
    }

    if (tst_Databases::isMSAccess(db)) {
        QVERIFY_SQL(q, exec("create table " + qTableName("qtest_precision") + " (col1 number)"));
    } else {
        QVERIFY_SQL(q, exec("create table " + qTableName("qtest_precision") + " (col1 numeric(15, 14))"));
    }
}

void tst_Q3SqlCursor::dropTestTables( QSqlDatabase db )
{
    if ( !db.isValid() )
	return;
    QStringList tableNames;
    tableNames << qTableName( "qtest" )
            << qTableName( "qtest_unicode" )
            << qTableName( "qtest_precision" )
            << qTableName( "qtest_ovchar" )
            << qTableName( "qtest_onvchar" )
            << qTableName( "qtestPK" );
    tst_Databases::safeDropTables( db, tableNames );
}

void tst_Q3SqlCursor::populateTestTables( QSqlDatabase db )
{
    if (!db.isValid())
	return;
    QSqlQuery q( db );

    q.exec( "delete from " + qTableName( "qtest" ) ); //not fatal
    QVERIFY_SQL(q, prepare("insert into " + qTableName( "qtest" ) + " (id, t_varchar, t_char, t_numeric) values (?, ?, ?, ?)"));
    q.addBindValue(QVariantList() << 0 << 1 << 2 << 3);
    q.addBindValue(QVariantList() << "VarChar0" << "VarChar1" << "VarChar2" << "VarChar3");
    q.addBindValue(QVariantList() << "Char0" << "Char1" << "Char2" << "Char3");
    q.addBindValue(QVariantList() << 1.1 << 2.2 << 3.3 << 4.4);
    QVERIFY_SQL(q, execBatch());
}

void tst_Q3SqlCursor::initTestCase()
{
    dbs.open();

    for ( QStringList::ConstIterator it = dbs.dbNames.begin(); it != dbs.dbNames.end(); ++it ) {
	QSqlDatabase db = QSqlDatabase::database( (*it) );
	CHECK_DATABASE( db );

	dropTestTables( db ); //in case of leftovers
	createTestTables( db );
	populateTestTables( db );
    }
}

void tst_Q3SqlCursor::cleanupTestCase()
{
    for ( QStringList::ConstIterator it = dbs.dbNames.begin(); it != dbs.dbNames.end(); ++it ) {
	QSqlDatabase db = QSqlDatabase::database( (*it) );
	CHECK_DATABASE( db );
	dropTestTables( db );
    }

    dbs.close();
}

void tst_Q3SqlCursor::init()
{
}

void tst_Q3SqlCursor::cleanup()
{
    QFETCH( QString, dbName );
    QSqlDatabase db = QSqlDatabase::database( dbName );
    CHECK_DATABASE( db );
    if ( QTest::currentTestFailed() ) {
	//since Oracle ODBC totally craps out on error, we init again
	db.close();
	db.open();
    }
}

void tst_Q3SqlCursor::copyConstructor()
{
    QFETCH( QString, dbName );
    QSqlDatabase db = QSqlDatabase::database( dbName );
    CHECK_DATABASE( db );

    Q3SqlCursor cur2;
    {
	Q3SqlCursor cur( qTableName( "qtest" ), true, db );
	QVERIFY_SQL(cur, select( cur.index( QString("id") ) ));
	cur2 = Q3SqlCursor( cur );
	// let "cur" run out of scope...
    }

    QSqlRecord* rec = cur2.primeUpdate();
    Q_ASSERT( rec );
    QCOMPARE( (int)rec->count(), 4 );

    int i = 0;
    while ( cur2.next() ) {
	QVERIFY( cur2.value("id").toInt() == i );
	i++;
    }
}

void tst_Q3SqlCursor::value()
{
    QFETCH( QString, dbName );
    QSqlDatabase db = QSqlDatabase::database( dbName );
    CHECK_DATABASE( db );

    Q3SqlCursor cur( qTableName( "qtest" ), true, db );
    QVERIFY_SQL(cur, select( cur.index( QString("id") ) ));
    int i = 0;
    while ( cur.next() ) {
	QCOMPARE(cur.value("id").toInt(), i);
	i++;
    }
}

void tst_Q3SqlCursor::primaryIndex()
{
    QFETCH( QString, dbName );
    QSqlDatabase db = QSqlDatabase::database( dbName );
    CHECK_DATABASE( db );

    Q3SqlCursor cur( qTableName( "qtest" ), true, db );
    QSqlIndex index = cur.primaryIndex();
    if ( tst_Databases::isMSAccess( db ) ) {
	QCOMPARE( index.fieldName(1).upper(), QString( "ID" ) );
	QCOMPARE( index.fieldName(0).upper(), QString( "T_VARCHAR" ) );
    } else {
	QCOMPARE( index.fieldName(0).upper(), QString( "ID" ) );
	QCOMPARE( index.fieldName(1).upper(), QString( "T_VARCHAR" ) );
    }
    QVERIFY(!index.isDescending(0));
    QVERIFY(!index.isDescending(1));
}

void tst_Q3SqlCursor::insert()
{
    QFETCH( QString, dbName );
    QSqlDatabase db = QSqlDatabase::database( dbName );
    CHECK_DATABASE( db );

    Q3SqlCursor cur( qTableName( "qtest" ), true, db );
    QSqlRecord* irec = cur.primeInsert();
    QVERIFY( irec != 0 );

    // check that primeInsert returns a valid QSqlRecord
    QCOMPARE( (int)irec->count(), 4 );
    if ( ( irec->field( 0 ).type() != QVariant::Int ) &&
	 ( irec->field( 0 ).type() != QVariant::String ) &&
         ( irec->field( 0 ).type() != QVariant::Double ) ) {
	QFAIL( QString( "Wrong datatype %1 for field 'ID'"
	    " (expected Int or String)" ).arg( QVariant::typeToName( irec->field( 0 ).type() ) ) );
    }
    QCOMPARE( QVariant::typeToName( irec->field( 1 ).type() ), QVariant::typeToName( QVariant::String ) );
    QCOMPARE( QVariant::typeToName( irec->field( 2 ).type() ), QVariant::typeToName( QVariant::String ) );
    QVERIFY((QVariant::typeToName(irec->field(3).type()) == QVariant::typeToName(QVariant::Double)) ||
            (QVariant::typeToName(irec->field(3).type()) == QVariant::typeToName(QVariant::String)));
    QCOMPARE( irec->field( 0 ).name().upper(), QString( "ID" ) );
    QCOMPARE( irec->field( 1 ).name().upper(), QString( "T_VARCHAR" ) );
    QCOMPARE( irec->field( 2 ).name().upper(), QString( "T_CHAR" ) );
    QCOMPARE( irec->field( 3 ).name().upper(), QString( "T_NUMERIC" ) );

    irec->setValue( "id", 400 );
    irec->setValue( "t_varchar", "SomeVarChar" );
    irec->setValue( "t_char", "SomeChar" );
    irec->setValue( "t_numeric", 400.400 );

    QCOMPARE( cur.insert(), 1 );

    // restore old test-tables
    populateTestTables( db );
}

void tst_Q3SqlCursor::insertSpecial()
{
    QFETCH( QString, dbName );
    QSqlDatabase db = QSqlDatabase::database( dbName );
    CHECK_DATABASE( db );

    Q3SqlCursor cur( qTableName( "qtest" ), true, db );
    QSqlRecord* irec = cur.primeInsert();
    QVERIFY( irec != 0 );

    QStringList strings;
    strings << "StringWith'ATick" << "StringWith\"Doublequote" << "StringWith\\Backslash" << "StringWith~Tilde";
    strings << "StringWith%Percent" << "StringWith_Underscore" << "StringWith[SquareBracket" << "StringWith{Brace";
    strings << "StringWith''DoubleTick" << "StringWith\\Lot\\of\\Backslash" << "StringWith\"lot\"of\"quotes\"";
    strings << "'StartsAndEndsWithTick'" << "\"StartsAndEndsWithQuote\"";
    strings << "StringWith\nCR" << "StringWith\n\rCRLF";

    int i = 800;

    // INSERT the strings
    QStringList::Iterator it;
    for ( it = strings.begin(); it != strings.end(); ++it ) {
	QSqlRecord* irec = cur.primeInsert();
	QVERIFY( irec != 0 );
	irec->setValue( "id", i );
	irec->setValue( "t_varchar", (*it) );
	irec->setValue( "t_char", (*it) );
	irec->setValue( "t_numeric", (double)i );
	++i;
	QCOMPARE( cur.insert(), 1 );
    }

    QVERIFY( cur.select( "id >= 800 and id < 900" ) );

    int i2 = 800;
    while( cur.next() ) {
	QCOMPARE( cur.value( "id" ).toInt(), i2 );
	QCOMPARE( cur.value( "t_varchar" ).toString().stripWhiteSpace(), strings.at( i2 - 800 ) );
	QCOMPARE( cur.value( "t_char" ).toString().stripWhiteSpace(), strings.at( i2 - 800 ) );
	QCOMPARE( cur.value( "t_numeric" ).toDouble(), (double)i2 );
	++i2;
    }
    QCOMPARE( i, i2 );

    populateTestTables( db );
}

void tst_Q3SqlCursor::batchInsert()
{
    QFETCH( QString, dbName );
    QSqlDatabase db = QSqlDatabase::database( dbName );
    CHECK_DATABASE( db );

    QSqlQuery q( db );
    q.exec( "delete from " + qTableName( "qtest" ) );

    Q3SqlCursor cur( qTableName( "qtest" ), true, db );

    int i = 0;
    for ( ; i < 100; ++i ) {
	QSqlRecord* irec = cur.primeInsert();
	Q_ASSERT( irec );
	irec->setValue( "id", i );
	irec->setValue( "t_varchar", "blah" );
	irec->setValue( "t_char", "blah" );
	irec->setValue( "t_numeric", 1.1 );
	if ( db.driverName().startsWith( "QSQLITE" ) ) {
	    QVERIFY( cur.insert( true ) );
	} else {
	    QCOMPARE( cur.insert( true ), 1 );
        }
    }

    for ( ; i < 200; ++i ) {
        QSqlRecord* irec = cur.primeInsert();
        Q_ASSERT( irec );
        irec->setValue( "id", i );
        irec->setValue( "t_varchar", "blah" );
        irec->setValue( "t_char", "blah" );
        irec->setValue( "t_numeric", 1.1 );
	if ( db.driverName().startsWith( "QSQLITE" ) ) {
	    QVERIFY( cur.insert( false ) );
	} else {
	    QCOMPARE( cur.insert( false ), 1 );
        }
    }

    i = 0;
    QVERIFY_SQL(q, exec( "select * from " + qTableName( "qtest" ) + " order by id" ));
    while ( q.next() ) {
	QCOMPARE( q.value( 0 ).toInt(), i );
	i++;
    }

    QCOMPARE( i, 200 );

    populateTestTables( db );
}

static QString dumpUtf8( const QString& str )
{
    QString res;
    for ( int i = 0; i < (int)str.length(); ++i ) {
	res += "0x" + QString::number( str[ i ].unicode(), 16 ) + ' ';
    }
    return res;
}

void tst_Q3SqlCursor::insertORA()
{
    QFETCH( QString, dbName );
    QSqlDatabase db = QSqlDatabase::database( dbName );
    CHECK_DATABASE( db );

    if (tst_Databases::getOraVersion(db) < 9)
	QSKIP("Need Oracle >= 9", SkipSingle);

    /****** CHARSET TEST ******/

    QSqlQuery q( db );
    QVERIFY_SQL(q, exec( "create table " + qTableName( "qtest_ovchar" ) + " ( id int primary key, t_char varchar(40) )" ));

    static const QString val1( "blah1" );

    Q3SqlCursor cur ( qTableName( "qtest_ovchar" ), true, db );
    QSqlRecord* irec = cur.primeInsert();
    irec->setValue( "id", 1 );
    irec->setValue( "t_char", val1 );
    QVERIFY( cur.insert() );

    QVERIFY_SQL(cur, select());
    QVERIFY( cur.next() );
    if ( cur.value( "t_char" ).toString() != val1 )
	qDebug( QString( "Wrong value for t_char: expected '%1', got '%2'" ).arg( val1 ).arg(
		cur.value( "t_char" ).toString() ) );

    static const unsigned short utf8arr[] = { 0xd792,0xd79c,0xd792,0xd79c,0xd799,0x00 };
    static const QString utf8str = QString::fromUcs2( utf8arr );

    irec = cur.primeInsert();
    irec->setValue( "id", 2 );
    irec->setValue( "t_char", utf8str );
    QVERIFY( cur.insert() );

    QVERIFY_SQL(cur, select( "id=2" ));
    QVERIFY( cur.next() );

    // until qtest knows non-fatal errors we use qDebug instead
    if ( cur.value( "t_char" ).toString() != utf8str )
        qDebug( QString( "Wrong value for t_char: expected '%1', got '%2'" ).arg( dumpUtf8 ( utf8str ) ).arg(
                dumpUtf8( cur.value( "t_char" ).toString() ) ) );

    /****** NCHARSET TEST ********/

    QVERIFY_SQL(q, exec( "create table " + qTableName( "qtest_onvchar" ) + " ( id int primary key, t_nchar nvarchar2(40) )" ));

    Q3SqlCursor cur2 ( qTableName( "qtest_onvchar" ), true, db );
    irec = cur2.primeInsert();
    irec->setValue( "id", 1 );
    irec->setValue( "t_nchar", val1 );
    QVERIFY( cur2.insert() );

    QVERIFY_SQL(cur2, select());
    QVERIFY( cur2.next() );
    if ( cur2.value( "t_nchar" ).toString() != val1 )
        qDebug( QString( "Wrong value for t_nchar: expected '%1', got '%2'" ).arg( val1 ).arg(
                cur2.value( "t_nchar" ).toString() ) );

    irec = cur2.primeInsert();
    irec->setValue( "id", 2 );
    irec->setValue( "t_nchar", utf8str );
    QVERIFY( cur2.insert() );

    QVERIFY_SQL(cur2, select( "id=2" ));
    QVERIFY( cur2.next() );

    // until qtest knows non-fatal errors we use qDebug instead
    if ( cur2.value( "t_nchar" ).toString() != utf8str )
        qDebug( QString( "Wrong value for t_nchar: expected '%1', got '%2'" ).arg( dumpUtf8( utf8str ) ).arg(
                dumpUtf8( cur2.value( "t_nchar" ).toString() ) ) );

}

void tst_Q3SqlCursor::unicode()
{
    QFETCH( QString, dbName );
    QSqlDatabase db = QSqlDatabase::database( dbName );
    CHECK_DATABASE( db );

    static const unsigned short utf8arr[] = { 0xd792,0xd79c,0xd792,0xd79c,0xd799,0x20,0xd7a9,0xd799,0x00 };
    static const QString utf8str = QString::fromUcs2( utf8arr );
    if ( !db.driver()->hasFeature( QSqlDriver::Unicode ) ) {
	 QSKIP( "DBMS not Unicode capable", SkipSingle );
    }

    Q3SqlCursor cur( qTableName( "qtest_unicode" ), true, db );
    QSqlRecord* irec = cur.primeInsert();
    irec->setValue( 0, 500 );
    irec->setValue( 1, utf8str );
    irec->setValue( 2, utf8str );
    QVERIFY_SQL(cur, insert());
    QVERIFY_SQL(cur, select( "id=500" ));
    QVERIFY_SQL(cur, next());
    QString res = cur.value( 1 ).asString();
    cur.primeDelete();
    cur.del();

    if ( res != utf8str ) {
        int i;
        for ( i = 0; i < (int)res.length(); ++i ) {
            if ( res[ i ] != utf8str[ i ] )
                break;
        }
        if(db.driverName().startsWith("QMYSQL") || db.driverName().startsWith("QDB2"))
            qWarning() << "Needs someone with more Unicode knowledge than I have to fix:" << QString( "Strings differ at position %1: orig: %2, db: %3" ).arg( i ).arg( utf8str[ i ].unicode(), 0, 16 ).arg( res[ i ].unicode(), 0, 16 );
        else
            QFAIL( QString( "Strings differ at position %1: orig: %2, db: %3" ).arg( i ).arg( utf8str[ i ].unicode(), 0, 16 ).arg( res[ i ].unicode(), 0, 16 ) );
    }
    if((db.driverName().startsWith("QMYSQL") || db.driverName().startsWith("QDB2")) && res != utf8str)
        QEXPECT_FAIL("", "See above message", Continue);
    QVERIFY( res == utf8str );
}

void tst_Q3SqlCursor::precision()
{
    static const QString precStr = QLatin1String("1.23456789012345");
    static const double precDbl = 2.23456789012345;

    QFETCH( QString, dbName );
    QSqlDatabase db = QSqlDatabase::database( dbName );
    CHECK_DATABASE( db );

    Q3SqlCursor cur( qTableName( "qtest_precision" ), true, db );
    cur.setTrimmed( "col1", true );
    QSqlRecord* irec = cur.primeInsert();
    irec->setValue( 0, precStr );
    QVERIFY( cur.insert() );

    irec = cur.primeInsert();
    irec->setValue( 0, precDbl );
    QVERIFY( cur.insert() );

    QVERIFY_SQL(cur, select());
    QVERIFY( cur.next() );
    if(!tst_Databases::isSqlServer(db))
        QCOMPARE( cur.value( 0 ).asString(), precStr );
    else
        QCOMPARE( cur.value( 0 ).asString(), precStr.left(precStr.size()-1) ); // Sql server fails at counting.
    QVERIFY( cur.next() );
    QCOMPARE( cur.value( 0 ).asDouble(), precDbl );
}

void tst_Q3SqlCursor::setFilter()
{
    QFETCH( QString, dbName );
    QSqlDatabase db = QSqlDatabase::database( dbName );
    CHECK_DATABASE( db );

    Q3SqlCursor cur( qTableName( "qtest" ), true, db );
    cur.setFilter( "id = 2" );

    QVERIFY_SQL(cur, select());
    QVERIFY( cur.next() );
    QCOMPARE( cur.value( "id" ).toInt(), 2 );
    QVERIFY( !cur.next() );

    QVERIFY_SQL(cur, select());
    QVERIFY( cur.next() );
    QCOMPARE( cur.value( "id" ).toInt(), 2 );
    QVERIFY( !cur.next() );

    QVERIFY_SQL(cur, select( "id = 3" ));
    QVERIFY( cur.next() );
    QCOMPARE( cur.value( "id" ).toInt(), 3 );
    QVERIFY( !cur.next() );
    
    QVERIFY_SQL(cur, select());
    QVERIFY( cur.next() );
    QCOMPARE( cur.value( "id" ).toInt(), 3 );
    QVERIFY( !cur.next() );
}

void tst_Q3SqlCursor::select()
{
    QFETCH( QString, dbName );
    QSqlDatabase db = QSqlDatabase::database( dbName );
    CHECK_DATABASE( db );

    Q3SqlCursor cur( qTableName( "qtest" ), true, db );
    QVERIFY_SQL(cur, select());
    QVERIFY( cur.next() );
    QVERIFY( cur.next() );

    Q3SqlCursor cur2( qTableName( "qtest" ), true, db );
    QVERIFY_SQL(cur2, select( "id = 1" ));
    QVERIFY( cur2.next() );
    QCOMPARE( cur2.value( 0 ).toInt(), 1 );

    Q3SqlCursor cur3( qTableName( "qtest" ), true, db );
    QVERIFY_SQL(cur3, select( cur3.primaryIndex( false ) ));
    QVERIFY( cur3.next() );
    QVERIFY( cur3.next() );
    QCOMPARE( cur3.value( 0 ).toInt(), 1 );

    Q3SqlCursor cur4( qTableName( "qtest" ), true, db );
    QSqlIndex idx = cur4.primaryIndex( false );
    QCOMPARE( (int)idx.count(), 2 );
    if ( tst_Databases::isMSAccess( db ) ) {
	QCOMPARE( idx.field( 1 ).name().upper(), QString("ID") );
	QCOMPARE( idx.field( 0 ).name().upper(), QString("T_VARCHAR") );
    } else {
	QCOMPARE( idx.field( 0 ).name().upper(), QString("ID") );
	QCOMPARE( idx.field( 1 ).name().upper(), QString("T_VARCHAR") );
    }

#ifdef QT_DEBUG
    // for people too stupid to read docs we had to insert this debugging message.
    QTest::ignoreMessage(QtDebugMsg, "Q3SqlCursor::setValue(): This will not affect actual database values. Use primeInsert(), primeUpdate() or primeDelete().");
#endif
    cur4.setValue( "id", 1 );
#ifdef QT_DEBUG
    QTest::ignoreMessage(QtDebugMsg, "Q3SqlCursor::setValue(): This will not affect actual database values. Use primeInsert(), primeUpdate() or primeDelete().");
#endif
    cur4.setValue( "t_varchar", "VarChar1" );

    QVERIFY_SQL(cur4, select( idx, cur4.primaryIndex( false ) ));
    QVERIFY( cur4.next() );
    QCOMPARE( cur4.value( 0 ).toInt(), 1 );
}

void tst_Q3SqlCursor::setName()
{
    QFETCH( QString, dbName );
    QSqlDatabase db = QSqlDatabase::database( dbName );
    CHECK_DATABASE( db );

    Q3SqlCursor c2( qTableName( "qtest" ), true, db );
    QCOMPARE( c2.name(), qTableName( "qtest" ) );
    QCOMPARE( c2.fieldName( 0 ).lower(), QString( "id" ) );

    Q3SqlCursor c( QString(), true, db );
    c.setName( qTableName( "qtest" ) );
    QCOMPARE( c.name(), qTableName( "qtest" ) );
    QCOMPARE( c.fieldName( 0 ).lower(), QString( "id" ) );

    c.setName( qTableName( "qtest_precision" ) );
    QCOMPARE( c.name(), qTableName( "qtest_precision" ) );
    QCOMPARE( c.fieldName( 0 ).lower(), QString( "col1" ) );
}

/* Database independent test */
void tst_Q3SqlCursor::updateNoPK()
{
    QFETCH( QString, dbName );
    QSqlDatabase db = QSqlDatabase::database( dbName );
    CHECK_DATABASE( db );
    
    QSqlQuery q(db);
    QVERIFY_SQL(q, exec("create table " + qTableName( "qtestPK" ) + " (id int, name varchar(20), num numeric)"));
    
    Q3SqlCursor cur(qTableName("qtestPK"), true, db);
    QSqlRecord* rec = cur.primeInsert();
    Q_ASSERT(rec);
    rec->setNull(0);
    rec->setNull(1);
    rec->setNull(2);
    QVERIFY_SQL(cur, insert() == 1);
    if (!db.driver()->hasFeature(QSqlDriver::PreparedQueries)) {

        // Only QPSQL, QMYSQL, QODBC and QOCI drivers currently use escape identifiers for column names
        if (db.driverName().startsWith("QPSQL") ||
                db.driverName().startsWith("QMYSQL") ||
                db.driverName().startsWith("QODBC") ||  
                db.driverName().startsWith("QOCI")) {
            QString query = QString::fromLatin1("insert into " + qTableName("qtestPK") +
                                                " (" + db.driver()->escapeIdentifier("id", QSqlDriver::FieldName) + ','
                                                + db.driver()->escapeIdentifier("name", QSqlDriver::FieldName) + ','
                                                + db.driver()->escapeIdentifier("num", QSqlDriver::FieldName) + ')'
                                                + " values (NULL,NULL,NULL)");
            QCOMPARE(cur.lastQuery(), query);
        } else {
	    QCOMPARE(cur.lastQuery(), QString::fromLatin1("insert into " + qTableName("qtestPK") +
						         " (\"id\",\"name\",\"num\") values (NULL,NULL,NULL)"));
        }
    }

    rec = cur.primeUpdate();
    Q_ASSERT(rec);
    rec->setValue(0, 1);
    rec->setNull(1);
    rec->setNull(2);
    // Sqlite returns 2, don't ask why.
    QVERIFY(cur.update() != 0);
    QString expect = "update " + qTableName("qtestPK") +
            " set "+db.driver()->escapeIdentifier("id", QSqlDriver::FieldName)+" = 1 , "
            +db.driver()->escapeIdentifier("name", QSqlDriver::FieldName)+" = NULL , "
            +db.driver()->escapeIdentifier("num", QSqlDriver::FieldName)+" = NULL  where " + qTableName("qtestPK") + ".id"
            " IS NULL and " + qTableName("qtestPK") + ".name IS NULL and " +
            qTableName("qtestPK") + ".num IS NULL";
    if (!db.driver()->hasFeature(QSqlDriver::PreparedQueries)) {
        if (!db.driverName().startsWith("QSQLITE")) {
	    QCOMPARE(cur.lastQuery(), expect);
        }
    }
    QVERIFY(cur.select(cur.index(QString("id"))));
    QVERIFY(cur.next());
    QCOMPARE(cur.value("id").toInt(), 1);
    QVERIFY(cur.isNull("name"));
    QVERIFY(cur.isNull("num")); 
}

// For task 117996: Q3SqlCursor::insert() should not fail even if field names 
// contain white spaces.
void tst_Q3SqlCursor::insertFieldNameContainsWS() {

    QFETCH( QString, dbName );
    QSqlDatabase db = QSqlDatabase::database( dbName );
    CHECK_DATABASE( db );

    // The bugfix (and this test) depends on QSqlDriver::escapeIdentifier(...) 
    // to be implemented, which is currently only the case for the 
    // QPSQL, QODBC and QOCI drivers.
    if (!db.driverName().startsWith("QPSQL") && 
            !db.driverName().startsWith("QODBC") &&  
            !db.driverName().startsWith("QOCI")) {
       QSKIP("PSQL, QODBC or QOCI specific test", SkipSingle);
       return;
    }

    QString tableName = qTableName("qtestws");

    QSqlQuery q(db);
    q.exec(QString("DROP TABLE %1").arg(tableName));
    QString query = "CREATE TABLE %1 (id int, " 
        + db.driver()->escapeIdentifier("first Name", QSqlDriver::FieldName) 
        + " varchar(20), lastName varchar(20))";
    QVERIFY_SQL(q, exec(query.arg(tableName)));

    Q3SqlCursor cur(QString("%1").arg(tableName), true, db);
    cur.select();

    QSqlRecord *r = cur.primeInsert();
    r->setValue("id", 1);
    r->setValue("firsT NaMe", "Kong");
    r->setValue("lastNaMe", "Harald");

    QVERIFY(cur.insert() == 1);

    cur.select();
    cur.next();

    QVERIFY(cur.value(0) == 1);
    QCOMPARE(cur.value(1).toString(), QString("Kong"));
    QCOMPARE(cur.value(2).toString(), QString("Harald"));
    
    q.exec(QString("DROP TABLE %1").arg(tableName));

}

QTEST_MAIN(tst_Q3SqlCursor)
#include "tst_q3sqlcursor.moc"