libpgeasy moved to gborg ...

This commit is contained in:
Marc G. Fournier 2002-08-30 13:03:11 +00:00
parent df40e28850
commit c411f51dfb
12 changed files with 2 additions and 887 deletions

View File

@ -4,7 +4,7 @@
#
# Copyright (c) 1994, Regents of the University of California
#
# $Header: /cvsroot/pgsql/src/interfaces/Makefile,v 1.47 2002/08/22 22:43:11 scrappy Exp $
# $Header: /cvsroot/pgsql/src/interfaces/Makefile,v 1.48 2002/08/30 13:03:09 scrappy Exp $
#
#-------------------------------------------------------------------------
@ -12,7 +12,7 @@ subdir = src/interfaces
top_builddir = ../..
include $(top_builddir)/src/Makefile.global
DIRS := libpq ecpg libpgeasy
DIRS := libpq ecpg
ALLDIRS := $(DIRS) libpgtcl perl5 python jdbc

View File

@ -1,55 +0,0 @@
#-------------------------------------------------------------------------
#
# Makefile for src/interfaces/libpgeasy
#
# $Header: /cvsroot/pgsql/src/interfaces/libpgeasy/Attic/Makefile,v 1.7 2001/05/11 01:46:33 momjian Exp $
#
#-------------------------------------------------------------------------
subdir = src/interfaces/libpgeasy
top_builddir = ../../..
include $(top_builddir)/src/Makefile.global
# shared library parameters
NAME= pgeasy
SO_MAJOR_VERSION= 2
SO_MINOR_VERSION= 2
override CPPFLAGS := -I$(libpq_srcdir) $(CPPFLAGS)
OBJS= libpgeasy.o halt.o
SHLIB_LINK = $(libpq)
# If crypt is a separate library, rather than part of libc, it may need
# to be referenced separately to keep (broken) linkers happy. (This is
# braindead; users of libpq should not need to know what it depends on.)
SHLIB_LINK+= $(filter -L%, $(LDFLAGS)) $(filter -lcrypt, $(LIBS))
all: all-lib
# Shared library stuff
include $(top_srcdir)/src/Makefile.shlib
install: all installdirs install-headers install-lib
.PHONY: install-headers
install-headers: libpgeasy.h
$(INSTALL_DATA) $< $(DESTDIR)$(includedir)/libpgeasy.h
installdirs:
$(mkinstalldirs) $(DESTDIR)$(libdir) $(DESTDIR)$(includedir)
uninstall: uninstall-lib
rm -f $(DESTDIR)$(includedir)/libpgeasy.h
clean distclean maintainer-clean: clean-lib
rm -f $(OBJS)
depend dep:
$(CC) -MM $(CFLAGS) *.c >depend
ifeq (depend,$(wildcard depend))
include depend
endif

View File

@ -1,10 +0,0 @@
Pgeasy 2.0
(Formerly contrib/pginterface)
Attached is a copy of the Postgres support routines I wrote to allow me
to more cleanly interface to the libpq library, more like a 4gl SQL
interface.
Bruce Momjian (pgman@candle.pha.pa.us)

View File

@ -1,22 +0,0 @@
#-------------------------------------------------------------------------
#
# Makefile
# Makefile for pgeasy examples
#
# IDENTIFICATION
# $Header: /cvsroot/pgsql/src/interfaces/libpgeasy/examples/Attic/Makefile,v 1.3 2002/03/04 18:50:21 momjian Exp $
#
#-------------------------------------------------------------------------
CFLAGS=-I/usr/local/pgsql/include
TARGET = pginsert pgwordcount pgnulltest pgmultiresult
LDFLAGS = -L/usr/local/pgsql/lib -lpgeasy
all : $(TARGET)
%: %.c
gcc -o $@ $(CFLAGS) $@.c $(PGEASY) $(LDFLAGS)
clean:
rm -f *.o $(TARGET) log core

View File

