diff --git a/src/sql/kernel/qsqlerror.cpp b/src/sql/kernel/qsqlerror.cpp index d1fc5d4585..1427f58327 100644 --- a/src/sql/kernel/qsqlerror.cpp +++ b/src/sql/kernel/qsqlerror.cpp @@ -98,6 +98,34 @@ public: type \a type and the optional error number \a number. */ +/*! \fn QSqlError::QSqlError(QSqlError &&other) + Move-constructs a QSqlError instance, making it point at the same + object that \a other was pointing to. + + \note The moved-from object \a other is placed in a + partially-formed state, in which the only valid operations are + destruction and assignment of a new value. + + \since 5.10 +*/ + +/*! \fn QSqlError::operator=(QSqlError &&other) + Move-assigns \a other to this QSqlError instance. + + \note The moved-from object \a other is placed in a + partially-formed state, in which the only valid operations are + destruction and assignment of a new value. + + \since 5.10 +*/ + +/*! \fn QSqlError::swap(QSqlError &other) + Swaps error \other with this error. This operation is very fast + and never fails. + + \since 5.10 +*/ + #if QT_DEPRECATED_SINCE(5, 3) QSqlError::QSqlError(const QString& driverText, const QString& databaseText, ErrorType type, int number) @@ -117,7 +145,6 @@ QSqlError::QSqlError(const QString& driverText, const QString& databaseText, Err 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) { @@ -146,7 +173,10 @@ QSqlError::QSqlError(const QSqlError& other) QSqlError& QSqlError::operator=(const QSqlError& other) { - *d = *other.d; + if (d) + *d = *other.d; + else + d = new QSqlErrorPrivate(*other.d); return *this; } diff --git a/src/sql/kernel/qsqlerror.h b/src/sql/kernel/qsqlerror.h index 0ccd32159d..6dac47a7fe 100644 --- a/src/sql/kernel/qsqlerror.h +++ b/src/sql/kernel/qsqlerror.h @@ -66,11 +66,16 @@ public: ErrorType type = NoError, const QString &errorCode = QString()); QSqlError(const QSqlError& other); + QSqlError(QSqlError &&other) Q_DECL_NOTHROW : d(other.d) { other.d = nullptr; } QSqlError& operator=(const QSqlError& other); + QSqlError &operator=(QSqlError &&other) Q_DECL_NOTHROW { swap(other); return *this; } + bool operator==(const QSqlError& other) const; bool operator!=(const QSqlError& other) const; ~QSqlError(); + void swap(QSqlError &other) Q_DECL_NOTHROW { qSwap(d, other.d); } + QString driverText() const; QString databaseText() const; ErrorType type() const; @@ -102,6 +107,8 @@ private: }; }; +Q_DECLARE_SHARED_NOT_MOVABLE_UNTIL_QT6(QSqlError) + #ifndef QT_NO_DEBUG_STREAM Q_SQL_EXPORT QDebug operator<<(QDebug, const QSqlError &); #endif diff --git a/tests/auto/sql/kernel/qsqlerror/tst_qsqlerror.cpp b/tests/auto/sql/kernel/qsqlerror/tst_qsqlerror.cpp index 07a6c9c835..08c6039e37 100644 --- a/tests/auto/sql/kernel/qsqlerror/tst_qsqlerror.cpp +++ b/tests/auto/sql/kernel/qsqlerror/tst_qsqlerror.cpp @@ -44,6 +44,7 @@ public: private slots: void getSetCheck(); void construction(); + void moveOperator(); void operators(); }; @@ -143,6 +144,26 @@ void tst_QSqlError::construction() QCOMPARE(obj7.number(), -1); QCOMPARE(obj7.nativeErrorCode(), QString()); + // Move constructor + QSqlError obj8(std::move(obj3)); + QCOMPARE(obj8.driverText(), obj2.driverText()); + QCOMPARE(obj8.databaseText(), obj2.databaseText()); + QCOMPARE(obj8.type(), obj2.type()); + QCOMPARE(obj8.number(), obj2.number()); + QCOMPARE(obj8.nativeErrorCode(), obj2.nativeErrorCode()); + QVERIFY(obj8.isValid()); +} + +void tst_QSqlError::moveOperator() +{ + QSqlError obj1("drivertext", "databasetext", QSqlError::UnknownError, 123), obj2; + obj2 = std::move(obj1); + QCOMPARE(obj2.driverText(), QString("drivertext")); + QCOMPARE(obj2.databaseText(), QString("databasetext")); + QCOMPARE(obj2.type(), QSqlError::UnknownError); + QCOMPARE(obj2.number(), 123); + QCOMPARE(obj2.nativeErrorCode(), QStringLiteral("123")); + QVERIFY(obj2.isValid()); } void tst_QSqlError::operators()