util/tests/auto/qsqldatabase/tst_databases.h
changeset 7 f7bc934e204c
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 /* possible connection parameters */
       
    42 
       
    43 #ifndef TST_DATABASES_H
       
    44 #define TST_DATABASES_H
       
    45 
       
    46 #include <QSqlDatabase>
       
    47 #include <QSqlDriver>
       
    48 #include <QSqlError>
       
    49 #include <QSqlQuery>
       
    50 #include <QRegExp>
       
    51 #include <QDir>
       
    52 #include <QVariant>
       
    53 #include <QDebug>
       
    54 
       
    55 #include <QtTest/QtTest>
       
    56 
       
    57 #if defined (Q_OS_WIN) || defined (Q_OS_WIN32)
       
    58 #  include <qt_windows.h>
       
    59 #  if defined (Q_OS_WINCE)
       
    60 #    include <winsock2.h>
       
    61 #  endif
       
    62 #else
       
    63 #include <unistd.h>
       
    64 #endif
       
    65 
       
    66 #define CHECK_DATABASE( db ) \
       
    67     if ( !db.isValid() ) { qFatal( "db is Invalid" ); }
       
    68 
       
    69 #define QVERIFY_SQL(q, stmt) QVERIFY2((q).stmt, tst_Databases::printError((q).lastError(), db))
       
    70 #define QFAIL_SQL(q, stmt) QVERIFY2(!(q).stmt, tst_Databases::printError((q).lastError(), db))
       
    71 
       
    72 #define DBMS_SPECIFIC(db, driver) \
       
    73     if (!db.driverName().startsWith(driver)) { QSKIP(driver " specific test", SkipSingle); return; }
       
    74 
       
    75 // ### use QSystem::hostName if it is integrated in qtest/main
       
    76 static QString qGetHostName()
       
    77 {
       
    78     static QString hostname;
       
    79 
       
    80     if ( !hostname.isEmpty() )
       
    81         return hostname;
       
    82 
       
    83     char hn[257];
       
    84 
       
    85     if ( gethostname( hn, 255 ) == 0 ) {
       
    86         hn[256] = '\0';
       
    87         hostname = QString::fromLatin1( hn );
       
    88         hostname.replace( QLatin1Char( '.' ), QLatin1Char( '_' ) );
       
    89         hostname.replace( QLatin1Char( '-' ), QLatin1Char( '_' ) );
       
    90     }
       
    91 
       
    92     return hostname;
       
    93 }
       
    94 
       
    95 // to prevent nameclashes on our database server, each machine
       
    96 // will use its own set of table names. Call this function to get
       
    97 // "tablename_hostname"
       
    98 inline static QString qTableName( const QString& prefix, const char *sourceFileName )
       
    99 {
       
   100     return QLatin1String("dbtst")+QString::number(qHash(QLatin1String(sourceFileName) + "_" + qGetHostName().replace( "-", "_" )), 16)+"_"+prefix;
       
   101 }
       
   102 
       
   103 inline static QString qTableName( const QString& prefix, QSqlDriver* driver )
       
   104 {
       
   105     return driver->escapeIdentifier( prefix + "_" + qGetHostName(), QSqlDriver::TableName );
       
   106 }
       
   107 
       
   108 inline static bool testWhiteSpaceNames( const QString &name )
       
   109 {
       
   110 /*    return name.startsWith( "QPSQL" )
       
   111            || name.startsWith( "QODBC" )
       
   112            || name.startsWith( "QSQLITE" )
       
   113            || name.startsWith( "QMYSQL" );*/
       
   114     return name != QLatin1String("QSQLITE2");
       
   115 }
       
   116 
       
   117 inline static QString toHex( const QString& binary )
       
   118 {
       
   119     QString str;
       
   120     static char const hexchars[] = "0123456789ABCDEF";
       
   121 
       
   122     for ( int i = 0; i < binary.size(); i++ ) {
       
   123         ushort code = binary.at(i).unicode();
       
   124         str += (QChar)(hexchars[ (code >> 12) & 0x0F ]);
       
   125         str += (QChar)(hexchars[ (code >> 8) & 0x0F ]);
       
   126         str += (QChar)(hexchars[ (code >> 4) & 0x0F ]);
       
   127         str += (QChar)(hexchars[ code & 0x0F ]);
       
   128     }
       
   129 
       
   130     return str;
       
   131 }
       
   132 
       
   133 
       
   134 class tst_Databases
       
   135 {
       
   136 
       
   137 public:
       
   138     tst_Databases(): counter( 0 )
       
   139     {
       
   140     }
       
   141 
       
   142     ~tst_Databases()
       
   143     {
       
   144         close();
       
   145     }
       
   146 
       
   147     // returns a testtable consisting of the names of all database connections if
       
   148     // driverPrefix is empty, otherwise only those that start with driverPrefix.
       
   149     int fillTestTable( const QString& driverPrefix = QString() ) const
       
   150     {
       
   151         QTest::addColumn<QString>( "dbName" );
       
   152         int count = 0;
       
   153 
       
   154         for ( int i = 0; i < dbNames.count(); ++i ) {
       
   155             QSqlDatabase db = QSqlDatabase::database( dbNames.at( i ) );
       
   156 
       
   157             if ( !db.isValid() )
       
   158                 continue;
       
   159 
       
   160             if ( driverPrefix.isEmpty() || db.driverName().startsWith( driverPrefix ) ) {
       
   161                 QTest::newRow( dbNames.at( i ).toLatin1() ) << dbNames.at( i );
       
   162                 ++count;
       
   163             }
       
   164         }
       
   165 
       
   166         return count;
       
   167     }
       
   168 
       
   169     void addDb( const QString& driver, const QString& dbName,
       
   170                 const QString& user = QString(), const QString& passwd = QString(),
       
   171                 const QString& host = QString(), int port = -1, const QString params = QString() )
       
   172     {
       
   173         QSqlDatabase db;
       
   174 
       
   175         if ( !QSqlDatabase::drivers().contains( driver ) ) {
       
   176             qWarning() <<  "Driver" << driver << "is not installed";
       
   177             return;
       
   178         }
       
   179 
       
   180         // construct a stupid unique name
       
   181         QString cName = QString::number( counter++ ) + "_" + driver + "@";
       
   182 
       
   183         cName += host.isEmpty() ? dbName : host;
       
   184 
       
   185         if ( port > 0 )
       
   186             cName += ":" + QString::number( port );
       
   187 
       
   188         db = QSqlDatabase::addDatabase( driver, cName );
       
   189 
       
   190         if ( !db.isValid() ) {
       
   191             qWarning( "Could not create database object" );
       
   192             return;
       
   193         }
       
   194 
       
   195         db.setDatabaseName( dbName );
       
   196 
       
   197         db.setUserName( user );
       
   198         db.setPassword( passwd );
       
   199         db.setHostName( host );
       
   200         db.setPort( port );
       
   201         db.setConnectOptions( params );
       
   202         dbNames.append( cName );
       
   203     }
       
   204 
       
   205     void addDbs()
       
   206     {
       
   207 //         addDb( "QOCI8", "//horsehead.nokia.troll.no:1521/pony.troll.no", "scott", "tiger" ); // Oracle 9i on horsehead
       
   208 //         addDb( "QOCI8", "//horsehead.nokia.troll.no:1521/ustest.troll.no", "scott", "tiger", "" ); // Oracle 9i on horsehead
       
   209 //         addDb( "QOCI8", "//iceblink.nokia.troll.no:1521/ice.troll.no", "scott", "tiger", "" ); // Oracle 8 on iceblink (not currently working)
       
   210 //         addDb( "QOCI", "//silence.nokia.troll.no:1521/testdb", "scott", "tiger" ); // Oracle 10g on silence
       
   211 //         addDb( "QOCI", "//oracle10g-nokia.trolltech.com.au:1521/XE", "scott", "tiger" ); // Oracle 10gexpress on xen
       
   212 
       
   213 //      This requires a local ODBC data source to be configured( pointing to a MySql database )
       
   214 //         addDb( "QODBC", "mysqlodbc", "troll", "trond" );
       
   215 //         addDb( "QODBC", "SqlServer", "troll", "trond" );
       
   216 //         addDb( "QTDS7", "testdb", "troll", "trondk", "horsehead" );
       
   217 //         addDb( "QODBC", "silencetestdb", "troll", "trond", "silence" );
       
   218 //         addDb( "QODBC", "horseheadtestdb", "troll", "trondk", "horsehead" );
       
   219 
       
   220 //         addDb( "QMYSQL3", "testdb", "troll", "trond", "horsehead.nokia.troll.no" );
       
   221 //         addDb( "QMYSQL3", "testdb", "troll", "trond", "horsehead.nokia.troll.no", 3307 );
       
   222 //         addDb( "QMYSQL3", "testdb", "troll", "trond", "horsehead.nokia.troll.no", 3308, "CLIENT_COMPRESS=1;CLIENT_SSL=1" ); // MySQL 4.1.1
       
   223 //         addDb( "QMYSQL3", "testdb", "troll", "trond", "horsehead.nokia.troll.no", 3309, "CLIENT_COMPRESS=1;CLIENT_SSL=1" ); // MySQL 5.0.18 Linux
       
   224 //         addDb( "QMYSQL3", "testdb", "troll", "trond", "silence.nokia.troll.no" ); // MySQL 5.1.36 Windows
       
   225 //         addDb( "QMYSQL3", "testdb", "testuser", "Ee4Gabf6_", "mysql4-nokia.trolltech.com.au" ); // MySQL 4.1.22-2.el4  linux
       
   226 //         addDb( "QMYSQL3", "testdb", "testuser", "Ee4Gabf6_", "bq-mysql50.apac.nokia.com" ); // MySQL 5.0.45-7.el5 linux
       
   227 //         addDb( "QMYSQL3", "testdb", "testuser", "Ee4Gabf6_", "bq-mysql51.apac.nokia.com" ); // MySQL 5.1.36-6.7.2.i586 linux
       
   228 
       
   229 //         addDb( "QPSQL7", "testdb", "troll", "trond", "horsehead.nokia.troll.no" ); // V7.2 NOT SUPPORTED!
       
   230 //         addDb( "QPSQL7", "testdb", "troll", "trond", "horsehead.nokia.troll.no", 5434 ); // V7.2 NOT SUPPORTED! Multi-byte
       
   231 //         addDb( "QPSQL7", "testdb", "troll", "trond", "horsehead.nokia.troll.no", 5435 ); // V7.3
       
   232 //         addDb( "QPSQL7", "testdb", "troll", "trond", "horsehead.nokia.troll.no", 5436 ); // V7.4
       
   233 //         addDb( "QPSQL7", "testdb", "troll", "trond", "horsehead.nokia.troll.no", 5437 ); // V8.0.3
       
   234 //         addDb( "QPSQL7", "testdb", "troll", "trond", "silence.nokia.troll.no" );         // V8.2.1, UTF-8
       
   235 //         addDb( "QPSQL7", "testdb", "testuser", "Ee4Gabf6_", "postgres74-nokia.trolltech.com.au" );         // Version 7.4.19-1.el4_6.1
       
   236 //         addDb( "QPSQL7", "testdb", "testuser", "Ee4Gabf6_", "bq-pgsql81.apac.nokia.com" );         // Version 8.1.11-1.el5_1.1
       
   237 //         addDb( "QPSQL7", "testdb", "testuser", "Ee4Gabf6_", "bq-pgsql84.apac.nokia.com" );         // Version 8.4.1-2.1.i586
       
   238 
       
   239 //         addDb( "QDB2", "testdb", "troll", "trond", "silence.nokia.troll.no" ); // DB2 v9.1 on silence
       
   240 
       
   241 //      yes - interbase really wants the physical path on the host machine.
       
   242 //         addDb( "QIBASE", "/opt/interbase/qttest.gdb", "SYSDBA", "masterkey", "horsehead.nokia.troll.no" );
       
   243 //         addDb( "QIBASE", "silence.troll.no:c:\\ibase\\testdb", "SYSDBA", "masterkey", "" ); // InterBase 7.5 on silence
       
   244 //         addDb( "QIBASE", "silence.troll.no:c:\\ibase\\testdb_ascii", "SYSDBA", "masterkey", "" ); // InterBase 7.5 on silence
       
   245 //         addDb( "QIBASE", "/opt/firebird/databases/testdb.fdb", "testuser", "Ee4Gabf6_", "firebird1-nokia.trolltech.com.au" ); // Firebird 1.5.5
       
   246 //         addDb( "QIBASE", "/opt/firebird/databases/testdb.fdb", "testuser", "Ee4Gabf6_", "firebird2-nokia.trolltech.com.au" ); // Firebird 2.1.1
       
   247 
       
   248 //      use in-memory database to prevent local files
       
   249 //         addDb("QSQLITE", ":memory:");
       
   250          addDb( "QSQLITE", QDir::toNativeSeparators(QDir::tempPath()+"/foo.db") );
       
   251 //         addDb( "QSQLITE2", QDir::toNativeSeparators(QDir::tempPath()+"/foo2.db") );
       
   252 //         addDb( "QODBC3", "DRIVER={SQL SERVER};SERVER=iceblink.nokia.troll.no\\ICEBLINK", "troll", "trond", "" );
       
   253 //         addDb( "QODBC3", "DRIVER={SQL Native Client};SERVER=silence.nokia.troll.no\\SQLEXPRESS", "troll", "trond", "" );
       
   254 
       
   255 //         addDb( "QODBC", "DRIVER={MySQL ODBC 5.1 Driver};SERVER=bq-mysql50.apac.nokia.com;DATABASE=testdb", "testuser", "Ee4Gabf6_", "" );
       
   256 //         addDb( "QODBC", "DRIVER={MySQL ODBC 5.1 Driver};SERVER=bq-mysql51.apac.nokia.com;DATABASE=testdb", "testuser", "Ee4Gabf6_", "" );
       
   257 //         addDb( "QODBC", "DRIVER={FreeTDS};SERVER=horsehead.nokia.troll.no;DATABASE=testdb;PORT=4101;UID=troll;PWD=trondk", "troll", "trondk", "" );
       
   258 //         addDb( "QODBC", "DRIVER={FreeTDS};SERVER=silence.nokia.troll.no;DATABASE=testdb;PORT=2392;UID=troll;PWD=trond", "troll", "trond", "" );
       
   259 //         addDb( "QODBC", "DRIVER={FreeTDS};SERVER=bq-winserv2003-x86-01.apac.nokia.com;DATABASE=testdb;PORT=1433;UID=testuser;PWD=Ee4Gabf6_;TDS_Version=8.0", "", "", "" );
       
   260 //         addDb( "QODBC", "DRIVER={FreeTDS};SERVER=bq-winserv2008-x86-01.apac.nokia.com;DATABASE=testdb;PORT=1433;UID=testuser;PWD=Ee4Gabf6_;TDS_Version=8.0", "", "", "" );
       
   261 //         addDb( "QTDS7", "testdb", "testuser", "Ee4Gabf6_", "bq-winserv2003" );
       
   262 //         addDb( "QTDS7", "testdb", "testuser", "Ee4Gabf6_", "bq-winserv2008" );
       
   263 //         addDb( "QODBC3", "DRIVER={SQL SERVER};SERVER=bq-winserv2003-x86-01.apac.nokia.com;DATABASE=testdb;PORT=1433", "testuser", "Ee4Gabf6_", "" );
       
   264 //         addDb( "QODBC3", "DRIVER={SQL SERVER};SERVER=bq-winserv2008-x86-01.apac.nokia.com;DATABASE=testdb;PORT=1433", "testuser", "Ee4Gabf6_", "" );
       
   265 //         addDb( "QODBC", "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=c:\\dbs\\access\\testdb.mdb", "", "", "" );
       
   266 //         addDb( "QODBC", "DRIVER={Postgresql};SERVER=bq-pgsql84.apac.nokia.com;DATABASE=testdb", "testuser", "Ee4Gabf6_", "" );
       
   267     }
       
   268 
       
   269     void open()
       
   270     {
       
   271         addDbs();
       
   272 
       
   273         QStringList::Iterator it = dbNames.begin();
       
   274 
       
   275         while ( it != dbNames.end() ) {
       
   276             QSqlDatabase db = QSqlDatabase::database(( *it ), false );
       
   277             qDebug() << "Opening:" << (*it);
       
   278 
       
   279             if ( db.isValid() && !db.isOpen() ) {
       
   280                 if ( !db.open() ) {
       
   281                     qWarning( "tst_Databases: Unable to open %s on %s:\n%s", qPrintable( db.driverName() ), qPrintable( *it ), qPrintable( db.lastError().databaseText() ) );
       
   282                     // well... opening failed, so we just ignore the server, maybe it is not running
       
   283                     it = dbNames.erase( it );
       
   284                 } else {
       
   285                     ++it;
       
   286                 }
       
   287             }
       
   288         }
       
   289     }
       
   290 
       
   291     void close()
       
   292     {
       
   293         for ( QStringList::Iterator it = dbNames.begin(); it != dbNames.end(); ++it ) {
       
   294             {
       
   295                 QSqlDatabase db = QSqlDatabase::database(( *it ), false );
       
   296 
       
   297                 if ( db.isValid() && db.isOpen() )
       
   298                     db.close();
       
   299             }
       
   300 
       
   301             QSqlDatabase::removeDatabase(( *it ) );
       
   302         }
       
   303 
       
   304         dbNames.clear();
       
   305     }
       
   306 
       
   307     // for debugging only: outputs the connection as string
       
   308     static QString dbToString( const QSqlDatabase db )
       
   309     {
       
   310         QString res = db.driverName() + "@";
       
   311 
       
   312         if ( db.driverName().startsWith( "QODBC" ) || db.driverName().startsWith( "QOCI" ) ) {
       
   313             res += db.databaseName();
       
   314         } else {
       
   315             res += db.hostName();
       
   316         }
       
   317 
       
   318         if ( db.port() > 0 ) {
       
   319             res += ":" + QString::number( db.port() );
       
   320         }
       
   321 
       
   322         return res;
       
   323     }
       
   324 
       
   325     // drop a table only if it exists to prevent warnings
       
   326     static void safeDropTables( QSqlDatabase db, const QStringList& tableNames )
       
   327     {
       
   328         bool wasDropped;
       
   329         QSqlQuery q( db );
       
   330         QStringList dbtables=db.tables();
       
   331 
       
   332         foreach(const QString &tableName, tableNames)
       
   333         {
       
   334             wasDropped = true;
       
   335             QString table=tableName;
       
   336             if ( db.driver()->isIdentifierEscaped(table, QSqlDriver::TableName))
       
   337                 table = db.driver()->stripDelimiters(table, QSqlDriver::TableName);
       
   338 
       
   339             if ( dbtables.contains( table, Qt::CaseInsensitive ) ) {
       
   340                 foreach(const QString &table2, dbtables.filter(table, Qt::CaseInsensitive)) {
       
   341                     if(table2.compare(table.section('.', -1, -1), Qt::CaseInsensitive) == 0) {
       
   342                         table=db.driver()->escapeIdentifier(table2, QSqlDriver::TableName);
       
   343                         if(isPostgreSQL(db))
       
   344                             wasDropped = q.exec( "drop table " + table + " cascade");
       
   345                         else
       
   346                             wasDropped = q.exec( "drop table " + table);
       
   347                         dbtables.removeAll(table2);
       
   348                     }
       
   349                 }
       
   350             }
       
   351             if ( !wasDropped ) {
       
   352                 qWarning() << dbToString(db) << "unable to drop table" << tableName << ':' << q.lastError();
       
   353 //              qWarning() << "last query:" << q.lastQuery();
       
   354 //              qWarning() << "dbtables:" << dbtables;
       
   355 //              qWarning() << "db.tables():" << db.tables();
       
   356             }
       
   357         }
       
   358     }
       
   359 
       
   360     static void safeDropTable( QSqlDatabase db, const QString& tableName )
       
   361     {
       
   362         safeDropTables(db, QStringList() << tableName);
       
   363     }
       
   364 
       
   365     static void safeDropViews( QSqlDatabase db, const QStringList &viewNames )
       
   366     {
       
   367         if ( isMSAccess( db ) ) // Access is sooo stupid.
       
   368             safeDropTables( db, viewNames );
       
   369 
       
   370         bool wasDropped;
       
   371         QSqlQuery q( db );
       
   372         QStringList dbtables=db.tables(QSql::Views);
       
   373 
       
   374         foreach(QString viewName, viewNames)
       
   375         {
       
   376             wasDropped = true;
       
   377             QString view=viewName;
       
   378             if ( db.driver()->isIdentifierEscaped(view, QSqlDriver::TableName))
       
   379                 view = db.driver()->stripDelimiters(view, QSqlDriver::TableName);
       
   380 
       
   381             if ( dbtables.contains( view, Qt::CaseInsensitive ) ) {
       
   382                 foreach(const QString &view2, dbtables.filter(view, Qt::CaseInsensitive)) {
       
   383                     if(view2.compare(view.section('.', -1, -1), Qt::CaseInsensitive) == 0) {
       
   384                         view=db.driver()->escapeIdentifier(view2, QSqlDriver::TableName);
       
   385                         wasDropped = q.exec( "drop view " + view);
       
   386                         dbtables.removeAll(view);
       
   387                     }
       
   388                 }
       
   389             }
       
   390 
       
   391             if ( !wasDropped )
       
   392                 qWarning() << dbToString(db) << "unable to drop view" << viewName << ':' << q.lastError();
       
   393 //                  << "\nlast query:" << q.lastQuery()
       
   394 //                  << "\ndbtables:" << dbtables
       
   395 //                  << "\ndb.tables(QSql::Views):" << db.tables(QSql::Views);
       
   396         }
       
   397     }
       
   398 
       
   399     static void safeDropView( QSqlDatabase db, const QString& tableName )
       
   400     {
       
   401         safeDropViews(db, QStringList() << tableName);
       
   402     }
       
   403 
       
   404     // returns the type name of the blob datatype for the database db.
       
   405     // blobSize is only used if the db doesn't have a generic blob type
       
   406     static QString blobTypeName( QSqlDatabase db, int blobSize = 10000 )
       
   407     {
       
   408         if ( db.driverName().startsWith( "QMYSQL" ) )
       
   409             return "longblob";
       
   410 
       
   411         if ( db.driverName().startsWith( "QPSQL" ) )
       
   412             return "bytea";
       
   413 
       
   414         if ( db.driverName().startsWith( "QTDS" )
       
   415                 || isSqlServer( db )
       
   416                 || isMSAccess( db ) )
       
   417             return "image";
       
   418 
       
   419         if ( db.driverName().startsWith( "QDB2" ) )
       
   420             return QString( "blob(%1)" ).arg( blobSize );
       
   421 
       
   422         if ( db.driverName().startsWith( "QIBASE" ) )
       
   423             return QString( "blob sub_type 0 segment size 4096" );
       
   424 
       
   425         if ( db.driverName().startsWith( "QOCI" )
       
   426                 || db.driverName().startsWith( "QSQLITE" ) )
       
   427             return "blob";
       
   428 
       
   429         qDebug() <<  "tst_Databases::blobTypeName: Don't know the blob type for" << dbToString( db );
       
   430 
       
   431         return "blob";
       
   432     }
       
   433 
       
   434     static QString autoFieldName( QSqlDatabase db )
       
   435     {
       
   436         if ( db.driverName().startsWith( "QMYSQL" ) )
       
   437             return "AUTO_INCREMENT";
       
   438         if ( db.driverName().startsWith( "QTDS" ) )
       
   439             return "IDENTITY";
       
   440 /*        if ( db.driverName().startsWith( "QPSQL" ) )
       
   441             return "SERIAL";*/
       
   442 //        if ( db.driverName().startsWith( "QDB2" ) )
       
   443 //            return "GENERATED BY DEFAULT AS IDENTITY";
       
   444 
       
   445         return QString();
       
   446     }
       
   447 
       
   448     static QByteArray printError( const QSqlError& err )
       
   449     {
       
   450         QString result;
       
   451         if(err.number() > 0)
       
   452             result += '(' + QString::number(err.number()) + ") ";
       
   453         result += '\'';
       
   454         if(!err.driverText().isEmpty())
       
   455             result += err.driverText() + "' || '";
       
   456         result += err.databaseText() + "'";
       
   457         return result.toLocal8Bit();
       
   458     }
       
   459 
       
   460     static QByteArray printError( const QSqlError& err, const QSqlDatabase& db )
       
   461     {
       
   462         QString result(dbToString(db) + ": ");
       
   463         if(err.number() > 0)
       
   464             result += '(' + QString::number(err.number()) + ") ";
       
   465         result += '\'';
       
   466         if(!err.driverText().isEmpty())
       
   467             result += err.driverText() + "' || '";
       
   468         result += err.databaseText() + "'";
       
   469         return result.toLocal8Bit();
       
   470     }
       
   471 
       
   472     static bool isSqlServer( QSqlDatabase db )
       
   473     {
       
   474         return db.databaseName().contains( "sql server", Qt::CaseInsensitive )
       
   475                || db.databaseName().contains( "sqlserver", Qt::CaseInsensitive )
       
   476                || db.databaseName().contains( "sql native client", Qt::CaseInsensitive )
       
   477                || db.databaseName().contains( "bq-winserv", Qt::CaseInsensitive )
       
   478                || db.hostName().contains( "bq-winserv", Qt::CaseInsensitive );
       
   479     }
       
   480 
       
   481     static bool isMSAccess( QSqlDatabase db )
       
   482     {
       
   483         return db.databaseName().contains( "Access Driver", Qt::CaseInsensitive );
       
   484     }
       
   485 
       
   486     static bool isPostgreSQL( QSqlDatabase db )
       
   487     {
       
   488         return db.driverName().startsWith("QPSQL") || (db.driverName().startsWith("QODBC") && ( db.databaseName().contains("PostgreSQL", Qt::CaseInsensitive) || db.databaseName().contains("pgsql", Qt::CaseInsensitive) ) );
       
   489     }
       
   490 
       
   491     static bool isMySQL( QSqlDatabase db )
       
   492     {
       
   493         return db.driverName().startsWith("QMYSQL") || (db.driverName().startsWith("QODBC") && db.databaseName().contains("MySQL", Qt::CaseInsensitive) );
       
   494     }
       
   495     static bool isDB2( QSqlDatabase db )
       
   496     {
       
   497         return db.driverName().startsWith("QDB2") || (db.driverName().startsWith("QODBC") && db.databaseName().contains("db2", Qt::CaseInsensitive) );
       
   498     }
       
   499 
       
   500     // -1 on fail, else Oracle version
       
   501     static int getOraVersion( QSqlDatabase db )
       
   502     {
       
   503         int ver = -1;
       
   504         QSqlQuery q( "SELECT banner FROM v$version", db );
       
   505         q.next();
       
   506 
       
   507         QRegExp vers( "([0-9]+)\\.[0-9\\.]+[0-9]" );
       
   508 
       
   509         if ( vers.indexIn( q.value( 0 ).toString() ) ) {
       
   510             bool ok;
       
   511             ver = vers.cap( 1 ).toInt( &ok );
       
   512 
       
   513             if ( !ok )
       
   514                 ver = -1;
       
   515         }
       
   516 
       
   517         return ver;
       
   518     }
       
   519 
       
   520     static QString getMySqlVersion( const QSqlDatabase &db )
       
   521     {
       
   522         QSqlQuery q(db);
       
   523         q.exec( "select version()" );
       
   524         if(q.next())
       
   525             return q.value( 0 ).toString();
       
   526         else
       
   527             return QString();
       
   528     }
       
   529 
       
   530     static QString getPSQLVersion( const QSqlDatabase &db )
       
   531     {
       
   532         QSqlQuery q(db);
       
   533         q.exec( "select version()" );
       
   534         if(q.next())
       
   535             return q.value( 0 ).toString();
       
   536         else
       
   537             return QString();
       
   538     }
       
   539 
       
   540     QStringList     dbNames;
       
   541     int      counter;
       
   542 };
       
   543 
       
   544 #endif
       
   545