SQL/Tests: use TableScope where possible

Use TableScope helper class to make sure the table used for the test is
really cleaned up before usage.

Change-Id: I45fffcd13acae6032636ae07097b14af174ede21
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Christian Ehrlicher 2023-03-17 22:45:18 +01:00
parent b40df32ea3
commit 40045aeec8
4 changed files with 242 additions and 319 deletions

View File

@ -175,13 +175,13 @@ private slots:
void sqlite_check_json1(); void sqlite_check_json1();
private: private:
void createTestTables(QSqlDatabase db); void createTestTables(const QSqlDatabase &db);
void dropTestTables(QSqlDatabase db); void dropTestTables(const QSqlDatabase &db);
void populateTestTables(QSqlDatabase db); void populateTestTables(const QSqlDatabase &db);
void generic_data(const QString &engine=QString()); void generic_data(const QString &engine=QString());
void testRecord(const FieldDef fieldDefs[], const QSqlRecord& inf, QSqlDatabase db); void testRecord(const FieldDef fieldDefs[], const QSqlRecord& inf, const QSqlDatabase &db);
void commonFieldTest(const FieldDef fieldDefs[], QSqlDatabase, const int); void commonFieldTest(const FieldDef fieldDefs[], const QSqlDatabase &db, const int fieldCount);
tst_Databases dbs; tst_Databases dbs;
}; };
@ -216,7 +216,7 @@ struct FieldDef {
// creates a table out of the FieldDefs and returns the number of fields // creates a table out of the FieldDefs and returns the number of fields
// excluding the primary key field // excluding the primary key field
static int createFieldTable(const FieldDef fieldDefs[], QSqlDatabase db) static int createFieldTable(const FieldDef fieldDefs[], const QSqlDatabase &db)
{ {
QSqlDriver::DbmsType dbType = tst_Databases::getDatabaseType(db); QSqlDriver::DbmsType dbType = tst_Databases::getDatabaseType(db);
const QString tableName = qTableName("qtestfields", __FILE__, db); const QString tableName = qTableName("qtestfields", __FILE__, db);
@ -251,7 +251,7 @@ static int createFieldTable(const FieldDef fieldDefs[], QSqlDatabase db)
return i; return i;
} }
void tst_QSqlDatabase::createTestTables(QSqlDatabase db) void tst_QSqlDatabase::createTestTables(const QSqlDatabase &db)
{ {
if (!db.isValid()) if (!db.isValid())
return; return;
@ -285,7 +285,7 @@ void tst_QSqlDatabase::createTestTables(QSqlDatabase db)
QVERIFY_SQL(q, exec(qry)); QVERIFY_SQL(q, exec(qry));
} }
void tst_QSqlDatabase::dropTestTables(QSqlDatabase db) void tst_QSqlDatabase::dropTestTables(const QSqlDatabase &db)
{ {
if (!db.isValid()) if (!db.isValid())
return; return;
@ -297,30 +297,11 @@ void tst_QSqlDatabase::dropTestTables(QSqlDatabase db)
} }
// drop the view first, otherwise we'll get dependency problems // drop the view first, otherwise we'll get dependency problems
tst_Databases::safeDropViews(db, QStringList() << qTableName("qtest_view", __FILE__, db) << qTableName("qtest_view2", __FILE__, db)); tst_Databases::safeDropViews(db, {qTableName("qtest_view", __FILE__, db),
const QString qtestTable = qTableName("qtest", __FILE__, db); qTableName("qtest_view2", __FILE__, db)});
QStringList tableNames; QStringList tableNames = {qTableName("qtest", __FILE__, db),
tableNames << qtestTable qTableName("qtest test", __FILE__, db),
<< qTableName("qtest test", __FILE__, db) qTableName("qtestfields", __FILE__, db)};
<< qTableName("qtestfields", __FILE__, db)
<< qTableName("qtestalter", __FILE__, db)
<< qTableName("qtest_temp", __FILE__, db)
<< qTableName("qtest_xmltype", __FILE__, db)
<< qTableName("latin1table", __FILE__, db)
<< qTableName("qtest_sqlguid", __FILE__, db)
<< qTableName("batable", __FILE__, db)
<< qTableName("qtest_prec", __FILE__, db)
<< qTableName("uint", __FILE__, db)
<< qTableName("strings", __FILE__, db)
<< qTableName("numericfields", __FILE__, db)
<< qTableName("qtest_ibaseblobs", __FILE__, db)
<< qTableName("qtestBindBool", __FILE__, db)
<< qTableName("testqGetString", __FILE__, db)
<< qTableName("qtest_sqlguid", __FILE__, db)
<< qTableName("uint_table", __FILE__, db)
<< qTableName("uint_test", __FILE__, db)
<< qTableName("bug_249059", __FILE__, db)
<< qTableName("regexp_test", __FILE__, db);
QSqlQuery q(0, db); QSqlQuery q(0, db);
if (dbType == QSqlDriver::PostgreSQL) { if (dbType == QSqlDriver::PostgreSQL) {
@ -340,7 +321,7 @@ void tst_QSqlDatabase::dropTestTables(QSqlDatabase db)
} }
} }
void tst_QSqlDatabase::populateTestTables(QSqlDatabase db) void tst_QSqlDatabase::populateTestTables(const QSqlDatabase &db)
{ {
if (!db.isValid()) if (!db.isValid())
return; return;
@ -526,11 +507,11 @@ void tst_QSqlDatabase::alterTable()
QFETCH(QString, dbName); QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName); QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db); CHECK_DATABASE(db);
const QString qtestalter(qTableName("qtestalter", __FILE__, db)); TableScope ts(db, "qtestalter", __FILE__);
QSqlQuery q(db); QSqlQuery q(db);
QVERIFY_SQL(q, exec("create table " + qtestalter + " (F1 char(20), F2 char(20), F3 char(20))")); QVERIFY_SQL(q, exec("create table " + ts.tableName() + " (F1 char(20), F2 char(20), F3 char(20))"));
QSqlRecord rec = db.record(qtestalter); QSqlRecord rec = db.record(ts.tableName());
QCOMPARE((int)rec.count(), 3); QCOMPARE((int)rec.count(), 3);
int i; int i;
@ -538,18 +519,18 @@ void tst_QSqlDatabase::alterTable()
QCOMPARE(rec.field(i).name().toUpper(), QString("F%1").arg(i + 1)); QCOMPARE(rec.field(i).name().toUpper(), QString("F%1").arg(i + 1));
} }
if (!q.exec("alter table " + qtestalter + " drop column F2")) { if (!q.exec("alter table " + ts.tableName() + " drop column F2")) {
QSKIP("DBMS doesn't support dropping columns in ALTER TABLE statement"); QSKIP("DBMS doesn't support dropping columns in ALTER TABLE statement");
} }
rec = db.record(qtestalter); rec = db.record(ts.tableName());
QCOMPARE(rec.count(), 2); QCOMPARE(rec.count(), 2);
QCOMPARE(rec.field(0).name().toUpper(), QString("F1")); QCOMPARE(rec.field(0).name().toUpper(), QString("F1"));
QCOMPARE(rec.field(1).name().toUpper(), QString("F3")); QCOMPARE(rec.field(1).name().toUpper(), QString("F3"));
q.exec("select * from " + qtestalter); q.exec("select * from " + ts.tableName());
} }
#if 0 #if 0
@ -577,12 +558,11 @@ void tst_QSqlDatabase::record()
} }
#endif #endif
void tst_QSqlDatabase::testRecord(const FieldDef fieldDefs[], const QSqlRecord& inf, QSqlDatabase db) void tst_QSqlDatabase::testRecord(const FieldDef fieldDefs[], const QSqlRecord &inf, const QSqlDatabase &db)
{ {
int i = 0;
if (!tst_Databases::autoFieldName(db).isEmpty()) // Currently only MySQL is tested if (!tst_Databases::autoFieldName(db).isEmpty()) // Currently only MySQL is tested
QVERIFY2(inf.field(i).isAutoValue(), qPrintable(inf.field(i).name() + " should be reporting as an autovalue")); QVERIFY2(inf.field(0).isAutoValue(), qPrintable(inf.field(0).name() + " should be reporting as an autovalue"));
for (i = 0; !fieldDefs[ i ].typeName.isNull(); ++i) { for (int i = 0; !fieldDefs[ i ].typeName.isNull(); ++i) {
QCOMPARE(inf.field(i+1).name().toUpper(), fieldDefs[ i ].fieldName().toUpper()); QCOMPARE(inf.field(i+1).name().toUpper(), fieldDefs[ i ].fieldName().toUpper());
if (inf.field(i+1).metaType().id() != fieldDefs[ i ].type) { if (inf.field(i+1).metaType().id() != fieldDefs[ i ].type) {
QFAIL(qPrintable(QString(" Expected: '%1' Received: '%2' for field %3 in testRecord").arg( QFAIL(qPrintable(QString(" Expected: '%1' Received: '%2' for field %3 in testRecord").arg(
@ -597,7 +577,7 @@ void tst_QSqlDatabase::testRecord(const FieldDef fieldDefs[], const QSqlRecord&
} }
// non-dbms specific tests // non-dbms specific tests
void tst_QSqlDatabase::commonFieldTest(const FieldDef fieldDefs[], QSqlDatabase db, const int fieldCount) void tst_QSqlDatabase::commonFieldTest(const FieldDef fieldDefs[], const QSqlDatabase &db, const int fieldCount)
{ {
CHECK_DATABASE(db); CHECK_DATABASE(db);
@ -1267,24 +1247,24 @@ void tst_QSqlDatabase::psql_escapeBytea()
QFETCH(QString, dbName); QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName); QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db); CHECK_DATABASE(db);
TableScope ts(db, "batable", __FILE__);
const char dta[4] = {'\x71', '\x14', '\x32', '\x81'}; const char dta[4] = {'\x71', '\x14', '\x32', '\x81'};
QByteArray ba(dta, 4); QByteArray ba(dta, 4);
QSqlQuery q(db); QSqlQuery q(db);
const QString tableName(qTableName("batable", __FILE__, db)); QVERIFY_SQL(q, exec(QString("CREATE TABLE %1 (ba bytea)").arg(ts.tableName())));
QVERIFY_SQL(q, exec(QString("CREATE TABLE %1 (ba bytea)").arg(tableName)));
QSqlQuery iq(db); QSqlQuery iq(db);
QVERIFY_SQL(iq, prepare(QString("INSERT INTO %1 VALUES (?)").arg(tableName))); QVERIFY_SQL(iq, prepare(QString("INSERT INTO %1 VALUES (?)").arg(ts.tableName())));
iq.bindValue(0, QVariant(ba)); iq.bindValue(0, QVariant(ba));
QVERIFY_SQL(iq, exec()); QVERIFY_SQL(iq, exec());
QVERIFY_SQL(q, exec(QString("SELECT ba FROM %1").arg(tableName))); QVERIFY_SQL(q, exec(QString("SELECT ba FROM %1").arg(ts.tableName())));
QVERIFY_SQL(q, next()); QVERIFY_SQL(q, next());
QByteArray res = q.value(0).toByteArray(); QByteArray res = q.value(0).toByteArray();
int i = 0; qsizetype i = 0;
for (; i < ba.size(); ++i){ for (; i < ba.size(); ++i){
if (ba[i] != res[i]) if (ba[i] != res[i])
break; break;
@ -1298,13 +1278,13 @@ void tst_QSqlDatabase::psql_bug249059()
QFETCH(QString, dbName); QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName); QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db); CHECK_DATABASE(db);
TableScope ts(db, "bug_249059", __FILE__);
QSqlQuery q(db); QSqlQuery q(db);
const QString tableName(qTableName("bug_249059", __FILE__, db)); QVERIFY_SQL(q, exec(QString("CREATE TABLE %1 (dt timestamp, t time)").arg(ts.tableName())));
QVERIFY_SQL(q, exec(QString("CREATE TABLE %1 (dt timestamp, t time)").arg(tableName)));
QSqlQuery iq(db); QSqlQuery iq(db);
QVERIFY_SQL(iq, prepare(QString("INSERT INTO %1 VALUES (?, ?)").arg(tableName))); QVERIFY_SQL(iq, prepare(QString("INSERT INTO %1 VALUES (?, ?)").arg(ts.tableName())));
iq.bindValue(0, QVariant(QString("2001-09-09 04:05:06.789 -5:00"))); iq.bindValue(0, QVariant(QString("2001-09-09 04:05:06.789 -5:00")));
iq.bindValue(1, QVariant(QString("04:05:06.789 -5:00"))); iq.bindValue(1, QVariant(QString("04:05:06.789 -5:00")));
QVERIFY_SQL(iq, exec()); QVERIFY_SQL(iq, exec());
@ -1312,7 +1292,7 @@ void tst_QSqlDatabase::psql_bug249059()
iq.bindValue(1, QVariant(QString("04:05:06.789 +5:00"))); iq.bindValue(1, QVariant(QString("04:05:06.789 +5:00")));
QVERIFY_SQL(iq, exec()); QVERIFY_SQL(iq, exec());
QVERIFY_SQL(q, exec(QString("SELECT dt, t FROM %1").arg(tableName))); QVERIFY_SQL(q, exec(QString("SELECT dt, t FROM %1").arg(ts.tableName())));
QVERIFY_SQL(q, next()); QVERIFY_SQL(q, next());
QDateTime dt1=q.value(0).toDateTime(); QDateTime dt1=q.value(0).toDateTime();
QTime t1=q.value(1).toTime(); QTime t1=q.value(1).toTime();
@ -1325,17 +1305,15 @@ void tst_QSqlDatabase::psql_bug249059()
QCOMPARE(t1, t2); QCOMPARE(t1, t2);
} }
// This test should be rewritten to work with Oracle as well - or the Oracle driver
// should be fixed to make this test pass (handle overflows)
void tst_QSqlDatabase::precisionPolicy() void tst_QSqlDatabase::precisionPolicy()
{ {
QFETCH(QString, dbName); QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName); QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db); CHECK_DATABASE(db);
// DBMS_SPECIFIC(db, "QPSQL"); TableScope ts(db, "qtest_prec", __FILE__);
const auto &tableName = ts.tableName();
QSqlQuery q(db); QSqlQuery q(db);
const QString tableName(qTableName("qtest_prec", __FILE__, db));
if(!db.driver()->hasFeature(QSqlDriver::LowPrecisionNumbers)) if(!db.driver()->hasFeature(QSqlDriver::LowPrecisionNumbers))
QSKIP("Driver or database doesn't support setting precision policy"); QSKIP("Driver or database doesn't support setting precision policy");
@ -1466,14 +1444,14 @@ void tst_QSqlDatabase::mysqlOdbc_unsignedIntegers()
if (tst_Databases::getDatabaseType(db) != QSqlDriver::MySqlServer || !db.driverName().startsWith("QODBC")) if (tst_Databases::getDatabaseType(db) != QSqlDriver::MySqlServer || !db.driverName().startsWith("QODBC"))
QSKIP("MySQL through ODBC-driver specific test"); QSKIP("MySQL through ODBC-driver specific test");
TableScope ts(db, "uint", __FILE__);
QSqlQuery q(db); QSqlQuery q(db);
const QString tableName(qTableName("uint", __FILE__, db)); QVERIFY_SQL(q, exec(QString("CREATE TABLE %1 (foo integer(10) unsigned, bar integer(10))").arg(ts.tableName())));
QVERIFY_SQL(q, exec(QString("CREATE TABLE %1 (foo integer(10) unsigned, bar integer(10))").arg(tableName))); QVERIFY_SQL(q, exec(QString("INSERT INTO %1 VALUES (-4000000000, -4000000000)").arg(ts.tableName())));
QVERIFY_SQL(q, exec(QString("INSERT INTO %1 VALUES (-4000000000, -4000000000)").arg(tableName))); QVERIFY_SQL(q, exec(QString("INSERT INTO %1 VALUES (4000000000, 4000000000)").arg(ts.tableName())));
QVERIFY_SQL(q, exec(QString("INSERT INTO %1 VALUES (4000000000, 4000000000)").arg(tableName)));
QVERIFY_SQL(q, exec(QString("SELECT foo, bar FROM %1").arg(tableName))); QVERIFY_SQL(q, exec(QString("SELECT foo, bar FROM %1").arg(ts.tableName())));
QVERIFY(q.next()); QVERIFY(q.next());
QCOMPARE(q.value(0).toString(), QString("0")); QCOMPARE(q.value(0).toString(), QString("0"));
QCOMPARE(q.value(1).toString(), QString("-2147483648")); QCOMPARE(q.value(1).toString(), QString("-2147483648"));
@ -1490,13 +1468,13 @@ void tst_QSqlDatabase::accessOdbc_strings()
if (!tst_Databases::isMSAccess(db)) if (!tst_Databases::isMSAccess(db))
QSKIP("MS Access specific test"); QSKIP("MS Access specific test");
TableScope ts(db, "strings", __FILE__);
QSqlQuery q(db); QSqlQuery q(db);
const QString tableName(qTableName("strings", __FILE__, db));
QVERIFY_SQL(q, exec(QString("CREATE TABLE %1 (aStr memo, bStr memo, cStr memo, dStr memo" QVERIFY_SQL(q, exec(QString("CREATE TABLE %1 (aStr memo, bStr memo, cStr memo, dStr memo"
", eStr memo, fStr memo, gStr memo, hStr memo)").arg(tableName))); ", eStr memo, fStr memo, gStr memo, hStr memo)").arg(ts.tableName())));
QVERIFY_SQL(q, prepare(QString("INSERT INTO %1 VALUES (?, ?, ?, ?, ?, ?, ?, ?)").arg(tableName))); QVERIFY_SQL(q, prepare(QString("INSERT INTO %1 VALUES (?, ?, ?, ?, ?, ?, ?, ?)").arg(ts.tableName())));
QString aStr, bStr, cStr, dStr, eStr, fStr, gStr, hStr; QString aStr, bStr, cStr, dStr, eStr, fStr, gStr, hStr;
q.bindValue(0, aStr.fill('A', 32)); q.bindValue(0, aStr.fill('A', 32));
@ -1510,7 +1488,7 @@ void tst_QSqlDatabase::accessOdbc_strings()
QVERIFY_SQL(q, exec()); QVERIFY_SQL(q, exec());
QVERIFY_SQL(q, exec(QString("SELECT aStr, bStr, cStr, dStr, eStr, fStr, gStr, hStr FROM %1").arg(tableName))); QVERIFY_SQL(q, exec(QString("SELECT aStr, bStr, cStr, dStr, eStr, fStr, gStr, hStr FROM %1").arg(ts.tableName())));
q.next(); q.next();
QCOMPARE(q.value(0).toString(), aStr); QCOMPARE(q.value(0).toString(), aStr);
QCOMPARE(q.value(1).toString(), bStr); QCOMPARE(q.value(1).toString(), bStr);
@ -1528,9 +1506,10 @@ void tst_QSqlDatabase::ibase_numericFields()
QFETCH(QString, dbName); QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName); QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db); CHECK_DATABASE(db);
TableScope ts(db, "numericfields", __FILE__);
const auto &tableName = ts.tableName();
QSqlQuery q(db); QSqlQuery q(db);
const QString tableName(qTableName("numericfields", __FILE__, db));
QVERIFY_SQL(q, exec(QString("CREATE TABLE %1 (id int not null, num1 NUMERIC(2,1), " QVERIFY_SQL(q, exec(QString("CREATE TABLE %1 (id int not null, num1 NUMERIC(2,1), "
"num2 NUMERIC(5,2), num3 NUMERIC(10,3), " "num2 NUMERIC(5,2), num3 NUMERIC(10,3), "
"num4 NUMERIC(18,4))").arg(tableName))); "num4 NUMERIC(18,4))").arg(tableName)));
@ -1601,8 +1580,9 @@ void tst_QSqlDatabase::ibase_fetchBlobs()
QFETCH(QString, dbName); QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName); QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db); CHECK_DATABASE(db);
TableScope ts(db, "qtest_ibaseblobs", __FILE__);
const auto &tableName = ts.tableName();
const QString tableName(qTableName("qtest_ibaseblobs", __FILE__, db));
QSqlQuery q(db); QSqlQuery q(db);
QVERIFY_SQL(q, exec(QString("CREATE TABLE %1 (blob1 BLOB segment size 256)").arg(tableName))); QVERIFY_SQL(q, exec(QString("CREATE TABLE %1 (blob1 BLOB segment size 256)").arg(tableName)));
@ -1736,13 +1716,13 @@ void tst_QSqlDatabase::odbc_bindBoolean()
QSqlDriver::DbmsType dbType = tst_Databases::getDatabaseType(db); QSqlDriver::DbmsType dbType = tst_Databases::getDatabaseType(db);
if (dbType == QSqlDriver::MySqlServer) if (dbType == QSqlDriver::MySqlServer)
QSKIP("MySql has inconsistent behaviour of bit field type across versions."); QSKIP("MySql has inconsistent behaviour of bit field type across versions.");
TableScope ts(db, "qtestBindBool", __FILE__);
QSqlQuery q(db); QSqlQuery q(db);
const QString tableName = qTableName("qtestBindBool", __FILE__, db); QVERIFY_SQL(q, exec("CREATE TABLE " + ts.tableName() + "(id int, boolvalue bit)"));
QVERIFY_SQL(q, exec("CREATE TABLE " + tableName + "(id int, boolvalue bit)"));
// Bind and insert // Bind and insert
QVERIFY_SQL(q, prepare("INSERT INTO " + tableName + " VALUES(?, ?)")); QVERIFY_SQL(q, prepare("INSERT INTO " + ts.tableName() + " VALUES(?, ?)"));
q.bindValue(0, 1); q.bindValue(0, 1);
q.bindValue(1, true); q.bindValue(1, true);
QVERIFY_SQL(q, exec()); QVERIFY_SQL(q, exec());
@ -1751,7 +1731,7 @@ void tst_QSqlDatabase::odbc_bindBoolean()
QVERIFY_SQL(q, exec()); QVERIFY_SQL(q, exec());
// Retrive // Retrive
QVERIFY_SQL(q, exec("SELECT id, boolvalue FROM " + tableName + " ORDER BY id")); QVERIFY_SQL(q, exec("SELECT id, boolvalue FROM " + ts.tableName() + " ORDER BY id"));
QVERIFY_SQL(q, next()); QVERIFY_SQL(q, next());
QCOMPARE(q.value(0).toInt(), 1); QCOMPARE(q.value(0).toInt(), 1);
QCOMPARE(q.value(1).toBool(), true); QCOMPARE(q.value(1).toBool(), true);
@ -1765,7 +1745,8 @@ void tst_QSqlDatabase::odbc_testqGetString()
QFETCH(QString, dbName); QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName); QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db); CHECK_DATABASE(db);
const QString testqGetString(qTableName("testqGetString", __FILE__, db)); TableScope ts(db, "testqGetString", __FILE__);
const auto &testqGetString = ts.tableName();
QSqlQuery q(db); QSqlQuery q(db);
QSqlDriver::DbmsType dbType = tst_Databases::getDatabaseType(db); QSqlDriver::DbmsType dbType = tst_Databases::getDatabaseType(db);
@ -1845,8 +1826,9 @@ void tst_QSqlDatabase::oci_xmltypeSupport()
QFETCH(QString, dbName); QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName); QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db); CHECK_DATABASE(db);
TableScope ts(db, "qtest_xmltype", __FILE__);
const auto &tableName = ts.tableName();
const QString tableName(qTableName("qtest_xmltype", __FILE__, db));
QString xml("<?xml version=\"1.0\"?>\n<TABLE_NAME>MY_TABLE</TABLE_NAME>\n"); QString xml("<?xml version=\"1.0\"?>\n<TABLE_NAME>MY_TABLE</TABLE_NAME>\n");
QSqlQuery q(db); QSqlQuery q(db);
@ -1924,14 +1906,14 @@ void tst_QSqlDatabase::odbc_uniqueidentifier()
if (dbType != QSqlDriver::MSSqlServer) if (dbType != QSqlDriver::MSSqlServer)
QSKIP("SQL Server (ODBC) specific test"); QSKIP("SQL Server (ODBC) specific test");
const QString tableName(qTableName("qtest_sqlguid", __FILE__, db)); TableScope ts(db, "qtest_sqlguid", __FILE__);
QString guid = QString("AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE"); QString guid = QString("AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE");
QString invalidGuid = QString("GAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE"); QString invalidGuid = QString("GAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE");
QSqlQuery q(db); QSqlQuery q(db);
QVERIFY_SQL(q, exec(QString("CREATE TABLE %1(id uniqueidentifier)").arg(tableName))); QVERIFY_SQL(q, exec(QString("CREATE TABLE %1(id uniqueidentifier)").arg(ts.tableName())));
q.prepare(QString("INSERT INTO %1 VALUES(?)").arg(tableName));; q.prepare(QString("INSERT INTO %1 VALUES(?)").arg(ts.tableName()));
q.addBindValue(guid); q.addBindValue(guid);
QVERIFY_SQL(q, exec()); QVERIFY_SQL(q, exec());
@ -1940,7 +1922,7 @@ void tst_QSqlDatabase::odbc_uniqueidentifier()
Continue); Continue);
QVERIFY_SQL(q, exec()); QVERIFY_SQL(q, exec());
QVERIFY_SQL(q, exec(QString("SELECT id FROM %1").arg(tableName))); QVERIFY_SQL(q, exec(QString("SELECT id FROM %1").arg(ts.tableName())));
QVERIFY_SQL(q, next()); QVERIFY_SQL(q, next());
QCOMPARE(q.value(0).toString(), guid); QCOMPARE(q.value(0).toString(), guid);
} }
@ -1966,20 +1948,20 @@ void tst_QSqlDatabase::odbc_uintfield()
QFETCH(QString, dbName); QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName); QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db); CHECK_DATABASE(db);
TableScope ts(db, "uint_table", __FILE__);
const QString tableName(qTableName("uint_table", __FILE__, db)); constexpr auto val = std::numeric_limits<unsigned int>::max();
unsigned int val = 4294967295U;
QSqlQuery q(db); QSqlQuery q(db);
if ( tst_Databases::isMSAccess( db ) ) if ( tst_Databases::isMSAccess( db ) )
QVERIFY_SQL(q, exec(QString("CREATE TABLE %1(num number)").arg(tableName))); QVERIFY_SQL(q, exec(QString("CREATE TABLE %1(num number)").arg(ts.tableName())));
else else
QVERIFY_SQL(q, exec(QString("CREATE TABLE %1(num numeric(10))").arg(tableName))); QVERIFY_SQL(q, exec(QString("CREATE TABLE %1(num numeric(10))").arg(ts.tableName())));
q.prepare(QString("INSERT INTO %1 VALUES(?)").arg(tableName)); q.prepare(QString("INSERT INTO %1 VALUES(?)").arg(ts.tableName()));
q.addBindValue(val); q.addBindValue(val);
QVERIFY_SQL(q, exec()); QVERIFY_SQL(q, exec());
q.exec(QString("SELECT num FROM %1").arg(tableName)); q.exec(QString("SELECT num FROM %1").arg(ts.tableName()));
if (q.next()) if (q.next())
QCOMPARE(q.value(0).toUInt(), val); QCOMPARE(q.value(0).toUInt(), val);
} }
@ -2048,7 +2030,7 @@ void tst_QSqlDatabase::eventNotificationIBase()
q.exec(QString("DROP PROCEDURE %1").arg(procedureName)); q.exec(QString("DROP PROCEDURE %1").arg(procedureName));
q.exec(QString("CREATE PROCEDURE %1\nAS BEGIN\nPOST_EVENT '%1';\nEND;").arg(procedureName)); q.exec(QString("CREATE PROCEDURE %1\nAS BEGIN\nPOST_EVENT '%1';\nEND;").arg(procedureName));
q.exec(QString("EXECUTE PROCEDURE %1").arg(procedureName)); q.exec(QString("EXECUTE PROCEDURE %1").arg(procedureName));
QSignalSpy spy(driver, QOverload<const QString &, QSqlDriver::NotificationSource, const QVariant &>::of(&QSqlDriver::notification)); QSignalSpy spy(driver, &QSqlDriver::notification);
db.commit(); // No notifications are posted until the transaction is committed. db.commit(); // No notifications are posted until the transaction is committed.
// Interbase needs some time to post the notification and call the driver callback. // Interbase needs some time to post the notification and call the driver callback.
// This happends from another thread, and we have to process events in order for the // This happends from another thread, and we have to process events in order for the
@ -2064,8 +2046,6 @@ void tst_QSqlDatabase::eventNotificationPSQL()
{ {
QFETCH(QString, dbName); QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName); QSqlDatabase db = QSqlDatabase::database(dbName);
if (db.driverName().compare(QLatin1String("QPSQL"), Qt::CaseInsensitive))
QSKIP("QPSQL specific test");
CHECK_DATABASE(db); CHECK_DATABASE(db);
QSqlQuery query(db); QSqlQuery query(db);
@ -2073,7 +2053,7 @@ void tst_QSqlDatabase::eventNotificationPSQL()
QString payload = "payload"; QString payload = "payload";
QSqlDriver *driver = db.driver(); QSqlDriver *driver = db.driver();
QVERIFY_SQL(*driver, subscribeToNotification(procedureName)); QVERIFY_SQL(*driver, subscribeToNotification(procedureName));
QSignalSpy spy(driver, QOverload<const QString &, QSqlDriver::NotificationSource, const QVariant &>::of(&QSqlDriver::notification)); QSignalSpy spy(driver, &QSqlDriver::notification);
query.exec(QString("NOTIFY \"%1\", '%2'").arg(procedureName).arg(payload)); query.exec(QString("NOTIFY \"%1\", '%2'").arg(procedureName).arg(payload));
QTRY_COMPARE(spy.size(), 1); QTRY_COMPARE(spy.size(), 1);
QList<QVariant> arguments = spy.takeFirst(); QList<QVariant> arguments = spy.takeFirst();
@ -2087,15 +2067,13 @@ void tst_QSqlDatabase::eventNotificationSQLite()
{ {
QFETCH(QString, dbName); QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName); QSqlDatabase db = QSqlDatabase::database(dbName);
if (db.driverName().compare(QLatin1String("QSQLITE"), Qt::CaseInsensitive))
QSKIP("QSQLITE specific test");
CHECK_DATABASE(db); CHECK_DATABASE(db);
TableScope ts(db, "sqlitnotifytest", __FILE__); TableScope ts(db, "sqlitnotifytest", __FILE__);
TableScope tsEscape(db, "sqlitnotifytest", __FILE__, false); TableScope tsEscape(db, "sqlitnotifytest", __FILE__, false);
QSqlDriver *driver = db.driver(); QSqlDriver *driver = db.driver();
QSignalSpy spy(driver, QOverload<const QString &, QSqlDriver::NotificationSource, const QVariant &>::of(&QSqlDriver::notification)); QSignalSpy spy(driver, &QSqlDriver::notification);
QSqlQuery q(db); QSqlQuery q(db);
QVERIFY_SQL(q, exec("CREATE TABLE " + ts.tableName() + " (id INTEGER, realVal REAL)")); QVERIFY_SQL(q, exec("CREATE TABLE " + ts.tableName() + " (id INTEGER, realVal REAL)"));
driver->subscribeToNotification(tsEscape.tableName()); driver->subscribeToNotification(tsEscape.tableName());
@ -2113,13 +2091,13 @@ void tst_QSqlDatabase::sqlite_bindAndFetchUInt()
QFETCH(QString, dbName); QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName); QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db); CHECK_DATABASE(db);
TableScope ts(db, "uint_test", __FILE__);
QSqlQuery q(db); QSqlQuery q(db);
const QString tableName(qTableName("uint_test", __FILE__, db)); QVERIFY_SQL(q, exec(QString("CREATE TABLE %1(uint_field UNSIGNED INTEGER)").arg(ts.tableName())));
QVERIFY_SQL(q, exec(QString("CREATE TABLE %1(uint_field UNSIGNED INTEGER)").arg(tableName))); QVERIFY_SQL(q, prepare(QString("INSERT INTO %1 VALUES(?)").arg(ts.tableName())));
QVERIFY_SQL(q, prepare(QString("INSERT INTO %1 VALUES(?)").arg(tableName)));
q.addBindValue(4000000000U); q.addBindValue(4000000000U);
QVERIFY_SQL(q, exec()); QVERIFY_SQL(q, exec());
QVERIFY_SQL(q, exec(QString("SELECT uint_field FROM %1").arg(tableName))); QVERIFY_SQL(q, exec(QString("SELECT uint_field FROM %1").arg(ts.tableName())));
QVERIFY_SQL(q, next()); QVERIFY_SQL(q, next());
// All integers in SQLite are signed, so even though we bound the value // All integers in SQLite are signed, so even though we bound the value
@ -2229,18 +2207,18 @@ void tst_QSqlDatabase::sqlite_enableRegexp()
db.close(); db.close();
db.setConnectOptions("QSQLITE_ENABLE_REGEXP"); db.setConnectOptions("QSQLITE_ENABLE_REGEXP");
QVERIFY_SQL(db, open()); QVERIFY_SQL(db, open());
TableScope ts(db, "regexp_test", __FILE__);
QSqlQuery q(db); QSqlQuery q(db);
const QString tableName(qTableName("regexp_test", __FILE__, db)); QVERIFY_SQL(q, exec(QString("CREATE TABLE %1(text TEXT)").arg(ts.tableName())));
QVERIFY_SQL(q, exec(QString("CREATE TABLE %1(text TEXT)").arg(tableName))); QVERIFY_SQL(q, prepare(QString("INSERT INTO %1 VALUES(?)").arg(ts.tableName())));
QVERIFY_SQL(q, prepare(QString("INSERT INTO %1 VALUES(?)").arg(tableName)));
q.addBindValue("a0"); q.addBindValue("a0");
QVERIFY_SQL(q, exec()); QVERIFY_SQL(q, exec());
q.addBindValue("a1"); q.addBindValue("a1");
QVERIFY_SQL(q, exec()); QVERIFY_SQL(q, exec());
QVERIFY_SQL(q, exec(QString("SELECT text FROM %1 WHERE text REGEXP 'a[^0]' " QVERIFY_SQL(q, exec(QString("SELECT text FROM %1 WHERE text REGEXP 'a[^0]' "
"ORDER BY text").arg(tableName))); "ORDER BY text").arg(ts.tableName())));
QVERIFY_SQL(q, next()); QVERIFY_SQL(q, next());
QCOMPARE(q.value(0).toString(), QString("a1")); QCOMPARE(q.value(0).toString(), QString("a1"));
QFAIL_SQL(q, next()); QFAIL_SQL(q, next());

