diff options
author | Friedemann Kleint <Friedemann.Kleint@digia.com> | 2014-10-23 15:04:42 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@digia.com> | 2014-10-27 10:56:13 +0100 |
commit | da72e5538ebcc7e6008d0c4b3538d2a994f02a7e (patch) | |
tree | d7bfce43631d37cac486442f51ddfc6a1dc9c329 /src/sql | |
parent | 23cced453030492c72dea0968e4195eae18d6653 (diff) |
Retrieve auto-value for ODBC fields.
Introduce a static function returning the value called from
qMakeFieldInfo(SQLHANDLE). The field is currently only populated
when executing a query. Populating it from QODBCDriver::record()
would require executing a dummy query, which is problematic since
QSqlField does not have any provisions for delayed evaluation.
Document the limitation.
Task-number: QTBUG-39388
Change-Id: Ib2d2f2653b8b757389f627142c61c13a117fef72
Reviewed-by: Leena Miettinen <riitta-leena.miettinen@digia.com>
Reviewed-by: Joerg Bornemann <joerg.bornemann@theqtcompany.com>
Diffstat (limited to 'src/sql')
-rw-r--r-- | src/sql/drivers/odbc/qsql_odbc.cpp | 14 | ||||
-rw-r--r-- | src/sql/kernel/qsqlfield.cpp | 6 |
2 files changed, 20 insertions, 0 deletions
diff --git a/src/sql/drivers/odbc/qsql_odbc.cpp b/src/sql/drivers/odbc/qsql_odbc.cpp index df0e97b798..d9b31c7702 100644 --- a/src/sql/drivers/odbc/qsql_odbc.cpp +++ b/src/sql/drivers/odbc/qsql_odbc.cpp @@ -579,6 +579,19 @@ static QVariant qGetBigIntData(SQLHANDLE hStmt, int column, bool isSigned = true return quint64(lngbuf); } +static bool isAutoValue(const SQLHANDLE hStmt, int column) +{ + SQLLEN nNumericAttribute = 0; // Check for auto-increment + const SQLRETURN r = ::SQLColAttribute(hStmt, column + 1, SQL_DESC_AUTO_UNIQUE_VALUE, + 0, 0, 0, &nNumericAttribute); + if (r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO) { + qSqlWarning(QStringLiteral("qMakeField: Unable to get autovalue attribute for column ") + + QString::number(column), hStmt); + return false; + } + return nNumericAttribute != SQL_FALSE; +} + static QSqlField qMakeFieldInfo(const SQLHANDLE hStmt, int i, QString *errorMessage); // creates a QSqlField from a valid hStmt generated @@ -662,6 +675,7 @@ static QSqlField qMakeFieldInfo(const SQLHANDLE hStmt, int i, QString *errorMess else if (nullable == SQL_NULLABLE) f.setRequired(false); // else we don't know + f.setAutoValue(isAutoValue(hStmt, i)); return f; } diff --git a/src/sql/kernel/qsqlfield.cpp b/src/sql/kernel/qsqlfield.cpp index dfd4712071..2022871432 100644 --- a/src/sql/kernel/qsqlfield.cpp +++ b/src/sql/kernel/qsqlfield.cpp @@ -531,6 +531,12 @@ QDebug operator<<(QDebug dbg, const QSqlField &f) Returns \c true if the value is auto-generated by the database, for example auto-increment primary key values. + \note When using the ODBC driver, due to limitations in the ODBC API, + the \c isAutoValue() field is only populated in a QSqlField resulting from a + QSqlRecord obtained by executing a \c SELECT query. It is \c false in a QSqlField + resulting from a QSqlRecord returned from QSqlDatabase::record() or + QSqlDatabase::primaryIndex(). + \sa setAutoValue() */ bool QSqlField::isAutoValue() const |