Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a5d4642

Browse files
committed
Update pginterface for 6.4. add manual page.
1 parent 4da5714 commit a5d4642

File tree

3 files changed

+119
-23
lines changed

3 files changed

+119
-23
lines changed

contrib/pginterface/README

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ have a global variable that allows you to disable the error checking I
1616
have added to the doquery() routine.
1717

1818
I have added a function called fetch(), which allows you to pass
19-
pointers as parameters, and on return the variables are filled with the
20-
data from the binary cursor you opened. These binary cursors are not
21-
useful if you are running the query engine on a system with a different
19+
pointers as parameters, and on return the variables are filled with data
20+
from the binary cursor you opened. These binary cursors are not useful
21+
if you are running the query engine on a system with a different
2222
architecture than the database server. If you pass a NULL pointer, the
2323
column is skipped, and you can use libpq to handle it as you wish.
2424

25-
There are two functions, get_result() and set_result, that allow you to
26-
handle multiple result sets at the same time.
25+
There are two functions, get_result() and set_result(), that allow you
26+
to handle multiple result sets at the same time.
2727

2828
There is a reset_fetch() that starts the fetch back at the beginning.
2929

contrib/pginterface/pginterface.3

+71
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/contrib/pginterface/Attic/pginterface.3,v 1.1 1998/09/11 05:14:08 momjian Exp $
4+
.TH PGINTERFACE INTRO 08/08/98 PostgreSQL PostgreSQL
5+
.SH DESCRIPTION
6+
Pginterface 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 pginterface
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 pginterface
71+
source directory.

contrib/pginterface/pginterface.c

+43-18
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@
1313

1414
#define NUL '\0'
1515

16+
#ifndef TRUE
17+
#define TRUE 1
18+
#endif
19+
20+
#ifndef FALSE
21+
#define FALSE 0
22+
#endif
23+
1624
/* GLOBAL VARIABLES */
1725
static PGconn *conn;
1826
static PGresult *res = NULL;
@@ -22,8 +30,8 @@ static PGresult *res = NULL;
2230

2331
static int on_error_state = ON_ERROR_STOP;
2432

25-
static in_result_block = false;
26-
static was_get_unset_result = false;
33+
static in_result_block = FALSE;
34+
static was_get_unset_result = FALSE;
2735

2836
/* LOCAL VARIABLES */
2937
static int tuple;
@@ -67,10 +75,10 @@ disconnectdb()
6775
PGresult *
6876
doquery(char *query)
6977
{
70-
if (res != NULL && in_result_block == false && was_get_unset_result == false)
78+
if (res != NULL && in_result_block == FALSE && was_get_unset_result == FALSE)
7179
PQclear(res);
7280

73-
was_get_unset_result = false;
81+
was_get_unset_result = FALSE;
7482
res = PQexec(conn, query);
7583

7684
if (on_error_state == ON_ERROR_STOP &&
@@ -131,7 +139,7 @@ fetch(void *param,...)
131139
**
132140
** fetchwithnulls - returns tuple number (starts at 0),
133141
** or the value END_OF_TUPLES
134-
** Returns true or false into null indicator variables
142+
** Returns TRUE or FALSE into null indicator variables
135143
** NULL pointers are skipped
136144
*/
137145
int
@@ -200,9 +208,14 @@ on_error_continue()
200208
*/
201209
PGresult *get_result()
202210
{
203-
was_get_unset_result = true;
211+
char *cmdstatus = PQcmdStatus(res);
212+
213+
was_get_unset_result = TRUE;
214+
204215
/* we have to store the fetch location somewhere */
205-
memcpy(&res->cmdStatus[CMDSTATUS_LEN-sizeof(tuple)],&tuple, sizeof(tuple));
216+
cmdstatus[0] = NUL;
217+
memcpy(&cmdstatus[1],&tuple, sizeof(tuple));
218+
206219
return res;
207220
}
208221

@@ -213,18 +226,27 @@ PGresult *get_result()
213226
*/
214227
void set_result(PGresult *newres)
215228
{
229+
230+
char *cmdstatus = PQcmdStatus(res);
231+
216232
if (newres == NULL)
217233
halt("set_result called with null result pointer\n");
218234

219-
if (res != NULL && was_get_unset_result == false)
220-
if (in_result_block == false)
235+
if (res != NULL && was_get_unset_result == FALSE)
236+
if (in_result_block == FALSE)
221237
PQclear(res);
222238
else
223-
memcpy(&res->cmdStatus[CMDSTATUS_LEN-sizeof(tuple)], &tuple, sizeof(tuple));
224-
225-
in_result_block = true;
226-
was_get_unset_result = false;
227-
memcpy(&tuple, &newres->cmdStatus[CMDSTATUS_LEN-sizeof(tuple)], sizeof(tuple));
239+
{
240+
cmdstatus[0] = NUL;
241+
memcpy(&cmdstatus[1], &tuple, sizeof(tuple));
242+
}
243+
244+
in_result_block = TRUE;
245+
was_get_unset_result = FALSE;
246+
247+
cmdstatus = PQcmdStatus(newres);
248+
memcpy(&tuple, &cmdstatus[1], sizeof(tuple));
249+
228250
res = newres;
229251
}
230252

@@ -236,15 +258,18 @@ void set_result(PGresult *newres)
236258
*/
237259
void unset_result(PGresult *oldres)
238260
{
261+
char *cmdstatus = PQcmdStatus(oldres);
262+
239263
if (oldres == NULL)
240264
halt("unset_result called with null result pointer\n");
241265

242-
if (in_result_block == false)
266+
if (in_result_block == FALSE)
243267
halt("Unset of result without being set.\n");
244268

245-
was_get_unset_result = true;
246-
memcpy(&oldres->cmdStatus[CMDSTATUS_LEN-sizeof(tuple)], &tuple, sizeof(tuple));
247-
in_result_block = false;
269+
was_get_unset_result = TRUE;
270+
cmdstatus[0] = NUL;
271+
memcpy(&cmdstatus[1], &tuple, sizeof(tuple));
272+
in_result_block = FALSE;
248273
}
249274

250275
/*

0 commit comments

Comments
 (0)