DB2: Pass on the native error codes to QSqlError

Since DB2 can potentially have more than one error code, we need to join
these together using ';' as a separator.

Task-number: QTBUG-142
Change-Id: Idd376df84a8e3ae4c05b4722b4d0020fa4f3edad
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
bb10
Andy Shaw 2017-12-05 12:55:09 +01:00
parent fa1397882a
commit a899e45495
2 changed files with 49 additions and 11 deletions

View File

@ -156,7 +156,7 @@ static SQLTCHAR* qToTChar(const QString& str)
return (SQLTCHAR*)str.utf16();
}
static QString qWarnDB2Handle(int handleType, SQLHANDLE handle)
static QString qWarnDB2Handle(int handleType, SQLHANDLE handle, int *errorCode)
{
SQLINTEGER nativeCode;
SQLSMALLINT msgLen;
@ -171,22 +171,51 @@ static QString qWarnDB2Handle(int handleType, SQLHANDLE handle)
(SQLTCHAR*) description,
SQL_MAX_MESSAGE_LENGTH - 1, /* in bytes, not in characters */
&msgLen);
if (r == SQL_SUCCESS || r == SQL_SUCCESS_WITH_INFO)
if (r == SQL_SUCCESS || r == SQL_SUCCESS_WITH_INFO) {
if (errorCode)
*errorCode = nativeCode;
return QString(qFromTChar(description));
}
return QString();
}
static QString qDB2Warn(const QDB2DriverPrivate* d)
static QString qDB2Warn(const QDB2DriverPrivate* d, QStringList *errorCodes = nullptr)
{
return (qWarnDB2Handle(SQL_HANDLE_ENV, d->hEnv) + QLatin1Char(' ')
+ qWarnDB2Handle(SQL_HANDLE_DBC, d->hDbc));
int errorCode = 0;
QString error = qWarnDB2Handle(SQL_HANDLE_ENV, d->hEnv, &errorCode);
if (errorCodes && errorCode != 0) {
*errorCodes << QString::number(errorCode);
errorCode = 0;
}
if (!error.isEmpty())
error += QLatin1Char(' ');
error += qWarnDB2Handle(SQL_HANDLE_DBC, d->hDbc, &errorCode);
if (errorCodes && errorCode != 0)
*errorCodes << QString::number(errorCode);
return error;
}
static QString qDB2Warn(const QDB2ResultPrivate* d)
static QString qDB2Warn(const QDB2ResultPrivate* d, QStringList *errorCodes = nullptr)
{
return (qWarnDB2Handle(SQL_HANDLE_ENV, d->drv_d_func()->hEnv) + QLatin1Char(' ')
+ qWarnDB2Handle(SQL_HANDLE_DBC, d->drv_d_func()->hDbc)
+ qWarnDB2Handle(SQL_HANDLE_STMT, d->hStmt));
int errorCode = 0;
QString error = qWarnDB2Handle(SQL_HANDLE_ENV, d->drv_d_func()->hEnv, &errorCode);
if (errorCodes && errorCode != 0) {
*errorCodes << QString::number(errorCode);
errorCode = 0;
}
if (!error.isEmpty())
error += QLatin1Char(' ');
error += qWarnDB2Handle(SQL_HANDLE_DBC, d->drv_d_func()->hDbc, &errorCode);
if (errorCodes && errorCode != 0) {
*errorCodes << QString::number(errorCode);
errorCode = 0;
}
if (!error.isEmpty())
error += QLatin1Char(' ');
error += qWarnDB2Handle(SQL_HANDLE_STMT, d->hStmt, &errorCode);
if (errorCodes && errorCode != 0)
*errorCodes << QString::number(errorCode);
return error;
}
static void qSqlWarning(const QString& message, const QDB2DriverPrivate* d)
@ -204,13 +233,19 @@ static void qSqlWarning(const QString& message, const QDB2ResultPrivate* d)
static QSqlError qMakeError(const QString& err, QSqlError::ErrorType type,
const QDB2DriverPrivate* p)
{
return QSqlError(QLatin1String("QDB2: ") + err, qDB2Warn(p), type);
QStringList errorCodes;
const QString error = qDB2Warn(p, &errorCodes);
return QSqlError(QStringLiteral("QDB2: ") + err, error, type,
errorCodes.join(QLatin1Char(';')));
}
static QSqlError qMakeError(const QString& err, QSqlError::ErrorType type,
const QDB2ResultPrivate* p)
{
return QSqlError(QLatin1String("QDB2: ") + err, qDB2Warn(p), type);
QStringList errorCodes;
const QString error = qDB2Warn(p, &errorCodes);
return QSqlError(QStringLiteral("QDB2: ") + err, error, type,
errorCodes.join(QLatin1Char(';')));
}
static QVariant::Type qDecodeDB2Type(SQLSMALLINT sqltype)

View File

@ -144,6 +144,9 @@ QSqlError::QSqlError(const QString& driverText, const QString& databaseText, Err
Constructs an error containing the driver error text \a
driverText, the database-specific error text \a databaseText, the
type \a type and the error code \a code.
\note DB2: It is possible for DB2 to report more than one error code.
When this happens, \c ; is used as separator between the error codes.
*/
QSqlError::QSqlError(const QString &driverText, const QString &databaseText,
ErrorType type, const QString &code)