};
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);
}
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;
}
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;
}
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);
}
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);
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;
}
// 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;
}
}
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;
}