From 0843c6ca7fe604d8c44b9a81b8fcb072ab424172 Mon Sep 17 00:00:00 2001 From: Andy Shaw Date: Fri, 19 May 2017 15:31:46 +0200 Subject: QSqlField: Add a means to see what the tablename is for a given field When you are using a query that pulls from a number of different tables then it can be ambiguous as to which table a particular field belongs to. So this will make it possible to determine the table that a given field belongs to if it is set. Task-number: QTBUG-7170 Change-Id: I49b7890c0523d81272a153df3860df800ff853d5 Reviewed-by: Jesus Fernandez Reviewed-by: Edward Welbourne --- src/sql/kernel/qsqlrecord.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'src/sql/kernel/qsqlrecord.cpp') diff --git a/src/sql/kernel/qsqlrecord.cpp b/src/sql/kernel/qsqlrecord.cpp index d5adff67a4..1c9ad5ec63 100644 --- a/src/sql/kernel/qsqlrecord.cpp +++ b/src/sql/kernel/qsqlrecord.cpp @@ -232,10 +232,23 @@ QString QSqlRecord::fieldName(int index) const int QSqlRecord::indexOf(const QString& name) const { - QString nm = name.toUpper(); + QString tableName; + QString fieldName = name; + const int idx = name.indexOf(QLatin1Char('.')); + if (idx != -1) { + tableName = name.left(idx); + fieldName = name.mid(idx + 1); + } for (int i = 0; i < count(); ++i) { - if (d->fields.at(i).name().toUpper() == nm) // TODO: case-insensitive comparison + // Check the passed in name first in case it is an alias using a dot. + // Then check if both the table and field match when there is a table name specified. + const auto currentField = d->fields.at(i); + const auto currentFieldName = currentField.name(); + if (currentFieldName.compare(name, Qt::CaseInsensitive) == 0 + || (idx != -1 && currentFieldName.compare(fieldName, Qt::CaseInsensitive) == 0 + && currentField.tableName().compare(tableName, Qt::CaseInsensitive) == 0)) { return i; + } } return -1; } -- cgit v1.2.3