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