diff options
Diffstat (limited to 'src/sql')
-rw-r--r-- | src/sql/doc/src/sql-driver.qdoc | 4 | ||||
-rw-r--r-- | src/sql/drivers/psql/qsql_psql.cpp | 7 | ||||
-rw-r--r-- | src/sql/drivers/sqlite/qsql_sqlite.cpp | 2 | ||||
-rw-r--r-- | src/sql/kernel/qsqlerror.cpp | 107 | ||||
-rw-r--r-- | src/sql/kernel/qsqlerror.h | 25 | ||||
-rw-r--r-- | src/sql/kernel/qsqlquery.cpp | 33 | ||||
-rw-r--r-- | src/sql/kernel/qsqlquery.h | 1 |
7 files changed, 137 insertions, 42 deletions
diff --git a/src/sql/doc/src/sql-driver.qdoc b/src/sql/doc/src/sql-driver.qdoc index 849cdcd544..92727b2d1c 100644 --- a/src/sql/doc/src/sql-driver.qdoc +++ b/src/sql/doc/src/sql-driver.qdoc @@ -598,8 +598,8 @@ is not necessary to have a database server. SQLite operates on a single file, which must be set as the database name when opening a connection. If the file does not exist, SQLite will try to - create it. SQLite also supports in-memory databases, simply pass - ":memory:" as the database name. + create it. SQLite also supports in-memory and temporary databases. Simply + pass respectively ":memory:" or an empty string as the database name. SQLite has some restrictions regarding multiple users and multiple transactions. If you try to read/write on a resource from different 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/drivers/sqlite/qsql_sqlite.cpp b/src/sql/drivers/sqlite/qsql_sqlite.cpp index c98d6438fc..36a4b7d0c4 100644 --- a/src/sql/drivers/sqlite/qsql_sqlite.cpp +++ b/src/sql/drivers/sqlite/qsql_sqlite.cpp @@ -597,8 +597,6 @@ bool QSQLiteDriver::open(const QString & db, const QString &, const QString &, c if (isOpen()) close(); - if (db.isEmpty()) - return false; int timeOut = 5000; bool sharedCache = false; 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..4e27ab03ae 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,17 @@ public: #endif private: - QString driverError; - QString databaseError; - ErrorType errorType; - int errorNumber; + // ### Qt6: Keep the pointer and remove the rest. + QString unused1; + QString unused2; + struct Unused { + ErrorType unused3; + int unused4; + }; + union { + QSqlErrorPrivate *d; + Unused unused5; + }; }; #ifndef QT_NO_DEBUG_STREAM diff --git a/src/sql/kernel/qsqlquery.cpp b/src/sql/kernel/qsqlquery.cpp index 661a582afe..0cfc37833d 100644 --- a/src/sql/kernel/qsqlquery.cpp +++ b/src/sql/kernel/qsqlquery.cpp @@ -309,18 +309,37 @@ QSqlQuery& QSqlQuery::operator=(const QSqlQuery& other) } /*! - Returns \c true if the query is \l{isActive()}{active} and positioned - on a valid record and the \a field is NULL; otherwise returns - false. Note that for some drivers, isNull() will not return accurate - information until after an attempt is made to retrieve data. + Returns \c true if the query is not \l{isActive()}{active}, + the query is not positioned on a valid record, + there is no such field, or the field is null; otherwise \c false. + Note that for some drivers, isNull() will not return accurate + information until after an attempt is made to retrieve data. - \sa isActive(), isValid(), value() + \sa isActive(), isValid(), value() */ bool QSqlQuery::isNull(int field) const { - if (d->sqlResult->isActive() && d->sqlResult->isValid()) - return d->sqlResult->isNull(field); + return !d->sqlResult->isActive() + || !d->sqlResult->isValid() + || d->sqlResult->isNull(field); +} + +/*! + \overload + + Returns \c true if there is no field with this \a name; otherwise + returns isNull(int index) for the corresponding field index. + + This overload is less efficient than \l{QSqlQuery::}{isNull()} +*/ + +bool QSqlQuery::isNull(const QString &name) const +{ + int index = d->sqlResult->record().indexOf(name); + if (index > -1) + return isNull(index); + qWarning("QSqlQuery::isNull: unknown field name '%s'", qPrintable(name)); return true; } diff --git a/src/sql/kernel/qsqlquery.h b/src/sql/kernel/qsqlquery.h index 3719643174..ef48b91298 100644 --- a/src/sql/kernel/qsqlquery.h +++ b/src/sql/kernel/qsqlquery.h @@ -70,6 +70,7 @@ public: bool isValid() const; bool isActive() const; bool isNull(int field) const; + bool isNull(const QString &name) const; int at() const; QString lastQuery() const; int numRowsAffected() const; |