tests/auto/qsqldatabase/tst_qsqldatabase.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 
       
    43 #include <QtTest/QtTest>
       
    44 #include <qsqldatabase.h>
       
    45 #include <qsqlquery.h>
       
    46 #include <qsqldriver.h>
       
    47 #ifdef QT3_SUPPORT
       
    48 #include <q3sqlcursor.h>
       
    49 #include <q3sqlrecordinfo.h>
       
    50 #include <q3cstring.h>
       
    51 #endif
       
    52 #include <qsqlrecord.h>
       
    53 #include <qsqlfield.h>
       
    54 #include <qsqlindex.h>
       
    55 #include <qregexp.h>
       
    56 #include <qvariant.h>
       
    57 #include <qdatetime.h>
       
    58 #include <qdebug.h>
       
    59 
       
    60 #define NODATABASE_SKIP "No database drivers are available in this Qt configuration"
       
    61 
       
    62 #include "tst_databases.h"
       
    63 
       
    64 //TESTED_FILES=
       
    65 
       
    66 QT_FORWARD_DECLARE_CLASS(QSqlDatabase)
       
    67 struct FieldDef;
       
    68 
       
    69 class tst_QSqlDatabase : public QObject
       
    70 {
       
    71     Q_OBJECT
       
    72 
       
    73 public:
       
    74     tst_QSqlDatabase();
       
    75     virtual ~tst_QSqlDatabase();
       
    76 
       
    77 public slots:
       
    78     void initTestCase();
       
    79     void cleanupTestCase();
       
    80     void init();
       
    81     void cleanup();
       
    82 private slots:
       
    83     void record_data() { generic_data(); }
       
    84     //void record();
       
    85     void open_data() { generic_data(); }
       
    86     void open();
       
    87     void tables_data() { generic_data(); }
       
    88     void tables();
       
    89     void transaction_data() { generic_data(); }
       
    90     void transaction();
       
    91     void eventNotification_data() { generic_data(); }
       
    92     void eventNotification();
       
    93     void addDatabase();
       
    94 
       
    95     //database specific tests
       
    96     void recordMySQL_data() { generic_data("QMYSQL"); }
       
    97     void recordMySQL();
       
    98     void recordPSQL_data() { generic_data("QPSQL"); }
       
    99     void recordPSQL();
       
   100     void recordOCI_data() { generic_data("QOCI"); }
       
   101     void recordOCI();
       
   102     void recordTDS_data() { generic_data("QTDS"); }
       
   103     void recordTDS();
       
   104     void recordDB2_data() { generic_data("QDB2"); }
       
   105     void recordDB2();
       
   106     void recordSQLite_data() { generic_data("QSQLITE"); }
       
   107     void recordSQLite();
       
   108     void recordAccess_data() { generic_data("QODBC"); }
       
   109     void recordAccess();
       
   110     void recordSQLServer_data() { generic_data("QODBC"); }
       
   111     void recordSQLServer();
       
   112     void recordIBase_data() {generic_data("QIBASE"); }
       
   113     void recordIBase();
       
   114 
       
   115     void eventNotificationIBase_data() { generic_data("QIBASE"); }
       
   116     void eventNotificationIBase();
       
   117     void eventNotificationPSQL_data() { generic_data("QPSQL"); }
       
   118     void eventNotificationPSQL();
       
   119 
       
   120     //database specific 64 bit integer test
       
   121     void bigIntField_data() { generic_data(); }
       
   122     void bigIntField();
       
   123 
       
   124     // general tests
       
   125     void getConnectionName_data() { generic_data(); }
       
   126     void getConnectionName(); // For task 129992
       
   127 
       
   128     //problem specific tests
       
   129     void alterTable_data() { generic_data(); }
       
   130     void alterTable();
       
   131     void recordNonSelect_data() { generic_data(); }
       
   132     void recordNonSelect();
       
   133     void caseSensivity_data() { generic_data(); }
       
   134     void caseSensivity();
       
   135     void noEscapedFieldNamesInRecord_data() { generic_data(); }
       
   136     void noEscapedFieldNamesInRecord();
       
   137     void whitespaceInIdentifiers_data() { generic_data(); }
       
   138     void whitespaceInIdentifiers();
       
   139     void formatValueTrimStrings_data() { generic_data(); }
       
   140     void formatValueTrimStrings();
       
   141     void precisionPolicy_data() { generic_data(); }
       
   142     void precisionPolicy();
       
   143 
       
   144     void db2_valueCacheUpdate_data() { generic_data("QDB2"); }
       
   145     void db2_valueCacheUpdate();
       
   146 
       
   147     void psql_schemas_data() { generic_data("QPSQL"); }
       
   148     void psql_schemas();
       
   149     void psql_escapedIdentifiers_data() { generic_data("QPSQL"); }
       
   150     void psql_escapedIdentifiers();
       
   151     void psql_escapeBytea_data() { generic_data("QPSQL"); }
       
   152     void psql_escapeBytea();
       
   153     void bug_249059_data() { generic_data("QPSQL"); }
       
   154     void bug_249059();
       
   155 
       
   156     void mysqlOdbc_unsignedIntegers_data() { generic_data(); }
       
   157     void mysqlOdbc_unsignedIntegers();
       
   158     void mysql_multiselect_data() { generic_data("QMYSQL"); }
       
   159     void mysql_multiselect();  // For task 144331
       
   160     void mysql_savepointtest_data() { generic_data("QMYSQL"); }
       
   161     void mysql_savepointtest();
       
   162 
       
   163     void accessOdbc_strings_data() { generic_data(); }
       
   164     void accessOdbc_strings();
       
   165 
       
   166     void ibase_numericFields_data() { generic_data("QIBASE"); }
       
   167     void ibase_numericFields(); // For task 125053
       
   168     void ibase_fetchBlobs_data() { generic_data("QIBASE"); }
       
   169     void ibase_fetchBlobs(); // For task 143471
       
   170     void ibase_useCustomCharset_data() { generic_data("QIBASE"); }
       
   171     void ibase_useCustomCharset(); // For task 134608
       
   172     void ibase_procWithoutReturnValues_data() { generic_data("QIBASE"); } // For task 165423
       
   173     void ibase_procWithoutReturnValues();
       
   174     void ibase_procWithReturnValues_data() { generic_data("QIBASE"); } // For task 177530
       
   175     void ibase_procWithReturnValues();
       
   176 
       
   177     void odbc_reopenDatabase_data() { generic_data("QODBC"); }
       
   178     void odbc_reopenDatabase();
       
   179     void odbc_uniqueidentifier_data() { generic_data("QODBC"); }
       
   180     void odbc_uniqueidentifier(); // For task 141822
       
   181     void odbc_uintfield_data() { generic_data("QODBC"); }
       
   182     void odbc_uintfield();
       
   183     void odbc_bindBoolean_data() { generic_data("QODBC"); }
       
   184     void odbc_bindBoolean();
       
   185     void odbc_testqGetString_data() { generic_data("QODBC"); }
       
   186     void odbc_testqGetString();
       
   187 
       
   188     void oci_serverDetach_data() { generic_data("QOCI"); }
       
   189     void oci_serverDetach(); // For task 154518
       
   190     void oci_xmltypeSupport_data() { generic_data("QOCI"); }
       
   191     void oci_xmltypeSupport();
       
   192     void oci_fieldLength_data() { generic_data("QOCI"); }
       
   193     void oci_fieldLength();
       
   194     void oci_synonymstest_data() { generic_data("QOCI"); }
       
   195     void oci_synonymstest();
       
   196 
       
   197     void sqlite_bindAndFetchUInt_data() { generic_data("QSQLITE"); }
       
   198     void sqlite_bindAndFetchUInt();
       
   199 
       
   200     void sqlStatementUseIsNull_189093_data() { generic_data(); }
       
   201     void sqlStatementUseIsNull_189093();
       
   202 
       
   203 
       
   204 private:
       
   205     void createTestTables(QSqlDatabase db);
       
   206     void dropTestTables(QSqlDatabase db);
       
   207     void populateTestTables(QSqlDatabase db);
       
   208     void generic_data(const QString &engine=QString());
       
   209 
       
   210 #ifdef QT3_SUPPORT
       
   211     void testRecordInfo(const FieldDef fieldDefs[], const Q3SqlRecordInfo& inf);
       
   212 #endif
       
   213     void testRecord(const FieldDef fieldDefs[], const QSqlRecord& inf, QSqlDatabase db);
       
   214     void commonFieldTest(const FieldDef fieldDefs[], QSqlDatabase, const int);
       
   215     void checkValues(const FieldDef fieldDefs[], QSqlDatabase db);
       
   216     void checkNullValues(const FieldDef fieldDefs[], QSqlDatabase db);
       
   217 
       
   218     tst_Databases dbs;
       
   219 };
       
   220 
       
   221 // number of records to be inserted per testfunction
       
   222 static const int ITERATION_COUNT = 2;
       
   223 static int pkey = 1;
       
   224 
       
   225 //helper class for database specific tests
       
   226 struct FieldDef {
       
   227     FieldDef(QString tn = QString(),
       
   228           QVariant::Type t = QVariant::Invalid,
       
   229           QVariant v = QVariant(),
       
   230           bool nl = true):
       
   231         typeName(tn), type(t), val(v), nullable(nl) {}
       
   232 
       
   233     QString fieldName() const
       
   234     {
       
   235         QString rt = typeName;
       
   236         rt.replace(QRegExp("\\s"), QString("_"));
       
   237 #ifdef QT3_SUPPORT
       
   238         int i = rt.find("(");
       
   239 #else
       
   240         int i = rt.indexOf("(");
       
   241 #endif
       
   242         if (i == -1)
       
   243             i = rt.length();
       
   244         if (i > 20)
       
   245             i = 20;
       
   246         return "t_" + rt.left(i);
       
   247     }
       
   248     QString typeName;
       
   249     QVariant::Type type;
       
   250     QVariant val;
       
   251     bool nullable;
       
   252 };
       
   253 
       
   254 // creates a table out of the FieldDefs and returns the number of fields
       
   255 // excluding the primary key field
       
   256 static int createFieldTable(const FieldDef fieldDefs[], QSqlDatabase db)
       
   257 {
       
   258     tst_Databases::safeDropTable(db, qTableName("qtestfields"));
       
   259     QSqlQuery q(db);
       
   260     // construct a create table statement consisting of all fieldtypes
       
   261     QString qs = "create table " + qTableName("qtestfields");
       
   262     QString autoName = tst_Databases::autoFieldName(db);
       
   263     if (tst_Databases::isMSAccess(db))
       
   264         qs.append(" (id int not null");
       
   265     else if (tst_Databases::isPostgreSQL(db))
       
   266         qs.append(" (id serial not null");
       
   267     else
       
   268         qs.append(QString("(id integer not null %1 primary key").arg(autoName));
       
   269 
       
   270     int i = 0;
       
   271     for (i = 0; !fieldDefs[ i ].typeName.isNull(); ++i) {
       
   272         qs += QString(",\n %1 %2").arg(fieldDefs[ i ].fieldName()).arg(fieldDefs[ i ].typeName);
       
   273         if ((db.driverName().startsWith("QTDS") || tst_Databases::isSqlServer(db)) && fieldDefs[ i ].nullable) {
       
   274             qs += " null";
       
   275         }
       
   276     }
       
   277 
       
   278     if (tst_Databases::isMSAccess(db))
       
   279         qs.append(",\n primary key (id)");
       
   280 
       
   281     qs += ')';
       
   282     if (!q.exec(qs)) {
       
   283         qDebug() << "Creation of Table failed:" << tst_Databases::printError(q.lastError(), db);
       
   284         qDebug() << "Query: " << qs;
       
   285         return -1;
       
   286     }
       
   287     return i;
       
   288 }
       
   289 
       
   290 tst_QSqlDatabase::tst_QSqlDatabase()
       
   291 {
       
   292 }
       
   293 
       
   294 tst_QSqlDatabase::~tst_QSqlDatabase()
       
   295 {
       
   296 }
       
   297 
       
   298 void tst_QSqlDatabase::createTestTables(QSqlDatabase db)
       
   299 {
       
   300     if (!db.isValid())
       
   301     return;
       
   302     QSqlQuery q(db);
       
   303     if (db.driverName().startsWith("QMYSQL"))
       
   304         // ### stupid workaround until we find a way to hardcode this
       
   305         // in the MySQL server startup script
       
   306         q.exec("set table_type=innodb");
       
   307     else if (tst_Databases::isSqlServer(db)) {
       
   308         QVERIFY_SQL(q, exec("SET ANSI_DEFAULTS ON"));
       
   309         QVERIFY_SQL(q, exec("SET IMPLICIT_TRANSACTIONS OFF"));
       
   310     } else if(tst_Databases::isPostgreSQL(db))
       
   311         QVERIFY_SQL( q, exec("set client_min_messages='warning'"));
       
   312 
       
   313     // please never ever change this table; otherwise fix all tests ;)
       
   314     if (tst_Databases::isMSAccess(db)) {
       
   315         QVERIFY_SQL(q, exec("create table " + qTableName("qtest") +
       
   316                    " (id int not null, t_varchar varchar(40) not null, t_char char(40), "
       
   317                    "t_numeric number, primary key (id, t_varchar))"));
       
   318     } else {
       
   319         QVERIFY_SQL(q, exec("create table " + qTableName("qtest") +
       
   320                " (id integer not null, t_varchar varchar(40) not null, "
       
   321                "t_char char(40), t_numeric numeric(6, 3), primary key (id, t_varchar))"));
       
   322     }
       
   323 
       
   324     if (testWhiteSpaceNames(db.driverName())) {
       
   325         QString qry = "create table "
       
   326             + db.driver()->escapeIdentifier(qTableName("qtest") + " test", QSqlDriver::TableName)
       
   327             + '('
       
   328             + db.driver()->escapeIdentifier(QLatin1String("test test"), QSqlDriver::FieldName)
       
   329             + " int not null primary key)";
       
   330         QVERIFY_SQL(q, exec(qry));
       
   331     }
       
   332 }
       
   333 
       
   334 void tst_QSqlDatabase::dropTestTables(QSqlDatabase db)
       
   335 {
       
   336     if (!db.isValid())
       
   337         return;
       
   338 
       
   339     if(tst_Databases::isPostgreSQL(db)) {
       
   340         QSqlQuery q(db);
       
   341         QVERIFY_SQL( q, exec("set client_min_messages='warning'"));
       
   342     }
       
   343 
       
   344     // drop the view first, otherwise we'll get dependency problems
       
   345     tst_Databases::safeDropViews(db, QStringList() << qTableName("qtest_view") << qTableName("qtest_view2"));
       
   346 
       
   347     QStringList tableNames;
       
   348     tableNames << qTableName("qtest")
       
   349             << qTableName("qtestfields")
       
   350             << qTableName("qtestalter")
       
   351             << qTableName("qtest_temp")
       
   352             << qTableName("qtest_bigint")
       
   353             << qTableName("qtest_xmltype")
       
   354             << qTableName("latin1table")
       
   355             << qTableName("qtest_sqlguid")
       
   356             << qTableName("batable")
       
   357             << qTableName("qtest_prec")
       
   358             << qTableName("uint")
       
   359             << qTableName("strings")
       
   360             << qTableName("numericfields")
       
   361             << qTableName("qtest_ibaseblobs")
       
   362             << qTableName("qtestBindBool")
       
   363             << qTableName("testqGetString")
       
   364             << qTableName("qtest_sqlguid")
       
   365             << qTableName("uint_table")
       
   366             << qTableName("uint_test")
       
   367             << qTableName("bug_249059");
       
   368 
       
   369     QSqlQuery q(0, db);
       
   370     if (db.driverName().startsWith("QPSQL")) {
       
   371         q.exec("drop schema " + qTableName("qtestschema") + " cascade");
       
   372         q.exec("drop schema " + qTableName("qtestScHeMa") + " cascade");
       
   373     }
       
   374 
       
   375     if (testWhiteSpaceNames(db.driverName()))
       
   376         tableNames <<  db.driver()->escapeIdentifier(qTableName("qtest") + " test", QSqlDriver::TableName);
       
   377 
       
   378     tst_Databases::safeDropTables(db, tableNames);
       
   379 
       
   380     if (db.driverName().startsWith("QOCI")) {
       
   381         q.exec("drop user "+qTableName("CREATOR")+" cascade");
       
   382         q.exec("drop user "+qTableName("APPUSER")+" cascade");
       
   383 
       
   384     }
       
   385 }
       
   386 
       
   387 void tst_QSqlDatabase::populateTestTables(QSqlDatabase db)
       
   388 {
       
   389     if (!db.isValid())
       
   390         return;
       
   391     QSqlQuery q(db);
       
   392 
       
   393     q.exec("delete from " + qTableName("qtest")); //non-fatal
       
   394     QVERIFY_SQL(q, exec("insert into " + qTableName("qtest") + " (id, t_varchar, t_char, t_numeric) values (0, 'VarChar0', 'Char0', 1.1)"));
       
   395     QVERIFY_SQL(q, exec("insert into " + qTableName("qtest") + " (id, t_varchar, t_char, t_numeric) values (1, 'VarChar1', 'Char1', 2.2)"));
       
   396     QVERIFY_SQL(q, exec("insert into " + qTableName("qtest") + " (id, t_varchar, t_char, t_numeric) values (2, 'VarChar2', 'Char2', 3.3)"));
       
   397     QVERIFY_SQL(q, exec("insert into " + qTableName("qtest") + " (id, t_varchar, t_char, t_numeric) values (3, 'VarChar3', 'Char3', 4.4)"));
       
   398     QVERIFY_SQL(q, exec("insert into " + qTableName("qtest") + " (id, t_varchar, t_char, t_numeric) values (4, 'VarChar4', NULL, NULL)"));
       
   399 }
       
   400 
       
   401 void tst_QSqlDatabase::initTestCase()
       
   402 {
       
   403     dbs.open();
       
   404 
       
   405     for (QStringList::ConstIterator it = dbs.dbNames.begin(); it != dbs.dbNames.end(); ++it) {
       
   406         QSqlDatabase db = QSqlDatabase::database((*it));
       
   407         CHECK_DATABASE(db);
       
   408         dropTestTables(db); //in case of leftovers
       
   409         createTestTables(db);
       
   410         populateTestTables(db);
       
   411     }
       
   412 }
       
   413 
       
   414 void tst_QSqlDatabase::cleanupTestCase()
       
   415 {
       
   416     for (QStringList::ConstIterator it = dbs.dbNames.begin(); it != dbs.dbNames.end(); ++it) {
       
   417         QSqlDatabase db = QSqlDatabase::database((*it));
       
   418         CHECK_DATABASE(db);
       
   419         dropTestTables(db);
       
   420     }
       
   421 
       
   422     dbs.close();
       
   423 }
       
   424 
       
   425 void tst_QSqlDatabase::init()
       
   426 {
       
   427 }
       
   428 
       
   429 void tst_QSqlDatabase::cleanup()
       
   430 {
       
   431 }
       
   432 
       
   433 void tst_QSqlDatabase::generic_data(const QString& engine)
       
   434 {
       
   435     if ( dbs.fillTestTable(engine) == 0 ) {
       
   436         if(engine.isEmpty())
       
   437            QSKIP( "No database drivers are available in this Qt configuration", SkipAll );
       
   438         else
       
   439            QSKIP( (QString("No database drivers of type %1 are available in this Qt configuration").arg(engine)).toLocal8Bit(), SkipAll );
       
   440     }
       
   441 }
       
   442 
       
   443 void tst_QSqlDatabase::addDatabase()
       
   444 {
       
   445     QTest::ignoreMessage(QtWarningMsg, "QSqlDatabase: BLAH_FOO_NONEXISTENT_DRIVER driver not loaded");
       
   446     QTest::ignoreMessage(QtWarningMsg, qPrintable("QSqlDatabase: available drivers: " + QSqlDatabase::drivers().join(QLatin1String(" "))));
       
   447     {
       
   448         QSqlDatabase db = QSqlDatabase::addDatabase("BLAH_FOO_NONEXISTENT_DRIVER",
       
   449                                                     "INVALID_CONNECTION");
       
   450         QVERIFY(!db.isValid());
       
   451     }
       
   452     QVERIFY(QSqlDatabase::contains("INVALID_CONNECTION"));
       
   453     QSqlDatabase::removeDatabase("INVALID_CONNECTION");
       
   454     QVERIFY(!QSqlDatabase::contains("INVALID_CONNECTION"));
       
   455 }
       
   456 
       
   457 void tst_QSqlDatabase::open()
       
   458 {
       
   459     QFETCH(QString, dbName);
       
   460     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   461     CHECK_DATABASE(db);
       
   462 
       
   463     int i;
       
   464     for (i = 0; i < 10; ++i) {
       
   465         db.close();
       
   466         QVERIFY(!db.isOpen());
       
   467         QVERIFY_SQL(db, open());
       
   468         QVERIFY(db.isOpen());
       
   469         QVERIFY(!db.isOpenError());
       
   470     }
       
   471 
       
   472     if (db.driverName().startsWith("QSQLITE") && db.databaseName() == ":memory:") {
       
   473         // tables in in-memory databases don't survive an open/close
       
   474         createTestTables(db);
       
   475         populateTestTables(db);
       
   476     }
       
   477 }
       
   478 
       
   479 void tst_QSqlDatabase::recordNonSelect()
       
   480 {
       
   481 #ifdef QT3_SUPPORT
       
   482     QFETCH(QString, dbName);
       
   483     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   484     CHECK_DATABASE(db);
       
   485 
       
   486     QSqlQuery q(db);
       
   487 
       
   488     // nothing should happen on an empty query
       
   489     QSqlRecord rec = db.record(q);
       
   490     QVERIFY(rec.isEmpty());
       
   491     Q3SqlRecordInfo rInf = db.recordInfo(q);
       
   492     QVERIFY(rInf.isEmpty());
       
   493 
       
   494     QVERIFY_SQL(q, exec("create table " + qTableName("qtest_temp") + " (id int)"));
       
   495 
       
   496     // query without result set should return empty record
       
   497     rec = db.record(q);
       
   498     QVERIFY(rec.isEmpty());
       
   499     rInf = db.recordInfo(q);
       
   500     QVERIFY(rInf.isEmpty());
       
   501 #endif
       
   502 }
       
   503 
       
   504 void tst_QSqlDatabase::tables()
       
   505 {
       
   506     QFETCH(QString, dbName);
       
   507     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   508     CHECK_DATABASE(db);
       
   509 
       
   510     bool views = true;
       
   511     bool tempTables = false;
       
   512 
       
   513     QSqlQuery q(db);
       
   514     if ( db.driverName().startsWith( "QMYSQL" ) && tst_Databases::getMySqlVersion( db ).section( QChar('.'), 0, 0 ).toInt()<5 )
       
   515         QSKIP( "Test requires MySQL >= 5.0", SkipSingle );
       
   516 
       
   517 
       
   518     if (!q.exec("CREATE VIEW " + qTableName("qtest_view") + " as select * from " + qTableName("qtest"))) {
       
   519         qDebug(QString("DBMS '%1' cannot handle VIEWs: %2").arg(
       
   520                 tst_Databases::dbToString(db)).arg(QString(tst_Databases::printError(q.lastError()))).toLatin1());
       
   521         views = false;
       
   522     }
       
   523 
       
   524     if (db.driverName().startsWith("QSQLITE3")) {
       
   525         QVERIFY_SQL(q, exec("CREATE TEMPORARY TABLE " + qTableName("temp_tab") + " (id int)"));
       
   526         tempTables = true;
       
   527     }
       
   528 
       
   529     QStringList tables = db.tables(QSql::Tables);
       
   530     QVERIFY(tables.contains(qTableName("qtest"), Qt::CaseInsensitive));
       
   531     QVERIFY(!tables.contains("sql_features", Qt::CaseInsensitive)); //check for postgres 7.4 internal tables
       
   532     if (views) {
       
   533         QVERIFY(!tables.contains(qTableName("qtest_view"), Qt::CaseInsensitive));
       
   534     }
       
   535     if (tempTables)
       
   536         QVERIFY(tables.contains(qTableName("temp_tab"), Qt::CaseInsensitive));
       
   537 
       
   538     tables = db.tables(QSql::Views);
       
   539     if (views) {
       
   540         if(!tables.contains(qTableName("qtest_view"), Qt::CaseInsensitive))
       
   541             qDebug() << "failed to find" << qTableName("qtest_view") << "in" << tables;
       
   542         QVERIFY(tables.contains(qTableName("qtest_view"), Qt::CaseInsensitive));
       
   543     }
       
   544     if (tempTables)
       
   545         QVERIFY(!tables.contains(qTableName("temp_tab"), Qt::CaseInsensitive));
       
   546     QVERIFY(!tables.contains(qTableName("qtest"), Qt::CaseInsensitive));
       
   547 
       
   548     tables = db.tables(QSql::SystemTables);
       
   549     QVERIFY(!tables.contains(qTableName("qtest"), Qt::CaseInsensitive));
       
   550     QVERIFY(!tables.contains(qTableName("qtest_view"), Qt::CaseInsensitive));
       
   551     QVERIFY(!tables.contains(qTableName("temp_tab"), Qt::CaseInsensitive));
       
   552 
       
   553     tables = db.tables(QSql::AllTables);
       
   554     if (views)
       
   555         QVERIFY(tables.contains(qTableName("qtest_view"), Qt::CaseInsensitive));
       
   556     if (tempTables)
       
   557         QVERIFY(tables.contains(qTableName("temp_tab"), Qt::CaseInsensitive));
       
   558     QVERIFY(tables.contains(qTableName("qtest"), Qt::CaseInsensitive));
       
   559 
       
   560     if (db.driverName().startsWith("QPSQL")) {
       
   561         QVERIFY(tables.contains(qTableName("qtest") + " test"));
       
   562     }
       
   563 }
       
   564 
       
   565 void tst_QSqlDatabase::whitespaceInIdentifiers()
       
   566 {
       
   567     QFETCH(QString, dbName);
       
   568     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   569     CHECK_DATABASE(db);
       
   570 
       
   571     if (testWhiteSpaceNames(db.driverName())) {
       
   572         QString tableName = qTableName("qtest") + " test";
       
   573         QVERIFY(db.tables().contains(tableName, Qt::CaseInsensitive));
       
   574 
       
   575         QSqlRecord rec = db.record(db.driver()->escapeIdentifier(tableName, QSqlDriver::TableName));
       
   576         QCOMPARE(rec.count(), 1);
       
   577         QCOMPARE(rec.fieldName(0), QString("test test"));
       
   578         if(db.driverName().startsWith("QOCI"))
       
   579             QCOMPARE(rec.field(0).type(), QVariant::Double);
       
   580         else
       
   581             QCOMPARE(rec.field(0).type(), QVariant::Int);
       
   582 
       
   583         QSqlIndex idx = db.primaryIndex(db.driver()->escapeIdentifier(tableName, QSqlDriver::TableName));
       
   584         QCOMPARE(idx.count(), 1);
       
   585         QCOMPARE(idx.fieldName(0), QString("test test"));
       
   586         if(db.driverName().startsWith("QOCI"))
       
   587             QCOMPARE(idx.field(0).type(), QVariant::Double);
       
   588         else
       
   589             QCOMPARE(idx.field(0).type(), QVariant::Int);
       
   590     } else {
       
   591         QSKIP("DBMS does not support whitespaces in identifiers", SkipSingle);
       
   592     }
       
   593 }
       
   594 
       
   595 void tst_QSqlDatabase::alterTable()
       
   596 {
       
   597     QFETCH(QString, dbName);
       
   598     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   599     CHECK_DATABASE(db);
       
   600 
       
   601     QSqlQuery q(db);
       
   602 
       
   603     QVERIFY_SQL(q, exec("create table " + qTableName("qtestalter") + " (F1 char(20), F2 char(20), F3 char(20))"));
       
   604     QSqlRecord rec = db.record(qTableName("qtestalter"));
       
   605     QCOMPARE((int)rec.count(), 3);
       
   606 #ifdef QT3_SUPPORT
       
   607     Q3SqlRecordInfo rinf = db.recordInfo(qTableName("qtestalter"));
       
   608     QCOMPARE((int)rinf.count(), 3);
       
   609 #endif
       
   610 
       
   611 
       
   612     int i;
       
   613     for (i = 0; i < 3; ++i) {
       
   614         QCOMPARE(rec.field(i).name().toUpper(), QString("F%1").arg(i + 1));
       
   615 #ifdef QT3_SUPPORT
       
   616         QCOMPARE(rinf[ i ].name().upper(), QString("F%1").arg(i + 1));
       
   617 #endif
       
   618     }
       
   619 
       
   620     if (!q.exec("alter table " + qTableName("qtestalter") + " drop column F2")) {
       
   621         QSKIP("DBMS doesn't support dropping columns in ALTER TABLE statement", SkipSingle);
       
   622     }
       
   623 
       
   624     rec = db.record(qTableName("qtestalter"));
       
   625 #ifdef QT3_SUPPORT
       
   626     rinf = db.recordInfo(qTableName("qtestalter"));
       
   627 #endif
       
   628 
       
   629     QCOMPARE((int)rec.count(), 2);
       
   630 #ifdef QT3_SUPPORT
       
   631     QCOMPARE((int)rinf.count(), 2);
       
   632 #endif
       
   633 
       
   634     QCOMPARE(rec.field(0).name().toUpper(), QString("F1"));
       
   635     QCOMPARE(rec.field(1).name().toUpper(), QString("F3"));
       
   636 #ifdef QT3_SUPPORT
       
   637     QCOMPARE(rinf[ 0 ].name().upper(), QString("F1"));
       
   638     QCOMPARE(rinf[ 1 ].name().upper(), QString("F3"));
       
   639 #endif
       
   640 
       
   641     q.exec("select * from " + qTableName("qtestalter"));
       
   642 
       
   643 #ifdef QT3_SUPPORT
       
   644     rec = db.record(q);
       
   645     rinf = db.recordInfo(q);
       
   646 
       
   647     QCOMPARE((int)rec.count(), 2);
       
   648     QCOMPARE((int)rinf.count(), 2);
       
   649 
       
   650     QCOMPARE(rec.field(0).name().upper(), QString("F1"));
       
   651     QCOMPARE(rec.field(1).name().upper(), QString("F3"));
       
   652     QCOMPARE(rinf[ 0 ].name().upper(), QString("F1"));
       
   653     QCOMPARE(rinf[ 1 ].name().upper(), QString("F3"));
       
   654 #endif
       
   655 }
       
   656 
       
   657 #if 0
       
   658 // this is the general test that should work on all databases.
       
   659 // unfortunately no DBMS supports SQL 92/ 99 so the general
       
   660 // test is more or less a joke. Please write a test for each
       
   661 // database plugin (see recordOCI and so on). Use this test
       
   662 // as a template.
       
   663 void tst_QSqlDatabase::record()
       
   664 {
       
   665     QFETCH(QString, dbName);
       
   666     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   667     CHECK_DATABASE(db);
       
   668 
       
   669     static const FieldDef fieldDefs[] = {
       
   670         FieldDef("char(20)", QVariant::String,         QString("blah1"), false),
       
   671         FieldDef("varchar(20)", QVariant::String,      QString("blah2"), false),
       
   672         FieldDef()
       
   673     };
       
   674 
       
   675     const int fieldCount = createFieldTable(fieldDefs, db);
       
   676     QVERIFY(fieldCount > 0);
       
   677 
       
   678 // doesn't work with oracle:   checkNullValues(fieldDefs, db);
       
   679     commonFieldTest(fieldDefs, db, fieldCount);
       
   680     for (int i = 0; i < ITERATION_COUNT; ++i) {
       
   681         checkValues(fieldDefs, db);
       
   682     }
       
   683 }
       
   684 #endif
       
   685 
       
   686 #ifdef QT3_SUPPORT
       
   687 void tst_QSqlDatabase::testRecordInfo(const FieldDef fieldDefs[], const Q3SqlRecordInfo& inf)
       
   688 {
       
   689     int i = 0;
       
   690     for (i = 0; !fieldDefs[ i ].typeName.isNull(); ++i) {
       
   691         QCOMPARE(inf[i+1].name().upper(), fieldDefs[ i ].fieldName().upper());
       
   692         if (inf[i+1].type() != fieldDefs[ i ].type) {
       
   693             QFAIL(QString(" Expected: '%1' Received: '%2' for field %3 in testRecordInfo").arg(
       
   694             QVariant::typeToName(fieldDefs[ i ].type)).arg(
       
   695               QVariant::typeToName(inf[i+1].type())).arg(
       
   696             fieldDefs[ i ].fieldName()));
       
   697         }
       
   698     }
       
   699 }
       
   700 #endif
       
   701 
       
   702 void tst_QSqlDatabase::testRecord(const FieldDef fieldDefs[], const QSqlRecord& inf, QSqlDatabase db)
       
   703 {
       
   704     int i = 0;
       
   705     if (!tst_Databases::autoFieldName(db).isEmpty()) // Currently only MySQL is tested
       
   706         QVERIFY2(inf.field(i).isAutoValue(), qPrintable(inf.field(i).name() + " should be reporting as an autovalue"));
       
   707     for (i = 0; !fieldDefs[ i ].typeName.isNull(); ++i) {
       
   708         QCOMPARE(inf.field(i+1).name().toUpper(), fieldDefs[ i ].fieldName().toUpper());
       
   709         if (inf.field(i+1).type() != fieldDefs[ i ].type) {
       
   710             QFAIL(qPrintable(QString(" Expected: '%1' Received: '%2' for field %3 in testRecord").arg(
       
   711               QVariant::typeToName(fieldDefs[ i ].type)).arg(
       
   712             QVariant::typeToName(inf.field(i+1).type())).arg(
       
   713               fieldDefs[ i ].fieldName())));
       
   714         }
       
   715         QVERIFY(!inf.field(i+1).isAutoValue());
       
   716 
       
   717 //	qDebug(QString(" field: %1 type: %2 variant type: %3").arg(fieldDefs[ i ].fieldName()).arg(QVariant::typeToName(inf.field(i+1)->type())).arg(QVariant::typeToName(inf.field(i+1)->value().type())));
       
   718     }
       
   719 }
       
   720 
       
   721 // non-dbms specific tests
       
   722 void tst_QSqlDatabase::commonFieldTest(const FieldDef fieldDefs[], QSqlDatabase db, const int fieldCount)
       
   723 {
       
   724     CHECK_DATABASE(db);
       
   725 
       
   726     // check whether recordInfo returns the right types
       
   727 #ifdef QT3_SUPPORT
       
   728     Q3SqlRecordInfo inf = db.recordInfo(qTableName("qtestfields"));
       
   729     QCOMPARE((int)inf.count(), fieldCount+1);
       
   730     testRecordInfo(fieldDefs, inf);
       
   731 #endif
       
   732 
       
   733     QSqlRecord rec = db.record(qTableName("qtestfields"));
       
   734     QCOMPARE((int)rec.count(), fieldCount+1);
       
   735     testRecord(fieldDefs, rec, db);
       
   736 
       
   737     QSqlQuery q(db);
       
   738     QVERIFY_SQL(q, exec("select * from " + qTableName("qtestfields")));
       
   739 
       
   740 #ifdef QT3_SUPPORT
       
   741     inf = db.recordInfo(q);
       
   742     QCOMPARE((int)inf.count(), fieldCount+1);
       
   743     testRecordInfo(fieldDefs, inf);
       
   744 
       
   745     rec = db.record(q);
       
   746     QCOMPARE((int)rec.count(), fieldCount+1);
       
   747     testRecord(fieldDefs, rec, db);
       
   748 #endif
       
   749 }
       
   750 
       
   751 // inserts testdata into the testtable, fetches and compares them
       
   752 void tst_QSqlDatabase::checkValues(const FieldDef fieldDefs[], QSqlDatabase db)
       
   753 {
       
   754     Q_UNUSED(fieldDefs);
       
   755 #ifdef QT3_SUPPORT
       
   756     CHECK_DATABASE(db);
       
   757 
       
   758     Q3SqlCursor cur(qTableName("qtestfields"), true, db);
       
   759     QVERIFY_SQL(cur, select());
       
   760     QSqlRecord* rec = cur.primeInsert();
       
   761     Q_ASSERT(rec);
       
   762     rec->setValue("id", pkey++);
       
   763     int i = 0;
       
   764     for (i = 0; !fieldDefs[ i ].typeName.isNull(); ++i) {
       
   765         rec->setValue(fieldDefs[ i ].fieldName(), fieldDefs[ i ].val);
       
   766 //     qDebug(QString("inserting %1 into %2").arg(fieldDefs[ i ].val.toString()).arg(fieldDefs[ i ].fieldName()));
       
   767     }
       
   768     if (!cur.insert()) {
       
   769         QFAIL(QString("Couldn't insert record: %1 %2").arg(cur.lastError().databaseText()).arg(cur.lastError().driverText()));
       
   770     }
       
   771     cur.setForwardOnly(true);
       
   772     QVERIFY_SQL(cur, select("id = " + QString::number(pkey - 1)));
       
   773     QVERIFY_SQL(cur, next());
       
   774 
       
   775     for (i = 0; !fieldDefs[ i ].typeName.isNull(); ++i) {
       
   776         bool ok = false;
       
   777         QVariant val1 = cur.value(fieldDefs[ i ].fieldName());
       
   778         QVariant val2 = fieldDefs[ i ].val;
       
   779         if (val1.type() == QVariant::String)
       
   780             //TDS Workaround
       
   781             val1 = val1.toString().stripWhiteSpace();
       
   782         if (fieldDefs[ i ].fieldName() == "t_real") {
       
   783             // strip precision
       
   784             val1 = (float)val1.toDouble();
       
   785             val2 = (float)val2.toDouble();
       
   786         }
       
   787         if (val1.canCast(QVariant::Double) && val2.type() == QVariant::Double) {
       
   788             // we don't care about precision here, we just want to know whether
       
   789             // we can insert/fetch the right values
       
   790             ok = (val1.toDouble() - val2.toDouble() < 0.00001);
       
   791         } else if (val1.type() == val2.type()) {
       
   792                 ok = (val1 == val2);
       
   793         } else {
       
   794             ok = (val1.toString() == val2.toString());
       
   795         }
       
   796         if (!ok) {
       
   797             if (val2.type() == QVariant::DateTime || val2.type() == QVariant::Time)
       
   798                qDebug("Expected Time: " + val2.toTime().toString("hh:mm:ss.zzz"));
       
   799             if (val1.type() == QVariant::DateTime || val1.type() == QVariant::Time)
       
   800                qDebug("Received Time: " + val1.toTime().toString("hh:mm:ss.zzz"));
       
   801             QFAIL(QString(" Expected: '%1' Received: '%2' for field %3 (etype %4 rtype %5) in checkValues").arg(
       
   802             val2.type() == QVariant::ByteArray ? val2.toByteArray().toHex() : val2.toString()).arg(
       
   803             val1.type() == QVariant::ByteArray ? val1.toByteArray().toHex() : val1.toString()).arg(
       
   804             fieldDefs[ i ].fieldName()).arg(
       
   805             val2.typeName()).arg(
       
   806             val1.typeName())
       
   807             );
       
   808         }
       
   809     }
       
   810 #endif
       
   811 }
       
   812 
       
   813 // inserts a NULL value for each nullable field in testdata, fetches and checks whether
       
   814 // we get back NULL
       
   815 void tst_QSqlDatabase::checkNullValues(const FieldDef fieldDefs[], QSqlDatabase db)
       
   816 {
       
   817     Q_UNUSED(fieldDefs);
       
   818 #ifdef QT3_SUPPORT
       
   819     CHECK_DATABASE(db);
       
   820 
       
   821     Q3SqlCursor cur(qTableName("qtestfields"), true, db);
       
   822     QVERIFY_SQL(cur, select());
       
   823     QSqlRecord* rec = cur.primeInsert();
       
   824     Q_ASSERT(rec);
       
   825     rec->setValue("id", pkey++);
       
   826     int i = 0;
       
   827     for (i = 0; !fieldDefs[ i ].typeName.isNull(); ++i) {
       
   828         if (fieldDefs[ i ].fieldName(), fieldDefs[ i ].nullable)
       
   829             rec->setNull(fieldDefs[ i ].fieldName());
       
   830         else
       
   831             rec->setValue(fieldDefs[ i ].fieldName(), fieldDefs[ i ].val);
       
   832     }
       
   833     if (!cur.insert()) {
       
   834         QFAIL(QString("Couldn't insert record: %1 %2").arg(cur.lastError().databaseText()).arg(cur.lastError().driverText()));
       
   835     }
       
   836     cur.setForwardOnly(true);
       
   837     QVERIFY_SQL(cur, select("id = " + QString::number(pkey - 1)));
       
   838     QVERIFY_SQL(cur, next());
       
   839 
       
   840     for (i = 0; !fieldDefs[ i ].typeName.isNull(); ++i) {
       
   841         if (fieldDefs[ i ].nullable == false)
       
   842             continue;
       
   843         // multiple inheritance sucks so much
       
   844         QVERIFY2(((QSqlQuery)cur).isNull(i + 1), "Check whether '" + fieldDefs[ i ].fieldName() + "' is null in QSqlQuery");
       
   845         QVERIFY2(((QSqlRecord)cur).isNull(fieldDefs[ i ].fieldName()), "Check whether '" + fieldDefs[ i ].fieldName() + "' is null in QSqlRecord");
       
   846         if (!cur.value(fieldDefs[ i ].fieldName()).isNull())
       
   847             qDebug(QString("QVariant is not null for NULL-Value in Field '%1'").arg(fieldDefs[ i ].fieldName()));
       
   848     }
       
   849 #endif
       
   850 }
       
   851 
       
   852 void tst_QSqlDatabase::recordTDS()
       
   853 {
       
   854     QFETCH(QString, dbName);
       
   855     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   856     CHECK_DATABASE(db);
       
   857 
       
   858     static const FieldDef fieldDefs[] = {
       
   859     FieldDef("tinyint", QVariant::Int,		255),
       
   860     FieldDef("smallint", QVariant::Int,		32767),
       
   861     FieldDef("int", QVariant::Int,			2147483647),
       
   862     FieldDef("numeric(10,9)", QVariant::Double,	1.23456789),
       
   863     FieldDef("decimal(10,9)", QVariant::Double,	1.23456789),
       
   864     FieldDef("float(4)", QVariant::Double,		1.23456789),
       
   865     FieldDef("double precision", QVariant::Double,	1.23456789),
       
   866     FieldDef("real", QVariant::Double,		1.23456789),
       
   867     FieldDef("smallmoney", QVariant::Double,	100.42),
       
   868     FieldDef("money", QVariant::Double,		200.42),
       
   869     // accuracy is that of a minute
       
   870     FieldDef("smalldatetime", QVariant::DateTime,	QDateTime(QDate::currentDate(), QTime(1, 2, 0, 0))),
       
   871     // accuracy is that of a second
       
   872     FieldDef("datetime", QVariant::DateTime,	QDateTime(QDate::currentDate(), QTime(1, 2, 3, 0))),
       
   873     FieldDef("char(20)", QVariant::String,		"blah1"),
       
   874     FieldDef("varchar(20)", QVariant::String,	"blah2"),
       
   875     FieldDef("nchar(20)", QVariant::String,	"blah3"),
       
   876     FieldDef("nvarchar(20)", QVariant::String,	"blah4"),
       
   877     FieldDef("text", QVariant::String,		"blah5"),
       
   878 #ifdef QT3_SUPPORT
       
   879     FieldDef("binary(20)", QVariant::ByteArray,	Q3CString("blah6")),
       
   880     FieldDef("varbinary(20)", QVariant::ByteArray, Q3CString("blah7")),
       
   881     FieldDef("image", QVariant::ByteArray,		Q3CString("blah8")),
       
   882 #endif
       
   883     FieldDef("bit", QVariant::Int,			1, false),
       
   884 
       
   885     FieldDef()
       
   886     };
       
   887 
       
   888     const int fieldCount = createFieldTable(fieldDefs, db);
       
   889     QVERIFY(fieldCount > 0);
       
   890 
       
   891     commonFieldTest(fieldDefs, db, fieldCount);
       
   892     checkNullValues(fieldDefs, db);
       
   893     for (int i = 0; i < ITERATION_COUNT; ++i) {
       
   894     checkValues(fieldDefs, db);
       
   895     }
       
   896 }
       
   897 
       
   898 void tst_QSqlDatabase::recordOCI()
       
   899 {
       
   900     bool hasTimeStamp = false;
       
   901 
       
   902     QFETCH(QString, dbName);
       
   903     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   904     CHECK_DATABASE(db);
       
   905 
       
   906     // runtime check for Oracle version since V8 doesn't support TIMESTAMPs
       
   907     if (tst_Databases::getOraVersion(db) >= 9) {
       
   908     qDebug("Detected Oracle >= 9, TIMESTAMP test enabled");
       
   909     hasTimeStamp = true;
       
   910     } else {
       
   911     qDebug("Detected Oracle < 9, TIMESTAMP test disabled");
       
   912     }
       
   913 
       
   914     FieldDef tsdef;
       
   915     FieldDef tstzdef;
       
   916     FieldDef tsltzdef;
       
   917     FieldDef intytm;
       
   918     FieldDef intdts;
       
   919 
       
   920     static const QDateTime dt(QDate::currentDate(), QTime(1, 2, 3, 0));
       
   921 
       
   922     if (hasTimeStamp) {
       
   923     tsdef = FieldDef("timestamp", QVariant::DateTime,  dt);
       
   924     tstzdef = FieldDef("timestamp with time zone", QVariant::DateTime, dt);
       
   925     tsltzdef = FieldDef("timestamp with local time zone", QVariant::DateTime, dt);
       
   926     intytm = FieldDef("interval year to month", QVariant::String, QString("+01-01"));
       
   927     intdts = FieldDef("interval day to second", QVariant::String, QString("+01 00:00:01.000000"));
       
   928     }
       
   929 
       
   930     const FieldDef fieldDefs[] = {
       
   931         FieldDef("char(20)", QVariant::String,          QString("blah1")),
       
   932         FieldDef("varchar(20)", QVariant::String,       QString("blah2")),
       
   933         FieldDef("nchar(20)", QVariant::String,         QString("blah3")),
       
   934         FieldDef("nvarchar2(20)", QVariant::String,     QString("blah4")),
       
   935         FieldDef("number(10,5)", QVariant::Double,      1.1234567),
       
   936         FieldDef("date", QVariant::DateTime,            dt),
       
   937 #ifdef QT3_SUPPORT
       
   938 //X?    FieldDef("long raw", QVariant::ByteArray,       QByteArray(Q3CString("blah5"))),
       
   939         FieldDef("raw(2000)", QVariant::ByteArray,      QByteArray(Q3CString("blah6")), false),
       
   940         FieldDef("blob", QVariant::ByteArray,           QByteArray(Q3CString("blah7"))),
       
   941 #endif
       
   942 //FIXME FieldDef("clob", QVariant::CString,             Q3CString("blah8")),
       
   943 //FIXME FieldDef("nclob", QVariant::CString,            Q3CString("blah9")),
       
   944 //X     FieldDef("bfile", QVariant::ByteArray,          QByteArray(Q3CString("blah10"))),
       
   945 
       
   946     intytm,
       
   947     intdts,
       
   948     tsdef,
       
   949     tstzdef,
       
   950     tsltzdef,
       
   951     FieldDef()
       
   952     };
       
   953 
       
   954     const int fieldCount = createFieldTable(fieldDefs, db);
       
   955     QVERIFY(fieldCount > 0);
       
   956 
       
   957     commonFieldTest(fieldDefs, db, fieldCount);
       
   958     checkNullValues(fieldDefs, db);
       
   959     for (int i = 0; i < ITERATION_COUNT; ++i) {
       
   960     checkValues(fieldDefs, db);
       
   961     }
       
   962 
       
   963     // some additional tests
       
   964     QSqlRecord rec = db.record(qTableName("qtestfields"));
       
   965     QCOMPARE(rec.field("T_NUMBER").length(), 10);
       
   966     QCOMPARE(rec.field("T_NUMBER").precision(), 5);
       
   967 
       
   968     QSqlQuery q(db);
       
   969     QVERIFY_SQL(q, exec("SELECT * FROM " + qTableName("qtestfields")));
       
   970     rec = q.record();
       
   971     QCOMPARE(rec.field("T_NUMBER").length(), 10);
       
   972     QCOMPARE(rec.field("T_NUMBER").precision(), 5);
       
   973 }
       
   974 
       
   975 void tst_QSqlDatabase::recordPSQL()
       
   976 {
       
   977     QFETCH(QString, dbName);
       
   978     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   979     CHECK_DATABASE(db);
       
   980 
       
   981     FieldDef byteadef;
       
   982     if (db.driver()->hasFeature(QSqlDriver::BLOB))
       
   983 #ifdef QT3_SUPPORT
       
   984     byteadef = FieldDef("bytea", QVariant::ByteArray, QByteArray(Q3CString("bl\\ah")));
       
   985 #else
       
   986         byteadef = FieldDef("bytea", QVariant::ByteArray, QByteArray("bl\\ah"));
       
   987 #endif
       
   988     static FieldDef fieldDefs[] = {
       
   989     FieldDef("bigint", QVariant::LongLong,	Q_INT64_C(9223372036854775807)),
       
   990     FieldDef("bigserial", QVariant::LongLong, 100, false),
       
   991     FieldDef("bit", QVariant::String,	"1"), // a bit in postgres is a bit-string
       
   992 #ifdef QT3_SUPPORT
       
   993     FieldDef("boolean", QVariant::Bool,	QVariant(bool(true), 0)),
       
   994 #endif
       
   995     FieldDef("box", QVariant::String,	"(5,6),(1,2)"),
       
   996     FieldDef("char(20)", QVariant::String, "blah5678901234567890"),
       
   997     FieldDef("varchar(20)", QVariant::String, "blah5678901234567890"),
       
   998     FieldDef("cidr", QVariant::String,	"12.123.0.0/24"),
       
   999     FieldDef("circle", QVariant::String,	"<(1,2),3>"),
       
  1000     FieldDef("date", QVariant::Date,	QDate::currentDate()),
       
  1001     FieldDef("float8", QVariant::Double,	1.12345678912),
       
  1002     FieldDef("inet", QVariant::String,	"12.123.12.23"),
       
  1003     FieldDef("integer", QVariant::Int,	2147483647),
       
  1004     FieldDef("interval", QVariant::String, "1 day 12:59:10"),
       
  1005 //	LOL... you can create a "line" datatype in PostgreSQL <= 7.2.x but
       
  1006 //	as soon as you want to insert data you get a "not implemented yet" error
       
  1007 //	FieldDef("line", QVariant::Polygon, QPolygon(QRect(1, 2, 3, 4))),
       
  1008     FieldDef("lseg", QVariant::String,     "[(1,1),(2,2)]"),
       
  1009     FieldDef("macaddr", QVariant::String, "08:00:2b:01:02:03"),
       
  1010     FieldDef("money", QVariant::String,	"$12.23"),
       
  1011     FieldDef("numeric", QVariant::Double,  1.2345678912),
       
  1012     FieldDef("path", QVariant::String,	"((1,2),(3,2),(3,5),(1,5))"),
       
  1013     FieldDef("point", QVariant::String,	"(1,2)"),
       
  1014     FieldDef("polygon", QVariant::String,	"((1,2),(3,2),(3,5),(1,5))"),
       
  1015     FieldDef("real", QVariant::Double,	1.1234),
       
  1016     FieldDef("smallint", QVariant::Int,	32767),
       
  1017     FieldDef("serial", QVariant::Int,	100, false),
       
  1018     FieldDef("text", QVariant::String,	"blah"),
       
  1019     FieldDef("time(6)", QVariant::Time,	QTime(1, 2, 3)),
       
  1020     FieldDef("timetz", QVariant::Time,	QTime(1, 2, 3)),
       
  1021     FieldDef("timestamp(6)", QVariant::DateTime, QDateTime::currentDateTime()),
       
  1022     FieldDef("timestamptz", QVariant::DateTime, QDateTime::currentDateTime()),
       
  1023     byteadef,
       
  1024 
       
  1025     FieldDef()
       
  1026     };
       
  1027 
       
  1028     QSqlQuery q(db);
       
  1029 
       
  1030     if(tst_Databases::isPostgreSQL(db))
       
  1031         QVERIFY_SQL( q, exec("set client_min_messages='warning'"));
       
  1032 
       
  1033     q.exec("drop sequence " + qTableName("qtestfields") + "_t_bigserial_seq");
       
  1034     q.exec("drop sequence " + qTableName("qtestfields") + "_t_serial_seq");
       
  1035     // older psql cut off the table name
       
  1036     q.exec("drop sequence " + qTableName("qtestfields").left(15) + "_t_bigserial_seq");
       
  1037     q.exec("drop sequence " + qTableName("qtestfields").left(18) + "_t_serial_seq");
       
  1038 
       
  1039     const int fieldCount = createFieldTable(fieldDefs, db);
       
  1040     QVERIFY(fieldCount > 0);
       
  1041 
       
  1042     commonFieldTest(fieldDefs, db, fieldCount);
       
  1043     checkNullValues(fieldDefs, db);
       
  1044     for (int i = 0; i < ITERATION_COUNT; ++i) {
       
  1045     // increase serial values
       
  1046     for (int i2 = 0; !fieldDefs[ i2 ].typeName.isNull(); ++i2) {
       
  1047         if (fieldDefs[ i2 ].typeName == "serial" ||
       
  1048          fieldDefs[ i2 ].typeName == "bigserial") {
       
  1049 
       
  1050         FieldDef def = fieldDefs[ i2 ];
       
  1051 #ifdef QT3_SUPPORT
       
  1052         def.val = def.val.asInt() + 1;
       
  1053 #else
       
  1054         def.val = def.val.toInt() + 1;
       
  1055 #endif
       
  1056         fieldDefs[ i2 ] = def;
       
  1057         }
       
  1058     }
       
  1059     checkValues(fieldDefs, db);
       
  1060     }
       
  1061 }
       
  1062 
       
  1063 void tst_QSqlDatabase::recordMySQL()
       
  1064 {
       
  1065     QFETCH(QString, dbName);
       
  1066     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  1067     CHECK_DATABASE(db);
       
  1068 
       
  1069     FieldDef bin10, varbin10;
       
  1070     int major = tst_Databases::getMySqlVersion( db ).section( QChar('.'), 0, 0 ).toInt();
       
  1071     int minor = tst_Databases::getMySqlVersion( db ).section( QChar('.'), 1, 1 ).toInt();
       
  1072     int revision = tst_Databases::getMySqlVersion( db ).section( QChar('.'), 2, 2 ).toInt();
       
  1073     int vernum = (major << 16) + (minor << 8) + revision;
       
  1074 
       
  1075     /* The below is broken in mysql below 5.0.15
       
  1076         see http://dev.mysql.com/doc/refman/5.0/en/binary-varbinary.html
       
  1077         specifically: Before MySQL 5.0.15, the pad value is space. Values are right-padded
       
  1078         with space on insert, and trailing spaces are removed on select.
       
  1079     */
       
  1080     if( vernum >= ((5 << 16) + 15) ) {
       
  1081 #ifdef QT3_SUPPORT
       
  1082         bin10 = FieldDef("binary(10)", QVariant::ByteArray, QByteArray(Q3CString("123abc    ")));
       
  1083         varbin10 = FieldDef("varbinary(10)", QVariant::ByteArray, QByteArray(Q3CString("123abcv   ")));
       
  1084 #else
       
  1085         bin10 = FieldDef("binary(10)", QVariant::ByteArray, QString("123abc    "));
       
  1086         varbin10 = FieldDef("varbinary(10)", QVariant::ByteArray, QString("123abcv   "));
       
  1087 #endif
       
  1088     }
       
  1089 
       
  1090     static QDateTime dt(QDate::currentDate(), QTime(1, 2, 3, 0));
       
  1091     static const FieldDef fieldDefs[] = {
       
  1092     FieldDef("tinyint", QVariant::Int,	    127),
       
  1093     FieldDef("tinyint unsigned", QVariant::UInt, 255),
       
  1094     FieldDef("smallint", QVariant::Int,	    32767),
       
  1095     FieldDef("smallint unsigned", QVariant::UInt, 65535),
       
  1096     FieldDef("mediumint", QVariant::Int,	    8388607),
       
  1097     FieldDef("mediumint unsigned", QVariant::UInt, 16777215),
       
  1098     FieldDef("integer", QVariant::Int,	    2147483647),
       
  1099     FieldDef("integer unsigned", QVariant::UInt, 4294967295u),
       
  1100     FieldDef("bigint", QVariant::LongLong,	    Q_INT64_C(9223372036854775807)),
       
  1101     FieldDef("bigint unsigned", QVariant::ULongLong, Q_UINT64_C(18446744073709551615)),
       
  1102     FieldDef("float", QVariant::Double,	    1.12345),
       
  1103     FieldDef("double", QVariant::Double,	    1.123456789),
       
  1104     FieldDef("decimal(10, 9)", QVariant::Double,1.123456789),
       
  1105     FieldDef("numeric(5, 2)", QVariant::Double, 123.67),
       
  1106     FieldDef("date", QVariant::Date,	    QDate::currentDate()),
       
  1107     FieldDef("datetime", QVariant::DateTime,   dt),
       
  1108     FieldDef("timestamp", QVariant::DateTime,  dt, false),
       
  1109     FieldDef("time", QVariant::Time,	    dt.time()),
       
  1110     FieldDef("year", QVariant::Int,	    2003),
       
  1111     FieldDef("char(20)", QVariant::String,	    "Blah"),
       
  1112     FieldDef("varchar(20)", QVariant::String,  "BlahBlah"),
       
  1113 #ifdef QT3_SUPPORT
       
  1114     FieldDef("tinyblob", QVariant::ByteArray,  QByteArray(Q3CString("blah1"))),
       
  1115     FieldDef("blob", QVariant::ByteArray,	    QByteArray(Q3CString("blah2"))),
       
  1116     FieldDef("mediumblob", QVariant::ByteArray,QByteArray(Q3CString("blah3"))),
       
  1117     FieldDef("longblob", QVariant::ByteArray,  QByteArray(Q3CString("blah4"))),
       
  1118 #endif
       
  1119     FieldDef("tinytext", QVariant::String,    QString("blah5")),
       
  1120     FieldDef("text", QVariant::String,	    QString("blah6")),
       
  1121     FieldDef("mediumtext", QVariant::String,  QString("blah7")),
       
  1122     FieldDef("longtext", QVariant::String,    QString("blah8")),
       
  1123 #ifdef QT3_SUPPORT
       
  1124     bin10,
       
  1125     varbin10,
       
  1126 #endif
       
  1127     // SET OF?
       
  1128 
       
  1129     FieldDef()
       
  1130     };
       
  1131 
       
  1132     const int fieldCount = createFieldTable(fieldDefs, db);
       
  1133     QVERIFY(fieldCount > 0);
       
  1134 
       
  1135     commonFieldTest(fieldDefs, db, fieldCount);
       
  1136     checkNullValues(fieldDefs, db);
       
  1137     for (int i = 0; i < ITERATION_COUNT; ++i) {
       
  1138     checkValues(fieldDefs, db);
       
  1139     }
       
  1140 
       
  1141     QSqlQuery q(db);
       
  1142     QVERIFY_SQL(q, exec("SELECT DATE_SUB(CURDATE(), INTERVAL 2 DAY)"));
       
  1143     QVERIFY(q.next());
       
  1144     QCOMPARE(q.value(0).toDateTime().date(), QDate::currentDate().addDays(-2));
       
  1145 }
       
  1146 
       
  1147 void tst_QSqlDatabase::recordDB2()
       
  1148 {
       
  1149     QFETCH(QString, dbName);
       
  1150     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  1151     CHECK_DATABASE(db);
       
  1152 
       
  1153     static const FieldDef fieldDefs[] = {
       
  1154     FieldDef("char(20)", QVariant::String,		QString("Blah1")),
       
  1155     FieldDef("varchar(20)", QVariant::String,	QString("Blah2")),
       
  1156     FieldDef("long varchar", QVariant::String,	QString("Blah3")),
       
  1157     // using BOOLEAN results in "SQL0486N  The BOOLEAN data type is currently only supported internally."
       
  1158 //X	FieldDef("boolean" , QVariant::Bool,		QVariant(true, 1)),
       
  1159     FieldDef("smallint", QVariant::Int,		32767),
       
  1160     FieldDef("integer", QVariant::Int,		2147483647),
       
  1161     FieldDef("bigint", QVariant::LongLong,		Q_INT64_C(9223372036854775807)),
       
  1162     FieldDef("real", QVariant::Double,		1.12345),
       
  1163     FieldDef("double", QVariant::Double,		1.23456789),
       
  1164     FieldDef("float", QVariant::Double,		1.23456789),
       
  1165     FieldDef("decimal(10,9)", QVariant::Double,    1.234567891),
       
  1166     FieldDef("numeric(10,9)", QVariant::Double,    1.234567891),
       
  1167     FieldDef("date", QVariant::Date,		QDate::currentDate()),
       
  1168     FieldDef("time", QVariant::Time,		QTime(1, 2, 3)),
       
  1169     FieldDef("timestamp", QVariant::DateTime,	QDateTime::currentDateTime()),
       
  1170 //     FieldDef("graphic(20)", QVariant::String,	QString("Blah4")),
       
  1171 //     FieldDef("vargraphic(20)", QVariant::String,	QString("Blah5")),
       
  1172 //     FieldDef("long vargraphic", QVariant::String,	QString("Blah6")),
       
  1173 #ifdef QT3_SUPPORT
       
  1174 //     FieldDef("clob(20)", QVariant::CString,	QString("Blah7")),
       
  1175 //     FieldDef("dbclob(20)", QVariant::CString,	QString("Blah8")),
       
  1176 //     FieldDef("blob(20)", QVariant::ByteArray,	QByteArray(Q3CString("Blah9"))),
       
  1177 #endif
       
  1178     //X	FieldDef("datalink", QVariant::String,		QString("DLVALUE('Blah10')")),
       
  1179     FieldDef()
       
  1180     };
       
  1181 
       
  1182     const int fieldCount = createFieldTable(fieldDefs, db);
       
  1183     QVERIFY(fieldCount > 0);
       
  1184 
       
  1185     commonFieldTest(fieldDefs, db, fieldCount);
       
  1186     checkNullValues(fieldDefs, db);
       
  1187     for (int i = 0; i < ITERATION_COUNT; ++i) {
       
  1188     checkValues(fieldDefs, db);
       
  1189     }
       
  1190 }
       
  1191 
       
  1192 void tst_QSqlDatabase::recordIBase()
       
  1193 {
       
  1194     QFETCH(QString, dbName);
       
  1195     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  1196     CHECK_DATABASE(db);
       
  1197 
       
  1198     static const FieldDef fieldDefs[] = {
       
  1199         FieldDef("char(20)", QVariant::String, QString("Blah1"), false),
       
  1200         FieldDef("varchar(20)", QVariant::String, QString("Blah2")),
       
  1201         FieldDef("smallint", QVariant::Int, 32767),
       
  1202         FieldDef("float", QVariant::Double, 1.2345),
       
  1203         FieldDef("double precision", QVariant::Double, 1.2345678),
       
  1204         FieldDef("timestamp", QVariant::DateTime, QDateTime::currentDateTime()),
       
  1205         FieldDef("time", QVariant::Time, QTime::currentTime()),
       
  1206         FieldDef("decimal(18)", QVariant::LongLong, Q_INT64_C(9223372036854775807)),
       
  1207         FieldDef("numeric(5,2)", QVariant::Double, 123.45),
       
  1208 
       
  1209         FieldDef()
       
  1210     };
       
  1211 
       
  1212     const int fieldCount = createFieldTable(fieldDefs, db);
       
  1213     QVERIFY(fieldCount > 0);
       
  1214 
       
  1215     commonFieldTest(fieldDefs, db, fieldCount);
       
  1216     checkNullValues(fieldDefs, db);
       
  1217     for (int i = 0; i < ITERATION_COUNT; ++i) {
       
  1218         checkValues(fieldDefs, db);
       
  1219     }
       
  1220 }
       
  1221 
       
  1222 void tst_QSqlDatabase::recordSQLite()
       
  1223 {
       
  1224     QFETCH(QString, dbName);
       
  1225     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  1226     CHECK_DATABASE(db);
       
  1227 
       
  1228     static const FieldDef fieldDefs[] = {
       
  1229         // The affinity of these fields are TEXT so SQLite should give us strings, not ints or doubles.
       
  1230         FieldDef("char(20)", QVariant::String,          QString("123")),
       
  1231         FieldDef("varchar(20)", QVariant::String,       QString("123.4")),
       
  1232         FieldDef("clob", QVariant::String,              QString("123.45")),
       
  1233         FieldDef("text", QVariant::String,              QString("123.456")),
       
  1234 
       
  1235         FieldDef("integer", QVariant::Int,              QVariant(13)),
       
  1236         FieldDef("int", QVariant::Int,                  QVariant(12)),
       
  1237 
       
  1238         FieldDef()
       
  1239     };
       
  1240 
       
  1241     const int fieldCount = createFieldTable(fieldDefs, db);
       
  1242     QVERIFY(fieldCount > 0);
       
  1243 
       
  1244     commonFieldTest(fieldDefs, db, fieldCount);
       
  1245     checkNullValues(fieldDefs, db);
       
  1246     for (int i = 0; i < ITERATION_COUNT; ++i) {
       
  1247         checkValues(fieldDefs, db);
       
  1248     }
       
  1249 }
       
  1250 
       
  1251 void tst_QSqlDatabase::recordSQLServer()
       
  1252 {
       
  1253     QFETCH(QString, dbName);
       
  1254     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  1255     CHECK_DATABASE(db);
       
  1256 
       
  1257     if (!tst_Databases::isSqlServer(db)) {
       
  1258     QSKIP("SQL server specific test", SkipSingle);
       
  1259     return;
       
  1260     }
       
  1261 
       
  1262     // ### TODO: Add the rest of the fields
       
  1263     static const FieldDef fieldDefs[] = {
       
  1264         FieldDef("varchar(20)", QVariant::String, QString("Blah1")),
       
  1265         FieldDef("bigint", QVariant::LongLong, 12345),
       
  1266         FieldDef("int", QVariant::Int, 123456),
       
  1267         FieldDef("tinyint", QVariant::UInt, 255),
       
  1268 #ifdef QT3_SUPPORT
       
  1269         FieldDef("image", QVariant::ByteArray, Q3CString("Blah1")),
       
  1270 #endif
       
  1271         FieldDef("float", QVariant::Double, 1.12345),
       
  1272         FieldDef("numeric(5,2)", QVariant::Double, 123.45),
       
  1273         FieldDef("uniqueidentifier", QVariant::String,
       
  1274             QString("AA7DF450-F119-11CD-8465-00AA00425D90")),
       
  1275 
       
  1276         FieldDef()
       
  1277     };
       
  1278 
       
  1279     const int fieldCount = createFieldTable(fieldDefs, db);
       
  1280     QVERIFY(fieldCount > 0);
       
  1281 
       
  1282     commonFieldTest(fieldDefs, db, fieldCount);
       
  1283     checkNullValues(fieldDefs, db);
       
  1284     for (int i = 0; i < ITERATION_COUNT; ++i) {
       
  1285         checkValues(fieldDefs, db);
       
  1286     }
       
  1287 }
       
  1288 
       
  1289 void tst_QSqlDatabase::recordAccess()
       
  1290 {
       
  1291     QFETCH(QString, dbName);
       
  1292     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  1293     CHECK_DATABASE(db);
       
  1294 
       
  1295     if (!tst_Databases::isMSAccess(db)) {
       
  1296     QSKIP("MS Access specific test", SkipSingle);
       
  1297     return;
       
  1298     }
       
  1299 
       
  1300     QString memo;
       
  1301     for (int i = 0; i < 32; i++)
       
  1302         memo.append("ABCDEFGH12345678abcdefgh12345678");
       
  1303 
       
  1304     // ### TODO: Add the rest of the fields
       
  1305     static const FieldDef fieldDefs[] = {
       
  1306     FieldDef("varchar(20)", QVariant::String, QString("Blah1")),
       
  1307     FieldDef("single", QVariant::Double, 1.12345),
       
  1308     FieldDef("double", QVariant::Double, 1.123456),
       
  1309     FieldDef("byte", QVariant::UInt, 255),
       
  1310 #ifdef QT3_SUPPORT
       
  1311     FieldDef("binary(5)", QVariant::ByteArray, Q3CString("Blah2")),
       
  1312 #endif
       
  1313     FieldDef("long", QVariant::Int, 2147483647),
       
  1314         FieldDef("memo", QVariant::String, memo),
       
  1315     FieldDef()
       
  1316     };
       
  1317 
       
  1318     const int fieldCount = createFieldTable(fieldDefs, db);
       
  1319     QVERIFY(fieldCount > 0);
       
  1320 
       
  1321     commonFieldTest(fieldDefs, db, fieldCount);
       
  1322     checkNullValues(fieldDefs, db);
       
  1323     for (int i = 0; i < ITERATION_COUNT; ++i) {
       
  1324         checkValues(fieldDefs, db);
       
  1325     }
       
  1326 }
       
  1327 
       
  1328 void tst_QSqlDatabase::transaction()
       
  1329 {
       
  1330     QFETCH(QString, dbName);
       
  1331     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  1332     CHECK_DATABASE(db);
       
  1333 
       
  1334     if (!db.driver()->hasFeature(QSqlDriver::Transactions)) {
       
  1335     QSKIP("DBMS not transaction capable", SkipSingle);
       
  1336     }
       
  1337 
       
  1338     QVERIFY(db.transaction());
       
  1339 
       
  1340     QSqlQuery q(db);
       
  1341     QVERIFY_SQL(q, exec("insert into " + qTableName("qtest") + " values (40, 'VarChar40', 'Char40', 40.40)"));
       
  1342     QVERIFY_SQL(q, exec("select * from " + qTableName("qtest") + " where id = 40"));
       
  1343     QVERIFY(q.next());
       
  1344     QCOMPARE(q.value(0).toInt(), 40);
       
  1345     q.clear();
       
  1346 
       
  1347     QVERIFY(db.commit());
       
  1348 
       
  1349     QVERIFY(db.transaction());
       
  1350     QVERIFY_SQL(q, exec("select * from " + qTableName("qtest") + " where id = 40"));
       
  1351     QVERIFY(q.next());
       
  1352     QCOMPARE(q.value(0).toInt(), 40);
       
  1353     q.clear();
       
  1354     QVERIFY(db.commit());
       
  1355 
       
  1356     QVERIFY(db.transaction());
       
  1357     QVERIFY_SQL(q, exec("insert into " + qTableName("qtest") + " values (41, 'VarChar41', 'Char41', 41.41)"));
       
  1358     QVERIFY_SQL(q, exec("select * from " + qTableName("qtest") + " where id = 41"));
       
  1359     QVERIFY(q.next());
       
  1360     QCOMPARE(q.value(0).toInt(), 41);
       
  1361     q.clear(); // for SQLite which does not allow any references on rows that shall be rolled back
       
  1362     if (!db.rollback()) {
       
  1363     if (db.driverName().startsWith("QMYSQL")) {
       
  1364         qDebug("MySQL: " + tst_Databases::printError(db.lastError()));
       
  1365         QSKIP("MySQL transaction failed ", SkipSingle); //non-fatal
       
  1366     } else {
       
  1367         QFAIL("Could not rollback transaction: " + tst_Databases::printError(db.lastError()));
       
  1368         }
       
  1369     }
       
  1370 
       
  1371     QVERIFY_SQL(q, exec("select * from " + qTableName("qtest") + " where id = 41"));
       
  1372     if(db.driverName().startsWith("QODBC") && dbName.contains("MySQL"))
       
  1373         QEXPECT_FAIL("", "Some odbc drivers don't actually roll back despite telling us they do, especially the mysql driver", Continue);
       
  1374     QVERIFY(!q.next());
       
  1375 
       
  1376     populateTestTables(db);
       
  1377 }
       
  1378 
       
  1379 void tst_QSqlDatabase::bigIntField()
       
  1380 {
       
  1381     QFETCH(QString, dbName);
       
  1382     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  1383     CHECK_DATABASE(db);
       
  1384     QString drvName = db.driverName();
       
  1385 
       
  1386     QSqlQuery q(db);
       
  1387     q.setForwardOnly(true);
       
  1388     if (drvName.startsWith("QOCI"))
       
  1389         q.setNumericalPrecisionPolicy(QSql::LowPrecisionInt64);
       
  1390 
       
  1391     if (drvName.startsWith("QMYSQL")) {
       
  1392         QVERIFY_SQL(q, exec("create table " + qTableName("qtest_bigint") + " (id int, t_s64bit bigint, t_u64bit bigint unsigned)"));
       
  1393     } else if (drvName.startsWith("QPSQL")
       
  1394                 || drvName.startsWith("QDB2")
       
  1395                 || tst_Databases::isSqlServer(db)) {
       
  1396         QVERIFY_SQL(q, exec("create table " + qTableName("qtest_bigint") + "(id int, t_s64bit bigint, t_u64bit bigint)"));
       
  1397     } else if (drvName.startsWith("QOCI")) {
       
  1398         QVERIFY_SQL(q, exec("create table " + qTableName("qtest_bigint") + " (id int, t_s64bit int, t_u64bit int)"));
       
  1399     //} else if (drvName.startsWith("QIBASE")) {
       
  1400     //    QVERIFY_SQL(q, exec("create table " + qTableName("qtest_bigint") + " (id int, t_s64bit int64, t_u64bit int64)"));
       
  1401     } else {
       
  1402         QSKIP("no 64 bit integer support", SkipAll);
       
  1403     }
       
  1404     QVERIFY(q.prepare("insert into " + qTableName("qtest_bigint") + " values (?, ?, ?)"));
       
  1405     qlonglong ll = Q_INT64_C(9223372036854775807);
       
  1406     qulonglong ull = Q_UINT64_C(18446744073709551615);
       
  1407 
       
  1408     if (drvName.startsWith("QMYSQL") || drvName.startsWith("QOCI")) {
       
  1409         q.bindValue(0, 0);
       
  1410         q.bindValue(1, ll);
       
  1411         q.bindValue(2, ull);
       
  1412         QVERIFY_SQL(q, exec());
       
  1413         q.bindValue(0, 1);
       
  1414         q.bindValue(1, -ll);
       
  1415         q.bindValue(2, ull);
       
  1416         QVERIFY_SQL(q, exec());
       
  1417     } else {
       
  1418         // usinged bigint fields not supported - a cast is necessary
       
  1419         q.bindValue(0, 0);
       
  1420         q.bindValue(1, ll);
       
  1421         q.bindValue(2, (qlonglong) ull);
       
  1422         QVERIFY_SQL(q, exec());
       
  1423         q.bindValue(0, 1);
       
  1424         q.bindValue(1, -ll);
       
  1425         q.bindValue(2,  (qlonglong) ull);
       
  1426         QVERIFY_SQL(q, exec());
       
  1427     }
       
  1428     QVERIFY(q.exec("select * from " + qTableName("qtest_bigint") + " order by id"));
       
  1429     QVERIFY(q.next());
       
  1430     QCOMPARE(q.value(1).toDouble(), (double)ll);
       
  1431     QCOMPARE(q.value(1).toLongLong(), ll);
       
  1432     if(drvName.startsWith("QOCI"))
       
  1433         QEXPECT_FAIL("", "Oracle driver lacks support for unsigned int64 types", Continue);
       
  1434     QCOMPARE(q.value(2).toULongLong(), ull);
       
  1435     QVERIFY(q.next());
       
  1436     QCOMPARE(q.value(1).toLongLong(), -ll);
       
  1437     if(drvName.startsWith("QOCI"))
       
  1438         QEXPECT_FAIL("", "Oracle driver lacks support for unsigned int64 types", Continue);
       
  1439     QCOMPARE(q.value(2).toULongLong(), ull);
       
  1440 }
       
  1441 
       
  1442 void tst_QSqlDatabase::caseSensivity()
       
  1443 {
       
  1444     QFETCH(QString, dbName);
       
  1445     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  1446     CHECK_DATABASE(db);
       
  1447 
       
  1448     bool cs = false;
       
  1449     if (db.driverName().startsWith("QMYSQL")
       
  1450      || db.driverName().startsWith("QSQLITE")
       
  1451      || db.driverName().startsWith("QTDS")
       
  1452      || db.driverName().startsWith("QODBC"))
       
  1453     cs = true;
       
  1454 
       
  1455     QSqlRecord rec = db.record(qTableName("qtest"));
       
  1456     QVERIFY((int)rec.count() > 0);
       
  1457     if (!cs) {
       
  1458     rec = db.record(qTableName("QTEST").toUpper());
       
  1459     QVERIFY((int)rec.count() > 0);
       
  1460     rec = db.record(qTableName("qTesT"));
       
  1461     QVERIFY((int)rec.count() > 0);
       
  1462     }
       
  1463 
       
  1464 #ifdef QT3_SUPPORT
       
  1465     Q3SqlRecordInfo rInf = db.recordInfo(qTableName("qtest"));
       
  1466     QVERIFY((int)rInf.count() > 0);
       
  1467     if (!cs) {
       
  1468     rInf = db.recordInfo(qTableName("QTEST").upper());
       
  1469     QVERIFY((int)rInf.count() > 0);
       
  1470     rInf = db.recordInfo(qTableName("qTesT"));
       
  1471     QVERIFY((int)rInf.count() > 0);
       
  1472     }
       
  1473 #endif
       
  1474 
       
  1475     rec = db.primaryIndex(qTableName("qtest"));
       
  1476     QVERIFY((int)rec.count() > 0);
       
  1477     if (!cs) {
       
  1478     rec = db.primaryIndex(qTableName("QTEST").toUpper());
       
  1479     QVERIFY((int)rec.count() > 0);
       
  1480     rec = db.primaryIndex(qTableName("qTesT"));
       
  1481     QVERIFY((int)rec.count() > 0);
       
  1482     }
       
  1483 }
       
  1484 
       
  1485 void tst_QSqlDatabase::noEscapedFieldNamesInRecord()
       
  1486 {
       
  1487     QFETCH(QString, dbName);
       
  1488     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  1489     CHECK_DATABASE(db);
       
  1490 
       
  1491     QString fieldname("t_varchar");
       
  1492     if (db.driverName().startsWith("QOCI") || db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QDB2"))
       
  1493         fieldname = fieldname.toUpper();
       
  1494 
       
  1495     QSqlQuery q(db);
       
  1496     QString query = "SELECT " + db.driver()->escapeIdentifier(fieldname, QSqlDriver::FieldName) + " FROM " + qTableName("qtest");
       
  1497     QVERIFY_SQL(q, exec(query));
       
  1498     QCOMPARE(q.record().fieldName(0), fieldname);
       
  1499 }
       
  1500 
       
  1501 void tst_QSqlDatabase::psql_schemas()
       
  1502 {
       
  1503     QFETCH(QString, dbName);
       
  1504     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  1505     CHECK_DATABASE(db);
       
  1506 
       
  1507     if (!db.tables(QSql::SystemTables).contains("pg_namespace"))
       
  1508         QSKIP("server does not support schemas", SkipSingle);
       
  1509 
       
  1510     QSqlQuery q(db);
       
  1511 
       
  1512     if(tst_Databases::isPostgreSQL(db)) {
       
  1513         QVERIFY_SQL( q, exec("set client_min_messages='warning'"));
       
  1514     }
       
  1515 
       
  1516     QVERIFY_SQL(q, exec("CREATE SCHEMA " + qTableName("qtestschema")));
       
  1517 
       
  1518     QString table = qTableName("qtestschema") + '.' + qTableName("qtesttable");
       
  1519     QVERIFY_SQL(q, exec("CREATE TABLE " + table + " (id int primary key, name varchar(20))"));
       
  1520 
       
  1521     QVERIFY(db.tables().contains(table));
       
  1522 
       
  1523     QSqlRecord rec = db.record(table);
       
  1524     QCOMPARE(rec.count(), 2);
       
  1525     QCOMPARE(rec.fieldName(0), QString("id"));
       
  1526     QCOMPARE(rec.fieldName(1), QString("name"));
       
  1527 
       
  1528 #ifdef QT3_SUPPORT
       
  1529     rec = db.record(QSqlQuery("select * from " + table, db));
       
  1530     QCOMPARE(rec.count(), 2);
       
  1531     QCOMPARE(rec.fieldName(0), QString("id"));
       
  1532     QCOMPARE(rec.fieldName(1), QString("name"));
       
  1533 #endif
       
  1534     QSqlIndex idx = db.primaryIndex(table);
       
  1535     QCOMPARE(idx.count(), 1);
       
  1536     QCOMPARE(idx.fieldName(0), QString("id"));
       
  1537 }
       
  1538 
       
  1539 void tst_QSqlDatabase::psql_escapedIdentifiers()
       
  1540 {
       
  1541     QFETCH(QString, dbName);
       
  1542     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  1543     QSqlDriver* drv = db.driver();
       
  1544     CHECK_DATABASE(db);
       
  1545 
       
  1546     if (!db.tables(QSql::SystemTables).contains("pg_namespace"))
       
  1547         QSKIP("server does not support schemas", SkipSingle);
       
  1548 
       
  1549     QSqlQuery q(db);
       
  1550 
       
  1551     if(tst_Databases::isPostgreSQL(db))
       
  1552         QVERIFY_SQL( q, exec("set client_min_messages='warning'"));
       
  1553 
       
  1554     QString schemaName = qTableName("qtestScHeMa");
       
  1555     QString tableName = qTableName("qtest");
       
  1556     QString field1Name = QString("fIeLdNaMe");
       
  1557     QString field2Name = QString("ZuLu");
       
  1558 
       
  1559     q.exec(QString("DROP SCHEMA \"%1\" CASCADE").arg(schemaName));
       
  1560     QString createSchema = QString("CREATE SCHEMA \"%1\"").arg(schemaName);
       
  1561     QVERIFY_SQL(q, exec(createSchema));
       
  1562     QString createTable = QString("CREATE TABLE \"%1\".\"%2\" (\"%3\" int PRIMARY KEY, \"%4\" varchar(20))").arg(schemaName).arg(tableName).arg(field1Name).arg(field2Name);
       
  1563     QVERIFY_SQL(q, exec(createTable));
       
  1564 
       
  1565     QVERIFY(db.tables().contains(schemaName + '.' + tableName, Qt::CaseSensitive));
       
  1566 
       
  1567     QSqlField fld1(field1Name, QVariant::Int);
       
  1568     QSqlField fld2(field2Name, QVariant::String);
       
  1569     QSqlRecord rec;
       
  1570     rec.append(fld1);
       
  1571     rec.append(fld2);
       
  1572 
       
  1573     QVERIFY_SQL(q, exec(drv->sqlStatement(QSqlDriver::SelectStatement, db.driver()->escapeIdentifier(schemaName, QSqlDriver::TableName) + '.' + db.driver()->escapeIdentifier(tableName, QSqlDriver::TableName), rec, false)));
       
  1574 
       
  1575     rec = q.record();
       
  1576     QCOMPARE(rec.count(), 2);
       
  1577     QCOMPARE(rec.fieldName(0), field1Name);
       
  1578     QCOMPARE(rec.fieldName(1), field2Name);
       
  1579     QCOMPARE(rec.field(0).type(), QVariant::Int);
       
  1580 
       
  1581     q.exec(QString("DROP SCHEMA \"%1\" CASCADE").arg(schemaName));
       
  1582 }
       
  1583 
       
  1584 
       
  1585 void tst_QSqlDatabase::psql_escapeBytea()
       
  1586 {
       
  1587     QFETCH(QString, dbName);
       
  1588     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  1589     CHECK_DATABASE(db);
       
  1590 
       
  1591     const char dta[4] = {'\x71', '\x14', '\x32', '\x81'};
       
  1592     QByteArray ba(dta, 4);
       
  1593 
       
  1594     QSqlQuery q(db);
       
  1595     QString tableName = qTableName("batable");
       
  1596     QVERIFY_SQL(q, exec(QString("CREATE TABLE %1 (ba bytea)").arg(tableName)));
       
  1597 
       
  1598     QSqlQuery iq(db);
       
  1599     QVERIFY_SQL(iq, prepare(QString("INSERT INTO %1 VALUES (?)").arg(tableName)));
       
  1600     iq.bindValue(0, QVariant(ba));
       
  1601     QVERIFY_SQL(iq, exec());
       
  1602 
       
  1603     QVERIFY_SQL(q, exec(QString("SELECT ba FROM %1").arg(tableName)));
       
  1604     QVERIFY_SQL(q, next());
       
  1605 
       
  1606     QByteArray res = q.value(0).toByteArray();
       
  1607     int i = 0;
       
  1608     for (; i < ba.size(); ++i){
       
  1609         if (ba[i] != res[i])
       
  1610             break;
       
  1611     }
       
  1612 
       
  1613     QCOMPARE(i, 4);
       
  1614 }
       
  1615 
       
  1616 void tst_QSqlDatabase::bug_249059()
       
  1617 {
       
  1618     QFETCH(QString, dbName);
       
  1619     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  1620     CHECK_DATABASE(db);
       
  1621 
       
  1622     QString version=tst_Databases::getPSQLVersion( db );
       
  1623     double ver=version.section(QChar::fromLatin1('.'),0,1).toDouble();
       
  1624     if (ver < 7.3)
       
  1625         QSKIP("Test requires PostgreSQL >= 7.3", SkipSingle);
       
  1626 
       
  1627     QSqlQuery q(db);
       
  1628     QString tableName = qTableName("bug_249059");
       
  1629     QVERIFY_SQL(q, exec(QString("CREATE TABLE %1 (dt timestamp, t time)").arg(tableName)));
       
  1630 
       
  1631     QSqlQuery iq(db);
       
  1632     QVERIFY_SQL(iq, prepare(QString("INSERT INTO %1 VALUES (?, ?)").arg(tableName)));
       
  1633     iq.bindValue(0, QVariant(QString("2001-09-09 04:05:06.789 -5:00")));
       
  1634     iq.bindValue(1, QVariant(QString("04:05:06.789 -5:00")));
       
  1635     QVERIFY_SQL(iq, exec());
       
  1636     iq.bindValue(0, QVariant(QString("2001-09-09 04:05:06.789 +5:00")));
       
  1637     iq.bindValue(1, QVariant(QString("04:05:06.789 +5:00")));
       
  1638     QVERIFY_SQL(iq, exec());
       
  1639 
       
  1640     QVERIFY_SQL(q, exec(QString("SELECT dt, t FROM %1").arg(tableName)));
       
  1641     QVERIFY_SQL(q, next());
       
  1642     QDateTime dt1=q.value(0).toDateTime();
       
  1643     QTime t1=q.value(1).toTime();
       
  1644     QVERIFY_SQL(q, next());
       
  1645     QDateTime dt2=q.value(0).toDateTime();
       
  1646     QTime t2=q.value(1).toTime();
       
  1647 
       
  1648     // These will fail when timezone support is added, when that's the case, set the second record to 14:05:06.789 and it should work correctly
       
  1649     QCOMPARE(dt1, dt2);
       
  1650     QCOMPARE(t1, t2);
       
  1651 }
       
  1652 
       
  1653 // This test should be rewritten to work with Oracle as well - or the Oracle driver
       
  1654 // should be fixed to make this test pass (handle overflows)
       
  1655 void tst_QSqlDatabase::precisionPolicy()
       
  1656 {
       
  1657     QFETCH(QString, dbName);
       
  1658     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  1659     CHECK_DATABASE(db);
       
  1660 //     DBMS_SPECIFIC(db, "QPSQL");
       
  1661 
       
  1662     QSqlQuery q(db);
       
  1663     QString tableName = qTableName("qtest_prec");
       
  1664     if(!db.driver()->hasFeature(QSqlDriver::LowPrecisionNumbers))
       
  1665         QSKIP("Driver or database doesn't support setting precision policy", SkipSingle);
       
  1666 
       
  1667     // Create a test table with some data
       
  1668     if(tst_Databases::isMSAccess(db))
       
  1669         QVERIFY_SQL(q, exec(QString("CREATE TABLE %1 (id smallint, num number)").arg(tableName)));
       
  1670     else
       
  1671         QVERIFY_SQL(q, exec(QString("CREATE TABLE %1 (id smallint, num numeric(18,5))").arg(tableName)));
       
  1672     QVERIFY_SQL(q, prepare(QString("INSERT INTO %1 VALUES (?, ?)").arg(tableName)));
       
  1673     q.bindValue(0, 1);
       
  1674     q.bindValue(1, 123);
       
  1675     QVERIFY_SQL(q, exec());
       
  1676     q.bindValue(0, 2);
       
  1677     q.bindValue(1, 1850000000000.0001);
       
  1678     QVERIFY_SQL(q, exec());
       
  1679 
       
  1680     // These are expected to pass
       
  1681     q.setNumericalPrecisionPolicy(QSql::HighPrecision);
       
  1682     QString query = QString("SELECT num FROM %1 WHERE id = 1").arg(tableName);
       
  1683     QVERIFY_SQL(q, exec(query));
       
  1684     QVERIFY_SQL(q, next());
       
  1685     if(db.driverName().startsWith("QSQLITE"))
       
  1686         QEXPECT_FAIL("", "SQLite returns this value as determined by contents of the field, not the declaration", Continue);
       
  1687     QCOMPARE(q.value(0).type(), QVariant::String);
       
  1688 
       
  1689     q.setNumericalPrecisionPolicy(QSql::LowPrecisionInt64);
       
  1690     QVERIFY_SQL(q, exec(query));
       
  1691     QVERIFY_SQL(q, next());
       
  1692     if(q.value(0).type() != QVariant::LongLong)
       
  1693         QEXPECT_FAIL("", "SQLite returns this value as determined by contents of the field, not the declaration", Continue);
       
  1694     QCOMPARE(q.value(0).type(), QVariant::LongLong);
       
  1695     QCOMPARE(q.value(0).toLongLong(), (qlonglong)123);
       
  1696 
       
  1697     q.setNumericalPrecisionPolicy(QSql::LowPrecisionInt32);
       
  1698     QVERIFY_SQL(q, exec(query));
       
  1699     if(db.driverName().startsWith("QOCI"))
       
  1700         QEXPECT_FAIL("", "Oracle fails to move to next when data columns are oversize", Abort);
       
  1701     QVERIFY_SQL(q, next());
       
  1702     if(db.driverName().startsWith("QSQLITE"))
       
  1703         QEXPECT_FAIL("", "SQLite returns this value as determined by contents of the field, not the declaration", Continue);
       
  1704     QCOMPARE(q.value(0).type(), QVariant::Int);
       
  1705     QCOMPARE(q.value(0).toInt(), 123);
       
  1706 
       
  1707     q.setNumericalPrecisionPolicy(QSql::LowPrecisionDouble);
       
  1708     QVERIFY_SQL(q, exec(query));
       
  1709     QVERIFY_SQL(q, next());
       
  1710     if(db.driverName().startsWith("QSQLITE"))
       
  1711         QEXPECT_FAIL("", "SQLite returns this value as determined by contents of the field, not the declaration", Continue);
       
  1712     QCOMPARE(q.value(0).type(), QVariant::Double);
       
  1713     QCOMPARE(q.value(0).toDouble(), (double)123);
       
  1714 
       
  1715     query = QString("SELECT num FROM %1 WHERE id = 2").arg(tableName);
       
  1716     QVERIFY_SQL(q, exec(query));
       
  1717     QVERIFY_SQL(q, next());
       
  1718     if(db.driverName().startsWith("QSQLITE"))
       
  1719         QEXPECT_FAIL("", "SQLite returns this value as determined by contents of the field, not the declaration", Continue);
       
  1720     QCOMPARE(q.value(0).type(), QVariant::Double);
       
  1721     QCOMPARE(q.value(0).toDouble(), QString("1850000000000.0001").toDouble());
       
  1722 
       
  1723     // Postgres returns invalid QVariants on overflow
       
  1724     q.setNumericalPrecisionPolicy(QSql::HighPrecision);
       
  1725     QVERIFY_SQL(q, exec(query));
       
  1726     QVERIFY_SQL(q, next());
       
  1727     if(db.driverName().startsWith("QSQLITE"))
       
  1728         QEXPECT_FAIL("", "SQLite returns this value as determined by contents of the field, not the declaration", Continue);
       
  1729     QCOMPARE(q.value(0).type(), QVariant::String);
       
  1730 
       
  1731     q.setNumericalPrecisionPolicy(QSql::LowPrecisionInt64);
       
  1732     QEXPECT_FAIL("QOCI", "Oracle fails here, to retrieve next", Continue);
       
  1733     QVERIFY_SQL(q, exec(query));
       
  1734     QVERIFY_SQL(q, next());
       
  1735     QCOMPARE(q.value(0).type(), QVariant::LongLong);
       
  1736 
       
  1737     QSql::NumericalPrecisionPolicy oldPrecision= db.numericalPrecisionPolicy();
       
  1738     db.setNumericalPrecisionPolicy(QSql::LowPrecisionInt64);
       
  1739     QSqlQuery q2(db);
       
  1740     q2.exec(QString("SELECT num FROM %1 WHERE id = 2").arg(tableName));
       
  1741     QVERIFY_SQL(q2, exec(query));
       
  1742     QVERIFY_SQL(q2, next());
       
  1743     QCOMPARE(q2.value(0).type(), QVariant::LongLong);
       
  1744     db.setNumericalPrecisionPolicy(oldPrecision);
       
  1745 }
       
  1746 
       
  1747 // This test needs a ODBC data source containing MYSQL in it's name
       
  1748 void tst_QSqlDatabase::mysqlOdbc_unsignedIntegers()
       
  1749 {
       
  1750     QFETCH(QString, dbName);
       
  1751     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  1752     CHECK_DATABASE(db);
       
  1753 
       
  1754     if (!db.driverName().startsWith("QODBC") || !dbName.toUpper().contains("MYSQL")) {
       
  1755        QSKIP("MySQL through ODBC-driver specific test", SkipSingle);
       
  1756        return;
       
  1757     }
       
  1758 
       
  1759     QSqlQuery q(db);
       
  1760     QString tableName = qTableName("uint");
       
  1761     QVERIFY_SQL(q, exec(QString("CREATE TABLE %1 (foo integer(10) unsigned, bar integer(10))").arg(tableName)));
       
  1762     QVERIFY_SQL(q, exec(QString("INSERT INTO %1 VALUES (-4000000000, -4000000000)").arg(tableName)));
       
  1763     QVERIFY_SQL(q, exec(QString("INSERT INTO %1 VALUES (4000000000, 4000000000)").arg(tableName)));
       
  1764 
       
  1765     QVERIFY_SQL(q, exec(QString("SELECT foo, bar FROM %1").arg(tableName)));
       
  1766     QVERIFY(q.next());
       
  1767     QCOMPARE(q.value(0).toString(), QString("0"));
       
  1768     QCOMPARE(q.value(1).toString(), QString("-2147483648"));
       
  1769     QVERIFY(q.next());
       
  1770     QCOMPARE(q.value(0).toString(), QString("4000000000"));
       
  1771     QCOMPARE(q.value(1).toString(), QString("2147483647"));
       
  1772 }
       
  1773 
       
  1774 void tst_QSqlDatabase::accessOdbc_strings()
       
  1775 {
       
  1776     QFETCH(QString, dbName);
       
  1777     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  1778     CHECK_DATABASE(db);
       
  1779 
       
  1780     if (!tst_Databases::isMSAccess(db)) {
       
  1781     QSKIP("MS Access specific test", SkipSingle);
       
  1782     return;
       
  1783     }
       
  1784 
       
  1785     QSqlQuery q(db);
       
  1786     QString tableName = qTableName("strings");
       
  1787     QVERIFY_SQL(q, exec(QString("CREATE TABLE %1 (aStr memo, bStr memo, cStr memo, dStr memo"
       
  1788             ", eStr memo, fStr memo, gStr memo, hStr memo)").arg(tableName)));
       
  1789 
       
  1790     QVERIFY_SQL(q, prepare(QString("INSERT INTO %1 VALUES (?, ?, ?, ?, ?, ?, ?, ?)").arg(tableName)));
       
  1791     QString aStr, bStr, cStr, dStr, eStr, fStr, gStr, hStr;
       
  1792 
       
  1793     q.bindValue(0, aStr.fill('A', 32));
       
  1794     q.bindValue(1, bStr.fill('B', 127));
       
  1795     q.bindValue(2, cStr.fill('C', 128));
       
  1796     q.bindValue(3, dStr.fill('D', 129));
       
  1797     q.bindValue(4, eStr.fill('E', 254));
       
  1798     q.bindValue(5, fStr.fill('F', 255));
       
  1799     q.bindValue(6, gStr.fill('G', 256));
       
  1800     q.bindValue(7, hStr.fill('H', 512));
       
  1801 
       
  1802     QVERIFY_SQL(q, exec());
       
  1803 
       
  1804     QVERIFY_SQL(q, exec(QString("SELECT aStr, bStr, cStr, dStr, eStr, fStr, gStr, hStr FROM %1").arg(tableName)));
       
  1805     q.next();
       
  1806     QCOMPARE(q.value(0).toString(), aStr);
       
  1807     QCOMPARE(q.value(1).toString(), bStr);
       
  1808     QCOMPARE(q.value(2).toString(), cStr);
       
  1809     QCOMPARE(q.value(3).toString(), dStr);
       
  1810     QCOMPARE(q.value(4).toString(), eStr);
       
  1811     QCOMPARE(q.value(5).toString(), fStr);
       
  1812     QCOMPARE(q.value(6).toString(), gStr);
       
  1813     QCOMPARE(q.value(7).toString(), hStr);
       
  1814 }
       
  1815 
       
  1816 // For task 125053
       
  1817 void tst_QSqlDatabase::ibase_numericFields()
       
  1818 {
       
  1819     QFETCH(QString, dbName);
       
  1820     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  1821     CHECK_DATABASE(db);
       
  1822 
       
  1823     QSqlQuery q(db);
       
  1824     QString tableName = qTableName("numericfields");
       
  1825     QVERIFY_SQL(q, exec(QString("CREATE TABLE %1 (id int not null, num1 NUMERIC(2,1), "
       
  1826         "num2 NUMERIC(5,2), num3 NUMERIC(10,3), "
       
  1827         "num4 NUMERIC(18,4))").arg(tableName)));
       
  1828 
       
  1829     QVERIFY_SQL(q, exec(QString("INSERT INTO %1 VALUES (1, 1.1, 123.45, 1234567.123, 10203040506070.8090)").arg(tableName)));
       
  1830 
       
  1831     QVERIFY_SQL(q, prepare(QString("INSERT INTO %1 VALUES (?, ?, ?, ?, ?)").arg(tableName)));
       
  1832 
       
  1833     double num1 = 1.1;
       
  1834     double num2 = 123.45;
       
  1835     double num3 = 1234567.123;
       
  1836     double num4 = 10203040506070.8090;
       
  1837 
       
  1838     q.bindValue(0, 2);
       
  1839     q.bindValue(1, QVariant(num1));
       
  1840     q.bindValue(2, QVariant(num2));
       
  1841     q.bindValue(3, QVariant(num3));
       
  1842     q.bindValue(4, QVariant(num4));
       
  1843     QVERIFY_SQL(q, exec());
       
  1844 
       
  1845     QVERIFY_SQL(q, exec(QString("SELECT id, num1, num2, num3, num4 FROM %1").arg(tableName)));
       
  1846 
       
  1847     int id = 0;
       
  1848     while (q.next()) {
       
  1849         QCOMPARE(q.value(0).toInt(), ++id);
       
  1850         QCOMPARE(q.value(1).toString(), QString("%1").arg(num1));
       
  1851         QCOMPARE(q.value(2).toString(), QString("%1").arg(num2));
       
  1852         QCOMPARE(QString("%1").arg(q.value(3).toDouble()), QString("%1").arg(num3));
       
  1853         QCOMPARE(QString("%1").arg(q.value(4).toDouble()), QString("%1").arg(num4));
       
  1854         QVERIFY(q.value(0).type() == QVariant::Int);
       
  1855         QVERIFY(q.value(1).type() == QVariant::Double);
       
  1856         QVERIFY(q.value(2).type() == QVariant::Double);
       
  1857         QVERIFY(q.value(3).type() == QVariant::Double);
       
  1858         QVERIFY(q.value(4).type() == QVariant::Double);
       
  1859 
       
  1860         QCOMPARE(q.record().field(1).length(), 2);
       
  1861         QCOMPARE(q.record().field(1).precision(), 1);
       
  1862         QCOMPARE(q.record().field(2).length(), 5);
       
  1863         QCOMPARE(q.record().field(2).precision(), 2);
       
  1864         QCOMPARE(q.record().field(3).length(), 10);
       
  1865         QCOMPARE(q.record().field(3).precision(), 3);
       
  1866         QCOMPARE(q.record().field(4).length(), 18);
       
  1867         QCOMPARE(q.record().field(4).precision(), 4);
       
  1868         QVERIFY(q.record().field(0).requiredStatus() == QSqlField::Required);
       
  1869         QVERIFY(q.record().field(1).requiredStatus() == QSqlField::Optional);
       
  1870     }
       
  1871 
       
  1872     QSqlRecord r = db.record(tableName);
       
  1873     QVERIFY(r.field(0).type() == QVariant::Int);
       
  1874     QVERIFY(r.field(1).type() == QVariant::Double);
       
  1875     QVERIFY(r.field(2).type() == QVariant::Double);
       
  1876     QVERIFY(r.field(3).type() == QVariant::Double);
       
  1877     QVERIFY(r.field(4).type() == QVariant::Double);
       
  1878     QCOMPARE(r.field(1).length(), 2);
       
  1879     QCOMPARE(r.field(1).precision(), 1);
       
  1880     QCOMPARE(r.field(2).length(), 5);
       
  1881     QCOMPARE(r.field(2).precision(), 2);
       
  1882     QCOMPARE(r.field(3).length(), 10);
       
  1883     QCOMPARE(r.field(3).precision(), 3);
       
  1884     QCOMPARE(r.field(4).length(), 18);
       
  1885     QCOMPARE(r.field(4).precision(), 4);
       
  1886     QVERIFY(r.field(0).requiredStatus() == QSqlField::Required);
       
  1887     QVERIFY(r.field(1).requiredStatus() == QSqlField::Optional);
       
  1888 }
       
  1889 
       
  1890 void tst_QSqlDatabase::ibase_fetchBlobs()
       
  1891 {
       
  1892     QFETCH(QString, dbName);
       
  1893     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  1894     CHECK_DATABASE(db);
       
  1895 
       
  1896     QString tableName = qTableName("qtest_ibaseblobs");
       
  1897     QSqlQuery q(db);
       
  1898     QVERIFY_SQL(q, exec(QString("CREATE TABLE %1 (blob1 BLOB segment size 256)").arg(tableName)));
       
  1899 
       
  1900     QVERIFY_SQL(q, prepare(QString("INSERT INTO %1 VALUES (?)").arg(tableName)));
       
  1901     q.bindValue(0, QByteArray().fill('x', 1024));
       
  1902     QVERIFY_SQL(q, exec());
       
  1903 
       
  1904     QVERIFY_SQL(q, prepare(QString("INSERT INTO %1 VALUES (?)").arg(tableName)));
       
  1905     q.bindValue(0, QByteArray().fill('x', 16383));
       
  1906     QVERIFY_SQL(q, exec());
       
  1907 
       
  1908     QVERIFY_SQL(q, prepare(QString("INSERT INTO %1 VALUES (?)").arg(tableName)));
       
  1909     q.bindValue(0, QByteArray().fill('x', 17408));
       
  1910     QVERIFY_SQL(q, exec());
       
  1911 
       
  1912     QVERIFY_SQL(q, exec(QString("SELECT * FROM %1").arg(tableName)));
       
  1913 
       
  1914     QVERIFY_SQL(q, next());
       
  1915     QCOMPARE(q.value(0).toByteArray().size(), 1024);
       
  1916     QVERIFY_SQL(q, next());
       
  1917     QCOMPARE(q.value(0).toByteArray().size(), 16383);
       
  1918     QVERIFY_SQL(q, next());
       
  1919     QCOMPARE(q.value(0).toByteArray().size(), 17408);
       
  1920 }
       
  1921 
       
  1922 void tst_QSqlDatabase::ibase_procWithoutReturnValues()
       
  1923 {
       
  1924     QFETCH(QString, dbName);
       
  1925     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  1926     CHECK_DATABASE(db);
       
  1927 
       
  1928     QSqlQuery q(db);
       
  1929     QString procName = qTableName("qtest_proc1");
       
  1930     q.exec(QString("drop procedure %1").arg(procName));
       
  1931     QVERIFY_SQL(q, exec("CREATE PROCEDURE " + procName + " (str VARCHAR(10))\nAS BEGIN\nstr='test';\nEND;"));
       
  1932     QVERIFY_SQL(q, exec(QString("execute procedure %1('qtest')").arg(procName)));
       
  1933     q.exec(QString("drop procedure %1").arg(procName));
       
  1934 }
       
  1935 
       
  1936 void tst_QSqlDatabase::ibase_procWithReturnValues()
       
  1937 {
       
  1938     QFETCH(QString, dbName);
       
  1939     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  1940     CHECK_DATABASE(db);
       
  1941 
       
  1942     if (!db.driverName().startsWith("QIBASE")) {
       
  1943        QSKIP("InterBase specific test", SkipSingle);
       
  1944        return;
       
  1945     }
       
  1946 
       
  1947     QString procName = qTableName("qtest_proc2");
       
  1948 
       
  1949     QSqlQuery q(db);
       
  1950     q.exec(QString("drop procedure %1").arg(procName));
       
  1951     QVERIFY_SQL(q, exec("CREATE PROCEDURE " + procName + " ("
       
  1952                         "\nABC INTEGER)"
       
  1953                         "\nRETURNS ("
       
  1954                         "\nRESULT INTEGER)"
       
  1955                         "\nAS"
       
  1956                         "\nbegin"
       
  1957                         "\nRESULT = 10 * ABC;"
       
  1958                         "\nsuspend;"
       
  1959                         "\nend"));
       
  1960 
       
  1961     // Interbase procedures can be executed in two ways: EXECUTE PROCEDURE or SELECT
       
  1962     QVERIFY_SQL(q, exec(QString("execute procedure %1(123)").arg(procName)));
       
  1963     QVERIFY_SQL(q, next());
       
  1964     QCOMPARE(q.value(0).toInt(), 1230);
       
  1965     QVERIFY_SQL(q, exec(QString("select result from %1(456)").arg(procName)));
       
  1966     QVERIFY_SQL(q, next());
       
  1967     QCOMPARE(q.value(0).toInt(), 4560);
       
  1968     QVERIFY_SQL(q, prepare(QLatin1String("execute procedure ")+procName+QLatin1String("(?)")));
       
  1969     q.bindValue(0, 123);
       
  1970     QVERIFY_SQL(q, exec());
       
  1971     QVERIFY_SQL(q, next());
       
  1972     QCOMPARE(q.value(0).toInt(), 1230);
       
  1973     q.bindValue(0, 456);
       
  1974     QVERIFY_SQL(q, exec());
       
  1975     QVERIFY_SQL(q, next());
       
  1976     QCOMPARE(q.value(0).toInt(), 4560);
       
  1977 
       
  1978     q.exec(QString("drop procedure %1").arg(procName));
       
  1979 }
       
  1980 
       
  1981 void tst_QSqlDatabase::formatValueTrimStrings()
       
  1982 {
       
  1983     QFETCH(QString, dbName);
       
  1984     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  1985     CHECK_DATABASE(db);
       
  1986 
       
  1987     QSqlQuery q(db);
       
  1988 
       
  1989     QVERIFY_SQL(q, exec(QString("INSERT INTO %1 (id, t_varchar, t_char) values (50, 'Trim Test ', 'Trim Test 2   ')").arg(qTableName("qtest"))));
       
  1990     QVERIFY_SQL(q, exec(QString("INSERT INTO %1 (id, t_varchar, t_char) values (51, 'TrimTest', 'Trim Test 2')").arg(qTableName("qtest"))));
       
  1991     QVERIFY_SQL(q, exec(QString("INSERT INTO %1 (id, t_varchar, t_char) values (52, ' ', '    ')").arg(qTableName("qtest"))));
       
  1992 
       
  1993     QVERIFY_SQL(q, exec(QString("SELECT t_varchar, t_char FROM %1 WHERE id >= 50 AND id <= 52 ORDER BY id").arg(qTableName("qtest"))));
       
  1994 
       
  1995     QVERIFY_SQL(q, next());
       
  1996 
       
  1997     QCOMPARE(db.driver()->formatValue(q.record().field(0), true), QString("'Trim Test'"));
       
  1998     QCOMPARE(db.driver()->formatValue(q.record().field(1), true), QString("'Trim Test 2'"));
       
  1999 
       
  2000     QVERIFY_SQL(q, next());
       
  2001     QCOMPARE(db.driver()->formatValue(q.record().field(0), true), QString("'TrimTest'"));
       
  2002     QCOMPARE(db.driver()->formatValue(q.record().field(1), true), QString("'Trim Test 2'"));
       
  2003 
       
  2004     QVERIFY_SQL(q, next());
       
  2005     QCOMPARE(db.driver()->formatValue(q.record().field(0), true), QString("''"));
       
  2006     QCOMPARE(db.driver()->formatValue(q.record().field(1), true), QString("''"));
       
  2007 
       
  2008 }
       
  2009 
       
  2010 void tst_QSqlDatabase::odbc_reopenDatabase()
       
  2011 {
       
  2012     QFETCH(QString, dbName);
       
  2013     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  2014     CHECK_DATABASE(db);
       
  2015 
       
  2016     QSqlQuery q(db);
       
  2017     QVERIFY_SQL(q, exec("SELECT * from " + qTableName("qtest")));
       
  2018     QVERIFY_SQL(q, next());
       
  2019     db.open();
       
  2020     QVERIFY_SQL(q, exec("SELECT * from " + qTableName("qtest")));
       
  2021     QVERIFY_SQL(q, next());
       
  2022     db.open();
       
  2023 }
       
  2024 
       
  2025 void tst_QSqlDatabase::odbc_bindBoolean()
       
  2026 {
       
  2027     QFETCH(QString, dbName);
       
  2028     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  2029     CHECK_DATABASE(db);
       
  2030 
       
  2031     if (tst_Databases::isMySQL(db)) {
       
  2032         QSKIP("MySql has inconsistent behaviour of bit field type across versions.", SkipSingle);
       
  2033         return;
       
  2034     }
       
  2035 
       
  2036     QSqlQuery q(db);
       
  2037     QVERIFY_SQL(q, exec("CREATE TABLE " + qTableName("qtestBindBool") + "(id int, boolvalue bit)"));
       
  2038 
       
  2039     // Bind and insert
       
  2040     QVERIFY_SQL(q, prepare("INSERT INTO " + qTableName("qtestBindBool") + " VALUES(?, ?)"));
       
  2041     q.bindValue(0, 1);
       
  2042     q.bindValue(1, true);
       
  2043     QVERIFY_SQL(q, exec());
       
  2044     q.bindValue(0, 2);
       
  2045     q.bindValue(1, false);
       
  2046     QVERIFY_SQL(q, exec());
       
  2047 
       
  2048     // Retrive
       
  2049     QVERIFY_SQL(q, exec("SELECT id, boolvalue FROM " + qTableName("qtestBindBool") + " ORDER BY id"));
       
  2050     QVERIFY_SQL(q, next());
       
  2051     QCOMPARE(q.value(0).toInt(), 1);
       
  2052     QCOMPARE(q.value(1).toBool(), true);
       
  2053     QVERIFY_SQL(q, next());
       
  2054     QCOMPARE(q.value(0).toInt(), 2);
       
  2055     QCOMPARE(q.value(1).toBool(), false);
       
  2056 }
       
  2057 
       
  2058 void tst_QSqlDatabase::odbc_testqGetString()
       
  2059 {
       
  2060     QFETCH(QString, dbName);
       
  2061     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  2062     CHECK_DATABASE(db);
       
  2063 
       
  2064     QSqlQuery q(db);
       
  2065     if (tst_Databases::isSqlServer(db))
       
  2066         QVERIFY_SQL(q, exec("CREATE TABLE " + qTableName("testqGetString") + "(id int, vcvalue varchar(MAX))"));
       
  2067     else if(tst_Databases::isMSAccess(db))
       
  2068         QVERIFY_SQL(q, exec("CREATE TABLE " + qTableName("testqGetString") + "(id int, vcvalue memo)"));
       
  2069     else
       
  2070         QVERIFY_SQL(q, exec("CREATE TABLE " + qTableName("testqGetString") + "(id int, vcvalue varchar(65538))"));
       
  2071 
       
  2072     QString largeString;
       
  2073     largeString.fill('A', 65536);
       
  2074 
       
  2075     // Bind and insert
       
  2076     QVERIFY_SQL(q, prepare("INSERT INTO " + qTableName("testqGetString") + " VALUES(?, ?)"));
       
  2077     q.bindValue(0, 1);
       
  2078     q.bindValue(1, largeString);
       
  2079     QVERIFY_SQL(q, exec());
       
  2080     q.bindValue(0, 2);
       
  2081     q.bindValue(1, largeString+QLatin1Char('B'));
       
  2082     QVERIFY_SQL(q, exec());
       
  2083     q.bindValue(0, 3);
       
  2084     q.bindValue(1, largeString+QLatin1Char('B')+QLatin1Char('C'));
       
  2085     QVERIFY_SQL(q, exec());
       
  2086 
       
  2087     // Retrive
       
  2088     QVERIFY_SQL(q, exec("SELECT id, vcvalue FROM " + qTableName("testqGetString") + " ORDER BY id"));
       
  2089     QVERIFY_SQL(q, next());
       
  2090     QCOMPARE(q.value(0).toInt(), 1);
       
  2091     QCOMPARE(q.value(1).toString().length(), 65536);
       
  2092     QVERIFY_SQL(q, next());
       
  2093     QCOMPARE(q.value(0).toInt(), 2);
       
  2094     QCOMPARE(q.value(1).toString().length(), 65537);
       
  2095     QVERIFY_SQL(q, next());
       
  2096     QCOMPARE(q.value(0).toInt(), 3);
       
  2097     QCOMPARE(q.value(1).toString().length(), 65538);
       
  2098 }
       
  2099 
       
  2100 
       
  2101 void tst_QSqlDatabase::mysql_multiselect()
       
  2102 {
       
  2103     QFETCH(QString, dbName);
       
  2104     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  2105     CHECK_DATABASE(db);
       
  2106 
       
  2107     QSqlQuery q(db);
       
  2108     QString version=tst_Databases::getMySqlVersion( db );
       
  2109     double ver=version.section(QChar::fromLatin1('.'),0,1).toDouble();
       
  2110     if (ver < 4.1)
       
  2111         QSKIP("Test requires MySQL >= 4.1", SkipSingle);
       
  2112 
       
  2113     QVERIFY_SQL(q, exec("SELECT * FROM " + qTableName("qtest") + "; SELECT * FROM " + qTableName("qtest")));
       
  2114     QVERIFY_SQL(q, next());
       
  2115     QVERIFY_SQL(q, exec("SELECT * FROM " + qTableName("qtest") + "; SELECT * FROM " + qTableName("qtest")));
       
  2116     QVERIFY_SQL(q, next());
       
  2117     QVERIFY_SQL(q, exec("SELECT * FROM " + qTableName("qtest")));
       
  2118 }
       
  2119 
       
  2120 void tst_QSqlDatabase::ibase_useCustomCharset()
       
  2121 {
       
  2122     QFETCH(QString, dbName);
       
  2123     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  2124     CHECK_DATABASE(db);
       
  2125     QString nonlatin1string("��");
       
  2126 
       
  2127     db.close();
       
  2128     db.setConnectOptions("ISC_DPB_LC_CTYPE=Latin1");
       
  2129     db.open();
       
  2130 
       
  2131     QString tableName = qTableName("latin1table");
       
  2132 
       
  2133     QSqlQuery q(db);
       
  2134     QVERIFY_SQL(q, exec(QString("CREATE TABLE %1(text VARCHAR(6) CHARACTER SET Latin1)").arg(tableName)));
       
  2135     QVERIFY_SQL(q, prepare(QString("INSERT INTO %1 VALUES(?)").arg(tableName)));
       
  2136     q.addBindValue(nonlatin1string);
       
  2137     QVERIFY_SQL(q, exec());
       
  2138     QVERIFY_SQL(q, exec(QString("SELECT text FROM %1").arg(tableName)));
       
  2139     QVERIFY_SQL(q, next());
       
  2140     QCOMPARE(toHex(q.value(0).toString()), toHex(nonlatin1string));
       
  2141 }
       
  2142 
       
  2143 void tst_QSqlDatabase::oci_serverDetach()
       
  2144 {
       
  2145     QFETCH(QString, dbName);
       
  2146     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  2147     CHECK_DATABASE(db);
       
  2148 
       
  2149     for (int i = 0; i < 2; i++) {
       
  2150         db.close();
       
  2151         if (db.open()) {
       
  2152             QSqlQuery query(db);
       
  2153             query.exec("SELECT 1 FROM DUAL");
       
  2154             db.close();
       
  2155         } else {
       
  2156             QFAIL(tst_Databases::printError(db.lastError(), db));
       
  2157         }
       
  2158     }
       
  2159     if(!db.open())
       
  2160         qFatal(tst_Databases::printError(db.lastError(), db));
       
  2161 }
       
  2162 
       
  2163 void tst_QSqlDatabase::oci_xmltypeSupport()
       
  2164 {
       
  2165     QFETCH(QString, dbName);
       
  2166     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  2167     CHECK_DATABASE(db);
       
  2168 
       
  2169     QString tableName = qTableName("qtest_xmltype");
       
  2170     QString xml("<?xml version=\"1.0\"?><TABLE_NAME>MY_TABLE</TABLE_NAME>");
       
  2171     QSqlQuery q(db);
       
  2172 
       
  2173     // Embedding the XML in the statement
       
  2174     if(!q.exec(QString("CREATE TABLE %1(xmldata xmltype)").arg(tableName)))
       
  2175         QSKIP("This test requries xml type support", SkipSingle);
       
  2176     QVERIFY_SQL(q, exec(QString("INSERT INTO %1 values('%2')").arg(tableName).arg(xml)));
       
  2177     QVERIFY_SQL(q, exec(QString("SELECT a.xmldata.getStringVal() FROM %1 a").arg(tableName)));
       
  2178     QVERIFY_SQL(q, last());
       
  2179     QCOMPARE(q.value(0).toString(), xml);
       
  2180 
       
  2181     // Binding the XML with a prepared statement
       
  2182     QVERIFY_SQL(q, prepare(QString("INSERT INTO %1 values(?)").arg(tableName)));
       
  2183     q.addBindValue(xml);
       
  2184     QVERIFY_SQL(q, exec());
       
  2185     QVERIFY_SQL(q, exec(QString("SELECT a.xmldata.getStringVal() FROM %1 a").arg(tableName)));
       
  2186     QVERIFY_SQL(q, last());
       
  2187     QCOMPARE(q.value(0).toString(), xml);
       
  2188 }
       
  2189 
       
  2190 
       
  2191 void tst_QSqlDatabase::oci_fieldLength()
       
  2192 {
       
  2193     QFETCH(QString, dbName);
       
  2194     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  2195     CHECK_DATABASE(db);
       
  2196 
       
  2197     QString tableName = qTableName("qtest");
       
  2198     QSqlQuery q(db);
       
  2199 
       
  2200     QVERIFY_SQL(q, exec(QString("SELECT t_varchar, t_char FROM %1").arg(tableName)));
       
  2201     QVERIFY_SQL(q, next());
       
  2202     QCOMPARE(q.record().field(0).length(), 40);
       
  2203     QCOMPARE(q.record().field(1).length(), 40);
       
  2204 }
       
  2205 
       
  2206 void tst_QSqlDatabase::oci_synonymstest()
       
  2207 {
       
  2208     QFETCH(QString, dbName);
       
  2209     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  2210     CHECK_DATABASE(db);
       
  2211 
       
  2212     QSqlQuery q(db);
       
  2213     QString creator(qTableName("CREATOR")), appuser(qTableName("APPUSER")), table1(qTableName("TABLE1"));
       
  2214 //     QVERIFY_SQL(q, exec("drop public synonym "+table1));
       
  2215     QVERIFY_SQL(q, exec(QString("create user %1 identified by %2 default tablespace users temporary tablespace temp").arg(creator).arg(creator)));
       
  2216     QVERIFY_SQL(q, exec(QString("grant CONNECT to %1").arg(creator)));
       
  2217     QVERIFY_SQL(q, exec(QString("grant RESOURCE to %1").arg(creator)));
       
  2218     QSqlDatabase db2=db.cloneDatabase(db, QLatin1String("oci_synonymstest"));
       
  2219     db2.close();
       
  2220     QVERIFY_SQL(db2, open(creator,creator));
       
  2221     QSqlQuery q2(db2);
       
  2222     QVERIFY_SQL(q2, exec(QString("create table %1(id int primary key)").arg(table1)));
       
  2223     QVERIFY_SQL(q, exec(QString("create user %1 identified by %2 default tablespace users temporary tablespace temp").arg(appuser).arg(appuser)));
       
  2224     QVERIFY_SQL(q, exec(QString("grant CREATE ANY SYNONYM to %1").arg(appuser)));
       
  2225     QVERIFY_SQL(q, exec(QString("grant CONNECT to %1").arg(appuser)));
       
  2226     QVERIFY_SQL(q2, exec(QString("grant select, insert, update, delete on %1 to %2").arg(table1).arg(appuser)));
       
  2227     QSqlDatabase db3=db.cloneDatabase(db, QLatin1String("oci_synonymstest2"));
       
  2228     db3.close();
       
  2229     QVERIFY_SQL(db3, open(appuser,appuser));
       
  2230     QSqlQuery q3(db3);
       
  2231     QVERIFY_SQL(q3, exec("create synonym "+appuser+'.'+qTableName("synonyms")+" for "+creator+'.'+table1));
       
  2232     QVERIFY_SQL(db3, tables().filter(qTableName("synonyms"), Qt::CaseInsensitive).count() >= 1);
       
  2233 }
       
  2234 
       
  2235 
       
  2236 // This test isn't really necessary as SQL_GUID / uniqueidentifier is
       
  2237 // already tested in recordSQLServer().
       
  2238 void tst_QSqlDatabase::odbc_uniqueidentifier()
       
  2239 {
       
  2240     QFETCH(QString, dbName);
       
  2241     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  2242     CHECK_DATABASE(db);
       
  2243     if (!tst_Databases::isSqlServer(db)) {
       
  2244         QSKIP("SQL Server (ODBC) specific test", SkipSingle);
       
  2245         return;
       
  2246     }
       
  2247 
       
  2248     QString tableName = qTableName("qtest_sqlguid");
       
  2249     QString guid = QString("AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE");
       
  2250     QString invalidGuid = QString("GAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE");
       
  2251 
       
  2252     QSqlQuery q(db);
       
  2253     QVERIFY_SQL(q, exec(QString("CREATE TABLE %1(id uniqueidentifier)").arg(tableName)));
       
  2254 
       
  2255     q.prepare(QString("INSERT INTO %1 VALUES(?)").arg(tableName));;
       
  2256     q.addBindValue(guid);
       
  2257     QVERIFY_SQL(q, exec());
       
  2258 
       
  2259     q.addBindValue(invalidGuid);
       
  2260     QEXPECT_FAIL("", "The GUID string is required to be correctly formated!",
       
  2261         Continue);
       
  2262     QVERIFY_SQL(q, exec());
       
  2263 
       
  2264     QVERIFY_SQL(q, exec(QString("SELECT id FROM %1").arg(tableName)));
       
  2265     QVERIFY_SQL(q, next());
       
  2266     QCOMPARE(q.value(0).toString(), guid);
       
  2267 }
       
  2268 
       
  2269 void tst_QSqlDatabase::getConnectionName()
       
  2270 {
       
  2271     QFETCH(QString, dbName);
       
  2272     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  2273     CHECK_DATABASE(db);
       
  2274 
       
  2275     QCOMPARE(db.connectionName(), dbName);
       
  2276     QSqlDatabase clone = QSqlDatabase::cloneDatabase(db, "clonedDatabase");
       
  2277     QCOMPARE(clone.connectionName(), QString("clonedDatabase"));
       
  2278     QTest::ignoreMessage(QtWarningMsg, "QSqlDatabasePrivate::removeDatabase: "
       
  2279         "connection 'clonedDatabase' is still in use, all queries will cease to work.");
       
  2280     QSqlDatabase::removeDatabase("clonedDatabase");
       
  2281     QCOMPARE(clone.connectionName(), QString());
       
  2282     QCOMPARE(db.connectionName(), dbName);
       
  2283 }
       
  2284 
       
  2285 void tst_QSqlDatabase::odbc_uintfield()
       
  2286 {
       
  2287     QFETCH(QString, dbName);
       
  2288     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  2289     CHECK_DATABASE(db);
       
  2290 
       
  2291     QString tableName = qTableName("uint_table");
       
  2292     unsigned int val = 4294967295U;
       
  2293 
       
  2294     QSqlQuery q(db);
       
  2295     if ( tst_Databases::isMSAccess( db ) )
       
  2296         QVERIFY_SQL(q, exec(QString("CREATE TABLE %1(num number)").arg(tableName)));
       
  2297     else
       
  2298         QVERIFY_SQL(q, exec(QString("CREATE TABLE %1(num numeric(10))").arg(tableName)));
       
  2299     q.prepare(QString("INSERT INTO %1 VALUES(?)").arg(tableName));
       
  2300     q.addBindValue(val);
       
  2301     QVERIFY_SQL(q, exec());
       
  2302 
       
  2303     q.exec(QString("SELECT num FROM %1").arg(tableName));
       
  2304     if (q.next())
       
  2305         QCOMPARE(q.value(0).toUInt(), val);
       
  2306 }
       
  2307 
       
  2308 void tst_QSqlDatabase::eventNotification()
       
  2309 {
       
  2310     QFETCH(QString, dbName);
       
  2311     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  2312     CHECK_DATABASE(db);
       
  2313 
       
  2314     QSqlDriver *driver = db.driver();
       
  2315     if (!driver->hasFeature(QSqlDriver::EventNotifications))
       
  2316         QSKIP("DBMS doesn't support event notifications", SkipSingle);
       
  2317 
       
  2318     // Not subscribed to any events yet
       
  2319     QCOMPARE(driver->subscribedToNotifications().size(), 0);
       
  2320 
       
  2321     // Subscribe to "event_foo"
       
  2322     QVERIFY_SQL(*driver, subscribeToNotification(QLatin1String("event_foo")));
       
  2323     QCOMPARE(driver->subscribedToNotifications().size(), 1);
       
  2324     QVERIFY(driver->subscribedToNotifications().contains("event_foo"));
       
  2325 
       
  2326     // Can't subscribe to the same event multiple times
       
  2327     QVERIFY2(!driver->subscribeToNotification(QLatin1String("event_foo")), "Shouldn't be able to subscribe to event_foo twice");
       
  2328     QCOMPARE(driver->subscribedToNotifications().size(), 1);
       
  2329 
       
  2330     // Unsubscribe from "event_foo"
       
  2331     QVERIFY_SQL(*driver, unsubscribeFromNotification(QLatin1String("event_foo")));
       
  2332     QCOMPARE(driver->subscribedToNotifications().size(), 0);
       
  2333 
       
  2334     // Re-subscribing to "event_foo" now is allowed
       
  2335     QVERIFY_SQL(*driver, subscribeToNotification(QLatin1String("event_foo")));
       
  2336     QCOMPARE(driver->subscribedToNotifications().size(), 1);
       
  2337 
       
  2338     // closing the connection causes automatically unsubscription from all events
       
  2339     db.close();
       
  2340     QCOMPARE(driver->subscribedToNotifications().size(), 0);
       
  2341 
       
  2342     // Can't subscribe to anything while database is closed
       
  2343     QVERIFY2(!driver->subscribeToNotification(QLatin1String("event_foo")), "Shouldn't be able to subscribe to event_foo");
       
  2344     QCOMPARE(driver->subscribedToNotifications().size(), 0);
       
  2345 
       
  2346     db.open();
       
  2347 }
       
  2348 
       
  2349 void tst_QSqlDatabase::eventNotificationIBase()
       
  2350 {
       
  2351     QFETCH(QString, dbName);
       
  2352     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  2353     CHECK_DATABASE(db);
       
  2354 
       
  2355     QString procedureName = qTableName("posteventProc");
       
  2356     QSqlDriver *driver=db.driver();
       
  2357     QVERIFY_SQL(*driver, subscribeToNotification(procedureName));
       
  2358     QTest::qWait(300);  // Interbase needs some time to call the driver callback.
       
  2359 
       
  2360     db.transaction();   // InterBase events are posted from within transactions.
       
  2361     QSqlQuery q(db);
       
  2362     q.exec(QString("DROP PROCEDURE %1").arg(procedureName));
       
  2363     q.exec(QString("CREATE PROCEDURE %1\nAS BEGIN\nPOST_EVENT '%1';\nEND;").arg(procedureName));
       
  2364     q.exec(QString("EXECUTE PROCEDURE %1").arg(procedureName));
       
  2365     QSignalSpy spy(driver, SIGNAL(notification(const QString&)));
       
  2366     db.commit();        // No notifications are posted until the transaction is committed.
       
  2367     QTest::qWait(300);  // Interbase needs some time to post the notification and call the driver callback.
       
  2368                         // This happends from another thread, and we have to process events in order for the
       
  2369                         // event handler in the driver to be executed and emit the notification signal.
       
  2370 
       
  2371     QCOMPARE(spy.count(), 1);
       
  2372     QList<QVariant> arguments = spy.takeFirst();
       
  2373     QVERIFY(arguments.at(0).toString() == procedureName);
       
  2374     QVERIFY_SQL(*driver, unsubscribeFromNotification(procedureName));
       
  2375     q.exec(QString("DROP PROCEDURE %1").arg(procedureName));
       
  2376 }
       
  2377 
       
  2378 void tst_QSqlDatabase::eventNotificationPSQL()
       
  2379 {
       
  2380     QFETCH(QString, dbName);
       
  2381     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  2382     CHECK_DATABASE(db);
       
  2383 
       
  2384 #if defined(Q_OS_LINUX)
       
  2385     QSKIP( "Event support doesn't work on linux", SkipAll );
       
  2386 #endif
       
  2387 
       
  2388     QSqlQuery query(db);
       
  2389     QString procedureName = qTableName("posteventProc");
       
  2390 
       
  2391     QSqlDriver &driver=*(db.driver());
       
  2392     QVERIFY_SQL(driver, subscribeToNotification(procedureName));
       
  2393     QSignalSpy spy(db.driver(), SIGNAL(notification(const QString&)));
       
  2394     query.exec(QString("NOTIFY \"%1\"").arg(procedureName));
       
  2395     QCoreApplication::processEvents();
       
  2396     QCOMPARE(spy.count(), 1);
       
  2397     QList<QVariant> arguments = spy.takeFirst();
       
  2398     QVERIFY(arguments.at(0).toString() == procedureName);
       
  2399     QVERIFY_SQL(driver, unsubscribeFromNotification(procedureName));
       
  2400 }
       
  2401 
       
  2402 void tst_QSqlDatabase::sqlite_bindAndFetchUInt()
       
  2403 {
       
  2404     QFETCH(QString, dbName);
       
  2405     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  2406     CHECK_DATABASE(db);
       
  2407     if (db.driverName().startsWith("QSQLITE2")) { 
       
  2408         QSKIP("SQLite3 specific test", SkipSingle); 
       
  2409         return; 
       
  2410     }
       
  2411 
       
  2412     QSqlQuery q(db);
       
  2413     QString tableName = qTableName("uint_test");
       
  2414     QVERIFY_SQL(q, exec(QString("CREATE TABLE %1(uint_field UNSIGNED INTEGER)").arg(tableName)));
       
  2415     QVERIFY_SQL(q, prepare(QString("INSERT INTO %1 VALUES(?)").arg(tableName)));
       
  2416     q.addBindValue(4000000000U);
       
  2417     QVERIFY_SQL(q, exec());
       
  2418     QVERIFY_SQL(q, exec(QString("SELECT uint_field FROM %1").arg(tableName)));
       
  2419     QVERIFY_SQL(q, next());
       
  2420 
       
  2421     // All integers in SQLite are signed, so even though we bound the value
       
  2422     // as an UInt it will come back as a LongLong
       
  2423     QCOMPARE(q.value(0).type(), QVariant::LongLong);
       
  2424     QCOMPARE(q.value(0).toUInt(), 4000000000U);
       
  2425 }
       
  2426 
       
  2427 void tst_QSqlDatabase::db2_valueCacheUpdate()
       
  2428 {
       
  2429     QFETCH(QString, dbName);
       
  2430     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  2431     CHECK_DATABASE(db);
       
  2432 
       
  2433     QString tableName = qTableName("qtest");
       
  2434     QSqlQuery q(db);
       
  2435     q.exec(QString("SELECT id, t_varchar, t_char, t_numeric FROM %1").arg(tableName));
       
  2436     q.next();
       
  2437     QVariant c4 = q.value(3);
       
  2438     QVariant c3 = q.value(2);
       
  2439     QVariant c2 = q.value(1);
       
  2440     QVariant c1 = q.value(0);
       
  2441     QCOMPARE(c4.toString(), q.value(3).toString());
       
  2442     QCOMPARE(c3.toString(), q.value(2).toString());
       
  2443     QCOMPARE(c2.toString(), q.value(1).toString());
       
  2444     QCOMPARE(c1.toString(), q.value(0).toString());
       
  2445 }
       
  2446 
       
  2447 void tst_QSqlDatabase::sqlStatementUseIsNull_189093()
       
  2448 {
       
  2449     // NULL = NULL is unknown, the sqlStatment must use IS NULL
       
  2450     QFETCH(QString, dbName);
       
  2451     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  2452     CHECK_DATABASE(db);
       
  2453 
       
  2454     // select a record with NULL value
       
  2455     QSqlQuery q(QString::null, db);
       
  2456     QVERIFY_SQL(q, exec("select * from " + qTableName("qtest") + " where id = 4"));
       
  2457     QVERIFY_SQL(q, next());
       
  2458 
       
  2459     QSqlDriver *driver = db.driver();
       
  2460     QVERIFY(driver);
       
  2461 
       
  2462     QString preparedStatment = driver->sqlStatement(QSqlDriver::WhereStatement, QString("qtest"), q.record(), true);
       
  2463     QCOMPARE(preparedStatment.count("IS NULL", Qt::CaseInsensitive), 2);
       
  2464 
       
  2465     QString statment = driver->sqlStatement(QSqlDriver::WhereStatement, QString("qtest"), q.record(), false);
       
  2466     QCOMPARE(statment.count("IS NULL", Qt::CaseInsensitive), 2);
       
  2467 }
       
  2468 
       
  2469 void tst_QSqlDatabase::mysql_savepointtest()
       
  2470 {
       
  2471     QFETCH(QString, dbName);
       
  2472     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  2473     CHECK_DATABASE(db);
       
  2474     if ( db.driverName().startsWith( "QMYSQL" ) && tst_Databases::getMySqlVersion( db ).section( QChar('.'), 0, 1 ).toDouble()<4.1 )
       
  2475         QSKIP( "Test requires MySQL >= 4.1", SkipSingle );
       
  2476 
       
  2477     QSqlQuery q(db);
       
  2478     QVERIFY_SQL(q, exec("begin"));
       
  2479     QVERIFY_SQL(q, exec("insert into "+qTableName("qtest")+" VALUES (54, 'foo', 'foo', 54.54)"));
       
  2480     QVERIFY_SQL(q, exec("savepoint foo"));
       
  2481 }
       
  2482 
       
  2483 QTEST_MAIN(tst_QSqlDatabase)
       
  2484 #include "tst_qsqldatabase.moc"