diff --git a/src/sql/kernel/qsqlerror.cpp b/src/sql/kernel/qsqlerror.cpp index a2ae453756..1763722e8a 100644 --- a/src/sql/kernel/qsqlerror.cpp +++ b/src/sql/kernel/qsqlerror.cpp @@ -47,7 +47,7 @@ QT_BEGIN_NAMESPACE #ifndef QT_NO_DEBUG_STREAM QDebug operator<<(QDebug dbg, const QSqlError &s) { - dbg.nospace() << "QSqlError(" << s.number() << ", " << s.driverText() << + dbg.nospace() << "QSqlError(" << s.nativeErrorCode() << ", " << s.driverText() << ", " << s.databaseText() << ')'; return dbg.space(); } @@ -59,7 +59,7 @@ public: QString driverError; QString databaseError; QSqlError::ErrorType errorType; - int errorNumber; + QString errorCode; }; @@ -72,7 +72,7 @@ public: A QSqlError object can provide database-specific error data, including the driverText() and databaseText() messages (or both - concatenated together as text()), and the error number() and + concatenated together as text()), and the nativeErrorCode() and type(). \sa QSqlDatabase::lastError(), QSqlQuery::lastError() @@ -91,11 +91,14 @@ public: */ /*! + \obsolete + Constructs an error containing the driver error text \a driverText, the database-specific error text \a databaseText, the type \a type and the optional error number \a number. */ +#if QT_DEPRECATED_SINCE(5, 3) QSqlError::QSqlError(const QString& driverText, const QString& databaseText, ErrorType type, int number) { @@ -104,8 +107,27 @@ QSqlError::QSqlError(const QString& driverText, const QString& databaseText, Err d->driverError = driverText; d->databaseError = databaseText; d->errorType = type; - d->errorNumber = number; + d->errorCode = QString::number(number); } +#endif + +/*! + 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. +*/ + +QSqlError::QSqlError(const QString &driverText, const QString &databaseText, + ErrorType type, const QString &code) +{ + d = new QSqlErrorPrivate; + + d->driverError = driverText; + d->databaseError = databaseText; + d->errorType = type; + d->errorCode = code; +} + /*! Creates a copy of \a other. @@ -246,14 +268,28 @@ void QSqlError::setType(ErrorType type) #endif /*! + \fn int QSqlError::number() const + \obsolete + Returns the database-specific error number, or -1 if it cannot be determined. + + Returns 0 if the error code is not an integer. + + \warning Some databases use alphanumeric error codes, which makes + number() unreliable if such a database is used. + + Use nativeErrorCode() instead + + \sa nativeErrorCode() */ +#if QT_DEPRECATED_SINCE(5, 3) int QSqlError::number() const { - return d->errorNumber; + return d->errorCode.toInt(); } +#endif /*! \fn void QSqlError::setNumber(int number) @@ -270,10 +306,20 @@ int QSqlError::number() const #if QT_DEPRECATED_SINCE(5, 1) void QSqlError::setNumber(int number) { - d->errorNumber = number; + d->errorCode = QString::number(number); } #endif +/*! + Returns the database-specific error code, or an empty string if + it cannot be determined. +*/ + +QString QSqlError::nativeErrorCode() const +{ + return d->errorCode; +} + /*! This is a convenience function that returns databaseText() and driverText() concatenated into a single string. diff --git a/src/sql/kernel/qsqlerror.h b/src/sql/kernel/qsqlerror.h index 0efe0e07b0..c2afe7c819 100644 --- a/src/sql/kernel/qsqlerror.h +++ b/src/sql/kernel/qsqlerror.h @@ -59,10 +59,16 @@ public: TransactionError, UnknownError }; +#if QT_DEPRECATED_SINCE(5, 3) QSqlError( const QString& driverText = QString(), const QString& databaseText = QString(), ErrorType type = NoError, int number = -1); +#endif + QSqlError(const QString &driverText, + const QString &databaseText, + ErrorType type, + const QString &errorCode); QSqlError(const QSqlError& other); QSqlError& operator=(const QSqlError& other); bool operator==(const QSqlError& other) const; @@ -72,7 +78,10 @@ public: QString driverText() const; QString databaseText() const; ErrorType type() const; +#if QT_DEPRECATED_SINCE(5, 3) int number() const; +#endif + QString nativeErrorCode() const; QString text() const; bool isValid() const; diff --git a/tests/auto/sql/kernel/qsqldatabase/tst_databases.h b/tests/auto/sql/kernel/qsqldatabase/tst_databases.h index 69ecbcb019..59a16bc569 100644 --- a/tests/auto/sql/kernel/qsqldatabase/tst_databases.h +++ b/tests/auto/sql/kernel/qsqldatabase/tst_databases.h @@ -515,8 +515,8 @@ public: static QByteArray printError( const QSqlError& err ) { QString result; - if(err.number() > 0) - result += '(' + QString::number(err.number()) + ") "; + if (!err.nativeErrorCode().isEmpty()) + result += '(' + err.nativeErrorCode() + ") "; result += '\''; if(!err.driverText().isEmpty()) result += err.driverText() + "' || '"; @@ -527,8 +527,8 @@ public: static QByteArray printError( const QSqlError& err, const QSqlDatabase& db ) { QString result(dbToString(db) + ": "); - if(err.number() > 0) - result += '(' + QString::number(err.number()) + ") "; + if (!err.nativeErrorCode().isEmpty()) + result += '(' + err.nativeErrorCode() + ") "; result += '\''; if(!err.driverText().isEmpty()) result += err.driverText() + "' || '"; diff --git a/tests/auto/sql/kernel/qsqlerror/tst_qsqlerror.cpp b/tests/auto/sql/kernel/qsqlerror/tst_qsqlerror.cpp index bee4441c0f..9763e3e7e6 100644 --- a/tests/auto/sql/kernel/qsqlerror/tst_qsqlerror.cpp +++ b/tests/auto/sql/kernel/qsqlerror/tst_qsqlerror.cpp @@ -120,6 +120,24 @@ void tst_QSqlError::construction() QSqlError obj4; QVERIFY(!obj4.isValid()); + + QSqlError obj5(QStringLiteral("drivertext"), QStringLiteral("databasetext"), + QSqlError::UnknownError, QStringLiteral("123")); + QCOMPARE(obj5.driverText(), QString("drivertext")); + QCOMPARE(obj5.databaseText(), QString("databasetext")); + QCOMPARE(obj5.type(), QSqlError::UnknownError); + QCOMPARE(obj5.number(), 123); + QCOMPARE(obj5.nativeErrorCode(), QStringLiteral("123")); + QVERIFY(obj5.isValid()); + + QSqlError obj6(QStringLiteral("drivertext"), QStringLiteral("databasetext"), + QSqlError::UnknownError, QStringLiteral("Err123")); + QCOMPARE(obj6.driverText(), QString("drivertext")); + QCOMPARE(obj6.databaseText(), QString("databasetext")); + QCOMPARE(obj6.type(), QSqlError::UnknownError); + QCOMPARE(obj6.number(), 0); + QCOMPARE(obj6.nativeErrorCode(), QStringLiteral("Err123")); + QVERIFY(obj6.isValid()); } void tst_QSqlError::operators()