Enable QSqlError to handle alphanumeric error codes.

Some database systems (like PostgreSQL) use alphanumeric error codes.
Introduce a new method nativeErrorCode() which replaces number().
If the error code cannot be converted to int, number() will return 0.

Task-number: QTBUG-142
Change-Id: Ic7fba841737674b75c0c01c2263f51d2041da497
Reviewed-by: Mark Brand <mabrand@mabrand.nl>
This commit is contained in:
Marcel Krems 2013-12-16 23:26:44 +01:00 committed by The Qt Project
parent e35c2c57cf
commit 106316198d
4 changed files with 83 additions and 10 deletions

View File

@ -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.

View File

@ -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;

View File

@ -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() + "' || '";

View File

@ -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()