Skip to content

Commit 715c6b6

Browse files
committed
Newer version of Bruce's pginterface library...
1 parent ee9b801 commit 715c6b6

File tree

9 files changed

+667
-0
lines changed

9 files changed

+667
-0
lines changed

contrib/pginterface/Makefile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#
2+
# Makefile
3+
#
4+
#
5+
PGINTERFACE = pginterface.o halt.o
6+
TARGET = pginsert pgwordcount pgnulltest
7+
CFLAGS = -g -Wall -I/u/postgres95/include
8+
LDFLAGS = -L/u/postgres95/lib -lpq
9+
10+
all : $(TARGET)
11+
12+
$(TARGET): $(PGINTERFACE) $*.c
13+
cc -o $* $(CFLAGS) $*.c $(PGINTERFACE) $(LDFLAGS)
14+
15+
$(PGINTERFACE): pginterface.c halt.c
16+
cc -c $(CFLAGS) pginterface.c halt.c
17+
18+
clean:
19+
rm -f *.o $(TARGET) log core
20+
21+
install:
22+
make clean
23+
make CFLAGS=-O
24+
install -s -o bin -g bin $(TARGET) /usr/local/bin

contrib/pginterface/README

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
3+
Pginterface 1.0
4+
5+
Attached is a copy of the Postgres support routines I wrote to allow me
6+
to more cleanly interface to the libpg library, more like a 4gl SQL
7+
interface.
8+
9+
It has several features that may be useful for others:
10+
11+
I have simplified the C code that calls libpq by wrapping all the
12+
functionality of libpq in calls to connectdb(), doquery(), fetch(),
13+
fetchisnull() and disconnectdb(). Each call returns a structure or
14+
value, so if you need to do more work with the result, you can. Also, I
15+
have a global variable that allows you to disable the error checking I
16+
have added to the doquery() routine.
17+
18+
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
22+
architecture than the database server. If you pass a NULL pointer, the
23+
column is skipped, and you can use libpq to handle it as you wish.
24+
25+
I have used sigprocmask() to block the reception of certain signals
26+
while the program is executing SQL queries. This prevents a user
27+
pressing Control-C from stopping all the back ends. It blocks SIGHUP,
28+
SIGINT, and SIGTERM, but does not block SIGQUIT or obviously kill -9.
29+
If your platform does not support sigprocmask(), you can remove those
30+
function calls. ( Am I correct that abnormal termination can cause
31+
shared memory resynchronization?)
32+
33+
There is a demo program called pginsert that demonstrates how the
34+
library can be used.
35+
36+
You can create a library of pginterface.c and halt.c, and just include
37+
pginterface.h in your source code.
38+
39+
I am willing to maintain this if people find problems or want additional
40+
functionality.
41+
42+
Bruce Momjian (root@candle.pha.pa.us)

contrib/pginterface/halt.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
**
3+
** halt.c
4+
**
5+
** This is used to print out error messages and exit
6+
*/
7+
8+
#include <varargs.h>
9+
#include <signal.h>
10+
#include <stdio.h>
11+
#include <stdlib.h>
12+
#include <string.h>
13+
#include <errno.h>
14+
15+
16+
/*-------------------------------------------------------------------------
17+
**
18+
** halt - print error message, and call clean up routine or exit
19+
**
20+
**------------------------------------------------------------------------*/
21+
22+
/*VARARGS*/
23+
void halt(va_alist)
24+
va_dcl
25+
{
26+
va_list arg_ptr;
27+
char *format, *pstr;
28+
void (*sig_func)();
29+
30+
va_start(arg_ptr);
31+
format = va_arg(arg_ptr,char *);
32+
if (strncmp(format,"PERROR", 6) != 0)
33+
vfprintf(stderr,format,arg_ptr);
34+
else
35+
{
36+
for (pstr=format+6; *pstr == ' ' || *pstr == ':'; pstr++)
37+
;
38+
vfprintf(stderr,pstr,arg_ptr);
39+
perror("");
40+
}
41+
va_end(arg_ptr);
42+
fflush(stderr);
43+
44+
/* call one clean up function if defined */
45+
if ( (sig_func = signal(SIGTERM, SIG_DFL)) != SIG_DFL &&
46+
sig_func != SIG_IGN)
47+
(*sig_func)(0);
48+
else if ( (sig_func = signal(SIGHUP, SIG_DFL)) != SIG_DFL &&
49+
sig_func != SIG_IGN)
50+
(*sig_func)(0);
51+
else if ( (sig_func = signal(SIGINT, SIG_DFL)) != SIG_DFL &&
52+
sig_func != SIG_IGN)
53+
(*sig_func)(0);
54+
else if ( (sig_func = signal(SIGQUIT, SIG_DFL)) != SIG_DFL &&
55+
sig_func != SIG_IGN)
56+
(*sig_func)(0);
57+
exit(1);
58+
}

