summaryrefslogtreecommitdiff
path: root/src/sql
diff options
context:
space:
mode:
authorAndras Mantia <andras@kdab.com>2013-02-25 18:23:29 +0200
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-02-28 10:03:47 +0100
commitc4c5e8c595ce7c51094a7eff330b01e3ffbf05c0 (patch)
tree07fec1793859e1746286da949d3b6cedcd81ed36 /src/sql
parentf391ec72c2e4416bf8e5d7482ace49247cc2017c (diff)
downloadqt4-tools-c4c5e8c595ce7c51094a7eff330b01e3ffbf05c0.tar.gz
Add support for SQLSTATE error codes
Postgres can report detailed information about an error using error codes. See http://www.postgresql.org/docs/8.1/static/errcodes-appendix.html . The current driver doesn't report the error, nor is it supported by the QSqlError object. The patch appends the error to the error message, helping applications to: - handle different errors in a specific way - show correct, translated error messages, independently on the language of the postgres installation Change-Id: I5c7792606307afdefaeae37c512949c85c2c7674 Reviewed-by: Mark Brand <mabrand@mabrand.nl> (cherry picked from commit qtbase/8ccab9b029ed2a2444d89a9cfc6c4a1866b8e82d) Reviewed-by: Andy Shaw <andy.shaw@digia.com>
Diffstat (limited to 'src/sql')
-rw-r--r--src/sql/drivers/psql/qsql_psql.cpp30
1 files changed, 18 insertions, 12 deletions
diff --git a/src/sql/drivers/psql/qsql_psql.cpp b/src/sql/drivers/psql/qsql_psql.cpp
index 66cd431f93..51ddce0fd9 100644
--- a/src/sql/drivers/psql/qsql_psql.cpp
+++ b/src/sql/drivers/psql/qsql_psql.cpp
@@ -192,10 +192,14 @@ public:
};
static QSqlError qMakeError(const QString& err, QSqlError::ErrorType type,
- const QPSQLDriverPrivate *p)
+ const QPSQLDriverPrivate *p, PGresult* result = 0)
{
const char *s = PQerrorMessage(p->connection);
QString msg = p->isUtf8 ? QString::fromUtf8(s) : QString::fromLocal8Bit(s);
+ if (result) {
+ const char *sCode = PQresultErrorField(result, PG_DIAG_SQLSTATE);
+ msg += QString::fromLatin1("(%1)").arg(QString::fromLatin1(sCode));
+ }
return QSqlError(QLatin1String("QPSQL: ") + err, msg, type);
}
@@ -217,7 +221,7 @@ bool QPSQLResultPrivate::processResults()
return true;
}
q->setLastError(qMakeError(QCoreApplication::translate("QPSQLResult",
- "Unable to create query"), QSqlError::StatementError, driver));
+ "Unable to create query"), QSqlError::StatementError, driver, result));
return false;
}
@@ -583,7 +587,7 @@ bool QPSQLResult::prepare(const QString &query)
if (PQresultStatus(result) != PGRES_COMMAND_OK) {
setLastError(qMakeError(QCoreApplication::translate("QPSQLResult",
- "Unable to prepare statement"), QSqlError::StatementError, d->driver));
+ "Unable to prepare statement"), QSqlError::StatementError, d->driver, result));
PQclear(result);
d->preparedStmtId.clear();
return false;
@@ -878,9 +882,9 @@ bool QPSQLDriver::beginTransaction()
}
PGresult* res = d->exec("BEGIN");
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
- PQclear(res);
setLastError(qMakeError(tr("Could not begin transaction"),
- QSqlError::TransactionError, d));
+ QSqlError::TransactionError, d, res));
+ PQclear(res);
return false;
}
PQclear(res);
@@ -911,9 +915,9 @@ bool QPSQLDriver::commitTransaction()
}
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK || transaction_failed) {
- PQclear(res);
setLastError(qMakeError(tr("Could not commit transaction"),
- QSqlError::TransactionError, d));
+ QSqlError::TransactionError, d, res));
+ PQclear(res);
return false;
}
PQclear(res);
@@ -929,7 +933,7 @@ bool QPSQLDriver::rollbackTransaction()
PGresult* res = d->exec("ROLLBACK");
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
setLastError(qMakeError(tr("Could not rollback transaction"),
- QSqlError::TransactionError, d));
+ QSqlError::TransactionError, d, res));
PQclear(res);
return false;
}
@@ -1313,8 +1317,9 @@ bool QPSQLDriver::subscribeToNotificationImplementation(const QString &name)
// to check for notifications immediately after executing the LISTEN
d->seid << name;
QString query = QLatin1String("LISTEN ") + escapeIdentifier(name, QSqlDriver::TableName);
- if (PQresultStatus(d->exec(query)) != PGRES_COMMAND_OK) {
- setLastError(qMakeError(tr("Unable to subscribe"), QSqlError::StatementError, d));
+ PGresult *result = d->exec(query);
+ if (PQresultStatus(result) != PGRES_COMMAND_OK) {
+ setLastError(qMakeError(tr("Unable to subscribe"), QSqlError::StatementError, d, result));
return false;
}
@@ -1344,8 +1349,9 @@ bool QPSQLDriver::unsubscribeFromNotificationImplementation(const QString &name)
}
QString query = QLatin1String("UNLISTEN ") + escapeIdentifier(name, QSqlDriver::TableName);
- if (PQresultStatus(d->exec(query)) != PGRES_COMMAND_OK) {
- setLastError(qMakeError(tr("Unable to unsubscribe"), QSqlError::StatementError, d));
+ PGresult *result = d->exec(query);
+ if (PQresultStatus(result) != PGRES_COMMAND_OK) {
+ setLastError(qMakeError(tr("Unable to unsubscribe"), QSqlError::StatementError, d, result));
return false;
}