1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtTest/QtTest>
#include <QtSql/QtSql>
#include "../../../../auto/sql/kernel/qsqldatabase/tst_databases.h"
const QString qtest(qTableName("qtest", __FILE__, QSqlDatabase()));
class tst_QSqlRecord : public QObject
{
Q_OBJECT
public:
tst_QSqlRecord();
virtual ~tst_QSqlRecord();
public slots:
void initTestCase();
void cleanupTestCase();
void init();
void cleanup();
private slots:
void benchmarkRecord_data() { generic_data(); }
void benchmarkRecord();
void benchFieldName_data() { generic_data(); }
void benchFieldName();
void benchFieldIndex_data() { generic_data(); }
void benchFieldIndex();
private:
void generic_data(const QString &engine = QString());
void dropTestTables(QSqlDatabase db);
void createTestTables(QSqlDatabase db);
void populateTestTables(QSqlDatabase db);
tst_Databases dbs;
};
QTEST_MAIN(tst_QSqlRecord)
tst_QSqlRecord::tst_QSqlRecord()
{
}
tst_QSqlRecord::~tst_QSqlRecord()
{
}
void tst_QSqlRecord::initTestCase()
{
dbs.open();
for (const auto &dbName : qAsConst(dbs.dbNames)) {
QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db);
dropTestTables(db); // In case of leftovers
createTestTables(db);
populateTestTables(db);
}
}
void tst_QSqlRecord::cleanupTestCase()
{
for (const auto &dbName : qAsConst(dbs.dbNames)) {
QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db);
dropTestTables(db);
}
dbs.close();
}
void tst_QSqlRecord::init()
{
}
void tst_QSqlRecord::cleanup()
{
QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db);
const QSqlDriver::DbmsType dbType = tst_Databases::getDatabaseType(db);
if (QTest::currentTestFailed() && (dbType == QSqlDriver::Oracle ||
db.driverName().startsWith("QODBC"))) {
// Since Oracle ODBC has a problem when encountering an error, we init again
db.close();
db.open();
}
}
void tst_QSqlRecord::generic_data(const QString &engine)
{
if (dbs.fillTestTable(engine) == 0) {
if (engine.isEmpty())
QSKIP("No database drivers are available in this Qt configuration");
else
QSKIP(QString("No database drivers of type %1 are available in this Qt configuration").arg(engine).toLocal8Bit());
}
}
void tst_QSqlRecord::dropTestTables(QSqlDatabase db)
{
QSqlDriver::DbmsType dbType = tst_Databases::getDatabaseType(db);
QStringList tablenames;
// drop all the tables in case a testcase failed
tablenames << qtest
<< qTableName("record", __FILE__, db);
tst_Databases::safeDropTables(db, tablenames);
if (dbType == QSqlDriver::Oracle) {
QSqlQuery q(db);
q.exec("DROP PACKAGE " + qTableName("pkg", __FILE__, db));
}
}
void tst_QSqlRecord::createTestTables(QSqlDatabase db)
{
QSqlQuery q(db);
switch (tst_Databases::getDatabaseType(db)) {
case QSqlDriver::PostgreSQL:
QVERIFY_SQL(q, exec("set client_min_messages='warning'"));
QVERIFY_SQL(q, exec("create table " + qtest + " (id serial NOT NULL, t_varchar varchar(20), "
"t_char char(20), primary key(id)) WITH OIDS"));
break;
case QSqlDriver::MySqlServer:
QVERIFY_SQL(q, exec("set table_type=innodb"));
Q_FALLTHROUGH();
default:
QVERIFY_SQL(q, exec("create table " + qtest + " (id int " + tst_Databases::autoFieldName(db) +
" NOT NULL, t_varchar varchar(20), t_char char(20), primary key(id))"));
break;
}
}
void tst_QSqlRecord::populateTestTables(QSqlDatabase db)
{
QSqlQuery q(db);
QVERIFY_SQL(q, exec("delete from " + qtest));
QVERIFY_SQL(q, exec("insert into " + qtest + " values (1, 'VarChar1', 'Char1')"));
QVERIFY_SQL(q, exec("insert into " + qtest + " values (2, 'VarChar2', 'Char2')"));
QVERIFY_SQL(q, exec("insert into " + qtest + " values (3, 'VarChar3', 'Char3')"));
QVERIFY_SQL(q, exec("insert into " + qtest + " values (4, 'VarChar4', 'Char4')"));
QVERIFY_SQL(q, exec("insert into " + qtest + " values (5, 'VarChar5', 'Char5')"));
}
void tst_QSqlRecord::benchmarkRecord()
{
QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db);
const auto tableName = qTableName("record", __FILE__, db);
{
QSqlQuery qry(db);
QVERIFY_SQL(qry, exec("create table " + tableName +
" (id int NOT NULL, t_varchar varchar(20), "
"t_char char(20), primary key(id))"));
// Limit to 500: at 600, the set-up takes nearly 5 minutes
for (int i = 0; i < 500; i++)
QVERIFY_SQL(qry, exec(QString("INSERT INTO " + tableName +
" VALUES (%1, 'VarChar%1', 'Char%1')").arg(i)));
QVERIFY_SQL(qry, exec(QString("SELECT * from ") + tableName));
QBENCHMARK {
while (qry.next())
qry.record();
QVERIFY(qry.seek(0));
}
}
tst_Databases::safeDropTables(db, QStringList() << tableName);
}
void tst_QSqlRecord::benchFieldName()
{
QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName);
if (tst_Databases::getDatabaseType(db) == QSqlDriver::PostgreSQL) {
QSqlQuery qry(db);
QVERIFY_SQL(qry, exec("SELECT GENERATE_SERIES(1,5000) AS r"));
QBENCHMARK {
while (qry.next())
qry.value("r");
QVERIFY(qry.seek(0));
}
}
}
void tst_QSqlRecord::benchFieldIndex()
{
QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName);
if (tst_Databases::getDatabaseType(db) == QSqlDriver::PostgreSQL) {
QSqlQuery qry(db);
QVERIFY_SQL(qry, exec("SELECT GENERATE_SERIES(1,5000) AS r"));
qry = db.exec("SELECT GENERATE_SERIES(1,5000) AS r");
QBENCHMARK {
while (qry.next())
qry.value(0);
QVERIFY(qry.seek(0));
}
}
}
#include "tst_qsqlrecord.moc"
|