diff options
author | Andy Shaw <andy.shaw@qt.io> | 2017-05-19 15:31:46 +0200 |
---|---|---|
committer | Andy Shaw <andy.shaw@qt.io> | 2017-07-12 09:01:50 +0000 |
commit | 0843c6ca7fe604d8c44b9a81b8fcb072ab424172 (patch) | |
tree | e6784759b9dd8729d5bd179a38552170e084a723 /src/sql/kernel/qsqlrecord.cpp | |
parent | 9423be1f190bd7cdeb04add39f4ec621a33a3041 (diff) |
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 <Jesus.Fernandez@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'src/sql/kernel/qsqlrecord.cpp')
-rw-r--r-- | src/sql/kernel/qsqlrecord.cpp | 17 |
1 files changed, 15 insertions, 2 deletions
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; } |