tests/auto/qsqlthread/tst_qsqlthread.cpp
changeset 7 f7bc934e204c
parent 0 1918ee327afb
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
     1 /****************************************************************************
     1 /****************************************************************************
     2 **
     2 **
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
     4 ** All rights reserved.
     4 ** All rights reserved.
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     6 **
     6 **
     7 ** This file is part of the test suite of the Qt Toolkit.
     7 ** This file is part of the test suite of the Qt Toolkit.
     8 **
     8 **
    51 
    51 
    52 #ifdef Q_OS_LINUX
    52 #ifdef Q_OS_LINUX
    53 #include <pthread.h>
    53 #include <pthread.h>
    54 #endif
    54 #endif
    55 
    55 
       
    56 const QString qtest(qTableName("qtest", __FILE__));
    56 // set this define if Oracle is built with threading support
    57 // set this define if Oracle is built with threading support
    57 //#define QOCI_THREADED
    58 //#define QOCI_THREADED
    58 
    59 
    59 class tst_QSqlThread : public QObject
    60 class tst_QSqlThread : public QObject
    60 {
    61 {
   113     {
   114     {
   114         QSqlDatabase db = QSqlDatabase::cloneDatabase(sourceDb, dbName);
   115         QSqlDatabase db = QSqlDatabase::cloneDatabase(sourceDb, dbName);
   115         QVERIFY_SQL(db, open());
   116         QVERIFY_SQL(db, open());
   116 
   117 
   117         int sum = 0;
   118         int sum = 0;
   118         QSqlQuery q("select id from " + qTableName("test"), db);
   119         QSqlQuery q("select id from " + qtest, db);
   119         QVERIFY_SQL(q, isActive());
   120         QVERIFY_SQL(q, isActive());
   120         while (q.next())
   121         while (q.next())
   121             sum += q.value(0).toInt();
   122             sum += q.value(0).toInt();
   122         QCOMPARE(sum, 6);
   123         QCOMPARE(sum, 6);
   123         q.clear();
   124         q.clear();
   148     void runHelper(const QString &dbName)
   149     void runHelper(const QString &dbName)
   149     {
   150     {
   150         QSqlDatabase db = QSqlDatabase::cloneDatabase(sourceDb, dbName);
   151         QSqlDatabase db = QSqlDatabase::cloneDatabase(sourceDb, dbName);
   151         QVERIFY_SQL(db, open());
   152         QVERIFY_SQL(db, open());
   152         QSqlQuery q(db);
   153         QSqlQuery q(db);
   153         QVERIFY_SQL(q, prepare("insert into " + qTableName("test") + " values (?, ?, ?)"));
   154         QVERIFY_SQL(q, prepare("insert into " + qtest + " values (?, ?, ?)"));
   154         int id = 10;
   155         int id = 10;
   155         for (int i = 0; i < ProdConIterations; ++i) {
   156         for (int i = 0; i < ProdConIterations; ++i) {
   156             q.bindValue(0, ++id);
   157             q.bindValue(0, ++id);
   157             q.bindValue(1, "threaddy");
   158             q.bindValue(1, "threaddy");
   158             q.bindValue(2, 10);
   159             q.bindValue(2, 10);
   185     void runHelper(const QString &dbName)
   186     void runHelper(const QString &dbName)
   186     {
   187     {
   187         QSqlDatabase db = QSqlDatabase::cloneDatabase(sourceDb, dbName);
   188         QSqlDatabase db = QSqlDatabase::cloneDatabase(sourceDb, dbName);
   188         QVERIFY_SQL(db, open());
   189         QVERIFY_SQL(db, open());
   189         QSqlQuery q1(db), q2(db);
   190         QSqlQuery q1(db), q2(db);
   190         QVERIFY_SQL(q2, prepare("delete from " + qTableName("test") + " where id = :id"));
   191         QVERIFY_SQL(q2, prepare("delete from " + qtest + " where id = :id"));
   191 
   192 
   192         for (int i = 0; i < ProdConIterations; ++i) {
   193         for (int i = 0; i < ProdConIterations; ++i) {
   193             QVERIFY_SQL(q1, exec("select max(id) from " + qTableName("test")));
   194             QVERIFY_SQL(q1, exec("select max(id) from " + qtest));
   194             q1.first();
   195             q1.first();
   195             q2.bindValue("id", q1.value(0));
   196             q2.bindValue("id", q1.value(0));
   196             q1.clear();
   197             q1.clear();
   197             QVERIFY_SQL(q2, exec());
   198             QVERIFY_SQL(q2, exec());
   198 #ifdef Q_OS_LINUX
   199 #ifdef Q_OS_LINUX
   229         switch (mode) {
   230         switch (mode) {
   230         case SimpleReading: {
   231         case SimpleReading: {
   231             // Executes a Query for reading, iterates over the first 4 results
   232             // Executes a Query for reading, iterates over the first 4 results
   232             QSqlQuery q(sourceDb);
   233             QSqlQuery q(sourceDb);
   233             for (int j = 0; j < ProdConIterations; ++j) {
   234             for (int j = 0; j < ProdConIterations; ++j) {
   234                 QVERIFY_SQL(q, exec("select id,name from " + qTableName("test") + " order by id"));
   235                 QVERIFY_SQL(q, exec("select id,name from " + qtest + " order by id"));
   235                 for (int i = 1; i < 4; ++i) {
   236                 for (int i = 1; i < 4; ++i) {
   236                     QVERIFY_SQL(q, next());
   237                     QVERIFY_SQL(q, next());
   237                     QCOMPARE(q.value(0).toInt(), i);
   238                     QCOMPARE(q.value(0).toInt(), i);
   238                 }
   239                 }
   239             }
   240             }
   240             break; }
   241             break; }
   241         case SimpleWriting: {
   242         case SimpleWriting: {
   242             // Executes a query for writing (appends a new row)
   243             // Executes a query for writing (appends a new row)
   243             QSqlQuery q(sourceDb);
   244             QSqlQuery q(sourceDb);
   244             for (int j = 0; j < ProdConIterations; ++j) {
   245             for (int j = 0; j < ProdConIterations; ++j) {
   245                 QVERIFY_SQL(q, exec(QString("insert into " + qTableName("test")
   246                 QVERIFY_SQL(q, exec(QString("insert into " + qtest
   246                                 + " (id, name) values(%1, '%2')")
   247                                 + " (id, name) values(%1, '%2')")
   247                                       .arg(counter.fetchAndAddRelaxed(1)).arg("Robert")));
   248                                       .arg(counter.fetchAndAddRelaxed(1)).arg("Robert")));
   248             }
   249             }
   249             break; }
   250             break; }
   250         case PreparedReading: {
   251         case PreparedReading: {
   251             // Prepares a query for reading and iterates over the results
   252             // Prepares a query for reading and iterates over the results
   252             QSqlQuery q(sourceDb);
   253             QSqlQuery q(sourceDb);
   253             QVERIFY_SQL(q, prepare("select id, name from " + qTableName("test") + " where id = ?"));
   254             QVERIFY_SQL(q, prepare("select id, name from " + qtest + " where id = ?"));
   254             for (int j = 0; j < ProdConIterations; ++j) {
   255             for (int j = 0; j < ProdConIterations; ++j) {
   255                 q.addBindValue(j % 3 + 1);
   256                 q.addBindValue(j % 3 + 1);
   256                 QVERIFY_SQL(q, exec());
   257                 QVERIFY_SQL(q, exec());
   257                 QVERIFY_SQL(q, next());
   258                 QVERIFY_SQL(q, next());
   258                 QCOMPARE(q.value(0).toInt(), j % 3 + 1);
   259                 QCOMPARE(q.value(0).toInt(), j % 3 + 1);
   259             }
   260             }
   260             break; }
   261             break; }
   261         case PreparedWriting: {
   262         case PreparedWriting: {
   262             QSqlQuery q(sourceDb);
   263             QSqlQuery q(sourceDb);
   263             QVERIFY_SQL(q, prepare("insert into " + qTableName("test") + " (id, name) "
   264             QVERIFY_SQL(q, prepare("insert into " + qtest + " (id, name) "
   264                                      "values(?, ?)"));
   265                                      "values(?, ?)"));
   265             for (int i = 0; i < ProdConIterations; ++i) {
   266             for (int i = 0; i < ProdConIterations; ++i) {
   266                 q.addBindValue(counter.fetchAndAddRelaxed(1));
   267                 q.addBindValue(counter.fetchAndAddRelaxed(1));
   267                 q.addBindValue("Robert");
   268                 q.addBindValue("Robert");
   268                 QVERIFY_SQL(q, exec());
   269                 QVERIFY_SQL(q, exec());
   300 {
   301 {
   301     for (int i = 0; i < dbs.dbNames.count(); ++i) {
   302     for (int i = 0; i < dbs.dbNames.count(); ++i) {
   302         QSqlDatabase db = QSqlDatabase::database(dbs.dbNames.at(i));
   303         QSqlDatabase db = QSqlDatabase::database(dbs.dbNames.at(i));
   303         QSqlQuery q(db);
   304         QSqlQuery q(db);
   304 
   305 
   305         tst_Databases::safeDropTables(db, QStringList() << qTableName("test") << qTableName("test2") << qTableName("emptytable"));
   306         tst_Databases::safeDropTables(db, QStringList() << qtest << qTableName("qtest2", __FILE__) << qTableName("emptytable", __FILE__));
   306     }
   307     }
   307 }
   308 }
   308 
   309 
   309 void tst_QSqlThread::createTestTables()
   310 void tst_QSqlThread::createTestTables()
   310 {
   311 {
   311     for (int i = 0; i < dbs.dbNames.count(); ++i) {
   312     for (int i = 0; i < dbs.dbNames.count(); ++i) {
   312         QSqlDatabase db = QSqlDatabase::database(dbs.dbNames.at(i));
   313         QSqlDatabase db = QSqlDatabase::database(dbs.dbNames.at(i));
   313         QSqlQuery q(db);
   314         QSqlQuery q(db);
   314 
   315 
   315         QVERIFY_SQL(q, exec("create table " + qTableName("test")
   316         QVERIFY_SQL(q, exec("create table " + qtest
   316                        + "(id int NOT NULL primary key, name varchar(20), title int)"));
   317                        + "(id int NOT NULL primary key, name varchar(20), title int)"));
   317 
   318 
   318         QVERIFY_SQL(q, exec("create table " + qTableName("test2")
   319         QVERIFY_SQL(q, exec("create table " + qTableName("qtest2", __FILE__)
   319                        + "(id int NOT NULL primary key, title varchar(20))"));
   320                        + "(id int NOT NULL primary key, title varchar(20))"));
   320 
   321 
   321         QVERIFY_SQL(q, exec("create table " + qTableName("emptytable")
   322         QVERIFY_SQL(q, exec("create table " + qTableName("emptytable", __FILE__)
   322                        + "(id int NOT NULL primary key)"));
   323                        + "(id int NOT NULL primary key)"));
   323     }
   324     }
   324 }
   325 }
   325 
   326 
   326 void tst_QSqlThread::repopulateTestTables()
   327 void tst_QSqlThread::repopulateTestTables()
   327 {
   328 {
   328     for (int i = 0; i < dbs.dbNames.count(); ++i) {
   329     for (int i = 0; i < dbs.dbNames.count(); ++i) {
   329         QSqlDatabase db = QSqlDatabase::database(dbs.dbNames.at(i));
   330         QSqlDatabase db = QSqlDatabase::database(dbs.dbNames.at(i));
   330         QSqlQuery q(db);
   331         QSqlQuery q(db);
   331 
   332 
   332         QVERIFY_SQL(q, exec("delete from " + qTableName("test")));
   333         QVERIFY_SQL(q, exec("delete from " + qtest));
   333         QVERIFY_SQL(q, exec("insert into " + qTableName("test") + " values(1, 'harry', 1)"));
   334         QVERIFY_SQL(q, exec("insert into " + qtest + " values(1, 'harry', 1)"));
   334         QVERIFY_SQL(q, exec("insert into " + qTableName("test") + " values(2, 'trond', 2)"));
   335         QVERIFY_SQL(q, exec("insert into " + qtest + " values(2, 'trond', 2)"));
   335         QVERIFY_SQL(q, exec("insert into " + qTableName("test") + " values(3, 'vohi', 3)"));
   336         QVERIFY_SQL(q, exec("insert into " + qtest + " values(3, 'vohi', 3)"));
   336 
   337 
   337         QVERIFY_SQL(q, exec("delete from " + qTableName("test2")));
   338         QVERIFY_SQL(q, exec("delete from " + qTableName("qtest2", __FILE__)));
   338         QVERIFY_SQL(q, exec("insert into " + qTableName("test2") + " values(1, 'herr')"));
   339         QVERIFY_SQL(q, exec("insert into " + qTableName("qtest2", __FILE__) + " values(1, 'herr')"));
   339         QVERIFY_SQL(q, exec("insert into " + qTableName("test2") + " values(2, 'mister')"));
   340         QVERIFY_SQL(q, exec("insert into " + qTableName("qtest2", __FILE__) + " values(2, 'mister')"));
   340     }
   341     }
   341 }
   342 }
   342 
   343 
   343 void tst_QSqlThread::recreateTestTables()
   344 void tst_QSqlThread::recreateTestTables()
   344 {
   345 {