@ -1,98 +0,0 @@
/*
* insert.c
*
*/
#include <stdio.h>
#include <time.h>
#include "libpq-fe.h"
#include "../halt.h"
#include "libpgeasy.h"
int
main(int argc, char **argv)
{
char query[4000];
int row = 1;
int aint;
float afloat;
double adouble;
char achar[11],
abpchar[11],
avarchar[51],
atext[51];
time_t aabstime;
char optstr[256];
if (argc != 2)
halt("Usage: %s database\n", argv[0]);
snprintf(optstr, 256, "dbname=%s", argv[1]);
connectdb(optstr);
on_error_continue();
doquery("DROP TABLE testfetch");
on_error_stop();
doquery("\
CREATE TABLE testfetch( \
aint int4, \
afloat float4, \
adouble float8, \
achar char, \
abpchar char(10), \
avarchar varchar(50), \
atext text, \
aabstime abstime) \
");
while (1)
{
sprintf(query, "INSERT INTO testfetch VALUES ( \
%d, \
2322.12, \
'923121.0323'::float8, \
'A', \
'Betty', \
'Charley', \
'Doug', \
'now' )", row);
doquery(query);
doquery("BEGIN WORK");
doquery("DECLARE c_testfetch BINARY CURSOR FOR \
SELECT * FROM testfetch");
doquery("FETCH ALL IN c_testfetch");
while (fetch(
&aint,
&afloat,
&adouble,
achar,
abpchar,
avarchar,
atext,
&aabstime) != END_OF_TUPLES)
printf("int %d\nfloat %f\ndouble %f\nchar %s\n\
bpchar %s\nvarchar %s\ntext %s\nabstime %s",
aint,
afloat,
adouble,
achar,
abpchar,
avarchar,
atext,
ctime(&aabstime));
doquery("CLOSE c_testfetch");
doquery("COMMIT WORK");
printf("--- %-d rows inserted so far\n", row);
row++;
}
disconnectdb();
return 0;
}

View File

@ -1,68 +0,0 @@
/*
* pgmultiresult.c
*
*/
#include <stdio.h>
#include "libpq-fe.h"
#include "../halt.h"
#include "libpgeasy.h"
int
main(int argc, char **argv)
{
char query[4000];
char val[4000];
char optstr[256];
PGresult *res1, *res2;
int res1_done = 0, res2_done = 0;
if (argc != 2)
halt("Usage: %s database\n", argv[0]);
snprintf(optstr, 256, "dbname=%s", argv[1]);
connectdb(optstr);
doquery("\
SELECT lanname \
FROM pg_language \
ORDER BY lanname \
");
res1 = get_result();
doquery("\
SELECT amname \
FROM pg_am \
ORDER BY amname \
");
res2 = get_result();
while (!res1_done && !res2_done)
{
set_result(res1);
if (!res1_done)
{
if (fetch(val) != END_OF_TUPLES)
puts(val);
else res1_done = 1;
}
res1 = get_result();
set_result(res2);
if (!res2_done)
{
if (fetch(val) != END_OF_TUPLES)
puts(val);
else res2_done = 1;
}
res2 = get_result();
}
disconnectdb();
return 0;
}

View File

@ -1,135 +0,0 @@
/*
* pgnulltest.c
*
*/
#define TEST_NON_NULLS
#include <stdio.h>
#include <time.h>
#include "libpq-fe.h"
#include "../halt.h"
#include "libpgeasy.h"
int
main(int argc, char **argv)
{
char query[4000];
int row = 1;
int aint;
float afloat;
double adouble;
char achar[11],
abpchar[11],
avarchar[51],
atext[51];
time_t aabstime;
int aint_null,
afloat_null,
adouble_null,
achar_null,
abpchar_null,
avarchar_null,
atext_null,
aabstime_null;
char optstr[256];
if (argc != 2)
halt("Usage: %s database\n", argv[0]);
snprintf(optstr, 256, "dbname=%s", argv[1]);
connectdb(optstr);
on_error_continue();
doquery("DROP TABLE testfetch");
on_error_stop();
doquery("\
CREATE TABLE testfetch( \
aint int4, \
afloat float4, \
adouble float8, \
achar char, \
abpchar char(10), \
avarchar varchar(50), \
atext text, \
aabstime abstime) \
");
#ifdef TEST_NON_NULLS
sprintf(query, "INSERT INTO testfetch VALUES ( \
0, \
0, \
0, \
'', \
'', \
'', \
'', \
CURRENT_TIMESTAMP::abstime);");
#else
sprintf(query, "INSERT INTO testfetch VALUES ( \
NULL, \
NULL, \
NULL, \
NULL, \
NULL, \
NULL, \
NULL, \
NULL);");
#endif
doquery(query);
doquery("BEGIN WORK");
doquery("DECLARE c_testfetch BINARY CURSOR FOR \
SELECT * FROM testfetch");
doquery("FETCH ALL IN c_testfetch");
if (fetchwithnulls(
&aint,
&aint_null,
&afloat,
&afloat_null,
&adouble,
&adouble_null,
achar,
&achar_null,
abpchar,
&abpchar_null,
avarchar,
&avarchar_null,
atext,
&atext_null,
&aabstime,
&aabstime_null) != END_OF_TUPLES)
printf("int %d\nfloat %f\ndouble %f\nchar %s\n\
bpchar %s\nvarchar %s\ntext %s\nabstime %s\n",
aint,
afloat,
adouble,
achar,
abpchar,
avarchar,
atext,
ctime(&aabstime));
printf("NULL:\nint %d\nfloat %d\ndouble %d\nchar %d\n\
bpchar %d\nvarchar %d\ntext %d\nabstime %d\n",
aint_null,
afloat_null,
adouble_null,
achar_null,
abpchar_null,
avarchar_null,
atext_null,
aabstime_null);
doquery("CLOSE c_testfetch");
doquery("COMMIT WORK");
printf("--- %-d rows inserted so far\n", row);
row++;
disconnectdb();
return 0;
}

