From 0bcb0062baf58861139e911b07ac1057312b39f6 Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Tue, 14 Feb 2023 20:09:47 +0100 Subject: [PATCH] SQL/Tests: remove usage of 'foreach' And fix coding style a little bit around the change. Change-Id: Idfc232a751ccc08d0680351d48d4a68f09f18c1f Reviewed-by: Volker Hilsheimer --- tests/auto/sql/kernel/qsql/tst_qsql.cpp | 20 +----- .../sql/kernel/qsqldatabase/tst_databases.h | 61 +++++++++---------- .../sql/kernel/qsqldriver/tst_qsqldriver.cpp | 4 +- .../tst_qsqlrelationaldelegate.cpp | 6 +- .../tst_qsqlrelationaltablemodel.cpp | 6 +- .../qsqltablemodel/tst_qsqltablemodel.cpp | 4 +- 6 files changed, 42 insertions(+), 59 deletions(-) diff --git a/tests/auto/sql/kernel/qsql/tst_qsql.cpp b/tests/auto/sql/kernel/qsql/tst_qsql.cpp index 55cbfad421..78f48a4117 100644 --- a/tests/auto/sql/kernel/qsql/tst_qsql.cpp +++ b/tests/auto/sql/kernel/qsql/tst_qsql.cpp @@ -19,11 +19,6 @@ class tst_QSql : public QObject { Q_OBJECT -public: - tst_QSql(); - virtual ~tst_QSql(); - - public slots: void initTestCase(); void cleanupTestCase(); @@ -41,15 +36,6 @@ private slots: }; /****************** General Qt SQL Module tests *****************/ - -tst_QSql::tst_QSql() -{ -} - -tst_QSql::~tst_QSql() -{ -} - void tst_QSql::initTestCase() { } @@ -80,7 +66,7 @@ void tst_QSql::basicDriverTest() tst_Databases dbs; QVERIFY(dbs.open()); - foreach (const QString& dbName, dbs.dbNames) { + for (const QString &dbName : std::as_const(dbs.dbNames)) { QSqlDatabase db = QSqlDatabase::database(dbName); QVERIFY_SQL(db, isValid()); @@ -150,7 +136,7 @@ void tst_QSql::concurrentAccess() tst_Databases dbs; QVERIFY(dbs.open()); - foreach (const QString& dbName, dbs.dbNames) { + for (const QString &dbName : std::as_const(dbs.dbNames)) { QSqlDatabase db = QSqlDatabase::database(dbName); QVERIFY(db.isValid()); if (tst_Databases::isMSAccess(db)) @@ -182,7 +168,7 @@ void tst_QSql::openErrorRecovery() QVERIFY(dbs.addDbs()); if (dbs.dbNames.isEmpty()) QSKIP("No database drivers installed"); - foreach (const QString& dbName, dbs.dbNames) { + for (const QString &dbName : std::as_const(dbs.dbNames)) { QSqlDatabase db = QSqlDatabase::database(dbName, false); CHECK_DATABASE(db); diff --git a/tests/auto/sql/kernel/qsqldatabase/tst_databases.h b/tests/auto/sql/kernel/qsqldatabase/tst_databases.h index 8b1f443ae8..57605d6a12 100644 --- a/tests/auto/sql/kernel/qsqldatabase/tst_databases.h +++ b/tests/auto/sql/kernel/qsqldatabase/tst_databases.h @@ -289,23 +289,22 @@ public: } // drop a table only if it exists to prevent warnings - static void safeDropTables( QSqlDatabase db, const QStringList& tableNames ) + static void safeDropTables(QSqlDatabase db, const QStringList &tableNames) { - bool wasDropped; - QSqlQuery q( db ); - QStringList dbtables=db.tables(); + QSqlQuery q(db); + QStringList dbtables = db.tables(); QSqlDriver::DbmsType dbType = getDatabaseType(db); - foreach(const QString &tableName, tableNames) + for (const QString &tableName : tableNames) { - wasDropped = true; - QString table=tableName; - if ( db.driver()->isIdentifierEscaped(table, QSqlDriver::TableName)) + bool wasDropped = true; + QString table = tableName; + if (db.driver()->isIdentifierEscaped(table, QSqlDriver::TableName)) table = db.driver()->stripDelimiters(table, QSqlDriver::TableName); - if ( dbtables.contains( table, Qt::CaseInsensitive ) ) { - foreach(const QString &table2, dbtables.filter(table, Qt::CaseInsensitive)) { - if(table2.compare(table.section('.', -1, -1), Qt::CaseInsensitive) == 0) { - table=db.driver()->escapeIdentifier(table2, QSqlDriver::TableName); + if (dbtables.contains(table, Qt::CaseInsensitive)) { + for (const QString &table2 : dbtables.filter(table, Qt::CaseInsensitive)) { + if (table2.compare(table.section('.', -1, -1), Qt::CaseInsensitive) == 0) { + table = db.driver()->escapeIdentifier(table2, QSqlDriver::TableName); if (dbType == QSqlDriver::PostgreSQL || dbType == QSqlDriver::MimerSQL) wasDropped = q.exec( "drop table " + table + " cascade"); else @@ -314,7 +313,7 @@ public: } } } - if ( !wasDropped ) { + if (!wasDropped) { qWarning() << dbToString(db) << "unable to drop table" << tableName << ':' << q.lastError(); // qWarning() << "last query:" << q.lastQuery(); // qWarning() << "dbtables:" << dbtables; @@ -323,38 +322,36 @@ public: } } - static void safeDropTable( QSqlDatabase db, const QString& tableName ) + static void safeDropTable(QSqlDatabase db, const QString &tableName) { - safeDropTables(db, QStringList() << tableName); + safeDropTables(db, {tableName}); } - static void safeDropViews( QSqlDatabase db, const QStringList &viewNames ) + static void safeDropViews(QSqlDatabase db, const QStringList &viewNames) { - if ( isMSAccess( db ) ) // Access is sooo stupid. - safeDropTables( db, viewNames ); + if (isMSAccess(db)) // Access is sooo stupid. + safeDropTables(db, viewNames); - bool wasDropped; - QSqlQuery q( db ); - QStringList dbtables=db.tables(QSql::Views); - - foreach(QString viewName, viewNames) + QSqlQuery q(db); + QStringList dbtables = db.tables(QSql::Views); + for (const QString &viewName : viewNames) { - wasDropped = true; - QString view=viewName; - if ( db.driver()->isIdentifierEscaped(view, QSqlDriver::TableName)) + bool wasDropped = true; + QString view = viewName; + if (db.driver()->isIdentifierEscaped(view, QSqlDriver::TableName)) view = db.driver()->stripDelimiters(view, QSqlDriver::TableName); - if ( dbtables.contains( view, Qt::CaseInsensitive ) ) { - foreach(const QString &view2, dbtables.filter(view, Qt::CaseInsensitive)) { - if(view2.compare(view.section('.', -1, -1), Qt::CaseInsensitive) == 0) { - view=db.driver()->escapeIdentifier(view2, QSqlDriver::TableName); - wasDropped = q.exec( "drop view " + view); + if (dbtables.contains(view, Qt::CaseInsensitive)) { + for (const QString &view2 : dbtables.filter(view, Qt::CaseInsensitive)) { + if (view2.compare(view.section('.', -1, -1), Qt::CaseInsensitive) == 0) { + view = db.driver()->escapeIdentifier(view2, QSqlDriver::TableName); + wasDropped = q.exec("drop view " + view); dbtables.removeAll(view); } } } - if ( !wasDropped ) + if (!wasDropped) qWarning() << dbToString(db) << "unable to drop view" << viewName << ':' << q.lastError(); // << "\nlast query:" << q.lastQuery() // << "\ndbtables:" << dbtables diff --git a/tests/auto/sql/kernel/qsqldriver/tst_qsqldriver.cpp b/tests/auto/sql/kernel/qsqldriver/tst_qsqldriver.cpp index 5bd30948c8..7c0d737b3b 100644 --- a/tests/auto/sql/kernel/qsqldriver/tst_qsqldriver.cpp +++ b/tests/auto/sql/kernel/qsqldriver/tst_qsqldriver.cpp @@ -81,13 +81,13 @@ void tst_QSqlDriver::recreateTestTables(QSqlDatabase db) void tst_QSqlDriver::initTestCase() { - foreach (const QString &dbname, dbs.dbNames) + for (const QString &dbname : std::as_const(dbs.dbNames)) recreateTestTables(QSqlDatabase::database(dbname)); } void tst_QSqlDriver::cleanupTestCase() { - foreach (const QString &dbName, dbs.dbNames) { + for (const QString &dbName : std::as_const(dbs.dbNames)) { QSqlDatabase db = QSqlDatabase::database(dbName); QStringList tables = {qTableName("relTEST1", __FILE__, db)}; const QSqlDriver::DbmsType dbType = tst_Databases::getDatabaseType(db); diff --git a/tests/auto/sql/models/qsqlrelationaldelegate/tst_qsqlrelationaldelegate.cpp b/tests/auto/sql/models/qsqlrelationaldelegate/tst_qsqlrelationaldelegate.cpp index 80a3a6d84e..575b7bc45b 100644 --- a/tests/auto/sql/models/qsqlrelationaldelegate/tst_qsqlrelationaldelegate.cpp +++ b/tests/auto/sql/models/qsqlrelationaldelegate/tst_qsqlrelationaldelegate.cpp @@ -77,8 +77,8 @@ void tst_QSqlRelationalDelegate::recreateTestTables(QSqlDatabase db) void tst_QSqlRelationalDelegate::initTestCase() { - foreach (const QString &dbname, dbs.dbNames) { - QSqlDatabase db = QSqlDatabase::database(dbname); + for (const QString &dbName : std::as_const(dbs.dbNames)) { + QSqlDatabase db = QSqlDatabase::database(dbName); QSqlQuery q(db); QSqlDriver::DbmsType dbType = tst_Databases::getDatabaseType(db); if (dbType == QSqlDriver::Interbase) { @@ -95,7 +95,7 @@ void tst_QSqlRelationalDelegate::initTestCase() void tst_QSqlRelationalDelegate::cleanupTestCase() { - foreach (const QString &dbName, dbs.dbNames) { + for (const QString &dbName : std::as_const(dbs.dbNames)) { QSqlDatabase db = QSqlDatabase::database(dbName); CHECK_DATABASE(db); dropTestTables(QSqlDatabase::database(dbName)); diff --git a/tests/auto/sql/models/qsqlrelationaltablemodel/tst_qsqlrelationaltablemodel.cpp b/tests/auto/sql/models/qsqlrelationaltablemodel/tst_qsqlrelationaltablemodel.cpp index 5277d7b371..51dcf39bfa 100644 --- a/tests/auto/sql/models/qsqlrelationaltablemodel/tst_qsqlrelationaltablemodel.cpp +++ b/tests/auto/sql/models/qsqlrelationaltablemodel/tst_qsqlrelationaltablemodel.cpp @@ -133,8 +133,8 @@ void tst_QSqlRelationalTableModel::recreateTestTables(QSqlDatabase db) void tst_QSqlRelationalTableModel::initTestCase() { - foreach (const QString &dbname, dbs.dbNames) { - QSqlDatabase db = QSqlDatabase::database(dbname); + for (const QString &dbName : std::as_const(dbs.dbNames)) { + QSqlDatabase db = QSqlDatabase::database(dbName); QSqlQuery q(db); QSqlDriver::DbmsType dbType = tst_Databases::getDatabaseType(db); if (dbType == QSqlDriver::Interbase) { @@ -151,7 +151,7 @@ void tst_QSqlRelationalTableModel::initTestCase() void tst_QSqlRelationalTableModel::cleanupTestCase() { - foreach (const QString &dbName, dbs.dbNames) { + for (const QString &dbName : std::as_const(dbs.dbNames)) { QSqlDatabase db = QSqlDatabase::database(dbName); CHECK_DATABASE( db ); dropTestTables( QSqlDatabase::database(dbName) ); diff --git a/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp b/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp index 8883fc6ba5..2a9bc05294 100644 --- a/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp +++ b/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp @@ -576,10 +576,10 @@ void tst_QSqlTableModel::setRecord() CHECK_DATABASE(db); const auto test3 = qTableName("test3", __FILE__, db); - QList policies = QList() << QSqlTableModel::OnFieldChange << QSqlTableModel::OnRowChange << QSqlTableModel::OnManualSubmit; + const auto policies = { QSqlTableModel::OnFieldChange, QSqlTableModel::OnRowChange, QSqlTableModel::OnManualSubmit }; QString Xsuffix; - foreach( QSqlTableModel::EditStrategy submitpolicy, policies) { + for (QSqlTableModel::EditStrategy submitpolicy : policies) { QSqlTableModel model(0, db); model.setEditStrategy((QSqlTableModel::EditStrategy)submitpolicy);