diff --git a/src/sql/kernel/qsqlerror.cpp b/src/sql/kernel/qsqlerror.cpp index 1763722e8a..1979e5d1f5 100644 --- a/src/sql/kernel/qsqlerror.cpp +++ b/src/sql/kernel/qsqlerror.cpp @@ -107,7 +107,8 @@ QSqlError::QSqlError(const QString& driverText, const QString& databaseText, Err d->driverError = driverText; d->databaseError = databaseText; d->errorType = type; - d->errorCode = QString::number(number); + if (number != -1) + d->errorCode = QString::number(number); } #endif @@ -287,7 +288,7 @@ void QSqlError::setType(ErrorType type) #if QT_DEPRECATED_SINCE(5, 3) int QSqlError::number() const { - return d->errorCode.toInt(); + return d->errorCode.isEmpty() ? -1 : d->errorCode.toInt(); } #endif diff --git a/src/sql/kernel/qsqlerror.h b/src/sql/kernel/qsqlerror.h index 4e27ab03ae..679e0f9e3c 100644 --- a/src/sql/kernel/qsqlerror.h +++ b/src/sql/kernel/qsqlerror.h @@ -60,15 +60,13 @@ public: UnknownError }; #if QT_DEPRECATED_SINCE(5, 3) - QSqlError( const QString& driverText = QString(), - const QString& databaseText = QString(), - ErrorType type = NoError, - int number = -1); + QT_DEPRECATED QSqlError(const QString &driverText, const QString &databaseText, + ErrorType type, int number); #endif - QSqlError(const QString &driverText, - const QString &databaseText, - ErrorType type, - const QString &errorCode); + QSqlError(const QString &driverText = QString(), + const QString &databaseText = QString(), + ErrorType type = NoError, + const QString &errorCode = QString()); QSqlError(const QSqlError& other); QSqlError& operator=(const QSqlError& other); bool operator==(const QSqlError& other) const; @@ -79,7 +77,7 @@ public: QString databaseText() const; ErrorType type() const; #if QT_DEPRECATED_SINCE(5, 3) - int number() const; + QT_DEPRECATED int number() const; #endif QString nativeErrorCode() const; QString text() const; diff --git a/tests/auto/sql/kernel/qsqlerror/tst_qsqlerror.cpp b/tests/auto/sql/kernel/qsqlerror/tst_qsqlerror.cpp index 9763e3e7e6..847453b3ed 100644 --- a/tests/auto/sql/kernel/qsqlerror/tst_qsqlerror.cpp +++ b/tests/auto/sql/kernel/qsqlerror/tst_qsqlerror.cpp @@ -102,6 +102,7 @@ void tst_QSqlError::construction() QCOMPARE(obj1.databaseText(), QString("databasetext")); QCOMPARE(obj1.type(), QSqlError::UnknownError); QCOMPARE(obj1.number(), 123); + QCOMPARE(obj1.nativeErrorCode(), QStringLiteral("123")); QVERIFY(obj1.isValid()); QSqlError obj2(obj1); @@ -109,6 +110,7 @@ void tst_QSqlError::construction() QCOMPARE(obj2.databaseText(), obj1.databaseText()); QCOMPARE(obj2.type(), obj1.type()); QCOMPARE(obj2.number(), obj1.number()); + QCOMPARE(obj2.nativeErrorCode(), obj1.nativeErrorCode()); QVERIFY(obj2.isValid()); QSqlError obj3 = obj2; @@ -116,10 +118,16 @@ void tst_QSqlError::construction() QCOMPARE(obj3.databaseText(), obj2.databaseText()); QCOMPARE(obj3.type(), obj2.type()); QCOMPARE(obj3.number(), obj2.number()); + QCOMPARE(obj3.nativeErrorCode(), obj2.nativeErrorCode()); QVERIFY(obj3.isValid()); QSqlError obj4; QVERIFY(!obj4.isValid()); + QCOMPARE(obj4.driverText(), QString()); + QCOMPARE(obj4.databaseText(), QString()); + QCOMPARE(obj4.type(), QSqlError::NoError); + QCOMPARE(obj4.number(), -1); + QCOMPARE(obj4.nativeErrorCode(), QString()); QSqlError obj5(QStringLiteral("drivertext"), QStringLiteral("databasetext"), QSqlError::UnknownError, QStringLiteral("123")); @@ -138,6 +146,16 @@ void tst_QSqlError::construction() QCOMPARE(obj6.number(), 0); QCOMPARE(obj6.nativeErrorCode(), QStringLiteral("Err123")); QVERIFY(obj6.isValid()); + + // Default constructed object as constructed before Qt 5.3 + QSqlError obj7(QString(), QString(), QSqlError::NoError, -1); + QVERIFY(!obj7.isValid()); + QCOMPARE(obj7.driverText(), QString()); + QCOMPARE(obj7.databaseText(), QString()); + QCOMPARE(obj7.type(), QSqlError::NoError); + QCOMPARE(obj7.number(), -1); + QCOMPARE(obj7.nativeErrorCode(), QString()); + } void tst_QSqlError::operators()