View File

@ -1,73 +0,0 @@
/*
* wordcount.c
*
*/
#include <stdio.h>
#include "libpq-fe.h"
#include "../halt.h"
#include "libpgeasy.h"
int
main(int argc, char **argv)
{
char query[4000];
int row = 0;
int count;
char line[4000];
char optstr[256];
if (argc != 2)
halt("Usage: %s database\n", argv[0]);
snprintf(optstr, 256, "dbname=%s", argv[1]);
connectdb(optstr);
on_error_continue();
doquery("DROP TABLE words");
on_error_stop();
doquery("\
CREATE TABLE words( \
matches int4, \
word text ) \
");
doquery("\
CREATE INDEX i_words_1 ON words USING btree ( \
word text_ops )\
");
while (1)
{
if (scanf("%s", line) != 1)
break;
doquery("BEGIN WORK");
sprintf(query, "\
DECLARE c_words BINARY CURSOR FOR \
SELECT count(*) \
FROM words \
WHERE word = '%s'", line);
doquery(query);
doquery("FETCH ALL IN c_words");
while (fetch(&count) == END_OF_TUPLES)
count = 0;
doquery("CLOSE c_words");
doquery("COMMIT WORK");
if (count == 0)
sprintf(query, "\
INSERT INTO words \
VALUES (1, '%s')", line);
else
sprintf(query, "\
UPDATE words \
SET matches = matches + 1 \
WHERE word = '%s'", line);
doquery(query);
row++;
}
disconnectdb();
return 0;
}

View File

@ -1,56 +0,0 @@
/*
**
** halt.c
**
** This is used to print out error messages and exit
*/
#include <stdarg.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "halt.h"
/*-------------------------------------------------------------------------
**
** halt - print error message, and call clean up routine or exit
**
**------------------------------------------------------------------------*/
void
halt(char *format,...)
{
va_list arg_ptr;
char *pstr;
void (*sig_func) ();
va_start(arg_ptr, format);
if (strncmp(format, "PERROR", 6) != 0)
vfprintf(stderr, format, arg_ptr);
else
{
for (pstr = format + 6; *pstr == ' ' || *pstr == ':'; pstr++)
;
vfprintf(stderr, pstr, arg_ptr);
perror("");
}
va_end(arg_ptr);
fflush(stderr);
/* call one clean up function if defined */
if ((sig_func = signal(SIGTERM, SIG_DFL)) != SIG_DFL &&
sig_func != SIG_IGN)
(*sig_func) (0);
else if ((sig_func = signal(SIGHUP, SIG_DFL)) != SIG_DFL &&
sig_func != SIG_IGN)
(*sig_func) (0);
else if ((sig_func = signal(SIGINT, SIG_DFL)) != SIG_DFL &&
sig_func != SIG_IGN)
(*sig_func) (0);
else if ((sig_func = signal(SIGQUIT, SIG_DFL)) != SIG_DFL &&
sig_func != SIG_IGN)
(*sig_func) (0);
exit(1);
}

View File

@ -1,6 +0,0 @@
/*
** halt.h
**
*/
void halt(char *format,...);

View File

