diff options
Diffstat (limited to 'src/sql/kernel')
-rw-r--r-- | src/sql/kernel/qsqlfield.cpp | 42 | ||||
-rw-r--r-- | src/sql/kernel/qsqlfield.h | 5 | ||||
-rw-r--r-- | src/sql/kernel/qsqlrecord.cpp | 17 |
3 files changed, 54 insertions, 10 deletions
diff --git a/src/sql/kernel/qsqlfield.cpp b/src/sql/kernel/qsqlfield.cpp index bb810b11df..59b992e803 100644 --- a/src/sql/kernel/qsqlfield.cpp +++ b/src/sql/kernel/qsqlfield.cpp @@ -47,9 +47,9 @@ class QSqlFieldPrivate { public: QSqlFieldPrivate(const QString &name, - QVariant::Type type) : + QVariant::Type type, const QString &tableName) : ref(1), nm(name), ro(false), type(type), req(QSqlField::Unknown), - len(-1), prec(-1), tp(-1), gen(true), autoval(false) + len(-1), prec(-1), tp(-1), gen(true), autoval(false), table(tableName) { } @@ -64,7 +64,8 @@ public: def(other.def), tp(other.tp), gen(other.gen), - autoval(other.autoval) + autoval(other.autoval), + table(other.table) {} bool operator==(const QSqlFieldPrivate& other) const @@ -77,7 +78,8 @@ public: && prec == other.prec && def == other.def && gen == other.gen - && autoval == other.autoval); + && autoval == other.autoval + && table == other.table); } QAtomicInt ref; @@ -91,6 +93,7 @@ public: int tp; uint gen: 1; uint autoval: 1; + QString table; }; @@ -153,14 +156,15 @@ public: /*! Constructs an empty field called \a fieldName of variant type \a - type. + type in \a table. \sa setRequiredStatus(), setLength(), setPrecision(), setDefaultValue(), setGenerated(), setReadOnly() */ -QSqlField::QSqlField(const QString& fieldName, QVariant::Type type) +QSqlField::QSqlField(const QString &fieldName, QVariant::Type type, + const QString &table) { - d = new QSqlFieldPrivate(fieldName, type); + d = new QSqlFieldPrivate(fieldName, type, table); val = QVariant(type); } @@ -518,6 +522,7 @@ QDebug operator<<(QDebug dbg, const QSqlField &f) QDebugStateSaver saver(dbg); dbg.nospace(); dbg << "QSqlField(" << f.name() << ", " << QMetaType::typeName(f.type()); + dbg << ", tableName: " << (f.tableName().isEmpty() ? QStringLiteral("(not specified)") : f.tableName()); if (f.length() >= 0) dbg << ", length: " << f.length(); if (f.precision() >= 0) @@ -565,4 +570,27 @@ void QSqlField::setAutoValue(bool autoVal) d->autoval = autoVal; } +/*! + Sets the tableName of the field to \a table. + + \sa tableName() +*/ + +void QSqlField::setTableName(const QString &table) +{ + detach(); + d->table = table; +} + +/*! + Returns the tableName of the field. + + \sa setTableName() +*/ + +QString QSqlField::tableName() const +{ + return d->table; +} + QT_END_NAMESPACE diff --git a/src/sql/kernel/qsqlfield.h b/src/sql/kernel/qsqlfield.h index 0d8c51f801..30474735f4 100644 --- a/src/sql/kernel/qsqlfield.h +++ b/src/sql/kernel/qsqlfield.h @@ -55,7 +55,8 @@ public: enum RequiredStatus { Unknown = -1, Optional = 0, Required = 1 }; explicit QSqlField(const QString& fieldName = QString(), - QVariant::Type type = QVariant::Invalid); + QVariant::Type type = QVariant::Invalid, + const QString &tableName = QString()); QSqlField(const QSqlField& other); QSqlField& operator=(const QSqlField& other); @@ -68,6 +69,8 @@ public: { return val; } void setName(const QString& name); QString name() const; + void setTableName(const QString &tableName); + QString tableName() const; bool isNull() const; void setReadOnly(bool readOnly); bool isReadOnly() const; 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; } |