diff options
Diffstat (limited to 'src/sql')
-rw-r--r-- | src/sql/drivers/psql/qsql_psql.cpp | 7 | ||||
-rw-r--r-- | src/sql/kernel/qsqlerror.cpp | 107 | ||||
-rw-r--r-- | src/sql/kernel/qsqlerror.h | 24 |
3 files changed, 107 insertions, 31 deletions
diff --git a/src/sql/drivers/psql/qsql_psql.cpp b/src/sql/drivers/psql/qsql_psql.cpp index fd5990f3ee..c052e4c2e7 100644 --- a/src/sql/drivers/psql/qsql_psql.cpp +++ b/src/sql/drivers/psql/qsql_psql.cpp @@ -224,11 +224,12 @@ static QSqlError qMakeError(const QString& err, QSqlError::ErrorType type, { const char *s = PQerrorMessage(p->connection); QString msg = p->isUtf8 ? QString::fromUtf8(s) : QString::fromLocal8Bit(s); + QString errorCode; if (result) { - const char *sCode = PQresultErrorField(result, PG_DIAG_SQLSTATE); - msg += QString::fromLatin1("(%1)").arg(QString::fromLatin1(sCode)); + errorCode = QString::fromLatin1(PQresultErrorField(result, PG_DIAG_SQLSTATE)); + msg += QString::fromLatin1("(%1)").arg(errorCode); } - return QSqlError(QLatin1String("QPSQL: ") + err, msg, type); + return QSqlError(QLatin1String("QPSQL: ") + err, msg, type, errorCode); } bool QPSQLResultPrivate::processResults() diff --git a/src/sql/kernel/qsqlerror.cpp b/src/sql/kernel/qsqlerror.cpp index e4b8aa0c6d..1763722e8a 100644 --- a/src/sql/kernel/qsqlerror.cpp +++ b/src/sql/kernel/qsqlerror.cpp @@ -47,12 +47,22 @@ 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(); } #endif +class QSqlErrorPrivate +{ +public: + QString driverError; + QString databaseError; + QSqlError::ErrorType errorType; + QString errorCode; +}; + + /*! \class QSqlError \brief The QSqlError class provides SQL database error information. @@ -62,7 +72,7 @@ QDebug operator<<(QDebug dbg, const QSqlError &s) 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() @@ -81,25 +91,52 @@ QDebug operator<<(QDebug dbg, const QSqlError &s) */ /*! + \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) - : driverError(driverText), databaseError(databaseText), errorType(type), errorNumber(number) { + d = new QSqlErrorPrivate; + + d->driverError = driverText; + d->databaseError = databaseText; + d->errorType = type; + 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. */ QSqlError::QSqlError(const QSqlError& other) - : driverError(other.driverError), databaseError(other.databaseError), - errorType(other.errorType), - errorNumber(other.errorNumber) { + d = new QSqlErrorPrivate; + + *d = *other.d; } /*! @@ -108,10 +145,7 @@ QSqlError::QSqlError(const QSqlError& other) QSqlError& QSqlError::operator=(const QSqlError& other) { - driverError = other.driverError; - databaseError = other.databaseError; - errorType = other.errorType; - errorNumber = other.errorNumber; + *d = *other.d; return *this; } @@ -121,7 +155,7 @@ QSqlError& QSqlError::operator=(const QSqlError& other) bool QSqlError::operator==(const QSqlError& other) const { - return (errorType == other.errorType); + return (d->errorType == other.d->errorType); } @@ -131,7 +165,7 @@ bool QSqlError::operator==(const QSqlError& other) const bool QSqlError::operator!=(const QSqlError& other) const { - return (errorType != other.errorType); + return (d->errorType != other.d->errorType); } @@ -141,6 +175,7 @@ bool QSqlError::operator!=(const QSqlError& other) const QSqlError::~QSqlError() { + delete d; } /*! @@ -151,7 +186,7 @@ QSqlError::~QSqlError() */ QString QSqlError::driverText() const { - return driverError; + return d->driverError; } /*! @@ -169,7 +204,7 @@ QString QSqlError::driverText() const #if QT_DEPRECATED_SINCE(5, 1) void QSqlError::setDriverText(const QString& driverText) { - driverError = driverText; + d->driverError = driverText; } #endif @@ -182,7 +217,7 @@ void QSqlError::setDriverText(const QString& driverText) QString QSqlError::databaseText() const { - return databaseError; + return d->databaseError; } /*! @@ -200,7 +235,7 @@ QString QSqlError::databaseText() const #if QT_DEPRECATED_SINCE(5, 1) void QSqlError::setDatabaseText(const QString& databaseText) { - databaseError = databaseText; + d->databaseError = databaseText; } #endif @@ -210,7 +245,7 @@ void QSqlError::setDatabaseText(const QString& databaseText) QSqlError::ErrorType QSqlError::type() const { - return errorType; + return d->errorType; } /*! @@ -228,19 +263,33 @@ QSqlError::ErrorType QSqlError::type() const #if QT_DEPRECATED_SINCE(5, 1) void QSqlError::setType(ErrorType type) { - errorType = type; + d->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 errorNumber; + return d->errorCode.toInt(); } +#endif /*! \fn void QSqlError::setNumber(int number) @@ -257,11 +306,21 @@ int QSqlError::number() const #if QT_DEPRECATED_SINCE(5, 1) void QSqlError::setNumber(int number) { - 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. @@ -270,10 +329,10 @@ void QSqlError::setNumber(int number) QString QSqlError::text() const { - QString result = databaseError; - if (!databaseError.endsWith(QLatin1String("\n"))) + QString result = d->databaseError; + if (!d->databaseError.endsWith(QLatin1String("\n"))) result += QLatin1Char(' '); - result += driverError; + result += d->driverError; return result; } @@ -287,7 +346,7 @@ QString QSqlError::text() const */ bool QSqlError::isValid() const { - return errorType != NoError; + return d->errorType != NoError; } QT_END_NAMESPACE diff --git a/src/sql/kernel/qsqlerror.h b/src/sql/kernel/qsqlerror.h index 39c4cda958..c2afe7c819 100644 --- a/src/sql/kernel/qsqlerror.h +++ b/src/sql/kernel/qsqlerror.h @@ -47,6 +47,7 @@ QT_BEGIN_NAMESPACE +class QSqlErrorPrivate; class Q_SQL_EXPORT QSqlError { @@ -58,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; @@ -71,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; @@ -83,10 +93,16 @@ public: #endif private: - QString driverError; - QString databaseError; - ErrorType errorType; - int errorNumber; + // ### Qt6: Keep the pointer and remove the rest. + QString unused1; + QString unused2; + union { + QSqlErrorPrivate *d; + struct { + ErrorType unused3; + int unused4; + } unused5; + }; }; #ifndef QT_NO_DEBUG_STREAM |