tests/auto/qsqlquery/tst_qsqlquery.cpp
changeset 19 fcece45ef507
parent 18 2f34d5167611
child 14 c0432d11811c
equal deleted inserted replaced
18:2f34d5167611 19:fcece45ef507
    41 
    41 
    42 #include <QtTest/QtTest>
    42 #include <QtTest/QtTest>
    43 #include <QtSql/QtSql>
    43 #include <QtSql/QtSql>
    44 
    44 
    45 #include "../qsqldatabase/tst_databases.h"
    45 #include "../qsqldatabase/tst_databases.h"
       
    46 
       
    47 const QString qtest(qTableName( "qtest", __FILE__ ));
    46 
    48 
    47 //TESTED_FILES=
    49 //TESTED_FILES=
    48 
    50 
    49 class tst_QSqlQuery : public QObject
    51 class tst_QSqlQuery : public QObject
    50 {
    52 {
   203     void QTBUG_6421();
   205     void QTBUG_6421();
   204     void QTBUG_6618_data() { generic_data("QODBC"); }
   206     void QTBUG_6618_data() { generic_data("QODBC"); }
   205     void QTBUG_6618();
   207     void QTBUG_6618();
   206     void QTBUG_6852_data() { generic_data("QMYSQL"); }
   208     void QTBUG_6852_data() { generic_data("QMYSQL"); }
   207     void QTBUG_6852();
   209     void QTBUG_6852();
       
   210     void QTBUG_5765_data() { generic_data("QMYSQL"); }
       
   211     void QTBUG_5765();
       
   212 
       
   213 #if 0
       
   214     void benchmark_data() { generic_data(); }
       
   215     void benchmark();
       
   216 #endif
   208 
   217 
   209 private:
   218 private:
   210     // returns all database connections
   219     // returns all database connections
   211     void generic_data(const QString &engine=QString());
   220     void generic_data(const QString &engine=QString());
   212     void dropTestTables( QSqlDatabase db );
   221     void dropTestTables( QSqlDatabase db );
   286 
   295 
   287 void tst_QSqlQuery::dropTestTables( QSqlDatabase db )
   296 void tst_QSqlQuery::dropTestTables( QSqlDatabase db )
   288 {
   297 {
   289     QStringList tablenames;
   298     QStringList tablenames;
   290     // drop all the table in case a testcase failed
   299     // drop all the table in case a testcase failed
   291     tablenames <<  qTableName( "qtest" )
   300     tablenames <<  qtest
   292                << qTableName( "qtest_null" )
   301                << qTableName( "qtest_null", __FILE__ )
   293                << qTableName( "qtest_blob" )
   302                << qTableName( "qtest_blob", __FILE__ )
   294                << qTableName( "qtest_bittest" )
   303                << qTableName( "qtest_bittest", __FILE__ )
   295                << qTableName( "qtest_nullblob" )
   304                << qTableName( "qtest_nullblob", __FILE__ )
   296                << qTableName( "qtest_rawtest" )
   305                << qTableName( "qtest_rawtest", __FILE__ )
   297                << qTableName( "qtest_precision" )
   306                << qTableName( "qtest_precision", __FILE__ )
   298                << qTableName( "qtest_prepare" )
   307                << qTableName( "qtest_prepare", __FILE__ )
   299                << qTableName( "qtestj1" )
   308                << qTableName( "qtestj1", __FILE__ )
   300                << qTableName( "qtestj2" )
   309                << qTableName( "qtestj2", __FILE__ )
   301                << qTableName( "char1Select" )
   310                << qTableName( "char1Select", __FILE__ )
   302                << qTableName( "char1SelectUnicode" )
   311                << qTableName( "char1SU", __FILE__ )
   303                << qTableName( "qxmltest" )
   312                << qTableName( "qxmltest", __FILE__ )
   304                << qTableName( "qtest_exerr" )
   313                << qTableName( "qtest_exerr", __FILE__ )
   305                << qTableName( "qtest_empty" )
   314                << qTableName( "qtest_empty", __FILE__ )
   306                << qTableName( "clobby" )
   315                << qTableName( "clobby", __FILE__ )
   307                << qTableName( "bindtest" )
   316                << qTableName( "bindtest", __FILE__ )
   308                << qTableName( "more_results" )
   317                << qTableName( "more_results", __FILE__ )
   309                << qTableName( "blobstest" )
   318                << qTableName( "blobstest", __FILE__ )
   310                << qTableName( "oraRowId" )
   319                << qTableName( "oraRowId", __FILE__ )
   311                << qTableName( "qtest_batch" )
   320                << qTableName( "qtest_batch", __FILE__ )
   312                << qTableName(QLatin1String("bug6421")).toUpper();
   321                << qTableName("bug6421", __FILE__).toUpper()
       
   322                << qTableName("bug5765", __FILE__)
       
   323                << qTableName("bug6852", __FILE__)
       
   324                << qTableName( "qtest_lockedtable", __FILE__ )
       
   325                << qTableName( "Planet", __FILE__ )
       
   326                << qTableName( "task_250026", __FILE__ )
       
   327                << qTableName( "task_234422", __FILE__ )
       
   328                << qTableName("test141895", __FILE__);
   313 
   329 
   314     if ( db.driverName().startsWith("QPSQL") )
   330     if ( db.driverName().startsWith("QPSQL") )
   315         tablenames << qTableName("task_233829");
   331         tablenames << qTableName("task_233829", __FILE__);
   316 
   332 
   317     if ( db.driverName().startsWith("QSQLITE") )
   333     if ( db.driverName().startsWith("QSQLITE") )
   318         tablenames << qTableName( "record_sqlite" );
   334         tablenames << qTableName( "record_sqlite", __FILE__ );
   319 
   335 
   320     if ( tst_Databases::isSqlServer( db ) || db.driverName().startsWith( "QOCI" ) )
   336     if ( tst_Databases::isSqlServer( db ) || db.driverName().startsWith( "QOCI" ) )
   321         tablenames << qTableName( "qtest_longstr" );
   337         tablenames << qTableName( "qtest_longstr", __FILE__ );
   322 
   338 
   323     tablenames <<  qTableName( "qtest_lockedtable" );
   339     if (tst_Databases::isSqlServer( db ))
   324 
   340         db.exec("DROP PROCEDURE " + qTableName("test141895_proc", __FILE__));
   325     tablenames <<  qTableName( "Planet" );
   341 
   326 
   342     if (tst_Databases::isMySQL( db ))
   327     tablenames << qTableName( "task_250026" );
   343         db.exec("DROP PROCEDURE IF EXISTS "+qTableName("bug6852_proc", __FILE__));
   328     tablenames << qTableName( "task_234422" );
       
   329 
       
   330     if (tst_Databases::isSqlServer( db )) {
       
   331         QSqlQuery q( db );
       
   332         q.exec("DROP PROCEDURE " + qTableName("test141895_proc"));
       
   333     }
       
   334 
       
   335     tablenames << qTableName("test141895");
       
   336 
   344 
   337     tst_Databases::safeDropTables( db, tablenames );
   345     tst_Databases::safeDropTables( db, tablenames );
   338 
   346 
   339     if ( db.driverName().startsWith( "QOCI" ) ) {
   347     if ( db.driverName().startsWith( "QOCI" ) ) {
   340         QSqlQuery q( db );
   348         QSqlQuery q( db );
   341         q.exec( "DROP PACKAGE " + qTableName("pkg") );
   349         q.exec( "DROP PACKAGE " + qTableName("pkg", __FILE__) );
   342     }
   350     }
   343 }
   351 }
   344 
   352 
   345 void tst_QSqlQuery::createTestTables( QSqlDatabase db )
   353 void tst_QSqlQuery::createTestTables( QSqlDatabase db )
   346 {
   354 {
   352         q.exec( "set table_type=innodb" );
   360         q.exec( "set table_type=innodb" );
   353     else if(tst_Databases::isPostgreSQL(db))
   361     else if(tst_Databases::isPostgreSQL(db))
   354         QVERIFY_SQL( q, exec("set client_min_messages='warning'"));
   362         QVERIFY_SQL( q, exec("set client_min_messages='warning'"));
   355 
   363 
   356     if(tst_Databases::isPostgreSQL(db))
   364     if(tst_Databases::isPostgreSQL(db))
   357         QVERIFY_SQL( q, exec( "create table " + qTableName( "qtest" ) + " (id serial NOT NULL, t_varchar varchar(20), t_char char(20), primary key(id)) WITH OIDS" ) );
   365         QVERIFY_SQL( q, exec( "create table " + qtest + " (id serial NOT NULL, t_varchar varchar(20), t_char char(20), primary key(id)) WITH OIDS" ) );
   358     else
   366     else
   359         QVERIFY_SQL( q, exec( "create table " + qTableName( "qtest" ) + " (id int "+tst_Databases::autoFieldName(db) +" NOT NULL, t_varchar varchar(20), t_char char(20), primary key(id))" ) );
   367         QVERIFY_SQL( q, exec( "create table " + qtest + " (id int "+tst_Databases::autoFieldName(db) +" NOT NULL, t_varchar varchar(20), t_char char(20), primary key(id))" ) );
   360 
   368 
   361     if ( tst_Databases::isSqlServer( db ) || db.driverName().startsWith( "QTDS" ) )
   369     if ( tst_Databases::isSqlServer( db ) || db.driverName().startsWith( "QTDS" ) )
   362         QVERIFY_SQL( q, exec( "create table " + qTableName( "qtest_null" ) + " (id int null, t_varchar varchar(20) null)" ) );
   370         QVERIFY_SQL( q, exec( "create table " + qTableName( "qtest_null", __FILE__ ) + " (id int null, t_varchar varchar(20) null)" ) );
   363     else
   371     else
   364         QVERIFY_SQL( q, exec( "create table " + qTableName( "qtest_null" ) + " (id int, t_varchar varchar(20))" ) );
   372         QVERIFY_SQL( q, exec( "create table " + qTableName( "qtest_null", __FILE__ ) + " (id int, t_varchar varchar(20))" ) );
   365 }
   373 }
   366 
   374 
   367 void tst_QSqlQuery::populateTestTables( QSqlDatabase db )
   375 void tst_QSqlQuery::populateTestTables( QSqlDatabase db )
   368 {
   376 {
   369     QSqlQuery q( db );
   377     QSqlQuery q( db );
   370     q.exec( "delete from " + qTableName( "qtest" ) );
   378     const QString qtest_null(qTableName( "qtest_null", __FILE__ ));
   371     QVERIFY_SQL( q, exec( "insert into " + qTableName( "qtest" ) + " values (1, 'VarChar1', 'Char1')" ) );
   379     q.exec( "delete from " + qtest );
   372     QVERIFY_SQL( q, exec( "insert into " + qTableName( "qtest" ) + " values (2, 'VarChar2', 'Char2')" ) );
   380     QVERIFY_SQL( q, exec( "insert into " + qtest + " values (1, 'VarChar1', 'Char1')" ) );
   373     QVERIFY_SQL( q, exec( "insert into " + qTableName( "qtest" ) + " values (3, 'VarChar3', 'Char3')" ) );
   381     QVERIFY_SQL( q, exec( "insert into " + qtest + " values (2, 'VarChar2', 'Char2')" ) );
   374     QVERIFY_SQL( q, exec( "insert into " + qTableName( "qtest" ) + " values (4, 'VarChar4', 'Char4')" ) );
   382     QVERIFY_SQL( q, exec( "insert into " + qtest + " values (3, 'VarChar3', 'Char3')" ) );
   375     QVERIFY_SQL( q, exec( "insert into " + qTableName( "qtest" ) + " values (5, 'VarChar5', 'Char5')" ) );
   383     QVERIFY_SQL( q, exec( "insert into " + qtest + " values (4, 'VarChar4', 'Char4')" ) );
   376 
   384     QVERIFY_SQL( q, exec( "insert into " + qtest + " values (5, 'VarChar5', 'Char5')" ) );
   377     q.exec( "delete from " + qTableName( "qtest_null" ) );
   385 
   378     QVERIFY_SQL( q, exec( "insert into " + qTableName( "qtest_null" ) + " values (0, NULL)" ) );
   386     q.exec( "delete from " + qtest_null );
   379     QVERIFY_SQL( q, exec( "insert into " + qTableName( "qtest_null" ) + " values (1, 'n')" ) );
   387     QVERIFY_SQL( q, exec( "insert into " + qtest_null + " values (0, NULL)" ) );
   380     QVERIFY_SQL( q, exec( "insert into " + qTableName( "qtest_null" ) + " values (2, 'i')" ) );
   388     QVERIFY_SQL( q, exec( "insert into " + qtest_null + " values (1, 'n')" ) );
   381     QVERIFY_SQL( q, exec( "insert into " + qTableName( "qtest_null" ) + " values (3, NULL)" ) );
   389     QVERIFY_SQL( q, exec( "insert into " + qtest_null + " values (2, 'i')" ) );
       
   390     QVERIFY_SQL( q, exec( "insert into " + qtest_null + " values (3, NULL)" ) );
   382 }
   391 }
   383 
   392 
   384 // There were problems with char fields of size 1
   393 // There were problems with char fields of size 1
   385 void tst_QSqlQuery::char1Select()
   394 void tst_QSqlQuery::char1Select()
   386 {
   395 {
   388     QSqlDatabase db = QSqlDatabase::database( dbName );
   397     QSqlDatabase db = QSqlDatabase::database( dbName );
   389     CHECK_DATABASE( db );
   398     CHECK_DATABASE( db );
   390 
   399 
   391     {
   400     {
   392         QSqlQuery q( db );
   401         QSqlQuery q( db );
   393         QVERIFY_SQL( q, exec( "create table " + qTableName( "char1Select" ) + " (id char(1))" ) );
   402         QVERIFY_SQL( q, exec( "create table " + qTableName( "char1Select", __FILE__ ) + " (id char(1))" ) );
   394         QVERIFY_SQL( q, exec( "insert into " + qTableName( "char1Select" ) + " values ('a')" ) );
   403         QVERIFY_SQL( q, exec( "insert into " + qTableName( "char1Select", __FILE__ ) + " values ('a')" ) );
   395         QVERIFY_SQL( q, exec( "select * from " + qTableName( "char1Select" ) ) );
   404         QVERIFY_SQL( q, exec( "select * from " + qTableName( "char1Select", __FILE__ ) ) );
   396         QVERIFY( q.next() );
   405         QVERIFY( q.next() );
   397 
   406 
   398         if ( db.driverName().startsWith( "QIBASE" ) )
   407         if ( db.driverName().startsWith( "QIBASE" ) )
   399             QCOMPARE( q.value( 0 ).toString().left( 1 ), QString( "a" ) );
   408             QCOMPARE( q.value( 0 ).toString().left( 1 ), QString( "a" ) );
   400         else
   409         else
   419 
   428 
   420         if ( db.driverName().startsWith( "QMYSQL" ) && tst_Databases::getMySqlVersion( db ).section( QChar('.'), 0, 0 ).toInt()<5 )
   429         if ( db.driverName().startsWith( "QMYSQL" ) && tst_Databases::getMySqlVersion( db ).section( QChar('.'), 0, 0 ).toInt()<5 )
   421             QSKIP( "Test requires MySQL >= 5.0", SkipSingle );
   430             QSKIP( "Test requires MySQL >= 5.0", SkipSingle );
   422 
   431 
   423         QString createQuery;
   432         QString createQuery;
       
   433         const QString char1SelectUnicode(qTableName( "char1SU", __FILE__ ));
   424 
   434 
   425         if ( tst_Databases::isSqlServer( db ) )
   435         if ( tst_Databases::isSqlServer( db ) )
   426             createQuery = "create table " + qTableName( "char1SelectUnicode" ) + "(id nchar(1))";
   436             createQuery = "create table " + char1SelectUnicode + "(id nchar(1))";
   427         else if ( db.driverName().startsWith( "QDB2" )
   437         else if ( db.driverName().startsWith( "QDB2" )
   428                   || db.driverName().startsWith( "QOCI" )
   438                   || db.driverName().startsWith( "QOCI" )
   429                   || db.driverName().startsWith( "QPSQL" ) )
   439                   || db.driverName().startsWith( "QPSQL" ) )
   430             createQuery = "create table " + qTableName( "char1SelectUnicode" ) + " (id char(3))";
   440             createQuery = "create table " + char1SelectUnicode + " (id char(3))";
   431         else if ( db.driverName().startsWith( "QIBASE" ) )
   441         else if ( db.driverName().startsWith( "QIBASE" ) )
   432             createQuery = "create table " + qTableName( "char1SelectUnicode" ) +
   442             createQuery = "create table " + char1SelectUnicode +
   433                           " (id char(1) character set unicode_fss)";
   443                           " (id char(1) character set unicode_fss)";
   434         else if ( db.driverName().startsWith( "QMYSQL" ) )
   444         else if ( db.driverName().startsWith( "QMYSQL" ) )
   435             createQuery = "create table " + qTableName( "char1SelectUnicode" ) + " (id char(1)) "
   445             createQuery = "create table " + char1SelectUnicode + " (id char(1)) "
   436                           "default character set 'utf8'";
   446                           "default character set 'utf8'";
   437         else
   447         else
   438             createQuery = "create table " + qTableName( "char1SelectUnicode" ) + " (id char(1))";
   448             createQuery = "create table " + char1SelectUnicode + " (id char(1))";
   439 
   449 
   440         QVERIFY_SQL( q, exec( createQuery ) );
   450         QVERIFY_SQL( q, exec( createQuery ) );
   441 
   451 
   442         QVERIFY_SQL( q, prepare( "insert into " + qTableName( "char1SelectUnicode" ) + " values(?)" ) );
   452         QVERIFY_SQL( q, prepare( "insert into " + char1SelectUnicode + " values(?)" ) );
   443 
   453 
   444         q.bindValue( 0, uniStr );
   454         q.bindValue( 0, uniStr );
   445 
   455 
   446         QVERIFY_SQL( q, exec() );
   456         QVERIFY_SQL( q, exec() );
   447 
   457 
   448         QVERIFY_SQL( q, exec( "select * from " + qTableName( "char1SelectUnicode" ) ) );
   458         QVERIFY_SQL( q, exec( "select * from " + char1SelectUnicode ) );
   449 
   459 
   450         QVERIFY( q.next() );
   460         QVERIFY( q.next() );
   451 
   461 
   452         if ( !q.value( 0 ).toString().isEmpty() )
   462         if ( !q.value( 0 ).toString().isEmpty() )
   453             QCOMPARE( q.value( 0 ).toString()[ 0 ].unicode(), uniStr[0].unicode() );
   463             QCOMPARE( q.value( 0 ).toString()[ 0 ].unicode(), uniStr[0].unicode() );
   463 void tst_QSqlQuery::oraRowId()
   473 void tst_QSqlQuery::oraRowId()
   464 {
   474 {
   465     QFETCH( QString, dbName );
   475     QFETCH( QString, dbName );
   466     QSqlDatabase db = QSqlDatabase::database( dbName );
   476     QSqlDatabase db = QSqlDatabase::database( dbName );
   467     CHECK_DATABASE( db );
   477     CHECK_DATABASE( db );
   468 
   478     const QString oraRowId(qTableName("oraRowId", __FILE__));
   469     QSqlQuery q( db );
   479 
   470     QVERIFY_SQL( q, exec( "select rowid from " + qTableName( "qtest" ) ) );
   480     QSqlQuery q( db );
       
   481     QVERIFY_SQL( q, exec( "select rowid from " + qtest ) );
   471     QVERIFY( q.next() );
   482     QVERIFY( q.next() );
   472     QCOMPARE( q.value( 0 ).type(), QVariant::String );
   483     QCOMPARE( q.value( 0 ).type(), QVariant::String );
   473     QVERIFY( !q.value( 0 ).toString().isEmpty() );
   484     QVERIFY( !q.value( 0 ).toString().isEmpty() );
   474 
   485 
   475     QVERIFY_SQL( q, exec( "create table " + qTableName( "oraRowId" ) + " (id char(1))" ) );
   486     QVERIFY_SQL( q, exec( "create table " + oraRowId + " (id char(1))" ) );
   476 
   487 
   477     QVERIFY_SQL( q, exec( "insert into " + qTableName( "oraRowId" ) + " values('a')" ) );
   488     QVERIFY_SQL( q, exec( "insert into " + oraRowId + " values('a')" ) );
   478     QVariant v1 = q.lastInsertId();
   489     QVariant v1 = q.lastInsertId();
   479     QVERIFY( v1.isValid() );
   490     QVERIFY( v1.isValid() );
   480 
   491 
   481     QVERIFY_SQL( q, exec( "insert into " + qTableName( "oraRowId" ) + " values('b')" ) );
   492     QVERIFY_SQL( q, exec( "insert into " + oraRowId + " values('b')" ) );
   482     QVariant v2 = q.lastInsertId();
   493     QVariant v2 = q.lastInsertId();
   483     QVERIFY( v2.isValid() );
   494     QVERIFY( v2.isValid() );
   484 
   495 
   485     QVERIFY_SQL( q, prepare( "select * from " + qTableName( "oraRowId" ) + " where rowid = ?" ) );
   496     QVERIFY_SQL( q, prepare( "select * from " + oraRowId + " where rowid = ?" ) );
   486     q.addBindValue( v1 );
   497     q.addBindValue( v1 );
   487     QVERIFY_SQL( q, exec() );
   498     QVERIFY_SQL( q, exec() );
   488     QVERIFY( q.next() );
   499     QVERIFY( q.next() );
   489     QCOMPARE( q.value( 0 ).toString(), QString( "a" ) );
   500     QCOMPARE( q.value( 0 ).toString(), QString( "a" ) );
   490 
   501 
   497 void tst_QSqlQuery::mysqlOutValues()
   508 void tst_QSqlQuery::mysqlOutValues()
   498 {
   509 {
   499     QFETCH( QString, dbName );
   510     QFETCH( QString, dbName );
   500     QSqlDatabase db = QSqlDatabase::database( dbName );
   511     QSqlDatabase db = QSqlDatabase::database( dbName );
   501     CHECK_DATABASE( db );
   512     CHECK_DATABASE( db );
       
   513     const QString hello(qTableName( "hello", __FILE__ )), qtestproc(qTableName( "qtestproc", __FILE__ ));
   502 
   514 
   503     QSqlQuery q( db );
   515     QSqlQuery q( db );
   504 
   516 
   505     if ( db.driverName().startsWith( "QMYSQL" ) && tst_Databases::getMySqlVersion( db ).section( QChar('.'), 0, 0 ).toInt()<5 )
   517     if ( db.driverName().startsWith( "QMYSQL" ) && tst_Databases::getMySqlVersion( db ).section( QChar('.'), 0, 0 ).toInt()<5 )
   506         QSKIP( "Test requires MySQL >= 5.0", SkipSingle );
   518         QSKIP( "Test requires MySQL >= 5.0", SkipSingle );
   507 
   519 
   508     q.exec( "drop function " + qTableName( "hello" ) );
   520     q.exec( "drop function " + hello );
   509 
   521 
   510     QVERIFY_SQL( q, exec( "create function " + qTableName( "hello" ) + " (s char(20)) returns varchar(50) return concat('Hello ', s)" ) );
   522     QVERIFY_SQL( q, exec( "create function " + hello + " (s char(20)) returns varchar(50) return concat('Hello ', s)" ) );
   511 
   523 
   512     QVERIFY_SQL( q, exec( "select " + qTableName( "hello" ) + "('world')" ) );
   524     QVERIFY_SQL( q, exec( "select " + hello + "('world')" ) );
   513     QVERIFY_SQL( q, next() );
   525     QVERIFY_SQL( q, next() );
   514 
   526 
   515     QCOMPARE( q.value( 0 ).toString(), QString( "Hello world" ) );
   527     QCOMPARE( q.value( 0 ).toString(), QString( "Hello world" ) );
   516 
   528 
   517     QVERIFY_SQL( q, prepare( "select " + qTableName( "hello" ) + "('harald')" ) );
   529     QVERIFY_SQL( q, prepare( "select " + hello + "('harald')" ) );
   518     QVERIFY_SQL( q, exec() );
   530     QVERIFY_SQL( q, exec() );
   519     QVERIFY_SQL( q, next() );
   531     QVERIFY_SQL( q, next() );
   520 
   532 
   521     QCOMPARE( q.value( 0 ).toString(), QString( "Hello harald" ) );
   533     QCOMPARE( q.value( 0 ).toString(), QString( "Hello harald" ) );
   522 
   534 
   523     QVERIFY_SQL( q, exec( "drop function " + qTableName( "hello" ) ) );
   535     QVERIFY_SQL( q, exec( "drop function " + hello ) );
   524 
   536 
   525     q.exec( "drop procedure " + qTableName( "qtestproc" ) );
   537     q.exec( "drop procedure " + qtestproc );
   526 
   538 
   527     QVERIFY_SQL( q, exec( "create procedure " + qTableName( "qtestproc" ) + " () "
   539     QVERIFY_SQL( q, exec( "create procedure " + qtestproc + " () "
   528                             "BEGIN select * from " + qTableName( "qtest" ) + " order by id; END" ) );
   540                             "BEGIN select * from " + qtest + " order by id; END" ) );
   529     QVERIFY_SQL( q, exec( "call " + qTableName( "qtestproc" ) + "()" ) );
   541     QVERIFY_SQL( q, exec( "call " + qtestproc + "()" ) );
   530     QVERIFY_SQL( q, next() );
   542     QVERIFY_SQL( q, next() );
   531     QCOMPARE( q.value( 1 ).toString(), QString( "VarChar1" ) );
   543     QCOMPARE( q.value( 1 ).toString(), QString( "VarChar1" ) );
   532 
   544 
   533     QVERIFY_SQL( q, exec( "drop procedure " + qTableName( "qtestproc" ) ) );
   545     QVERIFY_SQL( q, exec( "drop procedure " + qtestproc ) );
   534 
   546 
   535     QVERIFY_SQL( q, exec( "create procedure " + qTableName( "qtestproc" ) + " (OUT param1 INT) "
   547     QVERIFY_SQL( q, exec( "create procedure " + qtestproc + " (OUT param1 INT) "
   536                             "BEGIN set param1 = 42; END" ) );
   548                             "BEGIN set param1 = 42; END" ) );
   537 
   549 
   538     QVERIFY_SQL( q, exec( "call " + qTableName( "qtestproc" ) + " (@out)" ) );
   550     QVERIFY_SQL( q, exec( "call " + qtestproc + " (@out)" ) );
   539     QVERIFY_SQL( q, exec( "select @out" ) );
   551     QVERIFY_SQL( q, exec( "select @out" ) );
   540     QCOMPARE( q.record().fieldName( 0 ), QString( "@out" ) );
   552     QCOMPARE( q.record().fieldName( 0 ), QString( "@out" ) );
   541     QVERIFY_SQL( q, next() );
   553     QVERIFY_SQL( q, next() );
   542     QCOMPARE( q.value( 0 ).toInt(), 42 );
   554     QCOMPARE( q.value( 0 ).toInt(), 42 );
   543 
   555 
   544     QVERIFY_SQL( q, exec( "drop procedure " + qTableName( "qtestproc" ) ) );
   556     QVERIFY_SQL( q, exec( "drop procedure " + qtestproc ) );
   545 }
   557 }
   546 
   558 
   547 void tst_QSqlQuery::oraOutValues()
   559 void tst_QSqlQuery::oraOutValues()
   548 {
   560 {
   549     QFETCH( QString, dbName );
   561     QFETCH( QString, dbName );
   550     QSqlDatabase db = QSqlDatabase::database( dbName );
   562     QSqlDatabase db = QSqlDatabase::database( dbName );
   551     CHECK_DATABASE( db );
   563     CHECK_DATABASE( db );
       
   564     const QString tst_outValues(qTableName("tst_outValues", __FILE__));
   552 
   565 
   553     if ( !db.driver()->hasFeature( QSqlDriver::PreparedQueries ) ) {
   566     if ( !db.driver()->hasFeature( QSqlDriver::PreparedQueries ) ) {
   554         QSKIP( "Test requires prepared query support", SkipSingle );
   567         QSKIP( "Test requires prepared query support", SkipSingle );
   555         return;
   568         return;
   556     }
   569     }
   558     QSqlQuery q( db );
   571     QSqlQuery q( db );
   559 
   572 
   560     q.setForwardOnly( true );
   573     q.setForwardOnly( true );
   561 
   574 
   562     /*** outvalue int ***/
   575     /*** outvalue int ***/
   563     QVERIFY_SQL( q, exec( "create or replace procedure " + qTableName( "tst_outValues" ) + "(x out int) is\n"
   576     QVERIFY_SQL( q, exec( "create or replace procedure " + tst_outValues + "(x out int) is\n"
   564                             "begin\n"
   577                             "begin\n"
   565                             "    x := 42;\n"
   578                             "    x := 42;\n"
   566                             "end;\n" ) );
   579                             "end;\n" ) );
   567     QVERIFY( q.prepare( "call " + qTableName( "tst_outvalues" ) + "(?)" ) );
   580     QVERIFY( q.prepare( "call " + tst_outValues + "(?)" ) );
   568     q.addBindValue( 0, QSql::Out );
   581     q.addBindValue( 0, QSql::Out );
   569     QVERIFY_SQL( q, exec() );
   582     QVERIFY_SQL( q, exec() );
   570     QCOMPARE( q.boundValue( 0 ).toInt(), 42 );
   583     QCOMPARE( q.boundValue( 0 ).toInt(), 42 );
   571 
   584 
   572     // bind a null value, make sure the OCI driver resets the null flag
   585     // bind a null value, make sure the OCI driver resets the null flag
   574     QVERIFY_SQL( q, exec() );
   587     QVERIFY_SQL( q, exec() );
   575     QCOMPARE( q.boundValue( 0 ).toInt(), 42 );
   588     QCOMPARE( q.boundValue( 0 ).toInt(), 42 );
   576     QVERIFY( !q.boundValue( 0 ).isNull() );
   589     QVERIFY( !q.boundValue( 0 ).isNull() );
   577 
   590 
   578     /*** outvalue varchar ***/
   591     /*** outvalue varchar ***/
   579     QVERIFY_SQL( q, exec( "create or replace procedure " + qTableName( "tst_outValues" ) + "(x out varchar) is\n"
   592     QVERIFY_SQL( q, exec( "create or replace procedure " + tst_outValues + "(x out varchar) is\n"
   580                             "begin\n"
   593                             "begin\n"
   581                             "    x := 'blah';\n"
   594                             "    x := 'blah';\n"
   582                             "end;\n" ) );
   595                             "end;\n" ) );
   583     QVERIFY( q.prepare( "call " + qTableName( "tst_outvalues" ) + "(?)" ) );
   596     QVERIFY( q.prepare( "call " + tst_outValues + "(?)" ) );
   584     QString s1( "12345" );
   597     QString s1( "12345" );
   585     s1.reserve( 512 );
   598     s1.reserve( 512 );
   586     q.addBindValue( s1, QSql::Out );
   599     q.addBindValue( s1, QSql::Out );
   587     QVERIFY_SQL( q, exec() );
   600     QVERIFY_SQL( q, exec() );
   588     QCOMPARE( q.boundValue( 0 ).toString(), QString( "blah" ) );
   601     QCOMPARE( q.boundValue( 0 ).toString(), QString( "blah" ) );
   589 
   602 
   590     /*** in/outvalue numeric ***/
   603     /*** in/outvalue numeric ***/
   591     QVERIFY_SQL( q, exec( "create or replace procedure " + qTableName( "tst_outValues" ) + "(x in out numeric) is\n"
   604     QVERIFY_SQL( q, exec( "create or replace procedure " + tst_outValues + "(x in out numeric) is\n"
   592                             "begin\n"
   605                             "begin\n"
   593                             "    x := x + 10;\n"
   606                             "    x := x + 10;\n"
   594                             "end;\n" ) );
   607                             "end;\n" ) );
   595     QVERIFY( q.prepare( "call " + qTableName( "tst_outvalues" ) + "(?)" ) );
   608     QVERIFY( q.prepare( "call " + tst_outValues + "(?)" ) );
   596     q.addBindValue( 10, QSql::Out );
   609     q.addBindValue( 10, QSql::Out );
   597     QVERIFY_SQL( q, exec() );
   610     QVERIFY_SQL( q, exec() );
   598     QCOMPARE( q.boundValue( 0 ).toInt(), 20 );
   611     QCOMPARE( q.boundValue( 0 ).toInt(), 20 );
   599 
   612 
   600     /*** in/outvalue varchar ***/
   613     /*** in/outvalue varchar ***/
   601     QVERIFY_SQL( q, exec( "create or replace procedure " + qTableName( "tst_outValues" ) + "(x in out varchar) is\n"
   614     QVERIFY_SQL( q, exec( "create or replace procedure " + tst_outValues + "(x in out varchar) is\n"
   602                             "begin\n"
   615                             "begin\n"
   603                             "    x := 'homer';\n"
   616                             "    x := 'homer';\n"
   604                             "end;\n" ) );
   617                             "end;\n" ) );
   605     QVERIFY( q.prepare( "call " + qTableName( "tst_outvalues" ) + "(?)" ) );
   618     QVERIFY( q.prepare( "call " + tst_outValues + "(?)" ) );
   606     q.addBindValue( QString( "maggy" ), QSql::Out );
   619     q.addBindValue( QString( "maggy" ), QSql::Out );
   607     QVERIFY_SQL( q, exec() );
   620     QVERIFY_SQL( q, exec() );
   608     QCOMPARE( q.boundValue( 0 ).toString(), QString( "homer" ) );
   621     QCOMPARE( q.boundValue( 0 ).toString(), QString( "homer" ) );
   609 
   622 
   610     /*** in/outvalue varchar ***/
   623     /*** in/outvalue varchar ***/
   611     QVERIFY_SQL( q, exec( "create or replace procedure " + qTableName( "tst_outValues" ) + "(x in out varchar) is\n"
   624     QVERIFY_SQL( q, exec( "create or replace procedure " + tst_outValues + "(x in out varchar) is\n"
   612                             "begin\n"
   625                             "begin\n"
   613                             "    x := NULL;\n"
   626                             "    x := NULL;\n"
   614                             "end;\n" ) );
   627                             "end;\n" ) );
   615     QVERIFY( q.prepare( "call " + qTableName( "tst_outvalues" ) + "(?)" ) );
   628     QVERIFY( q.prepare( "call " + tst_outValues + "(?)" ) );
   616     q.addBindValue( QString( "maggy" ), QSql::Out );
   629     q.addBindValue( QString( "maggy" ), QSql::Out );
   617     QVERIFY_SQL( q, exec() );
   630     QVERIFY_SQL( q, exec() );
   618     QVERIFY( q.boundValue( 0 ).isNull() );
   631     QVERIFY( q.boundValue( 0 ).isNull() );
   619 
   632 
   620     /*** in/outvalue int ***/
   633     /*** in/outvalue int ***/
   621     QVERIFY_SQL( q, exec( "create or replace procedure " + qTableName( "tst_outValues" ) + "(x in out int) is\n"
   634     QVERIFY_SQL( q, exec( "create or replace procedure " + tst_outValues + "(x in out int) is\n"
   622                             "begin\n"
   635                             "begin\n"
   623                             "    x := NULL;\n"
   636                             "    x := NULL;\n"
   624                             "end;\n" ) );
   637                             "end;\n" ) );
   625     QVERIFY( q.prepare( "call " + qTableName( "tst_outvalues" ) + "(?)" ) );
   638     QVERIFY( q.prepare( "call " + tst_outValues + "(?)" ) );
   626     q.addBindValue( 42, QSql::Out );
   639     q.addBindValue( 42, QSql::Out );
   627     QVERIFY_SQL( q, exec() );
   640     QVERIFY_SQL( q, exec() );
   628     QVERIFY( q.boundValue( 0 ).isNull() );
   641     QVERIFY( q.boundValue( 0 ).isNull() );
   629 
   642 
   630     /*** in/outvalue varchar ***/
   643     /*** in/outvalue varchar ***/
   631     QVERIFY_SQL( q, exec( "create or replace procedure " + qTableName( "tst_outValues" ) + "(x in varchar, y out varchar) is\n"
   644     QVERIFY_SQL( q, exec( "create or replace procedure " + tst_outValues + "(x in varchar, y out varchar) is\n"
   632                             "begin\n"
   645                             "begin\n"
   633                             "    y := x||'bubulalakikikokololo';\n"
   646                             "    y := x||'bubulalakikikokololo';\n"
   634                             "end;\n" ) );
   647                             "end;\n" ) );
   635     QVERIFY( q.prepare( "call " + qTableName( "tst_outvalues" ) + "(?, ?)" ) );
   648     QVERIFY( q.prepare( "call " + tst_outValues + "(?, ?)" ) );
   636     q.addBindValue( QString( "fifi" ), QSql::In );
   649     q.addBindValue( QString( "fifi" ), QSql::In );
   637     QString out;
   650     QString out;
   638     out.reserve( 50 );
   651     out.reserve( 50 );
   639     q.addBindValue( out, QSql::Out );
   652     q.addBindValue( out, QSql::Out );
   640     QVERIFY_SQL( q, exec() );
   653     QVERIFY_SQL( q, exec() );
   644 void tst_QSqlQuery::oraClob()
   657 void tst_QSqlQuery::oraClob()
   645 {
   658 {
   646     QFETCH( QString, dbName );
   659     QFETCH( QString, dbName );
   647     QSqlDatabase db = QSqlDatabase::database( dbName );
   660     QSqlDatabase db = QSqlDatabase::database( dbName );
   648     CHECK_DATABASE( db );
   661     CHECK_DATABASE( db );
       
   662     const QString clobby(qTableName("clobby", __FILE__));
   649 
   663 
   650     QSqlQuery q( db );
   664     QSqlQuery q( db );
   651 
   665 
   652     // simple short string
   666     // simple short string
   653     QVERIFY_SQL( q, exec( "create table " + qTableName( "clobby" ) + "(id int primary key, cl clob, bl blob)" ) );
   667     QVERIFY_SQL( q, exec( "create table " + clobby + "(id int primary key, cl clob, bl blob)" ) );
   654     QVERIFY_SQL( q, prepare( "insert into " + qTableName( "clobby" ) + " (id, cl, bl) values(?, ?, ?)" ) );
   668     QVERIFY_SQL( q, prepare( "insert into " + clobby + " (id, cl, bl) values(?, ?, ?)" ) );
   655     q.addBindValue( 1 );
   669     q.addBindValue( 1 );
   656     q.addBindValue( "bubu" );
   670     q.addBindValue( "bubu" );
   657     q.addBindValue( QByteArray("bubu") );
   671     q.addBindValue( QByteArray("bubu") );
   658     QVERIFY_SQL( q, exec() );
   672     QVERIFY_SQL( q, exec() );
   659 
   673 
   660     QVERIFY_SQL( q, exec( "select bl, cl from " + qTableName( "clobby" ) + " where id = 1" ) );
   674     QVERIFY_SQL( q, exec( "select bl, cl from " + clobby + " where id = 1" ) );
   661     QVERIFY( q.next() );
   675     QVERIFY( q.next() );
   662     QCOMPARE( q.value( 0 ).toString(), QString( "bubu" ) );
   676     QCOMPARE( q.value( 0 ).toString(), QString( "bubu" ) );
   663     QCOMPARE( q.value( 1 ).toString(), QString( "bubu" ) );
   677     QCOMPARE( q.value( 1 ).toString(), QString( "bubu" ) );
   664 
   678 
   665     // simple short string with binding
   679     // simple short string with binding
   666     QVERIFY_SQL( q, prepare( "insert into " + qTableName( "clobby" ) + " (id, cl, bl) values(?, ?, ?)" ) );
   680     QVERIFY_SQL( q, prepare( "insert into " + clobby + " (id, cl, bl) values(?, ?, ?)" ) );
   667     q.addBindValue( 2 );
   681     q.addBindValue( 2 );
   668     q.addBindValue( "lala", QSql::Binary );
   682     q.addBindValue( "lala", QSql::Binary );
   669     q.addBindValue( QByteArray("lala"), QSql::Binary );
   683     q.addBindValue( QByteArray("lala"), QSql::Binary );
   670     QVERIFY_SQL( q, exec() );
   684     QVERIFY_SQL( q, exec() );
   671 
   685 
   672     QVERIFY_SQL( q, exec( "select bl, cl from " + qTableName( "clobby" ) + " where id = 2" ) );
   686     QVERIFY_SQL( q, exec( "select bl, cl from " + clobby + " where id = 2" ) );
   673     QVERIFY( q.next() );
   687     QVERIFY( q.next() );
   674     QCOMPARE( q.value( 0 ).toString(), QString( "lala" ) );
   688     QCOMPARE( q.value( 0 ).toString(), QString( "lala" ) );
   675     QCOMPARE( q.value( 1 ).toString(), QString( "lala" ) );
   689     QCOMPARE( q.value( 1 ).toString(), QString( "lala" ) );
   676 
   690 
   677     // loooong string
   691     // loooong string
   678     QString loong;
   692     QString loong;
   679     loong.fill( QLatin1Char( 'A' ), 25000 );
   693     loong.fill( QLatin1Char( 'A' ), 25000 );
   680     QVERIFY_SQL( q, prepare( "insert into " + qTableName( "clobby" ) + " (id, cl, bl) values(?, ?, ?)" ) );
   694     QVERIFY_SQL( q, prepare( "insert into " + clobby + " (id, cl, bl) values(?, ?, ?)" ) );
   681     q.addBindValue( 3 );
   695     q.addBindValue( 3 );
   682     q.addBindValue( loong, QSql::Binary );
   696     q.addBindValue( loong, QSql::Binary );
   683     q.addBindValue( loong.toLatin1(), QSql::Binary );
   697     q.addBindValue( loong.toLatin1(), QSql::Binary );
   684     QVERIFY_SQL( q, exec() );
   698     QVERIFY_SQL( q, exec() );
   685 
   699 
   686     QVERIFY_SQL( q, exec( "select bl, cl from " + qTableName( "clobby" ) + " where id = 3" ) );
   700     QVERIFY_SQL( q, exec( "select bl, cl from " + clobby + " where id = 3" ) );
   687     QVERIFY( q.next() );
   701     QVERIFY( q.next() );
   688     QCOMPARE( q.value( 0 ).toString().count(), loong.count() );
   702     QCOMPARE( q.value( 0 ).toString().count(), loong.count() );
   689     QVERIFY( q.value( 0 ).toString() == loong );
   703     QVERIFY( q.value( 0 ).toString() == loong );
   690     QCOMPARE( q.value( 1 ).toByteArray().count(), loong.toLatin1().count() );
   704     QCOMPARE( q.value( 1 ).toByteArray().count(), loong.toLatin1().count() );
   691     QVERIFY( q.value( 1 ).toByteArray() == loong.toLatin1() );
   705     QVERIFY( q.value( 1 ).toByteArray() == loong.toLatin1() );
   696     QFETCH( QString, dbName );
   710     QFETCH( QString, dbName );
   697     QSqlDatabase db = QSqlDatabase::database( dbName );
   711     QSqlDatabase db = QSqlDatabase::database( dbName );
   698     CHECK_DATABASE( db );
   712     CHECK_DATABASE( db );
   699 
   713 
   700     QSqlQuery q( db );
   714     QSqlQuery q( db );
   701     q.exec( "drop procedure " + qTableName( "TESTPROC" ) );
   715     q.exec( "drop procedure " + qTableName( "TESTPROC", __FILE__ ) );
   702 
   716 
   703     QVERIFY_SQL( q, exec( "create procedure " + qTableName( "TESTPROC" ) +
   717     QVERIFY_SQL( q, exec( "create procedure " + qTableName( "TESTPROC", __FILE__ ) +
   704                             " RETURNS (x integer, y varchar(20)) "
   718                             " RETURNS (x integer, y varchar(20)) "
   705                             "AS BEGIN "
   719                             "AS BEGIN "
   706                             "  x = 42; "
   720                             "  x = 42; "
   707                             "  y = 'Hello Anders'; "
   721                             "  y = 'Hello Anders'; "
   708                             "END" ) );
   722                             "END" ) );
   709 
   723 
   710     QVERIFY_SQL( q, prepare( "execute procedure " + qTableName( "TestProc" ) ) );
   724     QVERIFY_SQL( q, prepare( "execute procedure " + qTableName( "TestProc", __FILE__ ) ) );
   711     QVERIFY_SQL( q, exec() );
   725     QVERIFY_SQL( q, exec() );
   712 
   726 
   713     // check for a valid result set
   727     // check for a valid result set
   714     QSqlRecord rec = q.record();
   728     QSqlRecord rec = q.record();
   715     QCOMPARE( rec.count(), 2 );
   729     QCOMPARE( rec.count(), 2 );
   722     QCOMPARE( q.value( 1 ).toString(), QString( "Hello Anders" ) );
   736     QCOMPARE( q.value( 1 ).toString(), QString( "Hello Anders" ) );
   723 
   737 
   724     // the second next shall fail
   738     // the second next shall fail
   725     QVERIFY( !q.next() );
   739     QVERIFY( !q.next() );
   726 
   740 
   727     q.exec( "drop procedure " + qTableName( "TestProc" ) );
   741     q.exec( "drop procedure " + qTableName( "TestProc", __FILE__ ) );
   728 }
   742 }
   729 
   743 
   730 void tst_QSqlQuery::outValuesDB2()
   744 void tst_QSqlQuery::outValuesDB2()
   731 {
   745 {
   732     QFETCH( QString, dbName );
   746     QFETCH( QString, dbName );
   740 
   754 
   741     QSqlQuery q( db );
   755     QSqlQuery q( db );
   742 
   756 
   743     q.setForwardOnly( true );
   757     q.setForwardOnly( true );
   744 
   758 
   745     q.exec( "drop procedure " + qTableName( "tst_outValues" ) ); //non-fatal
   759     q.exec( "drop procedure " + qTableName( "tst_outValues", __FILE__ ) ); //non-fatal
   746     QVERIFY_SQL( q, exec( "CREATE PROCEDURE " + qTableName( "tst_outValues" ) +
   760     QVERIFY_SQL( q, exec( "CREATE PROCEDURE " + qTableName( "tst_outValues", __FILE__ ) +
   747                             " (OUT x int, OUT x2 double, OUT x3 char(20))\n"
   761                             " (OUT x int, OUT x2 double, OUT x3 char(20))\n"
   748                             "LANGUAGE SQL\n"
   762                             "LANGUAGE SQL\n"
   749                             "P1: BEGIN\n"
   763                             "P1: BEGIN\n"
   750                             " SET x = 42;\n"
   764                             " SET x = 42;\n"
   751                             " SET x2 = 4.2;\n"
   765                             " SET x2 = 4.2;\n"
   752                             " SET x3 = 'Homer';\n"
   766                             " SET x3 = 'Homer';\n"
   753                             "END P1" ) );
   767                             "END P1" ) );
   754 
   768 
   755     QVERIFY_SQL( q, prepare( "call " + qTableName( "tst_outValues" ) + "(?, ?, ?)" ) );
   769     QVERIFY_SQL( q, prepare( "call " + qTableName( "tst_outValues", __FILE__ ) + "(?, ?, ?)" ) );
   756 
   770 
   757     q.addBindValue( 0, QSql::Out );
   771     q.addBindValue( 0, QSql::Out );
   758     q.addBindValue( 0.0, QSql::Out );
   772     q.addBindValue( 0.0, QSql::Out );
   759     q.addBindValue( "Simpson", QSql::Out );
   773     q.addBindValue( "Simpson", QSql::Out );
   760 
   774 
   768 void tst_QSqlQuery::outValues()
   782 void tst_QSqlQuery::outValues()
   769 {
   783 {
   770     QFETCH( QString, dbName );
   784     QFETCH( QString, dbName );
   771     QSqlDatabase db = QSqlDatabase::database( dbName );
   785     QSqlDatabase db = QSqlDatabase::database( dbName );
   772     CHECK_DATABASE( db );
   786     CHECK_DATABASE( db );
       
   787     const QString tst_outValues(qTableName("tst_outValues", __FILE__));
   773 
   788 
   774     if ( !db.driver()->hasFeature( QSqlDriver::PreparedQueries ) ) {
   789     if ( !db.driver()->hasFeature( QSqlDriver::PreparedQueries ) ) {
   775         QSKIP( "Test requires prepared query support", SkipSingle );
   790         QSKIP( "Test requires prepared query support", SkipSingle );
   776         return;
   791         return;
   777     }
   792     }
   779     QSqlQuery q( db );
   794     QSqlQuery q( db );
   780 
   795 
   781     q.setForwardOnly( true );
   796     q.setForwardOnly( true );
   782 
   797 
   783     if ( db.driverName().startsWith( "QOCI" ) ) {
   798     if ( db.driverName().startsWith( "QOCI" ) ) {
   784         QVERIFY_SQL( q, exec( "create or replace procedure " + qTableName( "tst_outValues" ) + "(x out int) is\n"
   799         QVERIFY_SQL( q, exec( "create or replace procedure " + tst_outValues + "(x out int) is\n"
   785                                 "begin\n"
   800                                 "begin\n"
   786                                 "    x := 42;\n"
   801                                 "    x := 42;\n"
   787                                 "end;\n" ) );
   802                                 "end;\n" ) );
   788         QVERIFY( q.prepare( "call " + qTableName( "tst_outvalues" ) + "(?)" ) );
   803         QVERIFY( q.prepare( "call " + tst_outValues + "(?)" ) );
   789     } else if ( db.driverName().startsWith( "QDB2" ) ) {
   804     } else if ( db.driverName().startsWith( "QDB2" ) ) {
   790         q.exec( "drop procedure " + qTableName( "tst_outValues" ) ); //non-fatal
   805         q.exec( "drop procedure " + tst_outValues ); //non-fatal
   791         QVERIFY_SQL( q, exec( "CREATE PROCEDURE " + qTableName( "tst_outValues" ) + " (OUT x int)\n"
   806         QVERIFY_SQL( q, exec( "CREATE PROCEDURE " + tst_outValues + " (OUT x int)\n"
   792                                 "LANGUAGE SQL\n"
   807                                 "LANGUAGE SQL\n"
   793                                 "P1: BEGIN\n"
   808                                 "P1: BEGIN\n"
   794                                 " SET x = 42;\n"
   809                                 " SET x = 42;\n"
   795                                 "END P1" ) );
   810                                 "END P1" ) );
   796         QVERIFY( q.prepare( "call " + qTableName( "tst_outValues" ) + "(?)" ) );
   811         QVERIFY( q.prepare( "call " + tst_outValues + "(?)" ) );
   797     } else if ( tst_Databases::isSqlServer( db ) ) {
   812     } else if ( tst_Databases::isSqlServer( db ) ) {
   798         q.exec( "drop procedure " + qTableName( "tst_outValues" ) );  //non-fatal
   813         q.exec( "drop procedure " + tst_outValues );  //non-fatal
   799         QVERIFY_SQL( q, exec( "create procedure " + qTableName( "tst_outValues" ) + " (@x int out) as\n"
   814         QVERIFY_SQL( q, exec( "create procedure " + tst_outValues + " (@x int out) as\n"
   800                                 "begin\n"
   815                                 "begin\n"
   801                                 "    set @x = 42\n"
   816                                 "    set @x = 42\n"
   802                                 "end\n" ) );
   817                                 "end\n" ) );
   803         QVERIFY( q.prepare( "{call " + qTableName( "tst_outvalues" ) + "(?)}" ) );
   818         QVERIFY( q.prepare( "{call " + tst_outValues + "(?)}" ) );
   804     } else {
   819     } else {
   805         QSKIP( "Don't know how to create a stored procedure for this database server, please fix this test", SkipSingle );
   820         QSKIP( "Don't know how to create a stored procedure for this database server, please fix this test", SkipSingle );
   806         return;
   821         return;
   807     }
   822     }
   808 
   823 
   835 
   850 
   836     QSqlQuery q( db );
   851     QSqlQuery q( db );
   837 
   852 
   838     q.setForwardOnly( true );
   853     q.setForwardOnly( true );
   839 
   854 
   840     QString queryString = QString( "create table " + qTableName( "qtest_blob" ) +
   855     QString queryString = QString( "create table " + qTableName( "qtest_blob", __FILE__ ) +
   841                                    " (id int not null primary key, t_blob %1)" ).arg( tst_Databases::blobTypeName( db, BLOBSIZE ) );
   856                                    " (id int not null primary key, t_blob %1)" ).arg( tst_Databases::blobTypeName( db, BLOBSIZE ) );
   842     QVERIFY_SQL( q, exec( queryString ) );
   857     QVERIFY_SQL( q, exec( queryString ) );
   843 
   858 
   844     QVERIFY_SQL( q, prepare( "insert into " + qTableName( "qtest_blob" ) + " (id, t_blob) values (?, ?)" ) );
   859     QVERIFY_SQL( q, prepare( "insert into " + qTableName( "qtest_blob", __FILE__ ) + " (id, t_blob) values (?, ?)" ) );
   845 
   860 
   846     for ( i = 0; i < BLOBCOUNT; ++i ) {
   861     for ( i = 0; i < BLOBCOUNT; ++i ) {
   847         q.addBindValue( i );
   862         q.addBindValue( i );
   848         q.addBindValue( ba );
   863         q.addBindValue( ba );
   849         QVERIFY_SQL( q, exec() );
   864         QVERIFY_SQL( q, exec() );
   850     }
   865     }
   851 
   866 
   852     QVERIFY_SQL( q, exec( "select * from " + qTableName( "qtest_blob" ) ) );
   867     QVERIFY_SQL( q, exec( "select * from " + qTableName( "qtest_blob", __FILE__ ) ) );
   853 
   868 
   854     for ( i = 0; i < BLOBCOUNT; ++i ) {
   869     for ( i = 0; i < BLOBCOUNT; ++i ) {
   855         QVERIFY( q.next() );
   870         QVERIFY( q.next() );
   856         QByteArray res = q.value( 1 ).toByteArray();
   871         QByteArray res = q.value( 1 ).toByteArray();
   857         QVERIFY2( res.size() >= ba.size(),
   872         QVERIFY2( res.size() >= ba.size(),
   870     QFETCH( QString, dbName );
   885     QFETCH( QString, dbName );
   871     QSqlDatabase db = QSqlDatabase::database( dbName );
   886     QSqlDatabase db = QSqlDatabase::database( dbName );
   872     CHECK_DATABASE( db );
   887     CHECK_DATABASE( db );
   873 
   888 
   874     QSqlQuery q( db );
   889     QSqlQuery q( db );
   875     QVERIFY_SQL( q, exec( "select id, t_varchar, t_char from " + qTableName( "qtest" ) + " order by id" ) );
   890     QVERIFY_SQL( q, exec( "select id, t_varchar, t_char from " + qtest + " order by id" ) );
   876     int i = 1;
   891     int i = 1;
   877 
   892 
   878     while ( q.next() ) {
   893     while ( q.next() ) {
   879         QCOMPARE( q.value( 0 ).toInt(), i );
   894         QCOMPARE( q.value( 0 ).toInt(), i );
   880 
   895 
   902     QSqlDatabase db = QSqlDatabase::database( dbName );
   917     QSqlDatabase db = QSqlDatabase::database( dbName );
   903     CHECK_DATABASE( db );
   918     CHECK_DATABASE( db );
   904 
   919 
   905     QSqlQuery q( db );
   920     QSqlQuery q( db );
   906     QVERIFY( q.record().isEmpty() );
   921     QVERIFY( q.record().isEmpty() );
   907     QVERIFY_SQL( q, exec( "select id, t_varchar, t_char from " + qTableName( "qtest" ) + " order by id" ) );
   922     QVERIFY_SQL( q, exec( "select id, t_varchar, t_char from " + qtest + " order by id" ) );
   908     QSqlRecord rec = q.record();
   923     QSqlRecord rec = q.record();
   909     QCOMPARE( q.record().fieldName( 0 ).toLower(), QString( "id" ) );
   924     QCOMPARE( q.record().fieldName( 0 ).toLower(), QString( "id" ) );
   910     QCOMPARE( q.record().fieldName( 1 ).toLower(), QString( "t_varchar" ) );
   925     QCOMPARE( q.record().fieldName( 1 ).toLower(), QString( "t_varchar" ) );
   911     QCOMPARE( q.record().fieldName( 2 ).toLower(), QString( "t_char" ) );
   926     QCOMPARE( q.record().fieldName( 2 ).toLower(), QString( "t_char" ) );
   912     QVERIFY( !q.record().value( 0 ).isValid() );
   927     QVERIFY( !q.record().value( 0 ).isValid() );
   926     QSqlDatabase db = QSqlDatabase::database( dbName );
   941     QSqlDatabase db = QSqlDatabase::database( dbName );
   927     CHECK_DATABASE( db );
   942     CHECK_DATABASE( db );
   928 
   943 
   929     QSqlQuery q( db );
   944     QSqlQuery q( db );
   930     QVERIFY( !q.isValid() );
   945     QVERIFY( !q.isValid() );
   931     QVERIFY_SQL( q, exec( "select * from " + qTableName( "qtest" ) ) );
   946     QVERIFY_SQL( q, exec( "select * from " + qtest ) );
   932     QVERIFY( q.first() );
   947     QVERIFY( q.first() );
   933     QVERIFY( q.isValid() );
   948     QVERIFY( q.isValid() );
   934 }
   949 }
   935 
   950 
   936 void tst_QSqlQuery::isActive()
   951 void tst_QSqlQuery::isActive()
   939     QSqlDatabase db = QSqlDatabase::database( dbName );
   954     QSqlDatabase db = QSqlDatabase::database( dbName );
   940     CHECK_DATABASE( db );
   955     CHECK_DATABASE( db );
   941 
   956 
   942     QSqlQuery q( db );
   957     QSqlQuery q( db );
   943     QVERIFY( !q.isActive() );
   958     QVERIFY( !q.isActive() );
   944     QVERIFY_SQL( q, exec( "select * from " + qTableName( "qtest" ) ) );
   959     QVERIFY_SQL( q, exec( "select * from " + qtest ) );
   945     QVERIFY( q.isActive() );
   960     QVERIFY( q.isActive() );
   946     QVERIFY( q.last() );
   961     QVERIFY( q.last() );
   947 
   962 
   948     if ( !tst_Databases::isMSAccess( db ) )
   963     if ( !tst_Databases::isMSAccess( db ) )
   949         // Access is stupid enough to let you scroll over boundaries
   964         // Access is stupid enough to let you scroll over boundaries
   950         QVERIFY( !q.next() );
   965         QVERIFY( !q.next() );
   951 
   966 
   952     QVERIFY( q.isActive() );
   967     QVERIFY( q.isActive() );
   953 
   968 
   954     QVERIFY_SQL( q, exec( "insert into " + qTableName( "qtest" ) + " values (41, 'VarChar41', 'Char41')" ) );
   969     QVERIFY_SQL( q, exec( "insert into " + qtest + " values (41, 'VarChar41', 'Char41')" ) );
   955 
   970 
   956     QVERIFY( q.isActive() );
   971     QVERIFY( q.isActive() );
   957 
   972 
   958     QVERIFY_SQL( q, exec( "update " + qTableName( "qtest" ) + " set id = 42 where id = 41" ) );
   973     QVERIFY_SQL( q, exec( "update " + qtest + " set id = 42 where id = 41" ) );
   959 
   974 
   960     QVERIFY( q.isActive() );
   975     QVERIFY( q.isActive() );
   961 
   976 
   962     QVERIFY_SQL( q, exec( "delete from " + qTableName( "qtest" ) + " where id = 42" ) );
   977     QVERIFY_SQL( q, exec( "delete from " + qtest + " where id = 42" ) );
   963 
   978 
   964     QVERIFY( q.isActive() );
   979     QVERIFY( q.isActive() );
   965 }
   980 }
   966 
   981 
   967 void tst_QSqlQuery::numRowsAffected()
   982 void tst_QSqlQuery::numRowsAffected()
   971     CHECK_DATABASE( db );
   986     CHECK_DATABASE( db );
   972 
   987 
   973     QSqlQuery q( db );
   988     QSqlQuery q( db );
   974     QCOMPARE( q.numRowsAffected(), -1 );
   989     QCOMPARE( q.numRowsAffected(), -1 );
   975 
   990 
   976     QVERIFY_SQL( q, exec( "select * from " + qTableName( "qtest" ) ) );
   991     QVERIFY_SQL( q, exec( "select * from " + qtest ) );
   977     int i = 0;
   992     int i = 0;
   978 
   993 
   979     while ( q.next() )
   994     while ( q.next() )
   980         ++i;
   995         ++i;
   981 
   996 
   985     if ( q.numRowsAffected() != -1 && q.numRowsAffected() != 0 && q.numRowsAffected() != i ) {
  1000     if ( q.numRowsAffected() != -1 && q.numRowsAffected() != 0 && q.numRowsAffected() != i ) {
   986         // the value is undefined for SELECT, this check is just here for curiosity
  1001         // the value is undefined for SELECT, this check is just here for curiosity
   987         qDebug( "Expected numRowsAffected to be -1, 0 or %d, got %d", i, q.numRowsAffected() );
  1002         qDebug( "Expected numRowsAffected to be -1, 0 or %d, got %d", i, q.numRowsAffected() );
   988     }
  1003     }
   989 
  1004 
   990     QVERIFY_SQL( q, exec( "update " + qTableName( "qtest" ) + " set id = 100 where id = 1" ) );
  1005     QVERIFY_SQL( q, exec( "update " + qtest + " set id = 100 where id = 1" ) );
   991 
  1006 
   992     QCOMPARE( q.numRowsAffected(), 1 );
  1007     QCOMPARE( q.numRowsAffected(), 1 );
   993     QCOMPARE( q.numRowsAffected(), 1 ); // yes, we check twice
  1008     QCOMPARE( q.numRowsAffected(), 1 ); // yes, we check twice
   994 
  1009 
   995     QVERIFY_SQL( q, exec( "update " + qTableName( "qtest" ) + " set id = id + 100" ) );
  1010     QVERIFY_SQL( q, exec( "update " + qtest + " set id = id + 100" ) );
   996     QCOMPARE( q.numRowsAffected(), i );
  1011     QCOMPARE( q.numRowsAffected(), i );
   997     QCOMPARE( q.numRowsAffected(), i ); // yes, we check twice
  1012     QCOMPARE( q.numRowsAffected(), i ); // yes, we check twice
   998 
  1013 
   999     QVERIFY_SQL( q, exec( "insert into " + qTableName( "qtest" ) + " values (42000, 'homer', 'marge')" ) );
  1014     QVERIFY_SQL( q, exec( "insert into " + qtest + " values (42000, 'homer', 'marge')" ) );
  1000     QCOMPARE( q.numRowsAffected(), 1 );
  1015     QCOMPARE( q.numRowsAffected(), 1 );
  1001     QCOMPARE( q.numRowsAffected(), 1 ); // yes, we check twice
  1016     QCOMPARE( q.numRowsAffected(), 1 ); // yes, we check twice
  1002 
  1017 
  1003     QSqlQuery q2( db );
  1018     QSqlQuery q2( db );
  1004     QVERIFY_SQL( q2, exec( "insert into " + qTableName( "qtest" ) + " values (42001, 'homer', 'marge')" ) );
  1019     QVERIFY_SQL( q2, exec( "insert into " + qtest + " values (42001, 'homer', 'marge')" ) );
  1005 
  1020 
  1006     if ( !db.driverName().startsWith( "QSQLITE2" ) ) {
  1021     if ( !db.driverName().startsWith( "QSQLITE2" ) ) {
  1007         // SQLite 2.x accumulates changed rows in nested queries. See task 33794
  1022         // SQLite 2.x accumulates changed rows in nested queries. See task 33794
  1008         QCOMPARE( q2.numRowsAffected(), 1 );
  1023         QCOMPARE( q2.numRowsAffected(), 1 );
  1009         QCOMPARE( q2.numRowsAffected(), 1 ); // yes, we check twice
  1024         QCOMPARE( q2.numRowsAffected(), 1 ); // yes, we check twice
  1017     CHECK_DATABASE( db );
  1032     CHECK_DATABASE( db );
  1018 
  1033 
  1019     QSqlQuery q( db );
  1034     QSqlQuery q( db );
  1020     QCOMPARE( q.size(), -1 );
  1035     QCOMPARE( q.size(), -1 );
  1021 
  1036 
  1022     QVERIFY_SQL( q, exec( "select * from " + qTableName( "qtest" ) ) );
  1037     QVERIFY_SQL( q, exec( "select * from " + qtest ) );
  1023     int i = 0;
  1038     int i = 0;
  1024 
  1039 
  1025     while ( q.next() )
  1040     while ( q.next() )
  1026         ++i;
  1041         ++i;
  1027 
  1042 
  1031     } else {
  1046     } else {
  1032         QCOMPARE( q.size(), -1 );
  1047         QCOMPARE( q.size(), -1 );
  1033         QCOMPARE( q.size(), -1 ); // yes, twice
  1048         QCOMPARE( q.size(), -1 ); // yes, twice
  1034     }
  1049     }
  1035 
  1050 
  1036     QSqlQuery q2( "select * from " + qTableName( "qtest" ), db );
  1051     QSqlQuery q2( "select * from " + qtest, db );
  1037 
  1052 
  1038     if ( db.driver()->hasFeature( QSqlDriver::QuerySize ) )
  1053     if ( db.driver()->hasFeature( QSqlDriver::QuerySize ) )
  1039         QCOMPARE( q.size(), i );
  1054         QCOMPARE( q.size(), i );
  1040     else
  1055     else
  1041         QCOMPARE( q.size(), -1 );
  1056         QCOMPARE( q.size(), -1 );
  1042 
  1057 
  1043     q2.clear();
  1058     q2.clear();
  1044 
  1059 
  1045     QVERIFY_SQL( q, exec( "update " + qTableName( "qtest" ) + " set id = 100 where id = 1" ) );
  1060     QVERIFY_SQL( q, exec( "update " + qtest + " set id = 100 where id = 1" ) );
  1046     QCOMPARE( q.size(), -1 );
  1061     QCOMPARE( q.size(), -1 );
  1047     QCOMPARE( q.size(), -1 ); // yes, twice
  1062     QCOMPARE( q.size(), -1 ); // yes, twice
  1048 }
  1063 }
  1049 
  1064 
  1050 void tst_QSqlQuery::isSelect()
  1065 void tst_QSqlQuery::isSelect()
  1052     QFETCH( QString, dbName );
  1067     QFETCH( QString, dbName );
  1053     QSqlDatabase db = QSqlDatabase::database( dbName );
  1068     QSqlDatabase db = QSqlDatabase::database( dbName );
  1054     CHECK_DATABASE( db );
  1069     CHECK_DATABASE( db );
  1055 
  1070 
  1056     QSqlQuery q( db );
  1071     QSqlQuery q( db );
  1057     QVERIFY_SQL( q, exec( "select * from " + qTableName( "qtest" ) ) );
  1072     QVERIFY_SQL( q, exec( "select * from " + qtest ) );
  1058     QVERIFY( q.isSelect() );
  1073     QVERIFY( q.isSelect() );
  1059 
  1074 
  1060     QVERIFY_SQL( q, exec( "update " + qTableName( "qtest" ) + " set id = 1 where id = 1" ) );
  1075     QVERIFY_SQL( q, exec( "update " + qtest + " set id = 1 where id = 1" ) );
  1061     QVERIFY( q.isSelect() == false );
  1076     QVERIFY( q.isSelect() == false );
  1062 }
  1077 }
  1063 
  1078 
  1064 void tst_QSqlQuery::first()
  1079 void tst_QSqlQuery::first()
  1065 {
  1080 {
  1067     QSqlDatabase db = QSqlDatabase::database( dbName );
  1082     QSqlDatabase db = QSqlDatabase::database( dbName );
  1068     CHECK_DATABASE( db );
  1083     CHECK_DATABASE( db );
  1069 
  1084 
  1070     QSqlQuery q( db );
  1085     QSqlQuery q( db );
  1071     QVERIFY( q.at() == QSql::BeforeFirstRow );
  1086     QVERIFY( q.at() == QSql::BeforeFirstRow );
  1072     QVERIFY_SQL( q, exec( "select * from " + qTableName( "qtest" ) ) );
  1087     QVERIFY_SQL( q, exec( "select * from " + qtest ) );
  1073     QVERIFY( q.last() );
  1088     QVERIFY( q.last() );
  1074     QVERIFY_SQL( q, first() );
  1089     QVERIFY_SQL( q, first() );
  1075     QVERIFY( q.at() == 0 );
  1090     QVERIFY( q.at() == 0 );
  1076 }
  1091 }
  1077 
  1092 
  1081     QSqlDatabase db = QSqlDatabase::database( dbName );
  1096     QSqlDatabase db = QSqlDatabase::database( dbName );
  1082     CHECK_DATABASE( db );
  1097     CHECK_DATABASE( db );
  1083 
  1098 
  1084     QSqlQuery q( db );
  1099     QSqlQuery q( db );
  1085     QVERIFY( q.at() == QSql::BeforeFirstRow );
  1100     QVERIFY( q.at() == QSql::BeforeFirstRow );
  1086     QVERIFY_SQL( q, exec( "select * from " + qTableName( "qtest" ) ) );
  1101     QVERIFY_SQL( q, exec( "select * from " + qtest ) );
  1087     QVERIFY( q.first() );
  1102     QVERIFY( q.first() );
  1088     QVERIFY( q.next() );
  1103     QVERIFY( q.next() );
  1089     QVERIFY( q.at() == 1 );
  1104     QVERIFY( q.at() == 1 );
  1090 }
  1105 }
  1091 
  1106 
  1095     QSqlDatabase db = QSqlDatabase::database( dbName );
  1110     QSqlDatabase db = QSqlDatabase::database( dbName );
  1096     CHECK_DATABASE( db );
  1111     CHECK_DATABASE( db );
  1097 
  1112 
  1098     QSqlQuery q( db );
  1113     QSqlQuery q( db );
  1099     QVERIFY( q.at() == QSql::BeforeFirstRow );
  1114     QVERIFY( q.at() == QSql::BeforeFirstRow );
  1100     QVERIFY_SQL( q, exec( "select * from " + qTableName( "qtest" ) ) );
  1115     QVERIFY_SQL( q, exec( "select * from " + qtest ) );
  1101     QVERIFY( q.first() );
  1116     QVERIFY( q.first() );
  1102     QVERIFY( q.next() );
  1117     QVERIFY( q.next() );
  1103     QVERIFY( q.previous() );
  1118     QVERIFY( q.previous() );
  1104     QVERIFY( q.at() == 0 );
  1119     QVERIFY( q.at() == 0 );
  1105 }
  1120 }
  1110     QSqlDatabase db = QSqlDatabase::database( dbName );
  1125     QSqlDatabase db = QSqlDatabase::database( dbName );
  1111     CHECK_DATABASE( db );
  1126     CHECK_DATABASE( db );
  1112 
  1127 
  1113     QSqlQuery q( db );
  1128     QSqlQuery q( db );
  1114     QCOMPARE( q.at(), int( QSql::BeforeFirstRow ) );
  1129     QCOMPARE( q.at(), int( QSql::BeforeFirstRow ) );
  1115     QVERIFY_SQL( q, exec( "select * from " + qTableName( "qtest" ) ) );
  1130     QVERIFY_SQL( q, exec( "select * from " + qtest ) );
  1116     int i = 0;
  1131     int i = 0;
  1117 
  1132 
  1118     while ( q.next() )
  1133     while ( q.next() )
  1119         i++;
  1134         i++;
  1120 
  1135 
  1121     QCOMPARE( q.at(), int( QSql::AfterLastRow ) );
  1136     QCOMPARE( q.at(), int( QSql::AfterLastRow ) );
  1122 
  1137 
  1123     QVERIFY( q.last() );
  1138     QVERIFY( q.last() );
  1124 
  1139 
  1125     if ( !tst_Databases::isMSAccess( db ) )
  1140     QSet<int> validReturns(QSet<int>() << -1 << i-1);
  1126         // Access doesn't return the correct position
  1141     QVERIFY( validReturns.contains(q.at()) );
  1127         QCOMPARE( q.at(), ( i-1 ) );
  1142 
  1128 
  1143     QSqlQuery q2( "select * from " + qtest, db );
  1129     QSqlQuery q2( "select * from " + qTableName( "qtest" ), db );
       
  1130 
  1144 
  1131     QVERIFY( q2.last() );
  1145     QVERIFY( q2.last() );
  1132 
  1146 
  1133     if ( !tst_Databases::isMSAccess( db ) )
  1147     QVERIFY( validReturns.contains(q.at()) );
  1134         // Access doesn't return the correct position
       
  1135         QCOMPARE( q.at(), ( i-1 ) );
       
  1136 }
  1148 }
  1137 
  1149 
  1138 void tst_QSqlQuery::seek()
  1150 void tst_QSqlQuery::seek()
  1139 {
  1151 {
  1140     QFETCH( QString, dbName );
  1152     QFETCH( QString, dbName );
  1141     QSqlDatabase db = QSqlDatabase::database( dbName );
  1153     QSqlDatabase db = QSqlDatabase::database( dbName );
  1142     CHECK_DATABASE( db );
  1154     CHECK_DATABASE( db );
  1143     QSqlQuery q( db );
  1155     QSqlQuery q( db );
  1144     QVERIFY( q.at() == QSql::BeforeFirstRow );
  1156     QVERIFY( q.at() == QSql::BeforeFirstRow );
  1145     QVERIFY_SQL( q, exec( QString( "select id from %1 order by id" ).arg( qTableName( "qtest" ) ) ) );
  1157     QVERIFY_SQL( q, exec( QString( "select id from %1 order by id" ).arg( qtest ) ) );
  1146 
  1158 
  1147     // NB! The order of the calls below are important!
  1159     // NB! The order of the calls below are important!
  1148     QVERIFY( q.last() );
  1160     QVERIFY( q.last() );
  1149     QVERIFY( !q.seek( QSql::BeforeFirstRow ) );
  1161     QVERIFY( !q.seek( QSql::BeforeFirstRow ) );
  1150     QCOMPARE( q.at(), int( QSql::BeforeFirstRow ) );
  1162     QCOMPARE( q.at(), int( QSql::BeforeFirstRow ) );
  1176     QSqlQuery q( db );
  1188     QSqlQuery q( db );
  1177     q.setForwardOnly( false );
  1189     q.setForwardOnly( false );
  1178     QVERIFY( !q.isForwardOnly() );
  1190     QVERIFY( !q.isForwardOnly() );
  1179 
  1191 
  1180     QVERIFY( q.at() == QSql::BeforeFirstRow );
  1192     QVERIFY( q.at() == QSql::BeforeFirstRow );
  1181     QVERIFY_SQL( q, exec( QString( "select id from %1 order by id" ).arg( qTableName( "qtest" ) ) ) );
  1193     QVERIFY_SQL( q, exec( QString( "select id from %1 order by id" ).arg( qtest ) ) );
  1182 
  1194 
  1183     QSqlRecord rec;
  1195     QSqlRecord rec;
  1184 
  1196 
  1185     // NB! The order of the calls below are important!
  1197     // NB! The order of the calls below are important!
  1186     QVERIFY( q.seek( 0 ) );
  1198     QVERIFY( q.seek( 0 ) );
  1215 
  1227 
  1216     QSqlQuery q( db );
  1228     QSqlQuery q( db );
  1217     q.setForwardOnly( true );
  1229     q.setForwardOnly( true );
  1218     QVERIFY( q.isForwardOnly() );
  1230     QVERIFY( q.isForwardOnly() );
  1219     QVERIFY( q.at() == QSql::BeforeFirstRow );
  1231     QVERIFY( q.at() == QSql::BeforeFirstRow );
  1220     QVERIFY_SQL( q, exec( "select * from " + qTableName( "qtest" ) + " order by id" ) );
  1232     QVERIFY_SQL( q, exec( "select * from " + qtest + " order by id" ) );
  1221     QVERIFY( q.at() == QSql::BeforeFirstRow );
  1233     QVERIFY( q.at() == QSql::BeforeFirstRow );
  1222     QVERIFY( q.first() );
  1234     QVERIFY( q.first() );
  1223     QCOMPARE( q.at(), 0 );
  1235     QCOMPARE( q.at(), 0 );
  1224     QCOMPARE( q.value( 0 ).toInt(), 1 );
  1236     QCOMPARE( q.value( 0 ).toInt(), 1 );
  1225     QVERIFY( q.next() );
  1237     QVERIFY( q.next() );
  1240     QCOMPARE( q.value( 0 ).toInt(), 3 );
  1252     QCOMPARE( q.value( 0 ).toInt(), 3 );
  1241     QVERIFY( q.next() );
  1253     QVERIFY( q.next() );
  1242     QCOMPARE( q.at(), 3 );
  1254     QCOMPARE( q.at(), 3 );
  1243     QCOMPARE( q.value( 0 ).toInt(), 4 );
  1255     QCOMPARE( q.value( 0 ).toInt(), 4 );
  1244 
  1256 
  1245     QVERIFY_SQL( q, exec( "select * from " + qTableName( "qtest" ) ) );
  1257     QVERIFY_SQL( q, exec( "select * from " + qtest ) );
  1246     int i = 0;
  1258     int i = 0;
  1247 
  1259 
  1248     while ( q.next() )
  1260     while ( q.next() )
  1249         i++;
  1261         i++;
  1250 
  1262 
  1252 
  1264 
  1253     QSqlQuery q2 = q;
  1265     QSqlQuery q2 = q;
  1254 
  1266 
  1255     QVERIFY( q2.isForwardOnly() );
  1267     QVERIFY( q2.isForwardOnly() );
  1256 
  1268 
  1257     QVERIFY_SQL( q, exec( "select * from " + qTableName( "qtest" ) + " order by id" ) );
  1269     QVERIFY_SQL( q, exec( "select * from " + qtest + " order by id" ) );
  1258 
  1270 
  1259     QVERIFY( q.isForwardOnly() );
  1271     QVERIFY( q.isForwardOnly() );
  1260 
  1272 
  1261     QVERIFY( q2.isForwardOnly() );
  1273     QVERIFY( q2.isForwardOnly() );
  1262 
  1274 
  1298     CHECK_DATABASE( db );
  1310     CHECK_DATABASE( db );
  1299 
  1311 
  1300     QSqlQuery q( db );
  1312     QSqlQuery q( db );
  1301     QVERIFY( !q.isValid() );
  1313     QVERIFY( !q.isValid() );
  1302     QVERIFY( !q.isActive() );
  1314     QVERIFY( !q.isActive() );
  1303     QVERIFY_SQL( q, exec( "select * from " + qTableName( "qtest" ) ) );
  1315     QVERIFY_SQL( q, exec( "select * from " + qtest ) );
  1304     QVERIFY( q.isActive() );
  1316     QVERIFY( q.isActive() );
  1305     QVERIFY( q.next() );
  1317     QVERIFY( q.next() );
  1306     QVERIFY( q.isValid() );
  1318     QVERIFY( q.isValid() );
  1307 }
  1319 }
  1308 
  1320 
  1311     QFETCH( QString, dbName );
  1323     QFETCH( QString, dbName );
  1312     QSqlDatabase db = QSqlDatabase::database( dbName );
  1324     QSqlDatabase db = QSqlDatabase::database( dbName );
  1313     CHECK_DATABASE( db );
  1325     CHECK_DATABASE( db );
  1314 
  1326 
  1315     QSqlQuery q( db );
  1327     QSqlQuery q( db );
  1316     QVERIFY_SQL( q, exec( "select id, t_varchar from " + qTableName( "qtest_null" ) + " order by id" ) );
  1328     QVERIFY_SQL( q, exec( "select id, t_varchar from " + qTableName( "qtest_null", __FILE__ ) + " order by id" ) );
  1317     QVERIFY( q.next() );
  1329     QVERIFY( q.next() );
  1318     QVERIFY( !q.isNull( 0 ) );
  1330     QVERIFY( !q.isNull( 0 ) );
  1319     QVERIFY( q.isNull( 1 ) );
  1331     QVERIFY( q.isNull( 1 ) );
  1320     QCOMPARE( q.value( 0 ).toInt(), 0 );
  1332     QCOMPARE( q.value( 0 ).toInt(), 0 );
  1321     QCOMPARE( q.value( 1 ).toString(), QString() );
  1333     QCOMPARE( q.value( 1 ).toString(), QString() );
  1337     if ( !db.driverName().startsWith( "QTDS" ) )
  1349     if ( !db.driverName().startsWith( "QTDS" ) )
  1338         QSKIP( "TDS specific test", SkipSingle );
  1350         QSKIP( "TDS specific test", SkipSingle );
  1339 
  1351 
  1340     QSqlQuery q( db );
  1352     QSqlQuery q( db );
  1341 
  1353 
  1342     QVERIFY_SQL( q, exec( "create table " + qTableName( "qtest_bittest" ) + " (bitty bit)" ) );
  1354     QVERIFY_SQL( q, exec( "create table " + qTableName( "qtest_bittest", __FILE__ ) + " (bitty bit)" ) );
  1343 
  1355 
  1344     QVERIFY_SQL( q, exec( "insert into " + qTableName( "qtest_bittest" ) + " values (0)" ) );
  1356     QVERIFY_SQL( q, exec( "insert into " + qTableName( "qtest_bittest", __FILE__ ) + " values (0)" ) );
  1345 
  1357 
  1346     QVERIFY_SQL( q, exec( "insert into " + qTableName( "qtest_bittest" ) + " values (1)" ) );
  1358     QVERIFY_SQL( q, exec( "insert into " + qTableName( "qtest_bittest", __FILE__ ) + " values (1)" ) );
  1347 
  1359 
  1348     QVERIFY_SQL( q, exec( "select bitty from " + qTableName( "qtest_bittest" ) ) );
  1360     QVERIFY_SQL( q, exec( "select bitty from " + qTableName( "qtest_bittest", __FILE__ ) ) );
  1349 
  1361 
  1350     QVERIFY( q.next() );
  1362     QVERIFY( q.next() );
  1351 
  1363 
  1352     QVERIFY( q.value( 0 ).toInt() == 0 );
  1364     QVERIFY( q.value( 0 ).toInt() == 0 );
  1353 
  1365 
  1361 void tst_QSqlQuery::nullBlob()
  1373 void tst_QSqlQuery::nullBlob()
  1362 {
  1374 {
  1363     QFETCH( QString, dbName );
  1375     QFETCH( QString, dbName );
  1364     QSqlDatabase db = QSqlDatabase::database( dbName );
  1376     QSqlDatabase db = QSqlDatabase::database( dbName );
  1365     CHECK_DATABASE( db );
  1377     CHECK_DATABASE( db );
  1366 
  1378     const QString qtest_nullblob(qTableName("qtest_nullblob", __FILE__));
  1367     QSqlQuery q( db );
  1379 
  1368     QVERIFY_SQL( q, exec( "create table " + qTableName( "qtest_nullblob" ) + " (id int primary key, bb blob)" ) );
  1380     QSqlQuery q( db );
  1369     QVERIFY_SQL( q, exec( "insert into " + qTableName( "qtest_nullblob" ) + " values (0, EMPTY_BLOB())" ) );
  1381     QVERIFY_SQL( q, exec( "create table " + qtest_nullblob + " (id int primary key, bb blob)" ) );
  1370     QVERIFY_SQL( q, exec( "insert into " + qTableName( "qtest_nullblob" ) + " values (1, NULL)" ) );
  1382     QVERIFY_SQL( q, exec( "insert into " + qtest_nullblob + " values (0, EMPTY_BLOB())" ) );
  1371     QVERIFY_SQL( q, exec( "insert into " + qTableName( "qtest_nullblob" ) + " values (2, 'aabbcc00112233445566')" ) );
  1383     QVERIFY_SQL( q, exec( "insert into " + qtest_nullblob + " values (1, NULL)" ) );
       
  1384     QVERIFY_SQL( q, exec( "insert into " + qtest_nullblob + " values (2, 'aabbcc00112233445566')" ) );
  1372     // necessary otherwise oracle will bombard you with internal errors
  1385     // necessary otherwise oracle will bombard you with internal errors
  1373     q.setForwardOnly( true );
  1386     q.setForwardOnly( true );
  1374     QVERIFY_SQL( q, exec( "select * from " + qTableName( "qtest_nullblob" ) + " order by id" ) );
  1387     QVERIFY_SQL( q, exec( "select * from " + qtest_nullblob + " order by id" ) );
  1375 
  1388 
  1376     QVERIFY( q.next() );
  1389     QVERIFY( q.next() );
  1377     QCOMPARE(( int )q.value( 1 ).toByteArray().size(), 0 );
  1390     QCOMPARE(( int )q.value( 1 ).toByteArray().size(), 0 );
  1378     QVERIFY( !q.isNull( 1 ) );
  1391     QVERIFY( !q.isNull( 1 ) );
  1379 
  1392 
  1390 void tst_QSqlQuery::rawField()
  1403 void tst_QSqlQuery::rawField()
  1391 {
  1404 {
  1392     QFETCH( QString, dbName );
  1405     QFETCH( QString, dbName );
  1393     QSqlDatabase db = QSqlDatabase::database( dbName );
  1406     QSqlDatabase db = QSqlDatabase::database( dbName );
  1394     CHECK_DATABASE( db );
  1407     CHECK_DATABASE( db );
       
  1408     const QString qtest_rawtest(qTableName("qtest_rawtest", __FILE__));
  1395 
  1409 
  1396     QSqlQuery q( db );
  1410     QSqlQuery q( db );
  1397     q.setForwardOnly( true );
  1411     q.setForwardOnly( true );
  1398     QVERIFY_SQL( q, exec( "create table " + qTableName( "qtest_rawtest" ) +
  1412     QVERIFY_SQL( q, exec( "create table " + qtest_rawtest +
  1399                             " (id int, col raw(20))" ) );
  1413                             " (id int, col raw(20))" ) );
  1400     QVERIFY_SQL( q, exec( "insert into " + qTableName( "qtest_rawtest" ) + " values (0, NULL)" ) );
  1414     QVERIFY_SQL( q, exec( "insert into " + qtest_rawtest + " values (0, NULL)" ) );
  1401     QVERIFY_SQL( q, exec( "insert into " + qTableName( "qtest_rawtest" ) + " values (1, '00aa1100ddeeff')" ) );
  1415     QVERIFY_SQL( q, exec( "insert into " + qtest_rawtest + " values (1, '00aa1100ddeeff')" ) );
  1402     QVERIFY_SQL( q, exec( "select col from " + qTableName( "qtest_rawtest" ) + " order by id" ) );
  1416     QVERIFY_SQL( q, exec( "select col from " + qtest_rawtest + " order by id" ) );
  1403     QVERIFY( q.next() );
  1417     QVERIFY( q.next() );
  1404     QVERIFY( q.isNull( 0 ) );
  1418     QVERIFY( q.isNull( 0 ) );
  1405     QCOMPARE(( int )q.value( 0 ).toByteArray().size(), 0 );
  1419     QCOMPARE(( int )q.value( 0 ).toByteArray().size(), 0 );
  1406     QVERIFY( q.next() );
  1420     QVERIFY( q.next() );
  1407     QVERIFY( !q.isNull( 0 ) );
  1421     QVERIFY( !q.isNull( 0 ) );
  1414 void tst_QSqlQuery::precision()
  1428 void tst_QSqlQuery::precision()
  1415 {
  1429 {
  1416     QFETCH( QString, dbName );
  1430     QFETCH( QString, dbName );
  1417     QSqlDatabase db = QSqlDatabase::database( dbName );
  1431     QSqlDatabase db = QSqlDatabase::database( dbName );
  1418     CHECK_DATABASE( db );
  1432     CHECK_DATABASE( db );
       
  1433     const QString qtest_precision(qTableName( "qtest_precision", __FILE__ ));
  1419 
  1434 
  1420     static const char* precStr = "1.2345678901234567891";
  1435     static const char* precStr = "1.2345678901234567891";
  1421 
  1436 
  1422     if ( db.driverName().startsWith( "QIBASE" ) )
  1437     if ( db.driverName().startsWith( "QIBASE" ) )
  1423         QSKIP( "DB unable to store high precision", SkipSingle );
  1438         QSKIP( "DB unable to store high precision", SkipSingle );
  1425     {
  1440     {
  1426         // need a new scope for SQLITE
  1441         // need a new scope for SQLITE
  1427         QSqlQuery q( db );
  1442         QSqlQuery q( db );
  1428 
  1443 
  1429         if ( tst_Databases::isMSAccess( db ) )
  1444         if ( tst_Databases::isMSAccess( db ) )
  1430             QVERIFY_SQL( q, exec( "create table " + qTableName( "qtest_precision" ) + " (col1 number)" ) );
  1445             QVERIFY_SQL( q, exec( "create table " + qtest_precision + " (col1 number)" ) );
  1431         else
  1446         else
  1432             QVERIFY_SQL( q, exec( "create table " + qTableName( "qtest_precision" ) + " (col1 numeric(21, 20))" ) );
  1447             QVERIFY_SQL( q, exec( "create table " + qtest_precision + " (col1 numeric(21, 20))" ) );
  1433 
  1448 
  1434         QVERIFY_SQL( q, exec( "insert into " + qTableName( "qtest_precision" ) + " (col1) values (1.2345678901234567891)" ) );
  1449         QVERIFY_SQL( q, exec( "insert into " + qtest_precision + " (col1) values (1.2345678901234567891)" ) );
  1435 
  1450 
  1436         QVERIFY_SQL( q, exec( "select * from " + qTableName( "qtest_precision" ) ) );
  1451         QVERIFY_SQL( q, exec( "select * from " + qtest_precision ) );
  1437         QVERIFY( q.next() );
  1452         QVERIFY( q.next() );
  1438 
  1453 
  1439         QString val = q.value( 0 ).toString();
  1454         QString val = q.value( 0 ).toString();
  1440 
  1455 
  1441         if ( !val.startsWith( "1.2345678901234567891" ) ) {
  1456         if ( !val.startsWith( "1.2345678901234567891" ) ) {
  1464     QFETCH( QString, dbName );
  1479     QFETCH( QString, dbName );
  1465     QSqlDatabase db = QSqlDatabase::database( dbName );
  1480     QSqlDatabase db = QSqlDatabase::database( dbName );
  1466     CHECK_DATABASE( db );
  1481     CHECK_DATABASE( db );
  1467 
  1482 
  1468     QSqlQuery q( db );
  1483     QSqlQuery q( db );
  1469     QVERIFY_SQL( q, exec( "select * from " + qTableName( "qtest" ) + " where id > 50000" ) );
  1484     QVERIFY_SQL( q, exec( "select * from " + qtest + " where id > 50000" ) );
  1470 
  1485 
  1471     if ( q.driver()->hasFeature( QSqlDriver::QuerySize ) )
  1486     if ( q.driver()->hasFeature( QSqlDriver::QuerySize ) )
  1472         QCOMPARE( q.size(), 0 );
  1487         QCOMPARE( q.size(), 0 );
  1473 
  1488 
  1474     QVERIFY( q.next() == false );
  1489     QVERIFY( q.next() == false );
  1505     QSqlQuery q2( db );
  1520     QSqlQuery q2( db );
  1506 
  1521 
  1507     // test a working transaction
  1522     // test a working transaction
  1508     q.exec( startTransactionStr );
  1523     q.exec( startTransactionStr );
  1509 
  1524 
  1510     QVERIFY_SQL( q, exec( "insert into" + qTableName( "qtest" ) + " values (40, 'VarChar40', 'Char40')" ) );
  1525     QVERIFY_SQL( q, exec( "insert into" + qtest + " values (40, 'VarChar40', 'Char40')" ) );
  1511 
  1526 
  1512     QVERIFY_SQL( q, exec( "select * from" + qTableName( "qtest" ) + " where id = 40" ) );
  1527     QVERIFY_SQL( q, exec( "select * from" + qtest + " where id = 40" ) );
  1513 
  1528 
  1514     QVERIFY( q.next() );
  1529     QVERIFY( q.next() );
  1515 
  1530 
  1516     QCOMPARE( q.value( 0 ).toInt(), 40 );
  1531     QCOMPARE( q.value( 0 ).toInt(), 40 );
  1517 
  1532 
  1518     QVERIFY_SQL( q, exec( "commit" ) );
  1533     QVERIFY_SQL( q, exec( "commit" ) );
  1519 
  1534 
  1520     QVERIFY_SQL( q, exec( "select * from" + qTableName( "qtest" ) + " where id = 40" ) );
  1535     QVERIFY_SQL( q, exec( "select * from" + qtest + " where id = 40" ) );
  1521 
  1536 
  1522     QVERIFY( q.next() );
  1537     QVERIFY( q.next() );
  1523 
  1538 
  1524     QCOMPARE( q.value( 0 ).toInt(), 40 );
  1539     QCOMPARE( q.value( 0 ).toInt(), 40 );
  1525 
  1540 
  1526     // test a rollback
  1541     // test a rollback
  1527     q.exec( startTransactionStr );
  1542     q.exec( startTransactionStr );
  1528 
  1543 
  1529     QVERIFY_SQL( q, exec( "insert into" + qTableName( "qtest" ) + " values (41, 'VarChar41', 'Char41')" ) );
  1544     QVERIFY_SQL( q, exec( "insert into" + qtest + " values (41, 'VarChar41', 'Char41')" ) );
  1530 
  1545 
  1531     QVERIFY_SQL( q, exec( "select * from" + qTableName( "qtest" ) + " where id = 41" ) );
  1546     QVERIFY_SQL( q, exec( "select * from" + qtest + " where id = 41" ) );
  1532 
  1547 
  1533     QVERIFY( q.next() );
  1548     QVERIFY( q.next() );
  1534 
  1549 
  1535     QCOMPARE( q.value( 0 ).toInt(), 41 );
  1550     QCOMPARE( q.value( 0 ).toInt(), 41 );
  1536 
  1551 
  1540             QSKIP( "MySQL transaction failed ", SkipSingle ); //non-fatal
  1555             QSKIP( "MySQL transaction failed ", SkipSingle ); //non-fatal
  1541         } else
  1556         } else
  1542             QFAIL( "Could not rollback transaction: " + tst_Databases::printError( q.lastError() ) );
  1557             QFAIL( "Could not rollback transaction: " + tst_Databases::printError( q.lastError() ) );
  1543     }
  1558     }
  1544 
  1559 
  1545     QVERIFY_SQL( q, exec( "select * from" + qTableName( "qtest" ) + " where id = 41" ) );
  1560     QVERIFY_SQL( q, exec( "select * from" + qtest + " where id = 41" ) );
  1546 
  1561 
  1547     QVERIFY( q.next() == false );
  1562     QVERIFY( q.next() == false );
  1548 
  1563 
  1549     // test concurrent access
  1564     // test concurrent access
  1550     q.exec( startTransactionStr );
  1565     q.exec( startTransactionStr );
  1551     QVERIFY_SQL( q, exec( "insert into" + qTableName( "qtest" ) + " values (42, 'VarChar42', 'Char42')" ) );
  1566     QVERIFY_SQL( q, exec( "insert into" + qtest + " values (42, 'VarChar42', 'Char42')" ) );
  1552     QVERIFY_SQL( q, exec( "select * from" + qTableName( "qtest" ) + " where id = 42" ) );
  1567     QVERIFY_SQL( q, exec( "select * from" + qtest + " where id = 42" ) );
  1553     QVERIFY( q.next() );
  1568     QVERIFY( q.next() );
  1554     QCOMPARE( q.value( 0 ).toInt(), 42 );
  1569     QCOMPARE( q.value( 0 ).toInt(), 42 );
  1555 
  1570 
  1556     QVERIFY_SQL( q2, exec( "select * from" + qTableName( "qtest" ) + " where id = 42" ) );
  1571     QVERIFY_SQL( q2, exec( "select * from" + qtest + " where id = 42" ) );
  1557 
  1572 
  1558     if ( q2.next() )
  1573     if ( q2.next() )
  1559         qDebug( QString( "DBMS '%1' doesn't support query based transactions with concurrent access" ).arg(
  1574         qDebug( QString( "DBMS '%1' doesn't support query based transactions with concurrent access" ).arg(
  1560                     tst_Databases::dbToString( db ) ).toLatin1() );
  1575                     tst_Databases::dbToString( db ) ).toLatin1() );
  1561 
  1576 
  1562     QVERIFY_SQL( q, exec( "commit" ) );
  1577     QVERIFY_SQL( q, exec( "commit" ) );
  1563 
  1578 
  1564     QVERIFY_SQL( q2, exec( "select * from" + qTableName( "qtest" ) + " where id = 42" ) );
  1579     QVERIFY_SQL( q2, exec( "select * from" + qtest + " where id = 42" ) );
  1565 
  1580 
  1566     QVERIFY( q2.next() );
  1581     QVERIFY( q2.next() );
  1567 
  1582 
  1568     QCOMPARE( q2.value( 0 ).toInt(), 42 );
  1583     QCOMPARE( q2.value( 0 ).toInt(), 42 );
  1569 }
  1584 }
  1571 void tst_QSqlQuery::joins()
  1586 void tst_QSqlQuery::joins()
  1572 {
  1587 {
  1573     QFETCH( QString, dbName );
  1588     QFETCH( QString, dbName );
  1574     QSqlDatabase db = QSqlDatabase::database( dbName );
  1589     QSqlDatabase db = QSqlDatabase::database( dbName );
  1575     CHECK_DATABASE( db );
  1590     CHECK_DATABASE( db );
       
  1591     const QString qtestj1(qTableName("qtestj1", __FILE__)), qtestj2(qTableName("qtestj2", __FILE__));
  1576 
  1592 
  1577     if ( db.driverName().startsWith( "QOCI" )
  1593     if ( db.driverName().startsWith( "QOCI" )
  1578             || db.driverName().startsWith( "QTDS" )
  1594             || db.driverName().startsWith( "QTDS" )
  1579             || db.driverName().startsWith( "QODBC" )
  1595             || db.driverName().startsWith( "QODBC" )
  1580             || db.driverName().startsWith( "QIBASE" ) ) {
  1596             || db.driverName().startsWith( "QIBASE" ) ) {
  1584         return;
  1600         return;
  1585     }
  1601     }
  1586 
  1602 
  1587     QSqlQuery q( db );
  1603     QSqlQuery q( db );
  1588 
  1604 
  1589     QVERIFY_SQL( q, exec( "create table " + qTableName( "qtestj1" ) + " (id1 int, id2 int)" ) );
  1605     QVERIFY_SQL( q, exec( "create table " + qtestj1 + " (id1 int, id2 int)" ) );
  1590     QVERIFY_SQL( q, exec( "create table " + qTableName( "qtestj2" ) + " (id int, name varchar(20))" ) );
  1606     QVERIFY_SQL( q, exec( "create table " + qtestj2 + " (id int, name varchar(20))" ) );
  1591     QVERIFY_SQL( q, exec( "insert into " + qTableName( "qtestj1" ) + " values (1, 1)" ) );
  1607     QVERIFY_SQL( q, exec( "insert into " + qtestj1 + " values (1, 1)" ) );
  1592     QVERIFY_SQL( q, exec( "insert into " + qTableName( "qtestj1" ) + " values (1, 2)" ) );
  1608     QVERIFY_SQL( q, exec( "insert into " + qtestj1 + " values (1, 2)" ) );
  1593     QVERIFY_SQL( q, exec( "insert into " + qTableName( "qtestj2" ) + " values(1, 'trenton')" ) );
  1609     QVERIFY_SQL( q, exec( "insert into " + qtestj2 + " values(1, 'trenton')" ) );
  1594     QVERIFY_SQL( q, exec( "insert into " + qTableName( "qtestj2" ) + " values(2, 'marius')" ) );
  1610     QVERIFY_SQL( q, exec( "insert into " + qtestj2 + " values(2, 'marius')" ) );
  1595 
  1611 
  1596     QVERIFY_SQL( q, exec( "select qtestj1.id1, qtestj1.id2, qtestj2.id, qtestj2.name, qtestj3.id, qtestj3.name "
  1612     QVERIFY_SQL( q, exec( "select qtestj1.id1, qtestj1.id2, qtestj2.id, qtestj2.name, qtestj3.id, qtestj3.name "
  1597                             "from " + qTableName( "qtestj1" ) + " qtestj1 left outer join " + qTableName( "qtestj2" ) +
  1613                             "from " + qtestj1 + " qtestj1 left outer join " + qtestj2 +
  1598                             " qtestj2 on (qtestj1.id1 = qtestj2.id) "
  1614                             " qtestj2 on (qtestj1.id1 = qtestj2.id) "
  1599                             "left outer join " + qTableName( "qtestj2" ) + " as qtestj3 on (qtestj1.id2 = qtestj3.id)" ) );
  1615                             "left outer join " + qtestj2 + " as qtestj3 on (qtestj1.id2 = qtestj3.id)" ) );
  1600 
  1616 
  1601     QVERIFY( q.next() );
  1617     QVERIFY( q.next() );
  1602     QCOMPARE( q.value( 0 ).toInt(), 1 );
  1618     QCOMPARE( q.value( 0 ).toInt(), 1 );
  1603     QCOMPARE( q.value( 1 ).toInt(), 1 );
  1619     QCOMPARE( q.value( 1 ).toInt(), 1 );
  1604     QCOMPARE( q.value( 2 ).toInt(), 1 );
  1620     QCOMPARE( q.value( 2 ).toInt(), 1 );
  1620     QFETCH( QString, dbName );
  1636     QFETCH( QString, dbName );
  1621     QSqlDatabase db = QSqlDatabase::database( dbName );
  1637     QSqlDatabase db = QSqlDatabase::database( dbName );
  1622     CHECK_DATABASE( db );
  1638     CHECK_DATABASE( db );
  1623 
  1639 
  1624     QSqlQuery q(db);
  1640     QSqlQuery q(db);
  1625     QVERIFY_SQL( q, exec("select a.id, a.t_char, a.t_varchar from " + qTableName( "qtest" ) + " a where a.id = 1") );
  1641     QVERIFY_SQL( q, exec("select a.id, a.t_char, a.t_varchar from " + qtest + " a where a.id = 1") );
  1626     QVERIFY( q.next() );
  1642     QVERIFY( q.next() );
  1627     QCOMPARE( q.value( 0 ).toInt(), 1 );
  1643     QCOMPARE( q.value( 0 ).toInt(), 1 );
  1628     QCOMPARE( q.value( 1 ).toString().trimmed(), QString( "Char1" ) );
  1644     QCOMPARE( q.value( 1 ).toString().trimmed(), QString( "Char1" ) );
  1629     QCOMPARE( q.value( 2 ).toString().trimmed(), QString( "VarChar1" ) );
  1645     QCOMPARE( q.value( 2 ).toString().trimmed(), QString( "VarChar1" ) );
  1630 
  1646 
  1639 void tst_QSqlQuery::prepare_bind_exec()
  1655 void tst_QSqlQuery::prepare_bind_exec()
  1640 {
  1656 {
  1641     QFETCH( QString, dbName );
  1657     QFETCH( QString, dbName );
  1642     QSqlDatabase db = QSqlDatabase::database( dbName );
  1658     QSqlDatabase db = QSqlDatabase::database( dbName );
  1643     CHECK_DATABASE( db );
  1659     CHECK_DATABASE( db );
       
  1660     const QString qtest_prepare(qTableName("qtest_prepare", __FILE__));
       
  1661 
  1644     if(db.driverName().startsWith("QIBASE") && (db.databaseName() == "silence.nokia.troll.no:c:\\ibase\\testdb_ascii" || db.databaseName() == "/opt/interbase/qttest.gdb"))
  1662     if(db.driverName().startsWith("QIBASE") && (db.databaseName() == "silence.nokia.troll.no:c:\\ibase\\testdb_ascii" || db.databaseName() == "/opt/interbase/qttest.gdb"))
  1645         QSKIP("Can't transliterate extended unicode to ascii", SkipSingle);
  1663         QSKIP("Can't transliterate extended unicode to ascii", SkipSingle);
  1646     if(db.driverName().startsWith("QDB2"))
  1664     if(db.driverName().startsWith("QDB2"))
  1647         QSKIP("Needs someone with more Unicode knowledge than I have to fix", SkipSingle);
  1665         QSKIP("Needs someone with more Unicode knowledge than I have to fix", SkipSingle);
  1648 
  1666 
  1663 
  1681 
  1664         if(tst_Databases::isPostgreSQL(db))
  1682         if(tst_Databases::isPostgreSQL(db))
  1665             QVERIFY_SQL( q, exec("set client_min_messages='warning'"));
  1683             QVERIFY_SQL( q, exec("set client_min_messages='warning'"));
  1666 
  1684 
  1667         if ( tst_Databases::isSqlServer( db ) || db.driverName().startsWith( "QTDS" ) )
  1685         if ( tst_Databases::isSqlServer( db ) || db.driverName().startsWith( "QTDS" ) )
  1668             createQuery = "create table " + qTableName( "qtest_prepare" ) + " (id int primary key, name nvarchar(200) null)";
  1686             createQuery = "create table " + qtest_prepare + " (id int primary key, name nvarchar(200) null)";
  1669         else if ( tst_Databases::isMySQL(db) && useUnicode )
  1687         else if ( tst_Databases::isMySQL(db) && useUnicode )
  1670             createQuery = "create table " + qTableName( "qtest_prepare" ) + " (id int not null primary key, name varchar(200) character set utf8)";
  1688             createQuery = "create table " + qtest_prepare + " (id int not null primary key, name varchar(200) character set utf8)";
  1671         else
  1689         else
  1672             createQuery = "create table " + qTableName( "qtest_prepare" ) + " (id int not null primary key, name varchar(200))";
  1690             createQuery = "create table " + qtest_prepare + " (id int not null primary key, name varchar(200))";
  1673 
  1691 
  1674         QVERIFY_SQL( q, exec( createQuery ) );
  1692         QVERIFY_SQL( q, exec( createQuery ) );
  1675 
  1693 
  1676         QVERIFY( q.prepare( "insert into " + qTableName( "qtest_prepare" ) + " (id, name) values (:id, :name)" ) );
  1694         QVERIFY( q.prepare( "insert into " + qtest_prepare + " (id, name) values (:id, :name)" ) );
  1677         int i;
  1695         int i;
  1678 
  1696 
  1679         for ( i = 0; i < 6; ++i ) {
  1697         for ( i = 0; i < 6; ++i ) {
  1680             q.bindValue( ":name", values[i] );
  1698             q.bindValue( ":name", values[i] );
  1681             q.bindValue( ":id", i );
  1699             q.bindValue( ":id", i );
  1694             q.bindValue( ":id", 7 );
  1712             q.bindValue( ":id", 7 );
  1695             q.bindValue( ":name", utf8str );
  1713             q.bindValue( ":name", utf8str );
  1696             QVERIFY_SQL( q, exec() );
  1714             QVERIFY_SQL( q, exec() );
  1697         }
  1715         }
  1698 
  1716 
  1699         QVERIFY_SQL( q, exec( "SELECT * FROM " + qTableName( "qtest_prepare" ) + " order by id" ) );
  1717         QVERIFY_SQL( q, exec( "SELECT * FROM " + qtest_prepare + " order by id" ) );
  1700 
  1718 
  1701         for ( i = 0; i < 6; ++i ) {
  1719         for ( i = 0; i < 6; ++i ) {
  1702             QVERIFY( q.next() );
  1720             QVERIFY( q.next() );
  1703             QCOMPARE( q.value( 0 ).toInt(), i );
  1721             QCOMPARE( q.value( 0 ).toInt(), i );
  1704             QCOMPARE( q.value( 1 ).toString().trimmed(), values[ i ] );
  1722             QCOMPARE( q.value( 1 ).toString().trimmed(), values[ i ] );
  1713         QVERIFY_SQL( q, next() );
  1731         QVERIFY_SQL( q, next() );
  1714 
  1732 
  1715         QCOMPARE( q.value( 0 ).toInt(), 8 );
  1733         QCOMPARE( q.value( 0 ).toInt(), 8 );
  1716         QCOMPARE( q.value( 1 ).toString(), values[5] );
  1734         QCOMPARE( q.value( 1 ).toString(), values[5] );
  1717 
  1735 
  1718         QVERIFY( q.prepare( "insert into " + qTableName( "qtest_prepare" ) + " (id, name) values (:id, 'Bart')" ) );
  1736         QVERIFY( q.prepare( "insert into " + qtest_prepare + " (id, name) values (:id, 'Bart')" ) );
  1719         q.bindValue( ":id", 99 );
  1737         q.bindValue( ":id", 99 );
  1720         QVERIFY_SQL( q, exec() );
  1738         QVERIFY_SQL( q, exec() );
  1721         q.bindValue( ":id", 100 );
  1739         q.bindValue( ":id", 100 );
  1722         QVERIFY_SQL( q, exec() );
  1740         QVERIFY_SQL( q, exec() );
  1723         QVERIFY( q.exec( "select * from " + qTableName( "qtest_prepare" ) + " where id > 98 order by id" ) );
  1741         QVERIFY( q.exec( "select * from " + qtest_prepare + " where id > 98 order by id" ) );
  1724 
  1742 
  1725         for ( i = 99; i <= 100; ++i ) {
  1743         for ( i = 99; i <= 100; ++i ) {
  1726             QVERIFY( q.next() );
  1744             QVERIFY( q.next() );
  1727             QCOMPARE( q.value( 0 ).toInt(), i );
  1745             QCOMPARE( q.value( 0 ).toInt(), i );
  1728             QCOMPARE( q.value( 1 ).toString().trimmed(), QString( "Bart" ) );
  1746             QCOMPARE( q.value( 1 ).toString().trimmed(), QString( "Bart" ) );
  1729         }
  1747         }
  1730 
  1748 
  1731         /*** SELECT stuff ***/
  1749         /*** SELECT stuff ***/
  1732         QVERIFY( q.prepare( "select * from " + qTableName( "qtest_prepare" ) + " where id = :id" ) );
  1750         QVERIFY( q.prepare( "select * from " + qtest_prepare + " where id = :id" ) );
  1733 
  1751 
  1734         for ( i = 0; i < 6; ++i ) {
  1752         for ( i = 0; i < 6; ++i ) {
  1735             q.bindValue( ":id", i );
  1753             q.bindValue( ":id", i );
  1736             QVERIFY_SQL( q, exec() );
  1754             QVERIFY_SQL( q, exec() );
  1737             QVERIFY_SQL( q, next() );
  1755             QVERIFY_SQL( q, next() );
  1742             QCOMPARE( rInf.field( 0 ).name().toUpper(), QString( "ID" ) );
  1760             QCOMPARE( rInf.field( 0 ).name().toUpper(), QString( "ID" ) );
  1743             QCOMPARE( rInf.field( 1 ).name().toUpper(), QString( "NAME" ) );
  1761             QCOMPARE( rInf.field( 1 ).name().toUpper(), QString( "NAME" ) );
  1744             QVERIFY( !q.next() );
  1762             QVERIFY( !q.next() );
  1745         }
  1763         }
  1746 
  1764 
  1747         QVERIFY_SQL( q, exec( "DELETE FROM " + qTableName( "qtest_prepare" ) ) );
  1765         QVERIFY_SQL( q, exec( "DELETE FROM " + qtest_prepare ) );
  1748 
  1766 
  1749         QVERIFY( q.prepare( "insert into " + qTableName( "qtest_prepare" ) + " (id, name) values (?, ?)" ) );
  1767         QVERIFY( q.prepare( "insert into " + qtest_prepare + " (id, name) values (?, ?)" ) );
  1750         q.bindValue( 0, 0 );
  1768         q.bindValue( 0, 0 );
  1751         q.bindValue( 1, values[ 0 ] );
  1769         q.bindValue( 1, values[ 0 ] );
  1752         QVERIFY_SQL( q, exec() );
  1770         QVERIFY_SQL( q, exec() );
  1753         q.addBindValue( 1 );
  1771         q.addBindValue( 1 );
  1754         q.addBindValue( values[ 1 ] );
  1772         q.addBindValue( values[ 1 ] );
  1773             q.bindValue( 0, 7 );
  1791             q.bindValue( 0, 7 );
  1774             q.bindValue( 1, utf8str );
  1792             q.bindValue( 1, utf8str );
  1775             QVERIFY_SQL( q, exec() );
  1793             QVERIFY_SQL( q, exec() );
  1776         }
  1794         }
  1777 
  1795 
  1778         QVERIFY_SQL( q, exec( "SELECT * FROM " + qTableName( "qtest_prepare" ) + " order by id" ) );
  1796         QVERIFY_SQL( q, exec( "SELECT * FROM " + qtest_prepare + " order by id" ) );
  1779 
  1797 
  1780         for ( i = 0; i < 6; ++i ) {
  1798         for ( i = 0; i < 6; ++i ) {
  1781             QVERIFY( q.next() );
  1799             QVERIFY( q.next() );
  1782             QCOMPARE( q.value( 0 ).toInt(), i );
  1800             QCOMPARE( q.value( 0 ).toInt(), i );
  1783             QCOMPARE( q.value( 1 ).toString().trimmed(), values[ i ] );
  1801             QCOMPARE( q.value( 1 ).toString().trimmed(), values[ i ] );
  1792             QVERIFY( q.next() );
  1810             QVERIFY( q.next() );
  1793             QCOMPARE( q.value( 0 ).toInt(), 7 );
  1811             QCOMPARE( q.value( 0 ).toInt(), 7 );
  1794             QCOMPARE( q.value( 1 ).toString(), utf8str );
  1812             QCOMPARE( q.value( 1 ).toString(), utf8str );
  1795         }
  1813         }
  1796 
  1814 
  1797         QVERIFY( q.prepare( "insert into " + qTableName( "qtest_prepare" ) + " (id, name) values (?, 'Bart')" ) );
  1815         QVERIFY( q.prepare( "insert into " + qtest_prepare + " (id, name) values (?, 'Bart')" ) );
  1798 
  1816 
  1799         q.bindValue( 0, 99 );
  1817         q.bindValue( 0, 99 );
  1800         QVERIFY_SQL( q, exec() );
  1818         QVERIFY_SQL( q, exec() );
  1801         q.addBindValue( 100 );
  1819         q.addBindValue( 100 );
  1802         QVERIFY_SQL( q, exec() );
  1820         QVERIFY_SQL( q, exec() );
  1803         QVERIFY( q.exec( "select * from " + qTableName( "qtest_prepare" ) + " where id > 98 order by id" ) );
  1821         QVERIFY( q.exec( "select * from " + qtest_prepare + " where id > 98 order by id" ) );
  1804 
  1822 
  1805         for ( i = 99; i <= 100; ++i ) {
  1823         for ( i = 99; i <= 100; ++i ) {
  1806             QVERIFY( q.next() );
  1824             QVERIFY( q.next() );
  1807             QCOMPARE( q.value( 0 ).toInt(), i );
  1825             QCOMPARE( q.value( 0 ).toInt(), i );
  1808             QCOMPARE( q.value( 1 ).toString().trimmed(), QString( "Bart" ) );
  1826             QCOMPARE( q.value( 1 ).toString().trimmed(), QString( "Bart" ) );
  1809         }
  1827         }
  1810 
  1828 
  1811         /* insert a duplicate id and make sure the db bails out */
  1829         /* insert a duplicate id and make sure the db bails out */
  1812         QVERIFY( q.prepare( "insert into " + qTableName( "qtest_prepare" ) + " (id, name) values (?, ?)" ) );
  1830         QVERIFY( q.prepare( "insert into " + qtest_prepare + " (id, name) values (?, ?)" ) );
  1813 
  1831 
  1814         q.addBindValue( 99 );
  1832         q.addBindValue( 99 );
  1815 
  1833 
  1816         q.addBindValue( "something silly" );
  1834         q.addBindValue( "something silly" );
  1817 
  1835 
  1829     QFETCH( QString, dbName );
  1847     QFETCH( QString, dbName );
  1830     QSqlDatabase db = QSqlDatabase::database( dbName );
  1848     QSqlDatabase db = QSqlDatabase::database( dbName );
  1831     CHECK_DATABASE( db );
  1849     CHECK_DATABASE( db );
  1832 
  1850 
  1833     QSqlQuery q( db );
  1851     QSqlQuery q( db );
  1834     QVERIFY_SQL( q, prepare( "select a.id, a.t_char, a.t_varchar from " + qTableName( "qtest" ) + " a where a.id = ?" ) );
  1852     QVERIFY_SQL( q, prepare( "select a.id, a.t_char, a.t_varchar from " + qtest + " a where a.id = ?" ) );
  1835 
  1853 
  1836     q.bindValue( 0, 1 );
  1854     q.bindValue( 0, 1 );
  1837     QVERIFY_SQL( q, exec() );
  1855     QVERIFY_SQL( q, exec() );
  1838     QCOMPARE( q.at(), ( int )QSql::BeforeFirstRow );
  1856     QCOMPARE( q.at(), ( int )QSql::BeforeFirstRow );
  1839     QVERIFY( q.next() );
  1857     QVERIFY( q.next() );
  1849     QVERIFY_SQL( q, exec() );
  1867     QVERIFY_SQL( q, exec() );
  1850     QCOMPARE( q.at(), ( int )QSql::BeforeFirstRow );
  1868     QCOMPARE( q.at(), ( int )QSql::BeforeFirstRow );
  1851     QVERIFY( q.next() );
  1869     QVERIFY( q.next() );
  1852     QCOMPARE( q.value( 0 ).toInt(), 3 );
  1870     QCOMPARE( q.value( 0 ).toInt(), 3 );
  1853 
  1871 
  1854     QVERIFY_SQL( q, prepare( "select a.id, a.t_char, a.t_varchar from " + qTableName( "qtest" ) + " a where a.id = ?" ) );
  1872     QVERIFY_SQL( q, prepare( "select a.id, a.t_char, a.t_varchar from " + qtest + " a where a.id = ?" ) );
  1855     QCOMPARE( q.at(), ( int )QSql::BeforeFirstRow );
  1873     QCOMPARE( q.at(), ( int )QSql::BeforeFirstRow );
  1856     QVERIFY( !q.first() );
  1874     QVERIFY( !q.first() );
  1857 }
  1875 }
  1858 
  1876 
  1859 void tst_QSqlQuery::sqlServerLongStrings()
  1877 void tst_QSqlQuery::sqlServerLongStrings()
  1865     if ( !tst_Databases::isSqlServer( db ) )
  1883     if ( !tst_Databases::isSqlServer( db ) )
  1866         QSKIP( "SQL Server specific test", SkipSingle );
  1884         QSKIP( "SQL Server specific test", SkipSingle );
  1867 
  1885 
  1868     QSqlQuery q( db );
  1886     QSqlQuery q( db );
  1869 
  1887 
  1870     QVERIFY_SQL( q, exec( "CREATE TABLE " + qTableName( "qtest_longstr" ) + " (id int primary key, longstring ntext)" ) );
  1888     QVERIFY_SQL( q, exec( "CREATE TABLE " + qTableName( "qtest_longstr", __FILE__ ) + " (id int primary key, longstring ntext)" ) );
  1871 
  1889 
  1872     QVERIFY_SQL( q, prepare( "INSERT INTO " + qTableName( "qtest_longstr" ) + " VALUES (?, ?)" ) );
  1890     QVERIFY_SQL( q, prepare( "INSERT INTO " + qTableName( "qtest_longstr", __FILE__ ) + " VALUES (?, ?)" ) );
  1873 
  1891 
  1874     q.addBindValue( 0 );
  1892     q.addBindValue( 0 );
  1875 
  1893 
  1876     q.addBindValue( QString::fromLatin1( "bubu" ) );
  1894     q.addBindValue( QString::fromLatin1( "bubu" ) );
  1877 
  1895 
  1885 
  1903 
  1886     q.addBindValue( testStr );
  1904     q.addBindValue( testStr );
  1887 
  1905 
  1888     QVERIFY_SQL( q, exec() );
  1906     QVERIFY_SQL( q, exec() );
  1889 
  1907 
  1890     QVERIFY_SQL( q, exec( "select * from " + qTableName( "qtest_longstr" ) ) );
  1908     QVERIFY_SQL( q, exec( "select * from " + qTableName( "qtest_longstr", __FILE__ ) ) );
  1891 
  1909 
  1892     QVERIFY_SQL( q, next() );
  1910     QVERIFY_SQL( q, next() );
  1893 
  1911 
  1894     QCOMPARE( q.value( 0 ).toInt(), 0 );
  1912     QCOMPARE( q.value( 0 ).toInt(), 0 );
  1895 
  1913 
  1949 
  1967 
  1950     if ( !db.driver()->hasFeature( QSqlDriver::BatchOperations ) )
  1968     if ( !db.driver()->hasFeature( QSqlDriver::BatchOperations ) )
  1951         QSKIP( "Database can't do BatchOperations", SkipSingle );
  1969         QSKIP( "Database can't do BatchOperations", SkipSingle );
  1952 
  1970 
  1953     QSqlQuery q( db );
  1971     QSqlQuery q( db );
  1954     QString tableName = qTableName( "qtest_batch" );
  1972     const QString tableName = qTableName( "qtest_batch", __FILE__ );
  1955 
  1973 
  1956     QVERIFY_SQL( q, exec( "create table " + tableName + " (id int, name varchar(20), dt date, num numeric(8, 4))" ) );
  1974     QVERIFY_SQL( q, exec( "create table " + tableName + " (id int, name varchar(20), dt date, num numeric(8, 4))" ) );
  1957     QVERIFY_SQL( q, prepare( "insert into " + tableName + " (id, name, dt, num) values (?, ?, ?, ?)" ) );
  1975     QVERIFY_SQL( q, prepare( "insert into " + tableName + " (id, name, dt, num) values (?, ?, ?, ?)" ) );
  1958 
  1976 
  1959     QVariantList intCol;
  1977     QVariantList intCol;
  2093     QSqlDatabase db = QSqlDatabase::database( dbName );
  2111     QSqlDatabase db = QSqlDatabase::database( dbName );
  2094     CHECK_DATABASE( db );
  2112     CHECK_DATABASE( db );
  2095 
  2113 
  2096     QSqlQuery q( db );
  2114     QSqlQuery q( db );
  2097 
  2115 
  2098     QVERIFY_SQL( q, exec( "create table "+qTableName( "record_sqlite" )+"(id integer primary key, name varchar, title int)" ) );
  2116     QVERIFY_SQL( q, exec( "create table "+qTableName( "record_sqlite", __FILE__ )+"(id integer primary key, name varchar, title int)" ) );
  2099 
  2117 
  2100     QSqlRecord rec = db.record( qTableName( "record_sqlite" ) );
  2118     QSqlRecord rec = db.record( qTableName( "record_sqlite", __FILE__ ) );
  2101 
  2119 
  2102     QCOMPARE( rec.count(), 3 );
  2120     QCOMPARE( rec.count(), 3 );
  2103     QCOMPARE( rec.field( 0 ).type(), QVariant::Int );
  2121     QCOMPARE( rec.field( 0 ).type(), QVariant::Int );
  2104     QCOMPARE( rec.field( 1 ).type(), QVariant::String );
  2122     QCOMPARE( rec.field( 1 ).type(), QVariant::String );
  2105     QCOMPARE( rec.field( 2 ).type(), QVariant::Int );
  2123     QCOMPARE( rec.field( 2 ).type(), QVariant::Int );
  2106 
  2124 
  2107     /* important - select from an empty table */
  2125     /* important - select from an empty table */
  2108     QVERIFY_SQL( q, exec( "select id, name, title from "+qTableName( "record_sqlite" ) ) );
  2126     QVERIFY_SQL( q, exec( "select id, name, title from "+qTableName( "record_sqlite", __FILE__ ) ) );
  2109 
  2127 
  2110     rec = q.record();
  2128     rec = q.record();
  2111     QCOMPARE( rec.count(), 3 );
  2129     QCOMPARE( rec.count(), 3 );
  2112     QCOMPARE( rec.field( 0 ).type(), QVariant::Int );
  2130     QCOMPARE( rec.field( 0 ).type(), QVariant::Int );
  2113     QCOMPARE( rec.field( 1 ).type(), QVariant::String );
  2131     QCOMPARE( rec.field( 1 ).type(), QVariant::String );
  2122 
  2140 
  2123     QSqlQuery q( db );
  2141     QSqlQuery q( db );
  2124 
  2142 
  2125     QString aLotOfText( 127000, QLatin1Char( 'H' ) );
  2143     QString aLotOfText( 127000, QLatin1Char( 'H' ) );
  2126 
  2144 
  2127     QVERIFY_SQL( q, exec( "create table " + qTableName( "qtest_longstr" ) + " (id int primary key, astr long)" ) );
  2145     QVERIFY_SQL( q, exec( "create table " + qTableName( "qtest_longstr", __FILE__ ) + " (id int primary key, astr long)" ) );
  2128     QVERIFY_SQL( q, prepare( "insert into " + qTableName( "qtest_longstr" ) + " (id, astr) values (?, ?)" ) );
  2146     QVERIFY_SQL( q, prepare( "insert into " + qTableName( "qtest_longstr", __FILE__ ) + " (id, astr) values (?, ?)" ) );
  2129     q.addBindValue( 1 );
  2147     q.addBindValue( 1 );
  2130     q.addBindValue( aLotOfText );
  2148     q.addBindValue( aLotOfText );
  2131     QVERIFY_SQL( q, exec() );
  2149     QVERIFY_SQL( q, exec() );
  2132 
  2150 
  2133     QVERIFY_SQL( q, exec( "select id,astr from " + qTableName( "qtest_longstr" ) ) );
  2151     QVERIFY_SQL( q, exec( "select id,astr from " + qTableName( "qtest_longstr", __FILE__ ) ) );
  2134 
  2152 
  2135     QVERIFY( q.next() );
  2153     QVERIFY( q.next() );
  2136     QCOMPARE( q.value( 0 ).toInt(), 1 );
  2154     QCOMPARE( q.value( 0 ).toInt(), 1 );
  2137     QCOMPARE( q.value( 1 ).toString(), aLotOfText );
  2155     QCOMPARE( q.value( 1 ).toString(), aLotOfText );
  2138 }
  2156 }
  2143     QSqlDatabase db = QSqlDatabase::database( dbName );
  2161     QSqlDatabase db = QSqlDatabase::database( dbName );
  2144     CHECK_DATABASE( db );
  2162     CHECK_DATABASE( db );
  2145 
  2163 
  2146     QSqlQuery q( db );
  2164     QSqlQuery q( db );
  2147 
  2165 
  2148     QVERIFY_SQL( q, exec( "create table " + qTableName( "qtest_exerr" ) + " (id int not null primary key)" ) );
  2166     QVERIFY_SQL( q, exec( "create table " + qTableName( "qtest_exerr", __FILE__ ) + " (id int not null primary key)" ) );
  2149     QVERIFY_SQL( q, prepare( "insert into " + qTableName( "qtest_exerr" ) + " values (?)" ) );
  2167     QVERIFY_SQL( q, prepare( "insert into " + qTableName( "qtest_exerr", __FILE__ ) + " values (?)" ) );
  2150 
  2168 
  2151     q.addBindValue( 1 );
  2169     q.addBindValue( 1 );
  2152     QVERIFY_SQL( q, exec() );
  2170     QVERIFY_SQL( q, exec() );
  2153 
  2171 
  2154     q.addBindValue( 1 ); // binding the same pkey - should fail
  2172     q.addBindValue( 1 ); // binding the same pkey - should fail
  2167     if ( !db.driver()->hasFeature( QSqlDriver::LastInsertId ) )
  2185     if ( !db.driver()->hasFeature( QSqlDriver::LastInsertId ) )
  2168         QSKIP( "Database doesn't support lastInsertId", SkipSingle );
  2186         QSKIP( "Database doesn't support lastInsertId", SkipSingle );
  2169 
  2187 
  2170     QSqlQuery q( db );
  2188     QSqlQuery q( db );
  2171 
  2189 
  2172     QVERIFY_SQL( q, exec( "insert into " + qTableName( "qtest" ) + " values (41, 'VarChar41', 'Char41')" ) );
  2190     QVERIFY_SQL( q, exec( "insert into " + qtest + " values (41, 'VarChar41', 'Char41')" ) );
  2173 
  2191 
  2174     QVariant v = q.lastInsertId();
  2192     QVariant v = q.lastInsertId();
  2175 
  2193 
  2176     QVERIFY( v.isValid() );
  2194     QVERIFY( v.isValid() );
  2177 }
  2195 }
  2181     QFETCH( QString, dbName );
  2199     QFETCH( QString, dbName );
  2182     QSqlDatabase db = QSqlDatabase::database( dbName );
  2200     QSqlDatabase db = QSqlDatabase::database( dbName );
  2183     CHECK_DATABASE( db );
  2201     CHECK_DATABASE( db );
  2184 
  2202 
  2185     QSqlQuery q( db );
  2203     QSqlQuery q( db );
  2186     QString sql = "select * from " + qTableName( "qtest" );
  2204     QString sql = "select * from " + qtest;
  2187     QVERIFY_SQL( q, exec( sql ) );
  2205     QVERIFY_SQL( q, exec( sql ) );
  2188     QCOMPARE( q.lastQuery(), sql );
  2206     QCOMPARE( q.lastQuery(), sql );
  2189     QCOMPARE( q.executedQuery(), sql );
  2207     QCOMPARE( q.executedQuery(), sql );
  2190 }
  2208 }
  2191 
  2209 
  2200     if ( !db.driverName().startsWith( "QPSQL" ) ) {
  2218     if ( !db.driverName().startsWith( "QPSQL" ) ) {
  2201         QSKIP( "Test requires PostgreSQL", SkipSingle );
  2219         QSKIP( "Test requires PostgreSQL", SkipSingle );
  2202         return;
  2220         return;
  2203     }
  2221     }
  2204 
  2222 
  2205     QString tablename = qTableName( "bindtest" );
  2223     const QString tablename(qTableName( "bindtest", __FILE__ ));
  2206 
  2224 
  2207     QSqlQuery q( db );
  2225     QSqlQuery q( db );
  2208 
  2226 
  2209     QVERIFY_SQL( q, exec( "create table " + tablename + " (id1 int, id2 int, id3 int, fld1 int, fld2 int)" ) );
  2227     QVERIFY_SQL( q, exec( "create table " + tablename + " (id1 int, id2 int, id3 int, fld1 int, fld2 int)" ) );
  2210     QVERIFY_SQL( q, exec( "insert into " + tablename + " values (1, 2, 3, 10, 5)" ) );
  2228     QVERIFY_SQL( q, exec( "insert into " + tablename + " values (1, 2, 3, 10, 5)" ) );
  2274 
  2292 
  2275     db.close();
  2293     db.close();
  2276 
  2294 
  2277     QSqlQuery q( db );
  2295     QSqlQuery q( db );
  2278     db.open();
  2296     db.open();
  2279     QVERIFY_SQL( q, exec( QString( "select * from %1 where id = 1" ).arg( qTableName( "qtest" ) ) ) );
  2297     QVERIFY_SQL( q, exec( QString( "select * from %1 where id = 1" ).arg( qtest ) ) );
  2280 
  2298 
  2281     QVERIFY_SQL( q, next() );
  2299     QVERIFY_SQL( q, next() );
  2282     QCOMPARE( q.value( 0 ).toInt(), 1 );
  2300     QCOMPARE( q.value( 0 ).toInt(), 1 );
  2283     QCOMPARE( q.value( 1 ).toString().trimmed(), QLatin1String( "VarChar1" ) );
  2301     QCOMPARE( q.value( 1 ).toString().trimmed(), QLatin1String( "VarChar1" ) );
  2284     QCOMPARE( q.value( 2 ).toString().trimmed(), QLatin1String( "Char1" ) );
  2302     QCOMPARE( q.value( 2 ).toString().trimmed(), QLatin1String( "Char1" ) );
  2285 
  2303 
  2286     db.close();
  2304     db.close();
  2287     QVERIFY2( !q.exec( QString( "select * from %1 where id = 1" ).arg( qTableName( "qtest" ) ) ),
  2305     QVERIFY2( !q.exec( QString( "select * from %1 where id = 1" ).arg( qtest ) ),
  2288               qPrintable( QString( "This can't happen! The query should not have been executed!" ) ) );
  2306               qPrintable( QString( "This can't happen! The query should not have been executed!" ) ) );
  2289 }
  2307 }
  2290 
  2308 
  2291 void tst_QSqlQuery::reExecutePreparedForwardOnlyQuery()
  2309 void tst_QSqlQuery::reExecutePreparedForwardOnlyQuery()
  2292 {
  2310 {
  2295     CHECK_DATABASE( db );
  2313     CHECK_DATABASE( db );
  2296 
  2314 
  2297     QSqlQuery q( db );
  2315     QSqlQuery q( db );
  2298     q.setForwardOnly( true );
  2316     q.setForwardOnly( true );
  2299 
  2317 
  2300     QVERIFY_SQL( q, prepare( QString( "SELECT id, t_varchar, t_char FROM %1 WHERE id = :id" ).arg( qTableName( "qtest" ) ) ) );
  2318     QVERIFY_SQL( q, prepare( QString( "SELECT id, t_varchar, t_char FROM %1 WHERE id = :id" ).arg( qtest ) ) );
  2301     q.bindValue( ":id", 1 );
  2319     q.bindValue( ":id", 1 );
  2302     QVERIFY_SQL( q, exec() );
  2320     QVERIFY_SQL( q, exec() );
  2303 
  2321 
  2304     // Do something, like iterate over the result, or skip to the end
  2322     // Do something, like iterate over the result, or skip to the end
  2305     QVERIFY_SQL( q, last() );
  2323     QVERIFY_SQL( q, last() );
  2320     QFETCH( QString, dbName );
  2338     QFETCH( QString, dbName );
  2321     QSqlDatabase db = QSqlDatabase::database( dbName );
  2339     QSqlDatabase db = QSqlDatabase::database( dbName );
  2322     CHECK_DATABASE( db );
  2340     CHECK_DATABASE( db );
  2323 
  2341 
  2324     QSqlQuery q( db );
  2342     QSqlQuery q( db );
  2325     QVERIFY_SQL( q, prepare( "SELECT id FROM " + qTableName( "qtest" ) + " WHERE id = ?" ) );
  2343     QVERIFY_SQL( q, prepare( "SELECT id FROM " + qtest + " WHERE id = ?" ) );
  2326 
  2344 
  2327     int id = 4;
  2345     int id = 4;
  2328     q.bindValue( 0, id );
  2346     q.bindValue( 0, id );
  2329     QVERIFY_SQL( q, exec() );
  2347     QVERIFY_SQL( q, exec() );
  2330     QVERIFY( q.isActive() );
  2348     QVERIFY( q.isActive() );
  2341     QCOMPARE( q.value( 0 ).toInt(), id );
  2359     QCOMPARE( q.value( 0 ).toInt(), id );
  2342 
  2360 
  2343     q.finish();
  2361     q.finish();
  2344     QVERIFY( !q.isActive() );
  2362     QVERIFY( !q.isActive() );
  2345 
  2363 
  2346     QVERIFY_SQL( q, exec( "SELECT id FROM " + qTableName( "qtest" ) + " WHERE id = 1" ) );
  2364     QVERIFY_SQL( q, exec( "SELECT id FROM " + qtest + " WHERE id = 1" ) );
  2347     QVERIFY( q.isActive() );
  2365     QVERIFY( q.isActive() );
  2348     QVERIFY_SQL( q, next() );
  2366     QVERIFY_SQL( q, next() );
  2349     QCOMPARE( q.value( 0 ).toInt(), 1 );
  2367     QCOMPARE( q.value( 0 ).toInt(), 1 );
  2350     QCOMPARE( q.record().count(), 1 );
  2368     QCOMPARE( q.record().count(), 1 );
  2351 }
  2369 }
  2366     {
  2384     {
  2367         QSqlDatabase db2 = QSqlDatabase::addDatabase( "QSQLITE", "sqlite_finish_sqlite" );
  2385         QSqlDatabase db2 = QSqlDatabase::addDatabase( "QSQLITE", "sqlite_finish_sqlite" );
  2368         db2.setDatabaseName( db.databaseName() );
  2386         db2.setDatabaseName( db.databaseName() );
  2369         QVERIFY_SQL( db2, open() );
  2387         QVERIFY_SQL( db2, open() );
  2370 
  2388 
  2371         QString tableName = qTableName( "qtest_lockedtable" );
  2389         const QString tableName(qTableName( "qtest_lockedtable", __FILE__ ));
  2372         QSqlQuery q( db );
  2390         QSqlQuery q( db );
  2373 
  2391 
  2374         tst_Databases::safeDropTable( db, tableName );
  2392         tst_Databases::safeDropTable( db, tableName );
  2375         q.exec( "CREATE TABLE " + tableName + " (pk_id INTEGER PRIMARY KEY, whatever TEXT)" );
  2393         q.exec( "CREATE TABLE " + tableName + " (pk_id INTEGER PRIMARY KEY, whatever TEXT)" );
  2376         q.exec( "INSERT INTO " + tableName + " values(1, 'whatever')" );
  2394         q.exec( "INSERT INTO " + tableName + " values(1, 'whatever')" );
  2416     if ( db.driverName().startsWith( "QMYSQL" ) )
  2434     if ( db.driverName().startsWith( "QMYSQL" ) )
  2417         driverType = MYSQL;
  2435         driverType = MYSQL;
  2418     else if ( db.driverName().startsWith( "QDB2" ) )
  2436     else if ( db.driverName().startsWith( "QDB2" ) )
  2419         driverType = DB2;
  2437         driverType = DB2;
  2420 
  2438 
  2421     QString tableName = qTableName( "more_results" );
  2439     const QString tableName(qTableName( "more_results", __FILE__ ));
  2422 
  2440 
  2423     QVERIFY_SQL( q, exec( "CREATE TABLE " + tableName + " (id integer, text varchar(20), num numeric(6, 3), empty varchar(10));" ) );
  2441     QVERIFY_SQL( q, exec( "CREATE TABLE " + tableName + " (id integer, text varchar(20), num numeric(6, 3), empty varchar(10));" ) );
  2424 
  2442 
  2425     QVERIFY_SQL( q, exec( "INSERT INTO " + tableName + " VALUES(1, 'one', 1.1, '');" ) );
  2443     QVERIFY_SQL( q, exec( "INSERT INTO " + tableName + " VALUES(1, 'one', 1.1, '');" ) );
  2426 
  2444 
  2520         QCOMPARE( q.value( 0 ).toInt(), 1+i );
  2538         QCOMPARE( q.value( 0 ).toInt(), 1+i );
  2521         QCOMPARE( q.value( 1 ).toString(), QString( "Yatta!" ) );
  2539         QCOMPARE( q.value( 1 ).toString(), QString( "Yatta!" ) );
  2522     }
  2540     }
  2523 
  2541 
  2524     // Stored procedure with multiple result sets
  2542     // Stored procedure with multiple result sets
  2525     QString procName = qTableName( "proc_more_res" );
  2543     const QString procName(qTableName( "proc_more_res", __FILE__ ));
  2526 
  2544 
  2527     q.exec( QString( "DROP PROCEDURE %1;" ).arg( procName ) );
  2545     q.exec( QString( "DROP PROCEDURE %1;" ).arg( procName ) );
  2528 
  2546 
  2529     if ( driverType == MYSQL )
  2547     if ( driverType == MYSQL )
  2530         QVERIFY_SQL( q, exec( QString( "CREATE PROCEDURE %1()"
  2548         QVERIFY_SQL( q, exec( QString( "CREATE PROCEDURE %1()"
  2598     CHECK_DATABASE( db );
  2616     CHECK_DATABASE( db );
  2599 
  2617 
  2600     if ( !db.driver()->hasFeature( QSqlDriver::BLOB ) || !db.driver()->hasFeature( QSqlDriver::PreparedQueries ) )
  2618     if ( !db.driver()->hasFeature( QSqlDriver::BLOB ) || !db.driver()->hasFeature( QSqlDriver::PreparedQueries ) )
  2601         QSKIP( "DBMS does not support BLOBs or prepared queries", SkipSingle );
  2619         QSKIP( "DBMS does not support BLOBs or prepared queries", SkipSingle );
  2602 
  2620 
  2603     QString tableName = qTableName( "blobstest" );
  2621     const QString tableName(qTableName( "blobstest", __FILE__ ));
  2604 
  2622 
  2605     QSqlQuery q( db );
  2623     QSqlQuery q( db );
  2606     q.setForwardOnly( true ); // This is needed to make the test work with DB2.
  2624     q.setForwardOnly( true ); // This is needed to make the test work with DB2.
  2607     QString shortBLOB( "abc" );
  2625     QString shortBLOB( "abc" );
  2608     QString longerBLOB( "abcdefghijklmnopqrstuvxyz¿äëïöü¡  " );
  2626     QString longerBLOB( "abcdefghijklmnopqrstuvxyz¿äëïöü¡  " );
  2653     QSqlDatabase db = QSqlDatabase::database( dbName );
  2671     QSqlDatabase db = QSqlDatabase::database( dbName );
  2654     CHECK_DATABASE( db );
  2672     CHECK_DATABASE( db );
  2655 
  2673 
  2656     {
  2674     {
  2657         QSqlQuery q( db );
  2675         QSqlQuery q( db );
  2658         QVERIFY_SQL( q, exec( "create table " + qTableName( "qtest_empty" ) + " (id char(10))" ) );
  2676         QVERIFY_SQL( q, exec( "create table " + qTableName( "qtest_empty", __FILE__ ) + " (id char(10))" ) );
  2659         QVERIFY_SQL( q, prepare( "select * from " + qTableName( "qtest_empty" ) ) );
  2677         QVERIFY_SQL( q, prepare( "select * from " + qTableName( "qtest_empty", __FILE__ ) ) );
  2660         QVERIFY_SQL( q, exec() );
  2678         QVERIFY_SQL( q, exec() );
  2661         QVERIFY( !q.next() );
  2679         QVERIFY( !q.next() );
  2662         QCOMPARE( q.lastError().isValid(), false );
  2680         QCOMPARE( q.lastError().isValid(), false );
  2663     }
  2681     }
  2664 }
  2682 }
  2667 {
  2685 {
  2668     QFETCH( QString, dbName );
  2686     QFETCH( QString, dbName );
  2669     QSqlDatabase db = QSqlDatabase::database( dbName );
  2687     QSqlDatabase db = QSqlDatabase::database( dbName );
  2670     CHECK_DATABASE( db );
  2688     CHECK_DATABASE( db );
  2671     QSqlQuery q( db );
  2689     QSqlQuery q( db );
  2672 
  2690     const QString Planet(qTableName( "Planet", __FILE__));
  2673     QVERIFY_SQL( q, exec( "create table " + qTableName( "Planet" ) + " (Name varchar(20))" ) );
  2691 
  2674     QVERIFY_SQL( q, exec( "insert into " + qTableName( "Planet" ) + " VALUES ('Mercury')" ) );
  2692     QVERIFY_SQL( q, exec( "create table " + Planet + " (Name varchar(20))" ) );
  2675     QVERIFY_SQL( q, exec( "insert into " + qTableName( "Planet" ) + " VALUES ('Venus')" ) );
  2693     QVERIFY_SQL( q, exec( "insert into " + Planet + " VALUES ('Mercury')" ) );
  2676     QVERIFY_SQL( q, exec( "insert into " + qTableName( "Planet" ) + " VALUES ('Earth')" ) );
  2694     QVERIFY_SQL( q, exec( "insert into " + Planet + " VALUES ('Venus')" ) );
  2677     QVERIFY_SQL( q, exec( "insert into " + qTableName( "Planet" ) + " VALUES ('Mars')" ) );
  2695     QVERIFY_SQL( q, exec( "insert into " + Planet + " VALUES ('Earth')" ) );
  2678 
  2696     QVERIFY_SQL( q, exec( "insert into " + Planet + " VALUES ('Mars')" ) );
  2679     QVERIFY_SQL( q, exec( "SELECT Name FROM "+qTableName( "Planet" ) ) );
  2697 
       
  2698     QVERIFY_SQL( q, exec( "SELECT Name FROM " + Planet ) );
  2680     QVERIFY_SQL( q, seek( 3 ) );
  2699     QVERIFY_SQL( q, seek( 3 ) );
  2681     QCOMPARE( q.value( 0 ).toString(), QString( "Mars" ) );
  2700     QCOMPARE( q.value( 0 ).toString(), QString( "Mars" ) );
  2682     QVERIFY_SQL( q, seek( 1 ) );
  2701     QVERIFY_SQL( q, seek( 1 ) );
  2683     QCOMPARE( q.value( 0 ).toString(), QString( "Venus" ) );
  2702     QCOMPARE( q.value( 0 ).toString(), QString( "Venus" ) );
  2684     QVERIFY_SQL( q, exec( "SELECT Name FROM "+qTableName( "Planet" ) ) );
  2703     QVERIFY_SQL( q, exec( "SELECT Name FROM " + Planet ) );
  2685     QVERIFY_SQL( q, seek( 3 ) );
  2704     QVERIFY_SQL( q, seek( 3 ) );
  2686     QCOMPARE( q.value( 0 ).toString(), QString( "Mars" ) );
  2705     QCOMPARE( q.value( 0 ).toString(), QString( "Mars" ) );
  2687     QVERIFY_SQL( q, seek( 0 ) );
  2706     QVERIFY_SQL( q, seek( 0 ) );
  2688     QCOMPARE( q.value( 0 ).toString(), QString( "Mercury" ) );
  2707     QCOMPARE( q.value( 0 ).toString(), QString( "Mercury" ) );
  2689     QVERIFY_SQL( q, seek( 1 ) );
  2708     QVERIFY_SQL( q, seek( 1 ) );
  2696     QFETCH( QString, dbName );
  2715     QFETCH( QString, dbName );
  2697     QSqlDatabase db = QSqlDatabase::database( dbName );
  2716     QSqlDatabase db = QSqlDatabase::database( dbName );
  2698     CHECK_DATABASE( db );
  2717     CHECK_DATABASE( db );
  2699     QSqlQuery q( db );
  2718     QSqlQuery q( db );
  2700 
  2719 
  2701     QString tableName = qTableName( "task_250026" );
  2720     const QString tableName(qTableName( "task_250026", __FILE__ ));
  2702 
  2721 
  2703     if ( !q.exec( "create table " + tableName + " (longfield varchar(1100))" ) ) {
  2722     if ( !q.exec( "create table " + tableName + " (longfield varchar(1100))" ) ) {
  2704         qDebug() << "Error" << q.lastError();
  2723         qDebug() << "Error" << q.lastError();
  2705         QSKIP( "Db doesn't support \"1100\" as a size for fields", SkipSingle );
  2724         QSKIP( "Db doesn't support \"1100\" as a size for fields", SkipSingle );
  2706     }
  2725     }
  2744 
  2763 
  2745     if (!db.driverName().startsWith( "QODBC" )) return;
  2764     if (!db.driverName().startsWith( "QODBC" )) return;
  2746 
  2765 
  2747     QSqlQuery q( db );
  2766     QSqlQuery q( db );
  2748 
  2767 
  2749     QString tableName = qTableName( "task_229811" );
  2768     const QString tableName(qTableName( "task_229811", __FILE__ ));
  2750 
  2769 
  2751     if ( !q.exec( "CREATE TABLE " + tableName + " (Word varchar(20))" ) ) {
  2770     if ( !q.exec( "CREATE TABLE " + tableName + " (Word varchar(20))" ) ) {
  2752         qDebug() << "Warning" << q.lastError();
  2771         qDebug() << "Warning" << q.lastError();
  2753     }
  2772     }
  2754 
  2773 
  2791     QStringList m_countries;
  2810     QStringList m_countries;
  2792 
  2811 
  2793     m_airlines << "Lufthansa" << "SAS" << "United" << "KLM" << "Aeroflot";
  2812     m_airlines << "Lufthansa" << "SAS" << "United" << "KLM" << "Aeroflot";
  2794     m_countries << "DE" << "SE" << "US" << "NL" << "RU";
  2813     m_countries << "DE" << "SE" << "US" << "NL" << "RU";
  2795 
  2814 
  2796     QString tableName = qTableName( "task_234422" );
  2815     const QString tableName(qTableName( "task_234422", __FILE__ ));
  2797 
  2816 
  2798     QVERIFY_SQL(query,exec("CREATE TABLE " + tableName + " (id int primary key, "
  2817     QVERIFY_SQL(query,exec("CREATE TABLE " + tableName + " (id int primary key, "
  2799                 "name varchar(20), homecountry varchar(2))"));
  2818                 "name varchar(20), homecountry varchar(2))"));
  2800     for (int i = 0; i < m_airlines.count(); ++i) {
  2819     for (int i = 0; i < m_airlines.count(); ++i) {
  2801         QVERIFY(query.exec(QString("INSERT INTO " + tableName + " values(%1, '%2', '%3')")
  2820         QVERIFY(query.exec(QString("INSERT INTO " + tableName + " values(%1, '%2', '%3')")
  2823     QFETCH( QString, dbName );
  2842     QFETCH( QString, dbName );
  2824     QSqlDatabase db = QSqlDatabase::database( dbName );
  2843     QSqlDatabase db = QSqlDatabase::database( dbName );
  2825     CHECK_DATABASE( db );
  2844     CHECK_DATABASE( db );
  2826 
  2845 
  2827     QSqlQuery q( db );
  2846     QSqlQuery q( db );
  2828     QString tableName = qTableName("task_233829");
  2847     const QString tableName(qTableName("task_233829", __FILE__));
  2829     QVERIFY_SQL(q,exec("CREATE TABLE " + tableName  + "(dbl1 double precision,dbl2 double precision) without oids;"));
  2848     QVERIFY_SQL(q,exec("CREATE TABLE " + tableName  + "(dbl1 double precision,dbl2 double precision) without oids;"));
  2830 
  2849 
  2831     QString queryString("INSERT INTO " + tableName +"(dbl1, dbl2) VALUES(?,?)");
  2850     QString queryString("INSERT INTO " + tableName +"(dbl1, dbl2) VALUES(?,?)");
  2832 
  2851 
  2833     double k = 0.0;
  2852     double k = 0.0;
  2843     QSqlDatabase db = QSqlDatabase::database( dbName );
  2862     QSqlDatabase db = QSqlDatabase::database( dbName );
  2844     CHECK_DATABASE( db );
  2863     CHECK_DATABASE( db );
  2845     if (!tst_Databases::isSqlServer( db ))
  2864     if (!tst_Databases::isSqlServer( db ))
  2846         QSKIP("SQL Server specific test", SkipSingle);
  2865         QSKIP("SQL Server specific test", SkipSingle);
  2847 
  2866 
  2848     QString tableName(qTableName("test141895")), procName(qTableName("test141895_proc"));
  2867     const QString tableName(qTableName("test141895", __FILE__)), procName(qTableName("test141895_proc", __FILE__));
  2849     QSqlQuery q( db );
  2868     QSqlQuery q( db );
  2850     q.exec("DROP TABLE " + tableName);
  2869     q.exec("DROP TABLE " + tableName);
  2851     q.exec("DROP PROCEDURE " + procName);
  2870     q.exec("DROP PROCEDURE " + procName);
  2852     QVERIFY_SQL(q, exec("CREATE TABLE "+tableName+" (id integer)"));
  2871     QVERIFY_SQL(q, exec("CREATE TABLE "+tableName+" (id integer)"));
  2853     QVERIFY_SQL(q, exec("INSERT INTO "+tableName+" (id) VALUES (1)"));
  2872     QVERIFY_SQL(q, exec("INSERT INTO "+tableName+" (id) VALUES (1)"));
  2869 {
  2888 {
  2870     QFETCH( QString, dbName );
  2889     QFETCH( QString, dbName );
  2871     QSqlDatabase db = QSqlDatabase::database( dbName );
  2890     QSqlDatabase db = QSqlDatabase::database( dbName );
  2872     CHECK_DATABASE( db );
  2891     CHECK_DATABASE( db );
  2873     QSqlQuery q(db);
  2892     QSqlQuery q(db);
  2874     QString pkgname=qTableName("pkg");
  2893     const QString pkgname(qTableName("pkg", __FILE__));
  2875     QVERIFY_SQL(q, exec("CREATE OR REPLACE PACKAGE "+pkgname+" IS \n\
  2894     QVERIFY_SQL(q, exec("CREATE OR REPLACE PACKAGE "+pkgname+" IS \n\
  2876             \n\
  2895             \n\
  2877             TYPE IntType IS TABLE OF INTEGER      INDEX BY BINARY_INTEGER;\n\
  2896             TYPE IntType IS TABLE OF INTEGER      INDEX BY BINARY_INTEGER;\n\
  2878             TYPE VCType  IS TABLE OF VARCHAR2(60) INDEX BY BINARY_INTEGER;\n\
  2897             TYPE VCType  IS TABLE OF VARCHAR2(60) INDEX BY BINARY_INTEGER;\n\
  2879             PROCEDURE P (Inp IN IntType,  Outp OUT VCType);\n\
  2898             PROCEDURE P (Inp IN IntType,  Outp OUT VCType);\n\
  2915 void tst_QSqlQuery::QTBUG_5251()
  2934 void tst_QSqlQuery::QTBUG_5251()
  2916 {
  2935 {
  2917     QFETCH( QString, dbName );
  2936     QFETCH( QString, dbName );
  2918     QSqlDatabase db = QSqlDatabase::database( dbName );
  2937     QSqlDatabase db = QSqlDatabase::database( dbName );
  2919     CHECK_DATABASE( db );
  2938     CHECK_DATABASE( db );
       
  2939     const QString timetest(qTableName("timetest", __FILE__));
  2920 
  2940 
  2921     if (!db.driverName().startsWith( "QPSQL" )) return;
  2941     if (!db.driverName().startsWith( "QPSQL" )) return;
  2922 
  2942 
  2923     QSqlQuery q(db);
  2943     QSqlQuery q(db);
  2924     q.exec("DROP TABLE " + qTableName("timetest"));
  2944     q.exec("DROP TABLE " + timetest);
  2925     QVERIFY_SQL(q, exec("CREATE TABLE  " + qTableName("timetest") + " (t  TIME)"));
  2945     QVERIFY_SQL(q, exec("CREATE TABLE  " + timetest + " (t  TIME)"));
  2926     QVERIFY_SQL(q, exec("INSERT INTO " + qTableName("timetest") +  " VALUES ('1:2:3.666')"));
  2946     QVERIFY_SQL(q, exec("INSERT INTO " + timetest +  " VALUES ('1:2:3.666')"));
  2927 
  2947 
  2928     QSqlTableModel timetestModel(0,db);
  2948     QSqlTableModel timetestModel(0,db);
  2929     timetestModel.setEditStrategy(QSqlTableModel::OnManualSubmit);
  2949     timetestModel.setEditStrategy(QSqlTableModel::OnManualSubmit);
  2930     timetestModel.setTable(qTableName("timetest"));
  2950     timetestModel.setTable(timetest);
  2931     QVERIFY_SQL(timetestModel, select());
  2951     QVERIFY_SQL(timetestModel, select());
  2932 
  2952 
  2933     QCOMPARE(timetestModel.record(0).field(0).value().toTime().toString("HH:mm:ss.zzz"), QString("01:02:03.666"));
  2953     QCOMPARE(timetestModel.record(0).field(0).value().toTime().toString("HH:mm:ss.zzz"), QString("01:02:03.666"));
  2934     QVERIFY_SQL(timetestModel,setData(timetestModel.index(0, 0), QTime(0,12,34,500)));
  2954     QVERIFY_SQL(timetestModel,setData(timetestModel.index(0, 0), QTime(0,12,34,500)));
  2935     QCOMPARE(timetestModel.record(0).field(0).value().toTime().toString("HH:mm:ss.zzz"), QString("00:12:34.500"));
  2955     QCOMPARE(timetestModel.record(0).field(0).value().toTime().toString("HH:mm:ss.zzz"), QString("00:12:34.500"));
  2936     QVERIFY_SQL(timetestModel, submitAll());
  2956     QVERIFY_SQL(timetestModel, submitAll());
  2937     QCOMPARE(timetestModel.record(0).field(0).value().toTime().toString("HH:mm:ss.zzz"), QString("00:12:34.500"));
  2957     QCOMPARE(timetestModel.record(0).field(0).value().toTime().toString("HH:mm:ss.zzz"), QString("00:12:34.500"));
  2938 
  2958 
  2939     QVERIFY_SQL(q, exec("UPDATE " + qTableName("timetest") + " SET t = '0:11:22.33'"));
  2959     QVERIFY_SQL(q, exec("UPDATE " + timetest + " SET t = '0:11:22.33'"));
  2940     QVERIFY_SQL(timetestModel, select());
  2960     QVERIFY_SQL(timetestModel, select());
  2941     QCOMPARE(timetestModel.record(0).field(0).value().toTime().toString("HH:mm:ss.zzz"), QString("00:11:22.330"));
  2961     QCOMPARE(timetestModel.record(0).field(0).value().toTime().toString("HH:mm:ss.zzz"), QString("00:11:22.330"));
  2942 
  2962 
  2943 }
  2963 }
  2944 
  2964 
  2947     QFETCH( QString, dbName );
  2967     QFETCH( QString, dbName );
  2948     QSqlDatabase db = QSqlDatabase::database( dbName );
  2968     QSqlDatabase db = QSqlDatabase::database( dbName );
  2949     CHECK_DATABASE( db );
  2969     CHECK_DATABASE( db );
  2950 
  2970 
  2951     QSqlQuery q(db);
  2971     QSqlQuery q(db);
  2952     QString tableName=qTableName(QLatin1String("bug6421")).toUpper();
  2972     const QString tableName(qTableName("bug6421", __FILE__).toUpper());
  2953 
  2973 
  2954     QVERIFY_SQL(q, exec("create table "+tableName+"(COL1 char(10), COL2 char(10), COL3 char(10))"));
  2974     QVERIFY_SQL(q, exec("create table "+tableName+"(COL1 char(10), COL2 char(10), COL3 char(10))"));
  2955     QVERIFY_SQL(q, exec("create index INDEX1 on "+tableName+" (COL1 desc)"));
  2975     QVERIFY_SQL(q, exec("create index INDEX1 on "+tableName+" (COL1 desc)"));
  2956     QVERIFY_SQL(q, exec("create index INDEX2 on "+tableName+" (COL2 desc)"));
  2976     QVERIFY_SQL(q, exec("create index INDEX2 on "+tableName+" (COL2 desc)"));
  2957     QVERIFY_SQL(q, exec("create index INDEX3 on "+tableName+" (COL3 desc)"));
  2977     QVERIFY_SQL(q, exec("create index INDEX3 on "+tableName+" (COL3 desc)"));
  2972     CHECK_DATABASE( db );
  2992     CHECK_DATABASE( db );
  2973     if (!tst_Databases::isSqlServer( db ))
  2993     if (!tst_Databases::isSqlServer( db ))
  2974         QSKIP("SQL Server specific test", SkipSingle);
  2994         QSKIP("SQL Server specific test", SkipSingle);
  2975 
  2995 
  2976     QSqlQuery q(db);
  2996     QSqlQuery q(db);
  2977     q.exec( "drop procedure " + qTableName( "tst_raiseError" ) );  //non-fatal
  2997     q.exec( "drop procedure " + qTableName( "tst_raiseError", __FILE__ ) );  //non-fatal
  2978     QString errorString;
  2998     QString errorString;
  2979     for (int i=0;i<110;i++)
  2999     for (int i=0;i<110;i++)
  2980         errorString+="reallylong";
  3000         errorString+="reallylong";
  2981     errorString+=" error";
  3001     errorString+=" error";
  2982     QVERIFY_SQL( q, exec("create procedure " + qTableName( "tst_raiseError" ) + " as\n"
  3002     QVERIFY_SQL( q, exec("create procedure " + qTableName( "tst_raiseError", __FILE__ ) + " as\n"
  2983                          "begin\n"
  3003                          "begin\n"
  2984                          "    raiserror('" + errorString + "', 16, 1)\n"
  3004                          "    raiserror('" + errorString + "', 16, 1)\n"
  2985                          "end\n" ));
  3005                          "end\n" ));
  2986     q.exec( "{call " + qTableName( "tst_raiseError" ) + "}" );
  3006     q.exec( "{call " + qTableName( "tst_raiseError", __FILE__ ) + "}" );
  2987     QVERIFY(q.lastError().text().contains(errorString));
  3007     QVERIFY(q.lastError().text().contains(errorString));
  2988 }
  3008 }
  2989 
  3009 
  2990 void tst_QSqlQuery::QTBUG_6852()
  3010 void tst_QSqlQuery::QTBUG_6852()
  2991 {
  3011 {
  2994     CHECK_DATABASE( db );
  3014     CHECK_DATABASE( db );
  2995     if ( tst_Databases::getMySqlVersion( db ).section( QChar('.'), 0, 0 ).toInt()<5 )
  3015     if ( tst_Databases::getMySqlVersion( db ).section( QChar('.'), 0, 0 ).toInt()<5 )
  2996         QSKIP( "Test requires MySQL >= 5.0", SkipSingle );
  3016         QSKIP( "Test requires MySQL >= 5.0", SkipSingle );
  2997 
  3017 
  2998     QSqlQuery q(db);
  3018     QSqlQuery q(db);
  2999     QString tableName(qTableName(QLatin1String("bug6421"))), procName(qTableName(QLatin1String("bug6421_proc")));
  3019     const QString tableName(qTableName("bug6852", __FILE__)), procName(qTableName("bug6852_proc", __FILE__));
  3000 
  3020 
  3001     QVERIFY_SQL(q, exec("DROP PROCEDURE IF EXISTS "+procName));
  3021     QVERIFY_SQL(q, exec("DROP PROCEDURE IF EXISTS "+procName));
  3002     tst_Databases::safeDropTable(db, tableName);
       
  3003     QVERIFY_SQL(q, exec("CREATE TABLE "+tableName+"(\n"
  3022     QVERIFY_SQL(q, exec("CREATE TABLE "+tableName+"(\n"
  3004                         "MainKey INT NOT NULL,\n"
  3023                         "MainKey INT NOT NULL,\n"
  3005                         "OtherTextCol VARCHAR(45) NOT NULL,\n"
  3024                         "OtherTextCol VARCHAR(45) NOT NULL,\n"
  3006                         "PRIMARY KEY(`MainKey`))"));
  3025                         "PRIMARY KEY(`MainKey`))"));
  3007     QVERIFY_SQL(q, exec("INSERT INTO "+tableName+" VALUES(0, \"Disabled\")"));
  3026     QVERIFY_SQL(q, exec("INSERT INTO "+tableName+" VALUES(0, \"Disabled\")"));
  3020     QVERIFY_SQL(q, next());
  3039     QVERIFY_SQL(q, next());
  3021     QCOMPARE(q.value(0).toInt(), 0);
  3040     QCOMPARE(q.value(0).toInt(), 0);
  3022     QCOMPARE(q.value(1).toString(), QLatin1String("Disabled"));
  3041     QCOMPARE(q.value(1).toString(), QLatin1String("Disabled"));
  3023 }
  3042 }
  3024 
  3043 
       
  3044 void tst_QSqlQuery::QTBUG_5765()
       
  3045 {
       
  3046     QFETCH( QString, dbName );
       
  3047     QSqlDatabase db = QSqlDatabase::database( dbName );
       
  3048     CHECK_DATABASE( db );
       
  3049     if ( tst_Databases::getMySqlVersion( db ).section( QChar('.'), 0, 1 ).toFloat()<4.1 )
       
  3050         QSKIP( "Test requires MySQL >= 4.1", SkipSingle );
       
  3051 
       
  3052     QSqlQuery q(db);
       
  3053     const QString tableName(qTableName("bug5765", __FILE__));
       
  3054 
       
  3055     QVERIFY_SQL(q, exec("CREATE TABLE "+tableName+"(testval TINYINT(1) DEFAULT 0)"));
       
  3056     q.prepare("INSERT INTO "+tableName+" SET testval = :VALUE");
       
  3057     q.bindValue(":VALUE", 1);
       
  3058     QVERIFY_SQL(q, exec());
       
  3059     q.bindValue(":VALUE", 12);
       
  3060     QVERIFY_SQL(q, exec());
       
  3061     q.bindValue(":VALUE", 123);
       
  3062     QVERIFY_SQL(q, exec());
       
  3063     QString sql="select testval from "+tableName;
       
  3064     QVERIFY_SQL(q, exec(sql));
       
  3065     QVERIFY_SQL(q, next());
       
  3066     QCOMPARE(q.value(0).toInt(), 1);
       
  3067     QVERIFY_SQL(q, next());
       
  3068     QCOMPARE(q.value(0).toInt(), 12);
       
  3069     QVERIFY_SQL(q, next());
       
  3070     QCOMPARE(q.value(0).toInt(), 123);
       
  3071     QVERIFY_SQL(q, prepare(sql));
       
  3072     QVERIFY_SQL(q, exec());
       
  3073     QVERIFY_SQL(q, next());
       
  3074     QCOMPARE(q.value(0).toInt(), 1);
       
  3075     QVERIFY_SQL(q, next());
       
  3076     QCOMPARE(q.value(0).toInt(), 12);
       
  3077     QVERIFY_SQL(q, next());
       
  3078     QCOMPARE(q.value(0).toInt(), 123);
       
  3079 }
       
  3080 
       
  3081 #if 0
       
  3082 void tst_QSqlQuery::benchmark()
       
  3083 {
       
  3084     QFETCH( QString, dbName );
       
  3085     QSqlDatabase db = QSqlDatabase::database( dbName );
       
  3086     CHECK_DATABASE( db );
       
  3087     if ( tst_Databases::getMySqlVersion( db ).section( QChar('.'), 0, 0 ).toInt()<5 )
       
  3088         QSKIP( "Test requires MySQL >= 5.0", SkipSingle );
       
  3089 
       
  3090     QSqlQuery q(db);
       
  3091     const QString tableName(qTableName("benchmark", __FILE__));
       
  3092 
       
  3093     tst_Databases::safeDropTable( db, tableName );
       
  3094 
       
  3095     QVERIFY_SQL(q, exec("CREATE TABLE "+tableName+"(\n"
       
  3096                         "MainKey INT NOT NULL,\n"
       
  3097                         "OtherTextCol VARCHAR(45) NOT NULL,\n"
       
  3098                         "PRIMARY KEY(`MainKey`))"));
       
  3099 
       
  3100     int i=1;
       
  3101 
       
  3102     QBENCHMARK {
       
  3103         QVERIFY_SQL(q, exec("INSERT INTO "+tableName+" VALUES("+QString::number(i)+", \"Value"+QString::number(i)+"\")"));
       
  3104         i++;
       
  3105     }
       
  3106 
       
  3107     tst_Databases::safeDropTable( db, tableName );
       
  3108 }
       
  3109 #endif
  3025 
  3110 
  3026 QTEST_MAIN( tst_QSqlQuery )
  3111 QTEST_MAIN( tst_QSqlQuery )
  3027 #include "tst_qsqlquery.moc"
  3112 #include "tst_qsqlquery.moc"