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>
bb10
Friedemann Kleint 2014-10-23 15:04:42 +02:00
parent 23cced4530
commit da72e5538e
2 changed files with 20 additions and 0 deletions

View File

@ -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;
}

View File

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