contrib/pginterface/halt.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*
2+
** halt.h
3+
**
4+
*/
5+
6+
void halt();
7+

contrib/pginterface/pginsert.c

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* insert.c
3+
*
4+
*/
5+
6+
#include <stdio.h>
7+
#include <signal.h>
8+
#include <time.h>
9+
#include <libpq-fe.h>
10+
#include "halt.h"
11+
#include "pginterface.h"
12+
13+
int main(int argc, char **argv)
14+
{
15+
char query[4000];
16+
int row =1;
17+
int aint;
18+
float afloat;
19+
double adouble;
20+
char achar[11], achar16[17], abpchar[11], avarchar[51], atext[51];
21+
time_t aabstime;
22+
23+
if (argc != 2)
24+
halt("Usage: %s database\n",argv[0]);
25+
26+
connectdb(argv[1],NULL,NULL,NULL,NULL);
27+
28+
on_error_continue();
29+
doquery("DROP TABLE testfetch");
30+
on_error_stop();
31+
32+
doquery("\
33+
CREATE TABLE testfetch( \
34+
aint int4, \
35+
afloat float4, \
36+
adouble float8, \
37+
achar char, \
38+
achar16 char16, \
39+
abpchar char(10), \
40+
avarchar varchar(50), \
41+
atext text, \
42+
aabstime abstime) \
43+
");
44+
45+
while(1)
46+
{
47+
sprintf(query,"INSERT INTO testfetch VALUES ( \
48+
%d, \
49+
2322.12, \
50+
'923121.0323'::float8, \
51+
'A', \
52+
'Betty', \
53+
'Charley', \
54+
'Doug', \
55+
'Ernie', \
56+
'now' )", row);
57+
doquery(query);
58+
59+
doquery("BEGIN WORK");
60+
doquery("DECLARE c_testfetch BINARY CURSOR FOR \
61+
SELECT * FROM testfetch");
62+
63+
doquery("FETCH ALL IN c_testfetch");
64+
65+
while (fetch(
66+
&aint,
67+
&afloat,
68+
&adouble,
69+
achar,
70+
achar16,
71+
abpchar,
72+
avarchar,
73+
atext,
74+
&aabstime) != END_OF_TUPLES)
75+
printf("int %d\nfloat %f\ndouble %f\nchar %s\nchar16 %s\n\
76+
bpchar %s\nvarchar %s\ntext %s\nabstime %s",
77+
aint,
78+
afloat,
79+
adouble,
80+
achar,
81+
achar16,
82+
abpchar,
83+
avarchar,
84+
atext,
85+
ctime(&aabstime));
86+
87+
88+
doquery("CLOSE c_testfetch");
89+
doquery("COMMIT WORK");
90+
printf("--- %-d rows inserted so far\n",row);
91+
92+
row++;
93+
}
94+
95+
disconnectdb();
96+
return 0;
97+
}
98+

0 commit comments

Comments
 (0)