From 2f709952cf6acdda83e42e587d744f81a05f2c03 Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Sat, 25 Mar 2023 13:42:52 +0100 Subject: [PATCH] QSqlError: also compare nativeErrorCode() in operator==() / operator!=() A QSqlError is not equal when the native error code differs. The database and driver text should not be considered during the comparison because they might differ due to e.g. different locales. [ChangeLog][QtSql][QSqlError] The comparison operators have been fixed to take both error type and error code into account. Change-Id: Ie7511f183f88dd454eb165c6ff237e51b79d1c08 Reviewed-by: Volker Hilsheimer --- src/sql/kernel/qsqlerror.cpp | 12 ++++++++---- tests/auto/sql/kernel/qsqlerror/tst_qsqlerror.cpp | 10 +++++++--- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/sql/kernel/qsqlerror.cpp b/src/sql/kernel/qsqlerror.cpp index 1e093c236c..7483a52e5c 100644 --- a/src/sql/kernel/qsqlerror.cpp +++ b/src/sql/kernel/qsqlerror.cpp @@ -126,22 +126,26 @@ QSqlError &QSqlError::operator=(const QSqlError &other) } /*! - Compare the \a other error's values to this error and returns \c true, if it equal. + Compare the \a other error's type() and nativeErrorCode() + to this error and returns \c true, if it equal. */ bool QSqlError::operator==(const QSqlError &other) const { - return (d->errorType == other.d->errorType); + return (d->errorType == other.d->errorType && + d->errorCode == other.d->errorCode); } /*! - Compare the \a other error's values to this error and returns \c true if it is not equal. + Compare the \a other error's type() and nativeErrorCode() + to this error and returns \c true if it is not equal. */ bool QSqlError::operator!=(const QSqlError &other) const { - return (d->errorType != other.d->errorType); + return (d->errorType != other.d->errorType || + d->errorCode != other.d->errorCode); } diff --git a/tests/auto/sql/kernel/qsqlerror/tst_qsqlerror.cpp b/tests/auto/sql/kernel/qsqlerror/tst_qsqlerror.cpp index c83867fcc4..ddd4e2cda6 100644 --- a/tests/auto/sql/kernel/qsqlerror/tst_qsqlerror.cpp +++ b/tests/auto/sql/kernel/qsqlerror/tst_qsqlerror.cpp @@ -108,12 +108,16 @@ void tst_QSqlError::moveOperator() void tst_QSqlError::operators() { - QSqlError error1(QString(), QString(), QSqlError::NoError); - QSqlError error2(QString(), QString(), QSqlError::NoError); - QSqlError error3(QString(), QString(), QSqlError::UnknownError); + QSqlError error1(QStringLiteral("a"), QStringLiteral("b"), QSqlError::NoError, QStringLiteral("ec1")); + QSqlError error2(QStringLiteral("c"), QStringLiteral("d"), QSqlError::NoError, QStringLiteral("ec1")); + QSqlError error3(QString(), QString(), QSqlError::UnknownError, QStringLiteral("ec1")); + QSqlError error4(QString(), QString(), QSqlError::NoError, QStringLiteral("ec2")); + QSqlError error5(QString(), QString(), QSqlError::UnknownError, QStringLiteral("ec2")); QCOMPARE(error1, error2); QVERIFY(error1 != error3); + QVERIFY(error1 != error4); + QVERIFY(error4 != error5); } void tst_QSqlError::qtbug_74575()