util/tests/auto/qsqldriver/tst_qsqldriver.cpp
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 
       
    42 
       
    43 #include <QtTest/QtTest>
       
    44 #include <QtSql/QtSql>
       
    45 
       
    46 #include "../qsqldatabase/tst_databases.h"
       
    47 
       
    48 
       
    49 
       
    50 //TESTED_CLASS=
       
    51 //TESTED_FILES=
       
    52 
       
    53 class tst_QSqlDriver : public QObject
       
    54 {
       
    55     Q_OBJECT
       
    56 
       
    57 public:
       
    58     void recreateTestTables(QSqlDatabase);
       
    59 
       
    60     tst_Databases dbs;
       
    61 
       
    62 public slots:
       
    63     void initTestCase_data();
       
    64     void initTestCase();
       
    65     void cleanupTestCase();
       
    66     void init();
       
    67     void cleanup();
       
    68 
       
    69 private slots:
       
    70     void record();
       
    71     void primaryIndex();
       
    72 };
       
    73 
       
    74 
       
    75 void tst_QSqlDriver::initTestCase_data()
       
    76 {
       
    77     dbs.open();
       
    78     if (dbs.fillTestTable() == 0) {
       
    79         qWarning("NO DATABASES");
       
    80         QSKIP("No database drivers are available in this Qt configuration", SkipAll);
       
    81     }
       
    82 }
       
    83 
       
    84 void tst_QSqlDriver::recreateTestTables(QSqlDatabase db)
       
    85 {
       
    86     QSqlQuery q(db);
       
    87     const QString relTEST1(qTableName("relTEST1", __FILE__));
       
    88 
       
    89     if(tst_Databases::isPostgreSQL(db))
       
    90         QVERIFY_SQL( q, exec("set client_min_messages='warning'"));
       
    91 
       
    92     tst_Databases::safeDropTable( db, relTEST1 );
       
    93 
       
    94     QVERIFY_SQL( q, exec("create table " + relTEST1 +
       
    95             " (id int not null primary key, name varchar(20), title_key int, another_title_key int)"));
       
    96     QVERIFY_SQL( q, exec("insert into " + relTEST1 + " values(1, 'harry', 1, 2)"));
       
    97     QVERIFY_SQL( q, exec("insert into " + relTEST1 + " values(2, 'trond', 2, 1)"));
       
    98     QVERIFY_SQL( q, exec("insert into " + relTEST1 + " values(3, 'vohi', 1, 2)"));
       
    99     QVERIFY_SQL( q, exec("insert into " + relTEST1 + " values(4, 'boris', 2, 2)"));
       
   100 }
       
   101 
       
   102 void tst_QSqlDriver::initTestCase()
       
   103 {
       
   104     foreach (const QString &dbname, dbs.dbNames)
       
   105         recreateTestTables(QSqlDatabase::database(dbname));
       
   106 }
       
   107 
       
   108 void tst_QSqlDriver::cleanupTestCase()
       
   109 {
       
   110     foreach (const QString &dbName, dbs.dbNames) {
       
   111         QSqlDatabase db = QSqlDatabase::database(dbName);
       
   112         tst_Databases::safeDropTable( db, qTableName( "relTEST1", __FILE__ ) );
       
   113     }
       
   114     dbs.close();
       
   115 }
       
   116 
       
   117 void tst_QSqlDriver::init()
       
   118 {
       
   119 }
       
   120 
       
   121 void tst_QSqlDriver::cleanup()
       
   122 {
       
   123 }
       
   124 
       
   125 void tst_QSqlDriver::record()
       
   126 {
       
   127     QFETCH_GLOBAL(QString, dbName);
       
   128     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   129     CHECK_DATABASE(db);
       
   130 
       
   131     QString tablename(qTableName("relTEST1", __FILE__));
       
   132     QStringList fields;
       
   133     fields << "id" << "name" << "title_key" << "another_title_key";
       
   134 
       
   135     //check we can get records using an unquoted mixed case table name
       
   136     QSqlRecord rec = db.driver()->record(tablename);
       
   137     QCOMPARE(rec.count(), 4);
       
   138 
       
   139     if (db.driverName().startsWith("QIBASE")|| db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2"))
       
   140         for(int i = 0; i < fields.count(); ++i)
       
   141             fields[i] = fields[i].toUpper();
       
   142 
       
   143     for (int i = 0; i < fields.count(); ++i)
       
   144         QCOMPARE(rec.fieldName(i), fields[i]);
       
   145 
       
   146     if (db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2"))
       
   147         tablename = tablename.toUpper();
       
   148     else if (db.driverName().startsWith("QPSQL"))
       
   149         tablename = tablename.toLower();
       
   150 
       
   151     if(!db.driverName().startsWith("QODBC") && !db.databaseName().contains("PostgreSql")) {
       
   152         //check we can get records using a properly quoted table name
       
   153         rec = db.driver()->record(db.driver()->escapeIdentifier(tablename,QSqlDriver::TableName));
       
   154         QCOMPARE(rec.count(), 4);
       
   155     }
       
   156 
       
   157     for (int i = 0; i < fields.count(); ++i)
       
   158         QCOMPARE(rec.fieldName(i), fields[i]);
       
   159 
       
   160     if( db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2"))
       
   161         tablename = tablename.toLower();
       
   162     else if (db.driverName().startsWith("QPSQL"))
       
   163         tablename = tablename.toUpper();
       
   164 
       
   165     //check that we can't get records using incorrect tablename casing that's been quoted
       
   166     rec = db.driver()->record(db.driver()->escapeIdentifier(tablename,QSqlDriver::TableName));
       
   167     if (tst_Databases::isMySQL(db)
       
   168       || db.driverName().startsWith("QSQLITE")
       
   169       || db.driverName().startsWith("QTDS")
       
   170       || tst_Databases::isSqlServer(db)
       
   171       || tst_Databases::isMSAccess(db))
       
   172         QCOMPARE(rec.count(), 4); //mysql, sqlite and tds will match
       
   173     else
       
   174         QCOMPARE(rec.count(), 0);
       
   175 
       
   176 }
       
   177 
       
   178 void tst_QSqlDriver::primaryIndex()
       
   179 {
       
   180     QFETCH_GLOBAL(QString, dbName);
       
   181     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   182     CHECK_DATABASE(db);
       
   183 
       
   184     QString tablename(qTableName("relTEST1", __FILE__));
       
   185     //check that we can get primary index using unquoted mixed case table name
       
   186     QSqlIndex index = db.driver()->primaryIndex(tablename);
       
   187     QCOMPARE(index.count(), 1);
       
   188 
       
   189     if( db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2"))
       
   190         QCOMPARE(index.fieldName(0), QString::fromLatin1("ID"));
       
   191     else
       
   192         QCOMPARE(index.fieldName(0), QString::fromLatin1("id"));
       
   193 
       
   194 
       
   195     //check that we can get the primary index using a quoted tablename
       
   196     if( db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2"))
       
   197         tablename = tablename.toUpper();
       
   198     else if (db.driverName().startsWith("QPSQL"))
       
   199         tablename = tablename.toLower();
       
   200 
       
   201     if(!db.driverName().startsWith("QODBC") && !db.databaseName().contains("PostgreSql")) {
       
   202         index = db.driver()->primaryIndex(db.driver()->escapeIdentifier(tablename, QSqlDriver::TableName));
       
   203         QCOMPARE(index.count(), 1);
       
   204     }
       
   205     if( db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2"))
       
   206         QCOMPARE(index.fieldName(0), QString::fromLatin1("ID"));
       
   207     else
       
   208         QCOMPARE(index.fieldName(0), QString::fromLatin1("id"));
       
   209 
       
   210 
       
   211 
       
   212     //check that we can not get the primary index using a quoted but incorrect table name casing
       
   213     if( db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2"))
       
   214         tablename = tablename.toLower();
       
   215     else if (db.driverName().startsWith("QPSQL"))
       
   216         tablename = tablename.toUpper();
       
   217 
       
   218     index = db.driver()->primaryIndex(db.driver()->escapeIdentifier(tablename, QSqlDriver::TableName));
       
   219     if (tst_Databases::isMySQL(db)
       
   220       || db.driverName().startsWith("QSQLITE")
       
   221       || db.driverName().startsWith("QTDS")
       
   222       || tst_Databases::isSqlServer(db)
       
   223       || tst_Databases::isMSAccess(db))
       
   224         QCOMPARE(index.count(), 1); //mysql will always find the table name regardless of casing
       
   225     else
       
   226         QCOMPARE(index.count(), 0);
       
   227 }
       
   228 
       
   229 QTEST_MAIN(tst_QSqlDriver)
       
   230 #include "tst_qsqldriver.moc"