View File

@ -170,11 +170,11 @@ private slots:
void task_250026(); void task_250026();
void crashQueryOnCloseDatabase(); void crashQueryOnCloseDatabase();
void task_233829_data() { generic_data("QPSQL"); } void testNaN_data() { generic_data("QPSQL"); }
void task_233829(); void testNaN();
void QTBUG_12477_data() { generic_data("QPSQL"); } void psqlNumericMetadata_data() { generic_data("QPSQL"); }
void QTBUG_12477(); void psqlNumericMetadata();
void sqlServerReturn0_data() { generic_data(); } void sqlServerReturn0_data() { generic_data(); }
void sqlServerReturn0(); void sqlServerReturn0();
@ -340,49 +340,8 @@ void tst_QSqlQuery::dropTestTables(QSqlDatabase db)
// Drop all the table in case a testcase failed: // Drop all the table in case a testcase failed:
tablenames << qtest tablenames << qtest
<< qTableName("qtest_null", __FILE__, db) << qTableName("qtest_null", __FILE__, db)
<< qTableName("qtest_writenull", __FILE__, db)
<< qTableName("qtest_blob", __FILE__, db)
<< qTableName("qtest_bittest", __FILE__, db)
<< qTableName("qtest_nullblob", __FILE__, db)
<< qTableName("qtest_rawtest", __FILE__, db)
<< qTableName("qtest_precision", __FILE__, db)
<< qTableName("qtest_prepare", __FILE__, db)
<< qTableName("qtestj1", __FILE__, db)
<< qTableName("qtestj2", __FILE__, db)
<< qTableName("char1Select", __FILE__, db)
<< qTableName("char1SU", __FILE__, db)
<< qTableName("qxmltest", __FILE__, db)
<< qTableName("qtest_exerr", __FILE__, db)
<< qTableName("qtest_empty", __FILE__, db)
<< qTableName("clobby", __FILE__, db)
<< qTableName("bindtest", __FILE__, db)
<< qTableName("more_results", __FILE__, db)
<< qTableName("blobstest", __FILE__, db)
<< qTableName("oraRowId", __FILE__, db)
<< qTableName("bug43874", __FILE__, db)
<< qTableName("bug6421", __FILE__, db).toUpper()
<< qTableName("bug5765", __FILE__, db)
<< qTableName("bug6852", __FILE__, db)
<< qTableName("bug21884", __FILE__, db)
<< qTableName("bug23895", __FILE__, db)
<< qTableName("qtest_lockedtable", __FILE__, db)
<< qTableName("Planet", __FILE__, db)
<< qTableName("task_250026", __FILE__, db)
<< qTableName("task_234422", __FILE__, db)
<< qTableName("test141895", __FILE__, db)
<< qTableName("qtest_oraOCINumber", __FILE__, db)
<< qTableName("bug2192", __FILE__, db)
<< qTableName("tst_record", __FILE__, db); << qTableName("tst_record", __FILE__, db);
if (dbType == QSqlDriver::PostgreSQL)
tablenames << qTableName("task_233829", __FILE__, db);
if (dbType == QSqlDriver::SQLite)
tablenames << qTableName("record_sqlite", __FILE__, db);
if (dbType == QSqlDriver::MSSqlServer || dbType == QSqlDriver::Oracle)
tablenames << qTableName("qtest_longstr", __FILE__, db);
if (dbType == QSqlDriver::MSSqlServer) { if (dbType == QSqlDriver::MSSqlServer) {
QSqlQuery q(db); QSqlQuery q(db);
q.exec("DROP PROCEDURE " + qTableName("test141895_proc", __FILE__, db)); q.exec("DROP PROCEDURE " + qTableName("test141895_proc", __FILE__, db));
@ -460,12 +419,11 @@ void tst_QSqlQuery::char1Select()
CHECK_DATABASE(db); CHECK_DATABASE(db);
{ {
TableScope ts(db, "char1Select", __FILE__);
QSqlQuery q(db); QSqlQuery q(db);
const QString tbl = qTableName("char1Select", __FILE__, db); QVERIFY_SQL(q, exec(QLatin1String("create table %1 (id char(1))").arg(ts.tableName())));
q.exec("drop table " + tbl); QVERIFY_SQL(q, exec(QLatin1String("insert into %1 values ('a')").arg(ts.tableName())));
QVERIFY_SQL(q, exec(QLatin1String("create table %1 (id char(1))").arg(tbl))); QVERIFY_SQL(q, exec("select * from " + ts.tableName()));
QVERIFY_SQL(q, exec(QLatin1String("insert into %1 values ('a')").arg(tbl)));
QVERIFY_SQL(q, exec("select * from " + tbl));
QVERIFY(q.next()); QVERIFY(q.next());
if (tst_Databases::getDatabaseType(db) == QSqlDriver::Interbase) if (tst_Databases::getDatabaseType(db) == QSqlDriver::Interbase)
QCOMPARE(q.value(0).toString().left(1), u"a"); QCOMPARE(q.value(0).toString().left(1), u"a");
@ -489,9 +447,9 @@ void tst_QSqlQuery::char1SelectUnicode()
QSKIP("Database not unicode capable"); QSKIP("Database not unicode capable");
QString uniStr(QChar(0x0915)); // DEVANAGARI LETTER KA QString uniStr(QChar(0x0915)); // DEVANAGARI LETTER KA
TableScope ts(db, "char1SU", __FILE__);
QSqlQuery q(db); QSqlQuery q(db);
QLatin1String createQuery; QLatin1String createQuery;
const QString char1SelectUnicode(qTableName("char1SU", __FILE__, db));
switch (dbType) { switch (dbType) {
case QSqlDriver::MimerSQL: case QSqlDriver::MimerSQL:
@ -514,12 +472,12 @@ void tst_QSqlQuery::char1SelectUnicode()
break; break;
} }
QVERIFY_SQL(q, exec(createQuery.arg(char1SelectUnicode))); QVERIFY_SQL(q, exec(createQuery.arg(ts.tableName())));
QVERIFY_SQL(q, prepare(QLatin1String("insert into %1 values(?)").arg(char1SelectUnicode))); QVERIFY_SQL(q, prepare(QLatin1String("insert into %1 values(?)").arg(ts.tableName())));
q.bindValue(0, uniStr); q.bindValue(0, uniStr);
QVERIFY_SQL(q, exec()); QVERIFY_SQL(q, exec());
QVERIFY_SQL(q, exec("select * from " + char1SelectUnicode)); QVERIFY_SQL(q, exec("select * from " + ts.tableName()));
QVERIFY(q.next()); QVERIFY(q.next());
if (!q.value(0).toString().isEmpty()) if (!q.value(0).toString().isEmpty())
@ -534,7 +492,8 @@ void tst_QSqlQuery::oraRowId()
QFETCH(QString, dbName); QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName); QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db); CHECK_DATABASE(db);
const QString oraRowId(qTableName("oraRowId", __FILE__, db)); TableScope ts(db, "oraRowId", __FILE__);
const auto &oraRowId = ts.tableName();
QSqlQuery q(db); QSqlQuery q(db);
QVERIFY_SQL(q, exec("select rowid from " + qtest)); QVERIFY_SQL(q, exec("select rowid from " + qtest));
@ -793,7 +752,8 @@ void tst_QSqlQuery::oraClob()
QFETCH(QString, dbName); QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName); QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db); CHECK_DATABASE(db);
const QString clobby(qTableName("clobby", __FILE__, db)); TableScope ts(db, "clobby", __FILE__);
const auto &clobby = ts.tableName();
QSqlQuery q(db); QSqlQuery q(db);
@ -1006,15 +966,15 @@ void tst_QSqlQuery::blob()
for (int i = 0; i < ba.size(); ++i) for (int i = 0; i < ba.size(); ++i)
ba[i] = i % 256; ba[i] = i % 256;
TableScope ts(db, "qtest_blob", __FILE__);
QSqlQuery q(db); QSqlQuery q(db);
q.setForwardOnly(true); q.setForwardOnly(true);
const QString tableName = qTableName("qtest_blob", __FILE__, db);
QVERIFY_SQL(q, exec(QLatin1String("create table %1 (id int not null primary key, t_blob %2)") QVERIFY_SQL(q, exec(QLatin1String("create table %1 (id int not null primary key, t_blob %2)")
.arg(tableName, tst_Databases::blobTypeName(db, BLOBSIZE)))); .arg(ts.tableName(), tst_Databases::blobTypeName(db, BLOBSIZE))));
QVERIFY_SQL(q, prepare(QLatin1String("insert into %1 (id, t_blob) values (?, ?)") QVERIFY_SQL(q, prepare(QLatin1String("insert into %1 (id, t_blob) values (?, ?)")
.arg(tableName))); .arg(ts.tableName())));
for (int i = 0; i < BLOBCOUNT; ++i) { for (int i = 0; i < BLOBCOUNT; ++i) {
q.addBindValue(i); q.addBindValue(i);
@ -1022,7 +982,7 @@ void tst_QSqlQuery::blob()
QVERIFY_SQL(q, exec()); QVERIFY_SQL(q, exec());
} }
QVERIFY_SQL(q, exec("select * from " + tableName)); QVERIFY_SQL(q, exec("select * from " + ts.tableName()));
for (int i = 0; i < BLOBCOUNT; ++i) { for (int i = 0; i < BLOBCOUNT; ++i) {
QVERIFY(q.next()); QVERIFY(q.next());
@ -1032,10 +992,10 @@ void tst_QSqlQuery::blob()
"array sizes differ, expected (at least) %" PRIdQSIZETYPE "array sizes differ, expected (at least) %" PRIdQSIZETYPE
", got %" PRIdQSIZETYPE, ba.size(), res.size()))); ", got %" PRIdQSIZETYPE, ba.size(), res.size())));
for (int i2 = 0; i2 < ba.size(); ++i2) { for (qsizetype i2 = 0; i2 < ba.size(); ++i2) {
if (res[i2] != ba[i2]) { if (res[i2] != ba[i2]) {
QFAIL(qPrintable(QString::asprintf( QFAIL(qPrintable(QString::asprintf(
"ByteArrays differ at position %d, expected %hhu, got %hhu", "ByteArrays differ at position %lld, expected %hhu, got %hhu",
i2, ba[i2], res[i2]))); i2, ba[i2], res[i2])));
} }
} }
@ -1788,9 +1748,6 @@ void tst_QSqlQuery::writeNull()
CHECK_DATABASE(db); CHECK_DATABASE(db);
const QSqlDriver::DbmsType dbType = tst_Databases::getDatabaseType(db); const QSqlDriver::DbmsType dbType = tst_Databases::getDatabaseType(db);
QSqlQuery q(db);
const QString tableName = qTableName("qtest_writenull", __FILE__, db);
// The test data table is already used, so use a local hash to exercise the various // The test data table is already used, so use a local hash to exercise the various
// cases from the QSqlResultPrivate::isVariantNull helper. Only PostgreSQL and Mimer SQL // cases from the QSqlResultPrivate::isVariantNull helper. Only PostgreSQL and Mimer SQL
// supports QUuid. // supports QUuid.
@ -1808,7 +1765,7 @@ void tst_QSqlQuery::writeNull()
// Helper to count rows with null values in the data column. // Helper to count rows with null values in the data column.
// Since QSqlDriver::QuerySize might not be supported, we have to count anyway // Since QSqlDriver::QuerySize might not be supported, we have to count anyway
const auto countRowsWithNull = [&]{ const auto countRowsWithNull = [&](QSqlQuery &q, const QString &tableName){
q.exec(QLatin1String("select id, data from %1 where data is null").arg(tableName)); q.exec(QLatin1String("select id, data from %1 where data is null").arg(tableName));
int size = 0; int size = 0;
while (q.next()) while (q.next())
@ -1817,37 +1774,37 @@ void tst_QSqlQuery::writeNull()
}; };
for (const auto &nullableType : nullableTypes.keys()) { for (const auto &nullableType : nullableTypes.keys()) {
const auto tableGuard = qScopeGuard([&]{ TableScope ts(db, "qtest_writenull", __FILE__);
q.exec("drop table " + tableName); QSqlQuery q(db);
});
const QVariant nonNullValue = nullableTypes.value(nullableType); const QVariant nonNullValue = nullableTypes.value(nullableType);
// some useful diagnostic output in case of any test failure // some useful diagnostic output in case of any test failure
auto errorHandler = qScopeGuard([&]{ auto errorHandler = qScopeGuard([&]{
qWarning() << "Test failure for data type" << nonNullValue.metaType().name(); qWarning() << "Test failure for data type" << nonNullValue.metaType().name();
q.exec("select id, data from " + tableName); q.exec("select id, data from " + ts.tableName());
while (q.next()) while (q.next())
qWarning() << q.value(0) << q.value(1); qWarning() << q.value(0) << q.value(1);
}); });
QString createQuery = QLatin1String("create table %3 (id int, data %1%2)") QString createQuery = QLatin1String("create table %3 (id int, data %1%2)")
.arg(nullableType, .arg(nullableType,
dbType == QSqlDriver::MSSqlServer || dbType == QSqlDriver::Sybase ? " null" : "", dbType == QSqlDriver::MSSqlServer || dbType == QSqlDriver::Sybase ? " null" : "",
tableName); ts.tableName());
QVERIFY_SQL(q, exec(createQuery)); QVERIFY_SQL(q, exec(createQuery));
int expectedNullCount = 0; int expectedNullCount = 0;
// Verify that inserting a non-null value works: // Verify that inserting a non-null value works:
QVERIFY_SQL(q, prepare(QLatin1String("insert into %1 values(:id, :data)").arg(tableName))); QVERIFY_SQL(q, prepare(QLatin1String("insert into %1 values(:id, :data)").arg(ts.tableName())));
q.bindValue(":id", expectedNullCount); q.bindValue(":id", expectedNullCount);
q.bindValue(":data", nonNullValue); q.bindValue(":data", nonNullValue);
QVERIFY_SQL(q, exec()); QVERIFY_SQL(q, exec());
QCOMPARE(countRowsWithNull(), expectedNullCount); QCOMPARE(countRowsWithNull(q, ts.tableName()), expectedNullCount);
// Verify that inserting using a null QVariant produces a null entry in the database: // Verify that inserting using a null QVariant produces a null entry in the database:
QVERIFY_SQL(q, prepare(QLatin1String("insert into %1 values(:id, :data)").arg(tableName))); QVERIFY_SQL(q, prepare(QLatin1String("insert into %1 values(:id, :data)").arg(ts.tableName())));
q.bindValue(":id", ++expectedNullCount); q.bindValue(":id", ++expectedNullCount);
q.bindValue(":data", QVariant()); q.bindValue(":data", QVariant());
QVERIFY_SQL(q, exec()); QVERIFY_SQL(q, exec());
QCOMPARE(countRowsWithNull(), expectedNullCount); QCOMPARE(countRowsWithNull(q, ts.tableName()), expectedNullCount);
// Verify that writing a null-value (but not a null-variant) produces a // Verify that writing a null-value (but not a null-variant) produces a
// null entry in the database: // null entry in the database:
@ -1862,11 +1819,11 @@ void tst_QSqlQuery::writeNull()
const QVariant nullValueVariant(nullableMetaType, defaultData); const QVariant nullValueVariant(nullableMetaType, defaultData);
QVERIFY(!nullValueVariant.isNull()); QVERIFY(!nullValueVariant.isNull());
QVERIFY_SQL(q, prepare(QLatin1String("insert into %1 values(:id, :data)").arg(tableName))); QVERIFY_SQL(q, prepare(QLatin1String("insert into %1 values(:id, :data)").arg(ts.tableName())));
q.bindValue(":id", ++expectedNullCount); q.bindValue(":id", ++expectedNullCount);
q.bindValue(":data", nullValueVariant); q.bindValue(":data", nullValueVariant);
QVERIFY_SQL(q, exec()); QVERIFY_SQL(q, exec());
QCOMPARE(countRowsWithNull(), expectedNullCount); QCOMPARE(countRowsWithNull(q, ts.tableName()), expectedNullCount);
// All tests passed for this type if we got here, so don't print diagnostics: // All tests passed for this type if we got here, so don't print diagnostics:
errorHandler.dismiss(); errorHandler.dismiss();
@ -1879,19 +1836,19 @@ void tst_QSqlQuery::oci_nullBlob()
QFETCH(QString, dbName); QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName); QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db); CHECK_DATABASE(db);
const QString qtest_nullblob(qTableName("qtest_nullblob", __FILE__, db)); TableScope ts(db, "qtest_nullblob", __FILE__);
QSqlQuery q(db); QSqlQuery q(db);
QVERIFY_SQL(q, exec(QLatin1String("create table %1 (id int primary key, bb blob)") QVERIFY_SQL(q, exec(QLatin1String("create table %1 (id int primary key, bb blob)")
.arg(qtest_nullblob))); .arg(ts.tableName())));
QVERIFY_SQL(q, exec(QLatin1String("insert into %1 values (0, EMPTY_BLOB())") QVERIFY_SQL(q, exec(QLatin1String("insert into %1 values (0, EMPTY_BLOB())")
.arg(qtest_nullblob))); .arg(ts.tableName())));
QVERIFY_SQL(q, exec(QLatin1String("insert into %1 values (1, NULL)").arg(qtest_nullblob))); QVERIFY_SQL(q, exec(QLatin1String("insert into %1 values (1, NULL)").arg(ts.tableName())));
QVERIFY_SQL(q, exec(QLatin1String("insert into %1 values (2, 'aabbcc00112233445566')") QVERIFY_SQL(q, exec(QLatin1String("insert into %1 values (2, 'aabbcc00112233445566')")
.arg(qtest_nullblob))); .arg(ts.tableName())));
// Necessary otherwise Oracle will bombard you with internal errors: // Necessary otherwise Oracle will bombard you with internal errors:
q.setForwardOnly(true); q.setForwardOnly(true);
QVERIFY_SQL(q, exec(QLatin1String("select * from %1 order by id").arg(qtest_nullblob))); QVERIFY_SQL(q, exec(QLatin1String("select * from %1 order by id").arg(ts.tableName())));
QVERIFY(q.next()); QVERIFY(q.next());
QVERIFY(q.value(1).toByteArray().isEmpty()); QVERIFY(q.value(1).toByteArray().isEmpty());
@ -1912,15 +1869,15 @@ void tst_QSqlQuery::oci_rawField()
QFETCH(QString, dbName); QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName); QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db); CHECK_DATABASE(db);
const QString qtest_rawtest(qTableName("qtest_rawtest", __FILE__, db)); TableScope ts(db, "qtest_rawtest", __FILE__);
QSqlQuery q(db); QSqlQuery q(db);
q.setForwardOnly(true); q.setForwardOnly(true);
QVERIFY_SQL(q, exec(QLatin1String("create table %1 (id int, col raw(20))").arg(qtest_rawtest))); QVERIFY_SQL(q, exec(QLatin1String("create table %1 (id int, col raw(20))").arg(ts.tableName())));
QVERIFY_SQL(q, exec(QLatin1String("insert into %1 values (0, NULL)").arg(qtest_rawtest))); QVERIFY_SQL(q, exec(QLatin1String("insert into %1 values (0, NULL)").arg(ts.tableName())));
QVERIFY_SQL(q, exec(QLatin1String("insert into %1 values (1, '00aa1100ddeeff')") QVERIFY_SQL(q, exec(QLatin1String("insert into %1 values (1, '00aa1100ddeeff')")
.arg(qtest_rawtest))); .arg(ts.tableName())));
QVERIFY_SQL(q, exec(QLatin1String("select col from %1 order by id").arg(qtest_rawtest))); QVERIFY_SQL(q, exec(QLatin1String("select col from %1 order by id").arg(ts.tableName())));
QVERIFY(q.next()); QVERIFY(q.next());
QVERIFY(q.isNull(0)); QVERIFY(q.isNull(0));
QVERIFY(q.value(0).toByteArray().isEmpty()); QVERIFY(q.value(0).toByteArray().isEmpty());
@ -1946,22 +1903,21 @@ void tst_QSqlQuery::precision()
}); });
db.driver()->setNumericalPrecisionPolicy(QSql::HighPrecision); db.driver()->setNumericalPrecisionPolicy(QSql::HighPrecision);
const QString qtest_precision(qTableName("qtest_precision", __FILE__, db)); TableScope ts(db, "qtest_precision", __FILE__);
static const QLatin1String precStr("1.2345678901234567891"); static const QLatin1String precStr("1.2345678901234567891");
{ {
// need a new scope for SQLITE // need a new scope for SQLITE
QSqlQuery q(db); QSqlQuery q(db);
q.exec("drop table " + qtest_precision);
QVERIFY_SQL(q, exec(QLatin1String(tst_Databases::isMSAccess(db) QVERIFY_SQL(q, exec(QLatin1String(tst_Databases::isMSAccess(db)
? "CREATE TABLE %1 (col1 number)" ? "CREATE TABLE %1 (col1 number)"
: "CREATE TABLE %1 (col1 numeric(21, 20))") : "CREATE TABLE %1 (col1 numeric(21, 20))")
.arg(qtest_precision))); .arg(ts.tableName())));
QVERIFY_SQL(q, exec(QLatin1String("INSERT INTO %1 (col1) VALUES (%2)") QVERIFY_SQL(q, exec(QLatin1String("INSERT INTO %1 (col1) VALUES (%2)")
.arg(qtest_precision, precStr))); .arg(ts.tableName(), precStr)));
QVERIFY_SQL(q, exec("SELECT * FROM " + qtest_precision)); QVERIFY_SQL(q, exec("SELECT * FROM " + ts.tableName()));
QVERIFY(q.next()); QVERIFY(q.next());
const QString val = q.value(0).toString(); const QString val = q.value(0).toString();
if (!val.startsWith(precStr)) { if (!val.startsWith(precStr)) {
@ -2007,29 +1963,29 @@ void tst_QSqlQuery::joins()
QSqlDatabase db = QSqlDatabase::database(dbName); QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db); CHECK_DATABASE(db);
const QSqlDriver::DbmsType dbType = tst_Databases::getDatabaseType(db); const QSqlDriver::DbmsType dbType = tst_Databases::getDatabaseType(db);
const QString qtestj1(qTableName("qtestj1", __FILE__, db));
const QString qtestj2(qTableName("qtestj2", __FILE__, db));
if (dbType == QSqlDriver::Oracle || dbType == QSqlDriver::Sybase if (dbType == QSqlDriver::Oracle || dbType == QSqlDriver::Sybase
|| dbType == QSqlDriver::Interbase || db.driverName().startsWith("QODBC")) { || dbType == QSqlDriver::Interbase || db.driverName().startsWith("QODBC")) {
// Oracle broken beyond recognition - cannot outer join on more than one table: // Oracle broken beyond recognition - cannot outer join on more than one table:
QSKIP("DBMS cannot understand standard SQL"); QSKIP("DBMS cannot understand standard SQL");
} }
TableScope j1(db, "qtestj1", __FILE__);
TableScope j2(db, "qtestj2", __FILE__);
QSqlQuery q(db); QSqlQuery q(db);
QVERIFY_SQL(q, exec(QLatin1String("create table %1 (id1 int, id2 int)").arg(qtestj1))); QVERIFY_SQL(q, exec(QLatin1String("create table %1 (id1 int, id2 int)").arg(j1.tableName())));
QVERIFY_SQL(q, exec(QLatin1String("create table %1 (id int, name varchar(20))").arg(qtestj2))); QVERIFY_SQL(q, exec(QLatin1String("create table %1 (id int, name varchar(20))").arg(j2.tableName())));
QVERIFY_SQL(q, exec(QLatin1String("insert into %1 values (1, 1)").arg(qtestj1))); QVERIFY_SQL(q, exec(QLatin1String("insert into %1 values (1, 1)").arg(j1.tableName())));
QVERIFY_SQL(q, exec(QLatin1String("insert into %1 values (1, 2)").arg(qtestj1))); QVERIFY_SQL(q, exec(QLatin1String("insert into %1 values (1, 2)").arg(j1.tableName())));
QVERIFY_SQL(q, exec(QLatin1String("insert into %1 values(1, 'trenton')").arg(qtestj2))); QVERIFY_SQL(q, exec(QLatin1String("insert into %1 values(1, 'trenton')").arg(j2.tableName())));
QVERIFY_SQL(q, exec(QLatin1String("insert into %1 values(2, 'marius')").arg(qtestj2))); QVERIFY_SQL(q, exec(QLatin1String("insert into %1 values(2, 'marius')").arg(j2.tableName())));
QVERIFY_SQL(q, exec(QLatin1String( QVERIFY_SQL(q, exec(QLatin1String(
"select qtestj1.id1, qtestj1.id2, qtestj2.id, qtestj2.name, " "select qtestj1.id1, qtestj1.id2, qtestj2.id, qtestj2.name, "
"qtestj3.id, qtestj3.name from %1 qtestj1 left outer join %2 qtestj2 " "qtestj3.id, qtestj3.name from %1 qtestj1 left outer join %2 qtestj2 "
"on (qtestj1.id1 = qtestj2.id) left outer join %2 as qtestj3 " "on (qtestj1.id1 = qtestj2.id) left outer join %2 as qtestj3 "
"on (qtestj1.id2 = qtestj3.id)").arg(qtestj1, qtestj2))); "on (qtestj1.id2 = qtestj3.id)").arg(j1.tableName(), j2.tableName())));
QVERIFY(q.next()); QVERIFY(q.next());
QCOMPARE(q.value(0).toInt(), 1); QCOMPARE(q.value(0).toInt(), 1);
@ -2077,7 +2033,8 @@ void tst_QSqlQuery::prepare_bind_exec()
QSqlDatabase db = QSqlDatabase::database(dbName); QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db); CHECK_DATABASE(db);
const QSqlDriver::DbmsType dbType = tst_Databases::getDatabaseType(db); const QSqlDriver::DbmsType dbType = tst_Databases::getDatabaseType(db);
const QString qtest_prepare(qTableName("qtest_prepare", __FILE__, db)); TableScope ts(db, "qtest_prepare", __FILE__);
const auto &qtest_prepare = ts.tableName();
if (dbType == QSqlDriver::DB2) if (dbType == QSqlDriver::DB2)
QSKIP("Needs someone with more Unicode knowledge than I have to fix"); QSKIP("Needs someone with more Unicode knowledge than I have to fix");
@ -2118,13 +2075,10 @@ void tst_QSqlQuery::prepare_bind_exec()
break; break;
} }
q.exec("drop table " + qtest_prepare);
QVERIFY_SQL(q, exec(createQuery.arg(qtest_prepare))); QVERIFY_SQL(q, exec(createQuery.arg(qtest_prepare)));
QVERIFY(q.prepare(QLatin1String("insert into %1 (id, name) values (:id, :name)") QVERIFY(q.prepare(QLatin1String("insert into %1 (id, name) values (:id, :name)")
.arg(qtest_prepare))); .arg(qtest_prepare)));
int i; for (int i = 0; i < 6; ++i) {
for (i = 0; i < 6; ++i) {
q.bindValue(":name", values[i]); q.bindValue(":name", values[i]);
q.bindValue(":id", i); q.bindValue(":id", i);
QVERIFY_SQL(q, exec()); QVERIFY_SQL(q, exec());
@ -2150,7 +2104,7 @@ void tst_QSqlQuery::prepare_bind_exec()
} }
QVERIFY_SQL(q, exec(QLatin1String("SELECT * FROM %1 order by id").arg(qtest_prepare))); QVERIFY_SQL(q, exec(QLatin1String("SELECT * FROM %1 order by id").arg(qtest_prepare)));
for (i = 0; i < 6; ++i) { for (int i = 0; i < 6; ++i) {
QVERIFY(q.next()); QVERIFY(q.next());
QCOMPARE(q.value(0).toInt(), i); QCOMPARE(q.value(0).toInt(), i);
QCOMPARE(q.value(1).toString().trimmed(), values[i]); QCOMPARE(q.value(1).toString().trimmed(), values[i]);
@ -2175,7 +2129,7 @@ void tst_QSqlQuery::prepare_bind_exec()
QVERIFY(q.exec(QLatin1String("select * from %1 where id > 98 order by id") QVERIFY(q.exec(QLatin1String("select * from %1 where id > 98 order by id")
.arg(qtest_prepare))); .arg(qtest_prepare)));
for (i = 99; i <= 100; ++i) { for (int i = 99; i <= 100; ++i) {
QVERIFY(q.next()); QVERIFY(q.next());
QCOMPARE(q.value(0).toInt(), i); QCOMPARE(q.value(0).toInt(), i);
QCOMPARE(q.value(1).toString().trimmed(), u"Bart"); QCOMPARE(q.value(1).toString().trimmed(), u"Bart");
@ -2184,7 +2138,7 @@ void tst_QSqlQuery::prepare_bind_exec()
/*** SELECT stuff ***/ /*** SELECT stuff ***/
QVERIFY(q.prepare(QLatin1String("select * from %1 where id = :id").arg(qtest_prepare))); QVERIFY(q.prepare(QLatin1String("select * from %1 where id = :id").arg(qtest_prepare)));
for (i = 0; i < 6; ++i) { for (int i = 0; i < 6; ++i) {
q.bindValue(":id", i); q.bindValue(":id", i);
QVERIFY_SQL(q, exec()); QVERIFY_SQL(q, exec());
QVERIFY_SQL(q, next()); QVERIFY_SQL(q, next());
@ -2297,7 +2251,7 @@ void tst_QSqlQuery::prepare_bind_exec()
QFAIL_SQL(q, exec()); QFAIL_SQL(q, exec());
QVERIFY_SQL(q, exec(QLatin1String("SELECT * FROM %1 order by id").arg(qtest_prepare))); QVERIFY_SQL(q, exec(QLatin1String("SELECT * FROM %1 order by id").arg(qtest_prepare)));
for (i = 0; i < 6; ++i) { for (int i = 0; i < 6; ++i) {
QVERIFY(q.next()); QVERIFY(q.next());
QCOMPARE(q.value(0).toInt(), i); QCOMPARE(q.value(0).toInt(), i);
QCOMPARE(q.value(1).toString().trimmed(), values[i]); QCOMPARE(q.value(1).toString().trimmed(), values[i]);
@ -2323,7 +2277,7 @@ void tst_QSqlQuery::prepare_bind_exec()
QVERIFY(q.exec(QLatin1String("select * from %1 where id > 98 order by id") QVERIFY(q.exec(QLatin1String("select * from %1 where id > 98 order by id")
.arg(qtest_prepare))); .arg(qtest_prepare)));
for (i = 99; i <= 100; ++i) { for (int i = 99; i <= 100; ++i) {
QVERIFY(q.next()); QVERIFY(q.next());
QCOMPARE(q.value(0).toInt(), i); QCOMPARE(q.value(0).toInt(), i);
QCOMPARE(q.value(1).toString().trimmed(), u"Bart"); QCOMPARE(q.value(1).toString().trimmed(), u"Bart");
@ -2342,7 +2296,7 @@ void tst_QSqlQuery::prepare_bind_exec()
QVERIFY(q.prepare(QLatin1String( QVERIFY(q.prepare(QLatin1String(
"insert into %1 (id, name, name2) values (:id, :name, :name)") "insert into %1 (id, name, name2) values (:id, :name, :name)")
.arg(qtest_prepare))); .arg(qtest_prepare)));
for (i = 101; i < 103; ++i) { for (int i = 101; i < 103; ++i) {
q.bindValue(":id", i); q.bindValue(":id", i);
q.bindValue(":name", "name"); q.bindValue(":name", "name");
QVERIFY(q.exec()); QVERIFY(q.exec());
@ -2360,7 +2314,7 @@ void tst_QSqlQuery::prepare_bind_exec()
// works correctly - QTBUG-65150 // works correctly - QTBUG-65150
QVERIFY(q.prepare(QLatin1String("insert into %1 (id, name, name2) values (:id, :id, :name)") QVERIFY(q.prepare(QLatin1String("insert into %1 (id, name, name2) values (:id, :id, :name)")
.arg(qtest_prepare))); .arg(qtest_prepare)));
for (i = 104; i < 106; ++i) { for (int i = 104; i < 106; ++i) {
q.bindValue(":id", i); q.bindValue(":id", i);
q.bindValue(":name", "name"); q.bindValue(":name", "name");
QVERIFY(q.exec()); QVERIFY(q.exec());
@ -2375,7 +2329,7 @@ void tst_QSqlQuery::prepare_bind_exec()
// Test that duplicated named placeholders in any order // Test that duplicated named placeholders in any order
QVERIFY(q.prepare(QLatin1String("insert into %1 (id, name, name2) values (:id, :name, :id)") QVERIFY(q.prepare(QLatin1String("insert into %1 (id, name, name2) values (:id, :name, :id)")
.arg(qtest_prepare))); .arg(qtest_prepare)));
for (i = 107; i < 109; ++i) { for (int i = 107; i < 109; ++i) {
q.bindValue(":id", i); q.bindValue(":id", i);
q.bindValue(":name", "name"); q.bindValue(":name", "name");
QVERIFY(q.exec()); QVERIFY(q.exec());
@ -2444,12 +2398,12 @@ void tst_QSqlQuery::sqlServerLongStrings()
if (tst_Databases::getDatabaseType(db) != QSqlDriver::MSSqlServer) if (tst_Databases::getDatabaseType(db) != QSqlDriver::MSSqlServer)
QSKIP("Test is specific to SQL Server"); QSKIP("Test is specific to SQL Server");
TableScope ts(db, "qtest_longstr", __FILE__);
QSqlQuery q(db); QSqlQuery q(db);
const QString tableName = qTableName("qtest_longstr", __FILE__, db);
QVERIFY_SQL(q, exec(QLatin1String("CREATE TABLE %1 (id int primary key, longstring ntext)") QVERIFY_SQL(q, exec(QLatin1String("CREATE TABLE %1 (id int primary key, longstring ntext)")
.arg(tableName))); .arg(ts.tableName())));
QVERIFY_SQL(q, prepare(QLatin1String("INSERT INTO %1 VALUES (?, ?)").arg(tableName))); QVERIFY_SQL(q, prepare(QLatin1String("INSERT INTO %1 VALUES (?, ?)").arg(ts.tableName())));
q.addBindValue(0); q.addBindValue(0);
q.addBindValue(u"bubu"_s); q.addBindValue(u"bubu"_s);
@ -2460,7 +2414,7 @@ void tst_QSqlQuery::sqlServerLongStrings()
q.addBindValue(1); q.addBindValue(1);
q.addBindValue(testStr); q.addBindValue(testStr);
QVERIFY_SQL(q, exec()); QVERIFY_SQL(q, exec());
QVERIFY_SQL(q, exec("select * from " + tableName)); QVERIFY_SQL(q, exec("select * from " + ts.tableName()));
QVERIFY_SQL(q, next()); QVERIFY_SQL(q, next());
QCOMPARE(q.value(0).toInt(), 0); QCOMPARE(q.value(0).toInt(), 0);
@ -2617,19 +2571,19 @@ void tst_QSqlQuery::QTBUG_43874()
QFETCH(QString, dbName); QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName); QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db); CHECK_DATABASE(db);
TableScope ts(db, "bug43874", __FILE__);
QSqlQuery q(db); QSqlQuery q(db);
const QString tableName = qTableName("bug43874", __FILE__, db);
QVERIFY_SQL(q, exec(QLatin1String("CREATE TABLE %1 (id INT)").arg(tableName))); QVERIFY_SQL(q, exec(QLatin1String("CREATE TABLE %1 (id INT)").arg(ts.tableName())));
QVERIFY_SQL(q, prepare(QLatin1String("INSERT INTO %1 (id) VALUES (?)").arg(tableName))); QVERIFY_SQL(q, prepare(QLatin1String("INSERT INTO %1 (id) VALUES (?)").arg(ts.tableName())));
for (int i = 0; i < 2; ++i) { for (int i = 0; i < 2; ++i) {
const QVariantList ids = { i }; const QVariantList ids = { i };
q.addBindValue(ids); q.addBindValue(ids);
QVERIFY_SQL(q, execBatch()); QVERIFY_SQL(q, execBatch());
} }
QVERIFY_SQL(q, exec(QLatin1String("SELECT id FROM %1 ORDER BY id").arg(tableName))); QVERIFY_SQL(q, exec(QLatin1String("SELECT id FROM %1 ORDER BY id").arg(ts.tableName())));
QVERIFY(q.next()); QVERIFY(q.next());
QCOMPARE(q.value(0).toInt(), 0); QCOMPARE(q.value(0).toInt(), 0);
@ -2724,14 +2678,14 @@ void tst_QSqlQuery::record_sqlite()
QSqlDatabase db = QSqlDatabase::database(dbName); QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db); CHECK_DATABASE(db);
TableScope ts(db, "record_sqlite");
QSqlQuery q(db); QSqlQuery q(db);
const QString tableName = qTableName("record_sqlite", __FILE__, db);
QVERIFY_SQL(q, exec(QLatin1String( QVERIFY_SQL(q, exec(QLatin1String(
"create table %1(id integer primary key, name varchar, title int)") "create table %1(id integer primary key, name varchar, title int)")
.arg(tableName))); .arg(ts.tableName())));
QSqlRecord rec = db.record(tableName); QSqlRecord rec = db.record(ts.tableName());
QCOMPARE(rec.count(), 3); QCOMPARE(rec.count(), 3);
QCOMPARE(rec.field(0).metaType().id(), QMetaType::Int); QCOMPARE(rec.field(0).metaType().id(), QMetaType::Int);
@ -2739,7 +2693,7 @@ void tst_QSqlQuery::record_sqlite()
QCOMPARE(rec.field(2).metaType().id(), QMetaType::Int); QCOMPARE(rec.field(2).metaType().id(), QMetaType::Int);
// Important - select from an empty table: // Important - select from an empty table:
QVERIFY_SQL(q, exec("select id, name, title from " + tableName)); QVERIFY_SQL(q, exec("select id, name, title from " + ts.tableName()));
rec = q.record(); rec = q.record();
QCOMPARE(rec.count(), 3); QCOMPARE(rec.count(), 3);
@ -2754,20 +2708,19 @@ void tst_QSqlQuery::oraLong()
QSqlDatabase db = QSqlDatabase::database(dbName); QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db); CHECK_DATABASE(db);
QSqlQuery q(db);
QString aLotOfText(127000, QLatin1Char('H')); QString aLotOfText(127000, QLatin1Char('H'));
const QString tableName = qTableName("qtest_longstr", __FILE__, db); TableScope ts(db, "qtest_longstr", __FILE__);
QSqlQuery q(db);
QVERIFY_SQL(q, exec(QLatin1String("create table %1 (id int primary key, astr long)") QVERIFY_SQL(q, exec(QLatin1String("create table %1 (id int primary key, astr long)")
.arg(tableName))); .arg(ts.tableName())));
QVERIFY_SQL(q, prepare(QLatin1String("insert into %1 (id, astr) values (?, ?)") QVERIFY_SQL(q, prepare(QLatin1String("insert into %1 (id, astr) values (?, ?)")
.arg(tableName))); .arg(ts.tableName())));
q.addBindValue(1); q.addBindValue(1);
q.addBindValue(aLotOfText); q.addBindValue(aLotOfText);
QVERIFY_SQL(q, exec()); QVERIFY_SQL(q, exec());
QVERIFY_SQL(q, exec("select id,astr from " + tableName)); QVERIFY_SQL(q, exec("select id,astr from " + ts.tableName()));
QVERIFY(q.next()); QVERIFY(q.next());
QCOMPARE(q.value(0).toInt(), 1); QCOMPARE(q.value(0).toInt(), 1);
@ -2780,12 +2733,11 @@ void tst_QSqlQuery::execErrorRecovery()
QSqlDatabase db = QSqlDatabase::database(dbName); QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db); CHECK_DATABASE(db);
QSqlQuery q(db); TableScope ts(db, "qtest_exerr", __FILE__);
const QString tbl = qTableName("qtest_exerr", __FILE__, db); QSqlQuery q(db);
q.exec("drop table " + tbl); QVERIFY_SQL(q, exec(QLatin1String("create table %1 (id int not null primary key)").arg(ts.tableName())));
QVERIFY_SQL(q, exec(QLatin1String("create table %1 (id int not null primary key)").arg(tbl))); QVERIFY_SQL(q, prepare(QLatin1String("insert into %1 values (?)").arg(ts.tableName())));
QVERIFY_SQL(q, prepare(QLatin1String("insert into %1 values (?)").arg(tbl)));
q.addBindValue(1); q.addBindValue(1);
QVERIFY_SQL(q, exec()); QVERIFY_SQL(q, exec());
@ -2842,13 +2794,12 @@ void tst_QSqlQuery::lastInsertId()
// PostgreSQL >= 8.1 relies on lastval() which does not work if a value is // PostgreSQL >= 8.1 relies on lastval() which does not work if a value is
// manually inserted to the serial field, so we create a table specifically // manually inserted to the serial field, so we create a table specifically
if (tst_Databases::getDatabaseType(db) == QSqlDriver::PostgreSQL) { if (tst_Databases::getDatabaseType(db) == QSqlDriver::PostgreSQL) {
const auto tableName = qTableName("tst_lastInsertId", __FILE__, db); TableScope ts(db, "tst_lastInsertId", __FILE__);
tst_Databases::safeDropTables(db, {tableName});
QVERIFY_SQL(q, exec(QLatin1String("create table %1 (id serial not null, t_varchar " QVERIFY_SQL(q, exec(QLatin1String("create table %1 (id serial not null, t_varchar "
"varchar(20), t_char char(20), primary key(id))") "varchar(20), t_char char(20), primary key(id))")
.arg(tableName))); .arg(ts.tableName())));
QVERIFY_SQL(q, exec(QLatin1String("insert into %1 (t_varchar, t_char) values " QVERIFY_SQL(q, exec(QLatin1String("insert into %1 (t_varchar, t_char) values "
"('VarChar41', 'Char41')").arg(tableName))); "('VarChar41', 'Char41')").arg(ts.tableName())));
} else { } else {
QVERIFY_SQL(q, exec(QLatin1String("insert into %1 values (41, 'VarChar41', 'Char41')") QVERIFY_SQL(q, exec(QLatin1String("insert into %1 values (41, 'VarChar41', 'Char41')")
.arg(qtest))); .arg(qtest)));
@ -2894,18 +2845,18 @@ void tst_QSqlQuery::psql_bindWithDoubleColonCastOperator()
QSqlDatabase db = QSqlDatabase::database(dbName); QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db); CHECK_DATABASE(db);
const QString tablename(qTableName("bindtest", __FILE__, db)); TableScope ts(db, "bindtest", __FILE__);
QSqlQuery q(db); QSqlQuery q(db);
QVERIFY_SQL(q, exec(QLatin1String( QVERIFY_SQL(q, exec(QLatin1String(
"create table %1 (id1 int, id2 int, id3 int, fld1 int, fld2 int)") "create table %1 (id1 int, id2 int, id3 int, fld1 int, fld2 int)")
.arg(tablename))); .arg(ts.tableName())));
QVERIFY_SQL(q, exec(QLatin1String("insert into %1 values (1, 2, 3, 10, 5)").arg(tablename))); QVERIFY_SQL(q, exec(QLatin1String("insert into %1 values (1, 2, 3, 10, 5)").arg(ts.tableName())));
// Insert tableName last to let the other %-tokens' numbering match what they're replaced with: // Insert tableName last to let the other %-tokens' numbering match what they're replaced with:
const auto queryTemplate = QLatin1String("select sum((fld1 - fld2)::int) from %4 where " const auto queryTemplate = QLatin1String("select sum((fld1 - fld2)::int) from %4 where "
"id1 = %1 and id2 =%2 and id3=%3"); "id1 = %1 and id2 =%2 and id3=%3");
const QString query = queryTemplate.arg(":myid1", ":myid2", ":myid3", tablename); const QString query = queryTemplate.arg(":myid1", ":myid2", ":myid3", ts.tableName());
QVERIFY_SQL(q, prepare(query)); QVERIFY_SQL(q, prepare(query));
q.bindValue(":myid1", 1); q.bindValue(":myid1", 1);
q.bindValue(":myid2", 2); q.bindValue(":myid2", 2);
@ -2916,7 +2867,7 @@ void tst_QSqlQuery::psql_bindWithDoubleColonCastOperator()
// The positional placeholders are converted to named placeholders in executedQuery() // The positional placeholders are converted to named placeholders in executedQuery()
const QString expected = db.driver()->hasFeature(QSqlDriver::PreparedQueries) const QString expected = db.driver()->hasFeature(QSqlDriver::PreparedQueries)
? query : queryTemplate.arg("1", "2", "3", tablename); ? query : queryTemplate.arg("1", "2", "3", ts.tableName());
QCOMPARE(q.executedQuery(), expected); QCOMPARE(q.executedQuery(), expected);
} }
@ -3133,7 +3084,8 @@ void tst_QSqlQuery::nextResult()
QSKIP("DBMS does not support multiple result sets"); QSKIP("DBMS does not support multiple result sets");
QSqlQuery q(db); QSqlQuery q(db);
const QString tableName(qTableName("more_results", __FILE__, db)); TableScope ts(db, "more_results", __FILE__);
const auto &tableName = ts.tableName();
QVERIFY_SQL(q, exec(QLatin1String( QVERIFY_SQL(q, exec(QLatin1String(
"CREATE TABLE %1 (id integer, text varchar(20), " "CREATE TABLE %1 (id integer, text varchar(20), "
@ -3360,7 +3312,7 @@ void tst_QSqlQuery::blobsPreparedQuery()
QSKIP("DBMS does not support BLOBs or prepared queries"); QSKIP("DBMS does not support BLOBs or prepared queries");
} }
const QString tableName(qTableName("blobstest", __FILE__, db)); TableScope ts(db, "blobstest", __FILE__);
QSqlQuery q(db); QSqlQuery q(db);
q.setForwardOnly(true); // This is needed to make the test work with DB2. q.setForwardOnly(true); // This is needed to make the test work with DB2.
@ -3374,8 +3326,8 @@ void tst_QSqlQuery::blobsPreparedQuery()
: dbType == QSqlDriver::MSSqlServer ? "IMAGE" : "BLOB"); : dbType == QSqlDriver::MSSqlServer ? "IMAGE" : "BLOB");
QVERIFY_SQL(q, exec(QLatin1String("CREATE TABLE %1(id INTEGER, data %2)") QVERIFY_SQL(q, exec(QLatin1String("CREATE TABLE %1(id INTEGER, data %2)")
.arg(tableName, typeName))); .arg(ts.tableName(), typeName)));
q.prepare(QLatin1String("INSERT INTO %1(id, data) VALUES(:id, :data)").arg(tableName)); q.prepare(QLatin1String("INSERT INTO %1(id, data) VALUES(:id, :data)").arg(ts.tableName()));
q.bindValue(":id", 1); q.bindValue(":id", 1);
q.bindValue(":data", shortBLOB); q.bindValue(":data", shortBLOB);
QVERIFY_SQL(q, exec()); QVERIFY_SQL(q, exec());
@ -3385,7 +3337,7 @@ void tst_QSqlQuery::blobsPreparedQuery()
QVERIFY_SQL(q, exec()); QVERIFY_SQL(q, exec());
// Two executions and result sets // Two executions and result sets
q.prepare(QLatin1String("SELECT data FROM %1 WHERE id = ?").arg(tableName)); q.prepare(QLatin1String("SELECT data FROM %1 WHERE id = ?").arg(ts.tableName()));
q.bindValue(0, QVariant(1)); q.bindValue(0, QVariant(1));
QVERIFY_SQL(q, exec()); QVERIFY_SQL(q, exec());
QVERIFY_SQL(q, next()); QVERIFY_SQL(q, next());
@ -3397,7 +3349,7 @@ void tst_QSqlQuery::blobsPreparedQuery()
QCOMPARE(q.value(0).toString().toUtf8(), longerBLOB.toUtf8()); QCOMPARE(q.value(0).toString().toUtf8(), longerBLOB.toUtf8());
// Only one execution and result set // Only one execution and result set
q.prepare(QLatin1String("SELECT id, data FROM %1 ORDER BY id").arg(tableName)); q.prepare(QLatin1String("SELECT id, data FROM %1 ORDER BY id").arg(ts.tableName()));
QVERIFY_SQL(q, exec()); QVERIFY_SQL(q, exec());
QVERIFY_SQL(q, next()); QVERIFY_SQL(q, next());
QCOMPARE(q.value(1).toString(), shortBLOB); QCOMPARE(q.value(1).toString(), shortBLOB);
@ -3413,11 +3365,11 @@ void tst_QSqlQuery::emptyTableNavigate()
CHECK_DATABASE(db); CHECK_DATABASE(db);
{ {
TableScope ts(db, "qtest_empty", __FILE__);
QSqlQuery q(db); QSqlQuery q(db);
const QString tbl = qTableName("qtest_empty", __FILE__, db); q.exec("drop table " + ts.tableName());
q.exec("drop table " + tbl); QVERIFY_SQL(q, exec(QLatin1String("create table %1 (id char(10))").arg(ts.tableName())));
QVERIFY_SQL(q, exec(QLatin1String("create table %1 (id char(10))").arg(tbl))); QVERIFY_SQL(q, prepare("select * from " + ts.tableName()));
QVERIFY_SQL(q, prepare("select * from " + tbl));
QVERIFY_SQL(q, exec()); QVERIFY_SQL(q, exec());
QVERIFY(!q.next()); QVERIFY(!q.next());
QVERIFY(!q.lastError().isValid()); QVERIFY(!q.lastError().isValid());
@ -3478,10 +3430,10 @@ void tst_QSqlQuery::task_217003()
QFETCH(QString, dbName); QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName); QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db); CHECK_DATABASE(db);
QSqlQuery q(db); TableScope ts(db, "Planet", __FILE__);
const QString planets = qTableName("Planet", __FILE__, db); const auto &planets = ts.tableName();
q.exec("drop table " + planets); QSqlQuery q(db);
QVERIFY_SQL(q, exec(QLatin1String("create table %1 (Name varchar(20))").arg(planets))); QVERIFY_SQL(q, exec(QLatin1String("create table %1 (Name varchar(20))").arg(planets)));
QVERIFY_SQL(q, exec(QLatin1String("insert into %1 VALUES ('Mercury')").arg(planets))); QVERIFY_SQL(q, exec(QLatin1String("insert into %1 VALUES ('Mercury')").arg(planets)));
QVERIFY_SQL(q, exec(QLatin1String("insert into %1 VALUES ('Venus')").arg(planets))); QVERIFY_SQL(q, exec(QLatin1String("insert into %1 VALUES ('Venus')").arg(planets)));
@ -3507,11 +3459,10 @@ void tst_QSqlQuery::task_250026()
QFETCH(QString, dbName); QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName); QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db); CHECK_DATABASE(db);
TableScope ts(db, "task_250026", __FILE__);
QSqlQuery q(db); QSqlQuery q(db);
if (!q.exec(QLatin1String("create table %1 (longfield varchar(1100))").arg(ts.tableName()))) {
const QString tableName(qTableName("task_250026", __FILE__, db));
if (!q.exec(QLatin1String("create table %1 (longfield varchar(1100))").arg(tableName))) {
qDebug() << "Error" << q.lastError(); qDebug() << "Error" << q.lastError();
QSKIP("Db doesn't support \"1100\" as a size for fields"); QSKIP("Db doesn't support \"1100\" as a size for fields");
} }
@ -3519,12 +3470,12 @@ void tst_QSqlQuery::task_250026()
const QString data258(258, QLatin1Char('A')); const QString data258(258, QLatin1Char('A'));
const QString data1026(1026, QLatin1Char('A')); const QString data1026(1026, QLatin1Char('A'));
QVERIFY_SQL(q, prepare(QLatin1String("insert into %1(longfield) VALUES (:longfield)") QVERIFY_SQL(q, prepare(QLatin1String("insert into %1(longfield) VALUES (:longfield)")
.arg(tableName))); .arg(ts.tableName())));
q.bindValue(":longfield", data258); q.bindValue(":longfield", data258);
QVERIFY_SQL(q, exec()); QVERIFY_SQL(q, exec());
q.bindValue(":longfield", data1026); q.bindValue(":longfield", data1026);
QVERIFY_SQL(q, exec()); QVERIFY_SQL(q, exec());
QVERIFY_SQL(q, exec("select * from " + tableName)); QVERIFY_SQL(q, exec("select * from " + ts.tableName()));
QVERIFY_SQL(q, next()); QVERIFY_SQL(q, next());
QCOMPARE(q.value(0).toString().size(), data258.size()); QCOMPARE(q.value(0).toString().size(), data258.size());
QVERIFY_SQL(q, next()); QVERIFY_SQL(q, next());
@ -3545,19 +3496,19 @@ void tst_QSqlQuery::crashQueryOnCloseDatabase()
} }
} }
void tst_QSqlQuery::task_233829() void tst_QSqlQuery::testNaN()
{ {
QFETCH(QString, dbName); QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName); QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db); CHECK_DATABASE(db);
TableScope ts(db, "testNaN", __FILE__);
QSqlQuery q(db); QSqlQuery q(db);
const QString tableName(qTableName("task_233829", __FILE__, db));
QVERIFY_SQL(q, exec(QLatin1String( QVERIFY_SQL(q, exec(QLatin1String(
"CREATE TABLE %1(dbl1 double precision,dbl2 double precision) " "CREATE TABLE %1(dbl1 double precision,dbl2 double precision) "
"without oids;").arg(tableName))); "without oids;").arg(ts.tableName())));
const QString queryString = const QString queryString =
QLatin1String("INSERT INTO %1(dbl1, dbl2) VALUES(?,?)").arg(tableName); QLatin1String("INSERT INTO %1(dbl1, dbl2) VALUES(?,?)").arg(ts.tableName());
const double nan = qQNaN(); const double nan = qQNaN();
QVERIFY_SQL(q, prepare(queryString)); QVERIFY_SQL(q, prepare(queryString));
@ -3566,13 +3517,11 @@ void tst_QSqlQuery::task_233829()
QVERIFY_SQL(q, exec()); QVERIFY_SQL(q, exec());
} }
void tst_QSqlQuery::QTBUG_12477() void tst_QSqlQuery::psqlNumericMetadata()
{ {
QFETCH(QString, dbName); QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName); QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db); CHECK_DATABASE(db);
if (!db.driverName().startsWith("QPSQL"))
QSKIP("PostgreSQL-specific test");
QSqlQuery q(db); QSqlQuery q(db);
QVERIFY_SQL(q, exec("SELECT 1::bit, '10101010000111101101'::varbit, " QVERIFY_SQL(q, exec("SELECT 1::bit, '10101010000111101101'::varbit, "
@ -3615,10 +3564,10 @@ void tst_QSqlQuery::sqlServerReturn0()
if (tst_Databases::getDatabaseType(db) != QSqlDriver::MSSqlServer) if (tst_Databases::getDatabaseType(db) != QSqlDriver::MSSqlServer)
QSKIP("Test is specific to SQL Server"); QSKIP("Test is specific to SQL Server");
const QString tableName(qTableName("test141895", __FILE__, db)); TableScope ts(db, "test141895", __FILE__);
const auto &tableName = ts.tableName();
const QString procName(qTableName("test141895_proc", __FILE__, db)); const QString procName(qTableName("test141895_proc", __FILE__, db));
QSqlQuery q(db); QSqlQuery q(db);
q.exec("DROP TABLE " + tableName);
q.exec("DROP PROCEDURE " + procName); q.exec("DROP PROCEDURE " + procName);
QVERIFY_SQL(q, exec(QLatin1String("CREATE TABLE %1 (id integer)").arg(tableName))); QVERIFY_SQL(q, exec(QLatin1String("CREATE TABLE %1 (id integer)").arg(tableName)));
QVERIFY_SQL(q, exec(QLatin1String("INSERT INTO %1 (id) VALUES (1)").arg(tableName))); QVERIFY_SQL(q, exec(QLatin1String("INSERT INTO %1 (id) VALUES (1)").arg(tableName)));
@ -3785,9 +3734,10 @@ void tst_QSqlQuery::QTBUG_6421()
QFETCH(QString, dbName); QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName); QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db); CHECK_DATABASE(db);
const QString tableName(qTableName("bug6421", __FILE__, db).toUpper());
TableScope ts(db, tableName);
QSqlQuery q(db); QSqlQuery q(db);
const QString tableName(qTableName("bug6421", __FILE__, db).toUpper());
QVERIFY_SQL(q, exec(QLatin1String( QVERIFY_SQL(q, exec(QLatin1String(
"create table %1(COL1 char(10), COL2 char(10), COL3 char(10))") "create table %1(COL1 char(10), COL2 char(10), COL3 char(10))")
@ -3835,8 +3785,10 @@ void tst_QSqlQuery::QTBUG_6852()
QFETCH(QString, dbName); QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName); QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db); CHECK_DATABASE(db);
TableScope ts(db, "bug6852", __FILE__);
const auto &tableName = ts.tableName();
QSqlQuery q(db); QSqlQuery q(db);
const QString tableName(qTableName("bug6852", __FILE__, db));
const QString procName(qTableName("bug6852_proc", __FILE__, db)); const QString procName(qTableName("bug6852_proc", __FILE__, db));
QVERIFY_SQL(q, exec("DROP PROCEDURE IF EXISTS " + procName)); QVERIFY_SQL(q, exec("DROP PROCEDURE IF EXISTS " + procName));
@ -3867,19 +3819,19 @@ void tst_QSqlQuery::QTBUG_5765()
QFETCH(QString, dbName); QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName); QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db); CHECK_DATABASE(db);
QSqlQuery q(db); TableScope ts(db, "bug5765", __FILE__);
const QString tableName(qTableName("bug5765", __FILE__, db));
QSqlQuery q(db);
QVERIFY_SQL(q, exec(QLatin1String("CREATE TABLE %1(testval TINYINT(1) DEFAULT 0)") QVERIFY_SQL(q, exec(QLatin1String("CREATE TABLE %1(testval TINYINT(1) DEFAULT 0)")
.arg(tableName))); .arg(ts.tableName())));
q.prepare(QLatin1String("INSERT INTO %1 SET testval = :VALUE").arg(tableName)); q.prepare(QLatin1String("INSERT INTO %1 SET testval = :VALUE").arg(ts.tableName()));
q.bindValue(":VALUE", 1); q.bindValue(":VALUE", 1);
QVERIFY_SQL(q, exec()); QVERIFY_SQL(q, exec());
q.bindValue(":VALUE", 12); q.bindValue(":VALUE", 12);
QVERIFY_SQL(q, exec()); QVERIFY_SQL(q, exec());
q.bindValue(":VALUE", 123); q.bindValue(":VALUE", 123);
QVERIFY_SQL(q, exec()); QVERIFY_SQL(q, exec());
QString sql = "select testval from " + tableName; QString sql = "select testval from " + ts.tableName();
QVERIFY_SQL(q, exec(sql)); QVERIFY_SQL(q, exec(sql));
QVERIFY_SQL(q, next()); QVERIFY_SQL(q, next());
QCOMPARE(q.value(0).toInt(), 1); QCOMPARE(q.value(0).toInt(), 1);
@ -3906,9 +3858,10 @@ void tst_QSqlQuery::QTBUG_21884()
QFETCH(QString, dbName); QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName); QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db); CHECK_DATABASE(db);
TableScope ts(db, "bug21884", __FILE__);
const auto &tableName = ts.tableName();
QSqlQuery q(db); QSqlQuery q(db);
QString tableName(qTableName("bug21884", __FILE__, db));
{ {
const QString good[] = { const QString good[] = {
@ -4005,10 +3958,11 @@ void tst_QSqlQuery::QTBUG_23895()
QFETCH(QString, dbName); QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName); QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db); CHECK_DATABASE(db);
TableScope ts(db, "bug23895", __FILE__);
const auto &tableName = ts.tableName();
QSqlQuery q(db); QSqlQuery q(db);
QString tableName(qTableName("bug23895", __FILE__, db));
q.prepare(QLatin1String("create table %1(id integer primary key, val1 bool, val2 boolean)") q.prepare(QLatin1String("create table %1(id integer primary key, val1 bool, val2 boolean)")
.arg(tableName)); .arg(tableName));
QVERIFY_SQL(q, exec()); QVERIFY_SQL(q, exec());
@ -4059,11 +4013,9 @@ void tst_QSqlQuery::QTBUG_14904()
QFETCH(QString, dbName); QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName); QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db); CHECK_DATABASE(db);
QSqlQuery q(db);
TableScope ts(db, "bug14904", __FILE__); TableScope ts(db, "bug14904", __FILE__);
QSqlQuery q(db);
q.prepare(QLatin1String("create table %1(val1 bool)").arg(ts.tableName())); q.prepare(QLatin1String("create table %1(val1 bool)").arg(ts.tableName()));
QVERIFY_SQL(q, exec()); QVERIFY_SQL(q, exec());
q.prepare(QLatin1String("insert into %1(val1) values(?);").arg(ts.tableName())); q.prepare(QLatin1String("insert into %1(val1) values(?);").arg(ts.tableName()));
@ -4196,9 +4148,9 @@ void tst_QSqlQuery::gisPointDatatype()
QFETCH(QString, dbName); QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName); QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db); CHECK_DATABASE(db);
TableScope ts(db, "qtbug72140", __FILE__);
QSqlQuery sqlQuery(db); QSqlQuery sqlQuery(db);
TableScope ts(db, "qtbug72140", __FILE__);
QVERIFY(sqlQuery.exec(QLatin1String( QVERIFY(sqlQuery.exec(QLatin1String(
"CREATE TABLE %1 (`lonlat_point` POINT NULL) ENGINE = InnoDB;") "CREATE TABLE %1 (`lonlat_point` POINT NULL) ENGINE = InnoDB;")
.arg(ts.tableName()))); .arg(ts.tableName())));
@ -4215,13 +4167,13 @@ void tst_QSqlQuery::oraOCINumber()
QFETCH(QString, dbName); QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName); QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db); CHECK_DATABASE(db);
const QString qtest_oraOCINumber(qTableName("qtest_oraOCINumber", __FILE__, db)); TableScope ts(db, "qtest_oraOCINumber", __FILE__);
QSqlQuery q(db); QSqlQuery q(db);
q.setForwardOnly(true); q.setForwardOnly(true);
QVERIFY_SQL(q, exec(QLatin1String("create table %1 (col1 number(20), col2 number(20))") QVERIFY_SQL(q, exec(QLatin1String("create table %1 (col1 number(20), col2 number(20))")
.arg(qtest_oraOCINumber))); .arg(ts.tableName())));
QVERIFY(q.prepare(QLatin1String("insert into %1 values (?, ?)").arg(qtest_oraOCINumber))); QVERIFY(q.prepare(QLatin1String("insert into %1 values (?, ?)").arg(ts.tableName())));
const QVariantList col1Values = { const QVariantList col1Values = {
qulonglong(1), qulonglong(0), qulonglong(INT_MAX), qulonglong(UINT_MAX), qulonglong(1), qulonglong(0), qulonglong(INT_MAX), qulonglong(UINT_MAX),
@ -4238,7 +4190,7 @@ void tst_QSqlQuery::oraOCINumber()
QVERIFY(q.execBatch()); QVERIFY(q.execBatch());
QVERIFY(q.prepare(QLatin1String( QVERIFY(q.prepare(QLatin1String(
"select * from %1 where col1 = :bindValue0 AND col2 = :bindValue1") "select * from %1 where col1 = :bindValue0 AND col2 = :bindValue1")
.arg(qtest_oraOCINumber))); .arg(ts.tableName())));
q.bindValue(":bindValue0", qulonglong(1), QSql::InOut); q.bindValue(":bindValue0", qulonglong(1), QSql::InOut);
q.bindValue(":bindValue1", qlonglong(1), QSql::InOut); q.bindValue(":bindValue1", qlonglong(1), QSql::InOut);
@ -4310,8 +4262,6 @@ void tst_QSqlQuery::sqlite_constraint()
QFETCH(QString, dbName); QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName); QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db); CHECK_DATABASE(db);
if (tst_Databases::getDatabaseType(db) != QSqlDriver::SQLite)
QSKIP("SQLite3-specific test");
QSqlQuery q(db); QSqlQuery q(db);
const QString trigger(qTableName("test_constraint", __FILE__, db)); const QString trigger(qTableName("test_constraint", __FILE__, db));
@ -4555,9 +4505,9 @@ void runIntegralTypesMysqlTest(QSqlDatabase &db, const QString &tableName, const
{ {
QList<QVariant> variantValues; QList<QVariant> variantValues;
variantValues.reserve(values.size()); variantValues.reserve(values.size());
TableScope ts(db, tableName);
QSqlQuery q(db); QSqlQuery q(db);
QVERIFY_SQL(q, exec("DROP TABLE IF EXISTS " + tableName));
QVERIFY_SQL(q, exec(QLatin1String("CREATE TABLE %2 (id %1)").arg(type, tableName))); QVERIFY_SQL(q, exec(QLatin1String("CREATE TABLE %2 (id %1)").arg(type, tableName)));
if (withPreparedStatement) { if (withPreparedStatement) {
@ -4663,9 +4613,9 @@ void tst_QSqlQuery::QTBUG_57138()
QFETCH(QString, dbName); QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName); QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db); CHECK_DATABASE(db);
TableScope ts(db, "qtbug57138", __FILE__);
QSqlQuery create(db); QSqlQuery create(db);
TableScope ts(db, "qtbug57138", __FILE__);
QVERIFY_SQL(create, exec(QLatin1String( QVERIFY_SQL(create, exec(QLatin1String(
"create table %1 (id int, dt_utc datetime, dt_lt datetime, " "create table %1 (id int, dt_utc datetime, dt_lt datetime, "
@ -4697,10 +4647,9 @@ void tst_QSqlQuery::QTBUG_73286()
QFETCH(QString, dbName); QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName); QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db); CHECK_DATABASE(db);
QSqlQuery create(db);
TableScope ts(db, "qtbug73286", __FILE__); TableScope ts(db, "qtbug73286", __FILE__);
QSqlQuery create(db);
QVERIFY_SQL(create, exec(QLatin1String( QVERIFY_SQL(create, exec(QLatin1String(
"create table %1 (dec2 decimal(4,2), dec0 decimal(20,0), " "create table %1 (dec2 decimal(4,2), dec0 decimal(20,0), "
"dec3 decimal(20,3))").arg(ts.tableName()))); "dec3 decimal(20,3))").arg(ts.tableName())));
@ -4798,8 +4747,6 @@ void tst_QSqlQuery::dateTime_data()
if (!db.isValid()) if (!db.isValid())
continue; continue;
const QString tableNameTSWithTimeZone(qTableName("dateTimeTSWithTimeZone", __FILE__, db)); const QString tableNameTSWithTimeZone(qTableName("dateTimeTSWithTimeZone", __FILE__, db));
const QString tableNameTSWithLocalTimeZone(qTableName("dateTimeTSWithLocalTimeZone",
__FILE__, db));
const QString tableNameTS(qTableName("dateTimeTS", __FILE__, db)); const QString tableNameTS(qTableName("dateTimeTS", __FILE__, db));
const QString tableNameDate(qTableName("dateTimeDate", __FILE__, db)); const QString tableNameDate(qTableName("dateTimeDate", __FILE__, db));
QTest::newRow(QString(dbName + " timestamp with time zone").toLatin1()) QTest::newRow(QString(dbName + " timestamp with time zone").toLatin1())

View File

@ -258,18 +258,16 @@ void tst_QSqlThread::generic_data(const QString& engine)
void tst_QSqlThread::dropTestTables() void tst_QSqlThread::dropTestTables()
{ {
for (int i = 0; i < dbs.dbNames.size(); ++i) { for (const auto &dbName : dbs.dbNames) {
QSqlDatabase db = QSqlDatabase::database(dbs.dbNames.at(i)); QSqlDatabase db = QSqlDatabase::database(dbName);
QSqlQuery q(db); tst_Databases::safeDropTables(db, { qtest, qTableName("qtest2", __FILE__, db), qTableName("emptytable", __FILE__, db) });
tst_Databases::safeDropTables(db, QStringList() << qtest << qTableName("qtest2", __FILE__, db) << qTableName("emptytable", __FILE__, db));
} }
} }
void tst_QSqlThread::createTestTables() void tst_QSqlThread::createTestTables()
{ {
for (int i = 0; i < dbs.dbNames.size(); ++i) { for (const auto &dbName : dbs.dbNames) {
QSqlDatabase db = QSqlDatabase::database(dbs.dbNames.at(i)); QSqlDatabase db = QSqlDatabase::database(dbName);
QSqlQuery q(db); QSqlQuery q(db);
QVERIFY_SQL(q, exec("create table " + qtest QVERIFY_SQL(q, exec("create table " + qtest
@ -285,8 +283,8 @@ void tst_QSqlThread::createTestTables()
void tst_QSqlThread::repopulateTestTables() void tst_QSqlThread::repopulateTestTables()
{ {
for (int i = 0; i < dbs.dbNames.size(); ++i) { for (const auto &dbName : dbs.dbNames) {
QSqlDatabase db = QSqlDatabase::database(dbs.dbNames.at(i)); QSqlDatabase db = QSqlDatabase::database(dbName);
QSqlQuery q(db); QSqlQuery q(db);
QVERIFY_SQL(q, exec("delete from " + qtest)); QVERIFY_SQL(q, exec("delete from " + qtest));

View File

@ -62,9 +62,9 @@ private slots:
void task_QTBUG_4963_setHeaderDataWithProxyModel(); void task_QTBUG_4963_setHeaderDataWithProxyModel();
private: private:
void generic_data(const QString &engine=QString()); void generic_data(const QString &engine = QString());
void dropTestTables(QSqlDatabase db); void dropTestTables(const QSqlDatabase &db);
void createTestTables(QSqlDatabase db); void createTestTables(const QSqlDatabase &db);
void populateTestTables(QSqlDatabase db); void populateTestTables(QSqlDatabase db);
tst_Databases dbs; tst_Databases dbs;
}; };
@ -73,8 +73,8 @@ private:
class DBTestModel: public QSqlQueryModel class DBTestModel: public QSqlQueryModel
{ {
public: public:
DBTestModel(QObject *parent = nullptr): QSqlQueryModel(parent) {} using QSqlQueryModel::QSqlQueryModel;
QModelIndex indexInQuery(const QModelIndex &item) const override { return QSqlQueryModel::indexInQuery(item); } using QSqlQueryModel::indexInQuery;
}; };
tst_QSqlQueryModel::tst_QSqlQueryModel() tst_QSqlQueryModel::tst_QSqlQueryModel()
@ -88,8 +88,8 @@ tst_QSqlQueryModel::~tst_QSqlQueryModel()
void tst_QSqlQueryModel::initTestCase() void tst_QSqlQueryModel::initTestCase()
{ {
QVERIFY(dbs.open()); QVERIFY(dbs.open());
for (QStringList::ConstIterator it = dbs.dbNames.begin(); it != dbs.dbNames.end(); ++it) { for (const auto &dbName : std::as_const(dbs.dbNames)) {
QSqlDatabase db = QSqlDatabase::database((*it)); QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db); CHECK_DATABASE(db);
dropTestTables(db); //in case of leftovers dropTestTables(db); //in case of leftovers
createTestTables(db); createTestTables(db);
@ -99,15 +99,15 @@ void tst_QSqlQueryModel::initTestCase()
void tst_QSqlQueryModel::cleanupTestCase() void tst_QSqlQueryModel::cleanupTestCase()
{ {
for (QStringList::ConstIterator it = dbs.dbNames.begin(); it != dbs.dbNames.end(); ++it) { for (const auto &dbName : std::as_const(dbs.dbNames)) {
QSqlDatabase db = QSqlDatabase::database((*it)); QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db); CHECK_DATABASE(db);
dropTestTables(db); dropTestTables(db);
} }
dbs.close(); dbs.close();
} }
void tst_QSqlQueryModel::dropTestTables(QSqlDatabase db) void tst_QSqlQueryModel::dropTestTables(const QSqlDatabase &db)
{ {
QStringList tableNames; QStringList tableNames;
tableNames << qTableName("test", __FILE__, db) tableNames << qTableName("test", __FILE__, db)
@ -117,7 +117,7 @@ void tst_QSqlQueryModel::dropTestTables(QSqlDatabase db)
tst_Databases::safeDropTables(db, tableNames); tst_Databases::safeDropTables(db, tableNames);
} }
void tst_QSqlQueryModel::createTestTables(QSqlDatabase db) void tst_QSqlQueryModel::createTestTables(const QSqlDatabase &db)
{ {
dropTestTables(db); dropTestTables(db);
QSqlQuery q(db); QSqlQuery q(db);