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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
/*
* pgeasy.c
*
*/
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <libpq-fe.h>
#include "halt.h"
#include "libpgeasy.h"
#define NUL '\0'
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
/* GLOBAL VARIABLES */
static PGconn *conn;
static PGresult *res = NULL;
#define ON_ERROR_STOP 0
#define ON_ERROR_CONTINUE 1
static int on_error_state = ON_ERROR_STOP;
static int in_result_block = FALSE;
static int was_get_unset_result = FALSE;
/* LOCAL VARIABLES */
static int tuple;
/*
**
** connectdb - returns PGconn structure
**
*/
PGconn *
connectdb(char *pghost,
char *pgport,
char *pgoptions,
char *pgtty,
char *dbName)
{
/* make a connection to the database */
conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);
if (PQstatus(conn) == CONNECTION_BAD)
halt("Connection to database '%s' failed.\n%s\n", dbName,
PQerrorMessage(conn));
return conn;
}
/*
**
** disconnectdb
**
*/
void
disconnectdb()
{
PQfinish(conn);
}
/*
**
** doquery - returns PGresult structure
**
*/
PGresult *
doquery(char *query)
{
if (res != NULL && in_result_block == FALSE && was_get_unset_result == FALSE)
PQclear(res);
was_get_unset_result = FALSE;
res = PQexec(conn, query);
if (on_error_state == ON_ERROR_STOP &&
(res == NULL ||
PQresultStatus(res) == PGRES_BAD_RESPONSE ||
PQresultStatus(res) == PGRES_NONFATAL_ERROR ||
PQresultStatus(res) == PGRES_FATAL_ERROR))
{
if (res != NULL)
fprintf(stderr, "query error: %s\n", PQcmdStatus(res));
else
fprintf(stderr, "connection error: %s\n", PQerrorMessage(conn));
PQfinish(conn);
halt("failed request: %s\n", query);
}
tuple = 0;
return res;
}
/*
**
** fetch - returns tuple number (starts at 0), or the value END_OF_TUPLES
** NULL pointers are skipped
**
*/
int
fetch(void *param,...)
{
va_list ap;
int arg,
num_fields;
num_fields = PQnfields(res);
if (tuple >= PQntuples(res))
return END_OF_TUPLES;
va_start(ap, param);
for (arg = 0; arg < num_fields; arg++)
{
if (param != NULL)
{
if (PQfsize(res, arg) == -1)
{
memcpy(param, PQgetvalue(res, tuple, arg), PQgetlength(res, tuple, arg));
((char *) param)[PQgetlength(res, tuple, arg)] = NUL;
}
else
memcpy(param, PQgetvalue(res, tuple, arg), PQfsize(res, arg));
}
param = va_arg(ap, char *);
}
va_end(ap);
return tuple++;
}
/*
**
** fetchwithnulls - returns tuple number (starts at 0),
** or the value END_OF_TUPLES
** Returns TRUE or FALSE into null indicator variables
** NULL pointers are skipped
*/
int
fetchwithnulls(void *param,...)
{
va_list ap;
int arg,
num_fields;
num_fields = PQnfields(res);
if (tuple >= PQntuples(res))
return END_OF_TUPLES;
va_start(ap, param);
for (arg = 0; arg < num_fields; arg++)
{
if (param != NULL)
{
if (PQfsize(res, arg) == -1)
{
memcpy(param, PQgetvalue(res, tuple, arg), PQgetlength(res, tuple, arg));
((char *) param)[PQgetlength(res, tuple, arg)] = NUL;
}
else
memcpy(param, PQgetvalue(res, tuple, arg), PQfsize(res, arg));
}
param = va_arg(ap, char *);
if (PQgetisnull(res, tuple, arg) != 0)
*(int *) param = 1;
else
*(int *) param = 0;
param = va_arg(ap, char *);
}
va_end(ap);
return tuple++;
}
/*
**
** on_error_stop
**
*/
void
on_error_stop()
{
on_error_state = ON_ERROR_STOP;
}
/*
**
** on_error_continue
**
*/
void
on_error_continue()
{
on_error_state = ON_ERROR_CONTINUE;
}
/*
**
** get_result
**
*/
PGresult *
get_result()
{
char *cmdstatus = PQcmdStatus(res);
was_get_unset_result = TRUE;
/* we have to store the fetch location somewhere */
/* XXX THIS IS A NO-NO */
cmdstatus[0] = NUL;
memcpy(&cmdstatus[1], &tuple, sizeof(tuple));
return res;
}
/*
**
** set_result
**
*/
void
set_result(PGresult *newres)
{
char *cmdstatus = PQcmdStatus(res);
if (newres == NULL)
halt("set_result called with null result pointer\n");
if (res != NULL && was_get_unset_result == FALSE)
{
if (in_result_block == FALSE)
PQclear(res);
else
{
/* XXX THIS IS A NO-NO */
cmdstatus[0] = NUL;
memcpy(&cmdstatus[1], &tuple, sizeof(tuple));
}
}
in_result_block = TRUE;
was_get_unset_result = FALSE;
cmdstatus = PQcmdStatus(newres);
memcpy(&tuple, &cmdstatus[1], sizeof(tuple));
res = newres;
}
/*
**
** unset_result
**
*/
void
unset_result(PGresult *oldres)
{
char *cmdstatus = PQcmdStatus(oldres);
if (oldres == NULL)
halt("unset_result called with null result pointer\n");
if (in_result_block == FALSE)
halt("Unset of result without being set.\n");
was_get_unset_result = TRUE;
/* XXX THIS IS A NO-NO */
cmdstatus[0] = NUL;
memcpy(&cmdstatus[1], &tuple, sizeof(tuple));
in_result_block = FALSE;
}
/*
**
** reset_fetch
**
*/
void
reset_fetch()
{
tuple = 0;
}
|