SQL/Tests: remove usage of 'foreach'

And fix coding style a little bit around the change.

Change-Id: Idfc232a751ccc08d0680351d48d4a68f09f18c1f
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Christian Ehrlicher 2023-02-14 20:09:47 +01:00
parent 5bd93821e5
commit 0bcb0062ba
6 changed files with 42 additions and 59 deletions

View File

@ -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);

View File

@ -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

View File

@ -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);

View File

@ -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));

View File

@ -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) );

View File

@ -576,10 +576,10 @@ void tst_QSqlTableModel::setRecord()
CHECK_DATABASE(db);
const auto test3 = qTableName("test3", __FILE__, db);
QList<QSqlTableModel::EditStrategy> policies = QList<QSqlTableModel::EditStrategy>() << 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);