1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
/* pgsql-error.c -- PostgreSQL database driver for eurephia
*
* GPLv2 only - Copyright (C) 2012
* David Sommerseth <dazo@users.sourceforge.net>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; version 2
* of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
/**
* @file pgsql-error.c
* @author David Sommerseth <dazo@users.sourceforge.net>
* @date 2012-01-15
*
* @brief eurephia database driver for the PostgreSQL database.
* Shared functions for the PostgreSQL driver
*
*/
#include <string.h>
#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"
char *ePGgetValue(PGresult *res, int row, int col)
{
if( (res == NULL) || (PQgetisnull(res, row, col) == 1) ) {
return NULL;
} else {
// FIXME: should we check if we have the row number accesible first? row > PQntuples()
return PQgetvalue(res, row, col);
}
}
int ePGgetValue_bool(PGresult *res, int row, int col)
{
if( (res == NULL) || (PQgetisnull(res, row, col) == 1) ) {
return 0;
} else {
// FIXME: should we check if we have the row number accesible first? row > PQntuples()
char *str = PQgetvalue(res, row, col);
return (str[0] == 't' ? 1 : 0);
}
}
void __ePGerrorMessage(eurephiaCTX *ctx, PGresult *dbr, int logdst, int loglvl,
ePG_prepID prepid, const char *errfile, const long errline,
const char *fmt, ...)
{
char msgfmt[514];
va_list ap;
memset(&msgfmt, 0, 514);
va_start(ap, fmt);
if( prepid != PREPSQL_NONE ) {
snprintf(msgfmt, 512, "[%s] SQL query failed: %s %s: %s",
ePGprepStatementGetName(ctx, prepid),
(dbr != NULL ? PQresStatus(PQresultStatus(dbr)) : ""), fmt,
(dbr != NULL ? PQresultErrorMessage(dbr)
: (ctx->dbc != NULL ? PQerrorMessage(ctx->dbc->dbhandle)
: "[unknown error]")
));
} else {
snprintf(msgfmt, 512, "SQL query failed: %s %s: %s",
(dbr != NULL ? PQresStatus(PQresultStatus(dbr)) : ""),
fmt,
(dbr != NULL ? PQresultErrorMessage(dbr)
: (ctx->dbc != NULL ? PQerrorMessage(ctx->dbc->dbhandle)
: "[unknown error]")
));
}
#ifdef ENABLE_DEBUG
_veurephia_log_func(ctx, logdst, loglvl, errfile, errline, ap, msgfmt);
#else
veurephia_log(ctx, logdst, loglvl, ap, msgfmt);
#endif
va_end(ap);
memset(&msgfmt, 0, 514);
if( dbr ) {
PQclear(dbr);
}
if( (loglvl == LOG_EMERG) && (ctx->dbc != NULL)
&& (PQstatus(ctx->dbc->dbhandle) != CONNECTION_OK) ) {
PQfinish(ctx->dbc->dbhandle);
}
}
#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
|