Skip to content

Commit 3c44a13

Browse files
committed
Update pgeasy for missing files.
1 parent 5a20853 commit 3c44a13

File tree

3 files changed

+380
-0
lines changed

3 files changed

+380
-0
lines changed

src/interfaces/libpgeasy/libpgeasy.3

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
.\" This is -*-nroff-*-
2+
.\" XXX standard disclaimer belongs here....
3+
.\" $Header: /cvsroot/pgsql/src/interfaces/libpgeasy/Attic/libpgeasy.3,v 1.1 1999/10/12 14:06:49 momjian Exp $
4+
.TH PGEASY INTRO 08/08/98 PostgreSQL PostgreSQL
5+
.SH DESCRIPTION
6+
Pgeasy allows you to cleanly interface to the libpq library,
7+
more like a 4gl SQL interface.
8+
.PP
9+
It consists of set of simplified C functions that encapsulate the
10+
functionality of libpq.
11+
The functions are:
12+
13+
.nf
14+
PGresult *doquery(char *query);
15+
PGconn *connectdb();
16+
void disconnectdb();
17+
18+
int fetch(void *param,...);
19+
int fetchwithnulls(void *param,...);
20+
void reset_fetch();
21+
22+
void on_error_continue();
23+
void on_error_stop();
24+
25+
PGresult *get_result();
26+
void set_result(PGresult *newres);
27+
void unset_result(PGresult *oldres);
28+
.fi
29+
.PP
30+
Many functions return a structure or value, so you can do more work
31+
with the result if required.
32+
.PP
33+
You basically connect to the database with
34+
.BR connectdb ,
35+
issue your query with
36+
.BR doquery ,
37+
fetch the results with
38+
.BR fetch ,
39+
and finish with
40+
.BR disconnectdb .
41+
.PP
42+
For
43+
.IR select
44+
queries,
45+
.BR fetch
46+
allows you to pass pointers as parameters, and on return the variables
47+
are filled with data from the binary cursor you opened. These binary
48+
cursors can not be used if you are running the
49+
.BR pgeasy
50+
client on a system with a different architecture than the database
51+
server. If you pass a NULL pointer parameter, the column is skipped.
52+
.BR fetchwithnulls
53+
allows you to retieve the
54+
.IR null
55+
status of the field by passing an
56+
.IR int*
57+
after each result pointer, which returns true or false if the field is null.
58+
You can always use libpq functions on the PGresult pointer returned by
59+
.BR doquery .
60+
.BR reset_fetch
61+
starts the fetch back at the beginning.
62+
.PP
63+
.BR get_result ,
64+
.BR set_result ,
65+
and
66+
.BR unset_result
67+
allow you to handle multiple result sets at the same time.
68+
.PP
69+
There are a variety of demonstration programs in the
70+
.BR pgeasy
71+
source directory.

src/interfaces/libpgeasy/libpgeasy.c

