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 <andy.shaw@qt.io>
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Friedemann Kleint 2021-04-12 11:39:21 +02:00 committed by Andy Shaw
parent 18f5b83e97
commit 21b3b54193
2 changed files with 28 additions and 25 deletions

View File

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

View File

@ -310,10 +310,11 @@ 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(test);
model.setTable(tbl);
model.setSort(0, Qt::AscendingOrder);
QVERIFY_SQL(model, select());
@ -340,6 +341,7 @@ void tst_QSqlTableModel::select()
QCOMPARE(model.data(model.index(3, 2)), QVariant());
QCOMPARE(model.data(model.index(3, 3)), QVariant());
}
}
class SelectRowModel: public QSqlTableModel
{