summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Sommerseth <dazo@users.sourceforge.net>2012-02-26 00:03:36 +0100
committerDavid Sommerseth <dazo@users.sourceforge.net>2013-06-13 01:06:19 +0200
commitddcdca6ff23a16ca681f49ab090c9a5c426ed07f (patch)
treef1881727386f050364bfd10b468a6a9a28a23756
parent1b6f81682c71d056ae554ec557615d821f7d1f48 (diff)
downloadeurephia-ddcdca6ff23a16ca681f49ab090c9a5c426ed07f.tar.gz
eurephia-ddcdca6ff23a16ca681f49ab090c9a5c426ed07f.tar.xz
eurephia-ddcdca6ff23a16ca681f49ab090c9a5c426ed07f.zip
edb-pgsql: Added ePGerrorMessageXML() to return PostgreSQL errors in XML
Signed-off-by: David Sommerseth <dazo@users.sourceforge.net>
-rw-r--r--database/postgresql/pgsql-common.c65
-rw-r--r--database/postgresql/pgsql-common.h11
2 files changed, 76 insertions, 0 deletions
diff --git a/database/postgresql/pgsql-common.c b/database/postgresql/pgsql-common.c
index 35ae03a..57da949 100644
--- a/database/postgresql/pgsql-common.c
+++ b/database/postgresql/pgsql-common.c
@@ -33,6 +33,11 @@
#include <stdarg.h>
#include <libpq-fe.h>
+#ifdef HAVE_LIBXML2
+#include <libxml/tree.h>
+#endif
+
+#include <eurephia_nullsafe.h>
#include <eurephia_context.h>
#include <eurephia_log.h>
#include "prepared-sql.h"
@@ -91,3 +96,63 @@ void __ePGerrorMessage(eurephiaCTX *ctx, PGresult *dbr, int logdst, int loglvl,
}
}
+#ifdef HAVE_LIBXML2
+xmlNode * __ePGerrorMessageXML(eurephiaCTX *ctx, PGresult *dbr, int logdst,
+ ePG_prepID prepid, const char *errfile, const long errline)
+{
+ xmlNode *ret_n = NULL;
+
+
+ ret_n = xmlNewNode(NULL, (xmlChar *) "SQLError");
+ if( ret_n != NULL ) {
+ xmlNode *err_n = NULL;
+ xmlChar *errstr = NULL;
+
+ xmlNewProp(ret_n, (xmlChar *) "driver", (xmlChar *) "edb-sqlite.so");
+
+#ifdef ENABLE_DEBUG
+ {
+ xmlNode *dbg_n = xmlNewNode(NULL, (xmlChar *) "debug");
+ xmlChar linestr[10];
+
+ xmlStrPrintf(linestr, 8, (xmlChar *) "%u", errline);
+ xmlNewProp(dbg_n, (xmlChar *) "file", (xmlChar *) errfile);
+ xmlNewProp(dbg_n, (xmlChar *) "line", linestr);
+ xmlAddChild(ret_n, dbg_n);
+ }
+#endif
+
+ errstr = xmlCharStrdup(dbr != NULL ? PQresultErrorMessage(dbr)
+ : (ctx->dbc != NULL ? PQerrorMessage(ctx->dbc->dbhandle)
+ : "[unknown error]"));
+ err_n = xmlNewTextChild(ret_n, NULL, (xmlChar *) "ErrorMessage", errstr);
+
+ if( prepid != PREPSQL_NONE ) {
+ xmlNewProp(err_n, (xmlChar *) "prepstatement",
+ (xmlChar *) ePGprepStatementGetName(ctx, prepid));
+ }
+
+ if( dbr != NULL ) {
+ xmlNewProp(err_n, (xmlChar *) "resultstatus",
+ (xmlChar *) PQresStatus(PQresultStatus(dbr)));
+ } else {
+ xmlNewProp(err_n, (xmlChar *) "resultstatus",
+ (xmlChar *) "(unknown/NULL)");
+ }
+
+ xmlNewProp(err_n, (xmlChar *) "severity", (xmlChar *) eurephia_logPrioString(logdst));
+ free_nullsafe(NULL, errstr);
+ }
+
+ if( dbr ) {
+ PQclear(dbr);
+ }
+
+ if( (logdst == LOG_EMERG) && (ctx->dbc != NULL)
+ && (PQstatus(ctx->dbc->dbhandle) != CONNECTION_OK) ) {
+ PQfinish(ctx->dbc->dbhandle);
+ }
+ return ret_n;
+}
+#endif
+
diff --git a/database/postgresql/pgsql-common.h b/database/postgresql/pgsql-common.h
index a1bbed0..cf5131a 100644
--- a/database/postgresql/pgsql-common.h
+++ b/database/postgresql/pgsql-common.h
@@ -33,6 +33,11 @@
#define _PGSQL_ERROR_H
#include <stdarg.h>
+
+#ifdef HAVE_LIBXML2
+#include <libxml/tree.h>
+#endif
+
#include <eurephia_context.h>
#include "prepared-sql.h"
@@ -43,5 +48,11 @@ void __ePGerrorMessage(eurephiaCTX *ctx, PGresult *dbr, int logdst, int loglvl,
ePG_prepID prepid, const char *errfile, const long errline,
const char *fmt, ...);
+#ifdef HAVE_LIBXML2
+#define ePGerrorMessageXML(ctx, dbr, logdst, prepid) __ePGerrorMessageXML(ctx, dbr, logdst, prepid, __FILE__, __LINE__)
+xmlNode * __ePGerrorMessageXML(eurephiaCTX *ctx, PGresult *dbr, int logdst, ePG_prepID prepid,
+ const char *errfile, const long errline);
+
+#endif
#endif