Lines changed: 287 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,287 @@
1+
/*
2+
* pgeasy.c
3+
*
4+
*/
5+
6+
#include <stdio.h>
7+
#include <string.h>
8+
#include <stdarg.h>
9+
10+
#include <libpq-fe.h>
11+
#include "halt.h"
12+
#include "libpgeasy.h"
13+
14+
#define NUL '\0'
15+
16+
#ifndef TRUE
17+
#define TRUE 1
18+
#endif
19+
20+
#ifndef FALSE
21+
#define FALSE 0
22+
#endif
23+
24+
/* GLOBAL VARIABLES */
25+
static PGconn *conn;
26+
static PGresult *res = NULL;
27+
28+
#define ON_ERROR_STOP 0
29+
#define ON_ERROR_CONTINUE 1
30+
31+
static int on_error_state = ON_ERROR_STOP;
32+
33+
static in_result_block = FALSE;
34+
static was_get_unset_result = FALSE;
35+
36+
/* LOCAL VARIABLES */
37+
static int tuple;
38+
39+
/*
40+
**
41+
** connectdb - returns PGconn structure
42+
**
43+
*/
44+
PGconn *
45+
connectdb(char *dbName,
46+
char *pghost,
47+
char *pgport,
48+
char *pgoptions,
49+
char *pgtty)
50+
{
51+
/* make a connection to the database */
52+
conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);
53+
if (PQstatus(conn) == CONNECTION_BAD)
54+
halt("Connection to database '%s' failed.\n%s\n", dbName,
55+
PQerrorMessage(conn));
56+
return conn;
57+
}
58+
59+
/*
60+
**
61+
** disconnectdb
62+
**
63+
*/
64+
void
65+
disconnectdb()
66+
{
67+
PQfinish(conn);
68+
}
69+
70+
/*
71+
**
72+
** doquery - returns PGresult structure
73+
**
74+
*/
75+
PGresult *
76+
doquery(char *query)
77+
{
78+
if (res != NULL && in_result_block == FALSE && was_get_unset_result == FALSE)
79+
PQclear(res);
80+
81+
was_get_unset_result = FALSE;
82+
res = PQexec(conn, query);
83+
84+
if (on_error_state == ON_ERROR_STOP &&
85+
(res == NULL ||
86+
PQresultStatus(res) == PGRES_BAD_RESPONSE ||
87+
PQresultStatus(res) == PGRES_NONFATAL_ERROR ||
88+
PQresultStatus(res) == PGRES_FATAL_ERROR))
89+
{
90+
if (res != NULL)
91+
fprintf(stderr, "query error: %s\n", PQcmdStatus(res));
92+
else
93+
fprintf(stderr, "connection error: %s\n", PQerrorMessage(conn));
94+
PQfinish(conn);
95+
halt("failed request: %s\n", query);
96+
}
97+
tuple = 0;
98+
return res;
99+
}
100+
101+
/*
102+
**
103+
** fetch - returns tuple number (starts at 0), or the value END_OF_TUPLES
104+
** NULL pointers are skipped
105+
**
106+
*/
107+
int
108+
fetch(void *param,...)
109+
{
110+
va_list ap;
111+
int arg,
112+
num_fields;
113+
114+
num_fields = PQnfields(res);
115+
116+
if (tuple >= PQntuples(res))
117+
return END_OF_TUPLES;
118+
119+
va_start(ap, param);
120+
for (arg = 0; arg < num_fields; arg++)
121+
{
122+
if (param != NULL)
123+
{
124+
if (PQfsize(res, arg) == -1)
125+
{
126+
memcpy(param, PQgetvalue(res, tuple, arg), PQgetlength(res, tuple, arg));
127+
((char *) param)[PQgetlength(res, tuple, arg)] = NUL;
128+
}
129+
else
130+
memcpy(param, PQgetvalue(res, tuple, arg), PQfsize(res, arg));
131+
}
132+
param = va_arg(ap, char *);
133+
}
134+
va_end(ap);
135+
return tuple++;
136+
}
137+
138+
/*
139+
**
140+
** fetchwithnulls - returns tuple number (starts at 0),
141+
** or the value END_OF_TUPLES
142+
** Returns TRUE or FALSE into null indicator variables
143+
** NULL pointers are skipped
144+
*/
145+
int
146+
fetchwithnulls(void *param,...)
147+
{
148+
va_list ap;
149+
int arg,
150+
num_fields;
151+
152+
num_fields = PQnfields(res);
153+
154+
if (tuple >= PQntuples(res))
155+
return END_OF_TUPLES;
156+
157+
va_start(ap, param);
158+
for (arg = 0; arg < num_fields; arg++)
159+
{
160+
if (param != NULL)
161+
{
162+
if (PQfsize(res, arg) == -1)
163+
{
164+
memcpy(param, PQgetvalue(res, tuple, arg), PQgetlength(res, tuple, arg));
165+
((char *) param)[PQgetlength(res, tuple, arg)] = NUL;
166+
}
167+
else
168+
memcpy(param, PQgetvalue(res, tuple, arg), PQfsize(res, arg));
169+
}
170+
param = va_arg(ap, char *);
171+
if (PQgetisnull(res, tuple, arg) != 0)
172+
*(int *) param = 1;
173+
else
174+
*(int *) param = 0;
175+
param = va_arg(ap, char *);
176+
}
177+
va_end(ap);
178+
return tuple++;
179+
}
180+
181+
/*
182+
**
183+
** on_error_stop
184+
**
185+
*/
186+
void
187+
on_error_stop()
188+
{
189+
on_error_state = ON_ERROR_STOP;
190+
}
191+
192+
/*
193+
**
194+
** on_error_continue
195+
**
196+
*/
197+
void
198+
on_error_continue()
199+
{
200+
on_error_state = ON_ERROR_CONTINUE;
201+
}
202+
203+
204+
/*
205+
**
206+
** get_result
207+
**
208+
*/
209+
PGresult *
210+
get_result()
211+
{
212+
char *cmdstatus = PQcmdStatus(res);
213+
214+
was_get_unset_result = TRUE;
215+
216+
/* we have to store the fetch location somewhere */
217+
cmdstatus[0] = NUL;
218+
memcpy(&cmdstatus[1], &tuple, sizeof(tuple));
219+
220+
return res;
221+
}
222+
223+
/*
224+
**
225+
** set_result
226+
**
227+
*/
228+
void
229+
set_result(PGresult *newres)
230+
{
231+
232+
char *cmdstatus = PQcmdStatus(res);
233+
234+
if (newres == NULL)
235+
halt("set_result called with null result pointer\n");
236+
237+
if (res != NULL && was_get_unset_result == FALSE)
238+
if (in_result_block == FALSE)
239+
PQclear(res);
240+
else
241+
{
242+
cmdstatus[0] = NUL;
243+
memcpy(&cmdstatus[1], &tuple, sizeof(tuple));
244+
}
245+
246+
in_result_block = TRUE;
247+
was_get_unset_result = FALSE;
248+
249+
cmdstatus = PQcmdStatus(newres);
250+
memcpy(&tuple, &cmdstatus[1], sizeof(tuple));
251+
252+
res = newres;
253+
}
254+
255+
256+
/*
257+
**
258+
** unset_result
259+
**
260+
*/
261+
void
262+
unset_result(PGresult *oldres)
263+
{
264+
char *cmdstatus = PQcmdStatus(oldres);
265+
266+
if (oldres == NULL)
267+
halt("unset_result called with null result pointer\n");
268+
269+
if (in_result_block == FALSE)
270+
halt("Unset of result without being set.\n");
271+
272+
was_get_unset_result = TRUE;
273+
cmdstatus[0] = NUL;
274+
memcpy(&cmdstatus[1], &tuple, sizeof(tuple));
275+
in_result_block = FALSE;
276+
}
277+
278+
/*
279+
**
280+
** reset_fetch
281+
**
282+
*/
283+
void
284+
reset_fetch()
285+
{
286+
tuple = 0;
287+
}

src/interfaces/libpgeasy/libpgeasy.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* pglib.h
3+
*
4+
*/
5+
6+
PGresult *doquery(char *query);
7+
PGconn *connectdb(char *dbName,
8+
char *pghost,
9+
char *pgport,
10+
char *pgoptions,
11+
char *pgtty);
12+
void disconnectdb(void);
13+
int fetch(void *param,...);
14+
int fetchwithnulls(void *param,...);
15+
void on_error_continue(void);
16+
void on_error_stop(void);
17+
PGresult *get_result(void);
18+
void set_result(PGresult *newres);
19+
void unset_result(PGresult *oldres);
20+
void reset_fetch(void);
21+
22+
#define END_OF_TUPLES (-1)

0 commit comments

Comments
 (0)