@ -1,344 +0,0 @@
/*
* pgeasy.c
*
*/
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include "libpq-fe.h"
#include "halt.h"
#include "libpgeasy.h"
#ifndef NUL
#define NUL '\0'
#endif
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
/* GLOBAL VARIABLES */
static PGconn *conn;
static PGresult *res = NULL;
static int tuple; /* stores fetch location */
#define ON_ERROR_STOP 0
#define ON_ERROR_CONTINUE 1
static int on_error_state = ON_ERROR_STOP; /* halt on errors? */
static int user_has_res = FALSE;
static void add_res_tuple(void);
static void get_res_tuple(void);
static void del_res_tuple(void);
/*
* connectdb - returns PGconn structure
*/
PGconn *
connectdb(char *options)
{
/* make a connection to the database */
conn = PQconnectdb(options);
if (PQstatus(conn) == CONNECTION_BAD)
halt("Connection to database using '%s' failed.\n%s\n", options,
PQerrorMessage(conn));
return conn;
}
/*
* disconnectdb
*/
void
disconnectdb()
{
if (res != NULL && user_has_res == FALSE)
{
PQclear(res);
res = NULL;
}
PQfinish(conn);
}
/*
* doquery - returns PGresult structure
*/
PGresult *
doquery(char *query)
{
if (res != NULL && user_has_res == FALSE)
PQclear(res);
user_has_res = 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", PQresultErrorMessage(res));
else
fprintf(stderr, "connection error: %s\n", PQerrorMessage(conn));
PQfinish(conn);
halt("failed query: %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++;
}
/*
* reset_fetch
*/
void
reset_fetch()
{
tuple = 0;
}
/*
* 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()
{
if (res == NULL)
halt("get_result called with no result pointer used\n");
/* delete it if it is already there; we are about to re-add it */
del_res_tuple();
/* we have to store the fetch location */
add_res_tuple();
user_has_res = TRUE;
return res;
}
/*
* set_result
*/
void
set_result(PGresult *newres)
{
if (newres == NULL)
halt("set_result called with null result pointer\n");
if (res != NULL && user_has_res == FALSE)
{
/*
* Basically, throw away res. We can't return to it because the
* user doesn't have the res pointer.
*/
del_res_tuple();
PQclear(res);
}
user_has_res = FALSE;
res = newres;
get_res_tuple();
}
/*
* Routines to store res/tuple mapping
* This is used to keep track of fetch locations while using get/set on
* result sets.
* Auto-growing array is used, with empty slots marked by res == NULL
*/
static struct res_tuple
{
PGresult *res;
int tuple;
} *res_tuple = NULL;
static int res_tuple_len = 0;
/*
* add_res_tuple
*/
static void
add_res_tuple(void)
{
int i,
new_res_tuple_len = res_tuple_len ? res_tuple_len * 2 : 1;
for (i = 0; i < res_tuple_len; i++)
/* Put it in an empty slot */
if (res_tuple[i].res == NULL)
{
res_tuple[i].res = res;
res_tuple[i].tuple = tuple;
}
/* Need to grow array */
res_tuple = realloc(res_tuple, new_res_tuple_len * sizeof(struct res_tuple));
/* clear new elements */
for (i = res_tuple_len; i < new_res_tuple_len; i++)
{
res_tuple[i].res = NULL;
res_tuple[i].tuple = 0;
}
/* recursion to add entry */
add_res_tuple();
}
/*
* get_res_tuple
*/
static void
get_res_tuple(void)
{
int i;
for (i = 0; i < res_tuple_len; i++)
if (res_tuple[i].res == res)
{
tuple = res_tuple[i].tuple;
return;
}
halt("get_res_tuple called with invalid result pointer\n");
}
/*
* del_res_tuple
*/
static void
del_res_tuple(void)
{
int i;
for (i = 0; i < res_tuple_len; i++)
if (res_tuple[i].res == res)
{
res_tuple[i].res = NULL;
return;
}
return;
}

View File

@ -1,18 +0,0 @@
/*
* pglib.h
*
*/
PGresult *doquery(char *query);
PGconn *connectdb(char *options);
void disconnectdb(void);
int fetch(void *param,...);
int fetchwithnulls(void *param,...);
void on_error_continue(void);
void on_error_stop(void);
PGresult *get_result(void);
void set_result(PGresult *newres);
void unset_result(PGresult *oldres);
void reset_fetch(void);
#define END_OF_TUPLES (-1)