From 21b3b54193b9d90ee377fdfa306e21d2fdfe2837 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Mon, 12 Apr 2021 11:39:21 +0200 Subject: [PATCH] QSqlTableModel::orderByClause(): Quote the table name This ensures correct handling of names with special characters. Pick-to: 5.15 6.1 Fixes: QTBUG-92584 Change-Id: I95c7c54d9c7ee00b221a55f3d07ef1ec3a3bd217 Reviewed-by: Andy Shaw Reviewed-by: Friedemann Kleint Reviewed-by: Edward Welbourne --- src/sql/models/qsqltablemodel.cpp | 3 +- .../qsqltablemodel/tst_qsqltablemodel.cpp | 50 ++++++++++--------- 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/src/sql/models/qsqltablemodel.cpp b/src/sql/models/qsqltablemodel.cpp index 570101289c..5d1ce1784c 100644 --- a/src/sql/models/qsqltablemodel.cpp +++ b/src/sql/models/qsqltablemodel.cpp @@ -1001,7 +1001,8 @@ QString QSqlTableModel::orderByClause() const //we can safely escape the field because it would have been obtained from the database //and have the correct case - QString field = d->tableName + QLatin1Char('.') + QString field = d->db.driver()->escapeIdentifier(d->tableName, QSqlDriver::TableName) + + QLatin1Char('.') + d->db.driver()->escapeIdentifier(f.name(), QSqlDriver::FieldName); field = d->sortOrder == Qt::AscendingOrder ? Sql::asc(field) : Sql::desc(field); return Sql::orderBy(field); diff --git a/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp b/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp index a940506359..a83cb37605 100644 --- a/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp +++ b/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp @@ -310,35 +310,37 @@ void tst_QSqlTableModel::select() QFETCH(QString, dbName); QSqlDatabase db = QSqlDatabase::database(dbName); CHECK_DATABASE(db); - const auto test = qTableName("test1", __FILE__, db); + const QStringList tables = {qTableName("test1", __FILE__, db), + qTableName("test1", __FILE__, db).remove(QLatin1Char('"'))}; + for (const QString &tbl : tables) { + QSqlTableModel model(0, db); + model.setTable(tbl); + model.setSort(0, Qt::AscendingOrder); + QVERIFY_SQL(model, select()); - QSqlTableModel model(0, db); - model.setTable(test); - model.setSort(0, Qt::AscendingOrder); - QVERIFY_SQL(model, select()); + QCOMPARE(model.rowCount(), 3); + QCOMPARE(model.columnCount(), 3); - QCOMPARE(model.rowCount(), 3); - QCOMPARE(model.columnCount(), 3); + QCOMPARE(model.data(model.index(0, 0)).toInt(), 1); + QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry")); + QCOMPARE(model.data(model.index(0, 2)).toInt(), 1); + QCOMPARE(model.data(model.index(0, 3)), QVariant()); - QCOMPARE(model.data(model.index(0, 0)).toInt(), 1); - QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry")); - QCOMPARE(model.data(model.index(0, 2)).toInt(), 1); - QCOMPARE(model.data(model.index(0, 3)), QVariant()); + QCOMPARE(model.data(model.index(1, 0)).toInt(), 2); + QCOMPARE(model.data(model.index(1, 1)).toString(), QString("trond")); + QCOMPARE(model.data(model.index(1, 2)).toInt(), 2); + QCOMPARE(model.data(model.index(1, 3)), QVariant()); - QCOMPARE(model.data(model.index(1, 0)).toInt(), 2); - QCOMPARE(model.data(model.index(1, 1)).toString(), QString("trond")); - QCOMPARE(model.data(model.index(1, 2)).toInt(), 2); - QCOMPARE(model.data(model.index(1, 3)), QVariant()); + QCOMPARE(model.data(model.index(2, 0)).toInt(), 3); + QCOMPARE(model.data(model.index(2, 1)).toString(), QString("vohi")); + QCOMPARE(model.data(model.index(2, 2)).toInt(), 3); + QCOMPARE(model.data(model.index(2, 3)), QVariant()); - QCOMPARE(model.data(model.index(2, 0)).toInt(), 3); - QCOMPARE(model.data(model.index(2, 1)).toString(), QString("vohi")); - QCOMPARE(model.data(model.index(2, 2)).toInt(), 3); - QCOMPARE(model.data(model.index(2, 3)), QVariant()); - - QCOMPARE(model.data(model.index(3, 0)), QVariant()); - QCOMPARE(model.data(model.index(3, 1)), QVariant()); - QCOMPARE(model.data(model.index(3, 2)), QVariant()); - QCOMPARE(model.data(model.index(3, 3)), QVariant()); + QCOMPARE(model.data(model.index(3, 0)), QVariant()); + QCOMPARE(model.data(model.index(3, 1)), QVariant()); + QCOMPARE(model.data(model.index(3, 2)), QVariant()); + QCOMPARE(model.data(model.index(3, 3)), QVariant()); + } } class SelectRowModel: public QSqlTableModel