diff options
author | Marc G. Fournier <scrappy@hub.org> | 1996-07-09 06:22:35 +0000 |
---|---|---|
committer | Marc G. Fournier <scrappy@hub.org> | 1996-07-09 06:22:35 +0000 |
commit | d31084e9d1118b25fd16580d9d8c2924b5740dff (patch) | |
tree | 3179e66307d54df9c7b966543550e601eb55e668 /src/interfaces/libpq++/examples | |
download | postgresql-PG95-1_01.tar.gz postgresql-PG95-1_01.zip |
Postgres95 1.01 Distribution - Virgin SourcesPG95-1_01
Diffstat (limited to 'src/interfaces/libpq++/examples')
-rw-r--r-- | src/interfaces/libpq++/examples/Makefile | 70 | ||||
-rw-r--r-- | src/interfaces/libpq++/examples/testlibpq0.cc | 49 | ||||
-rw-r--r-- | src/interfaces/libpq++/examples/testlibpq1.cc | 84 | ||||
-rw-r--r-- | src/interfaces/libpq++/examples/testlibpq2.cc | 71 | ||||
-rw-r--r-- | src/interfaces/libpq++/examples/testlibpq2.sql | 5 | ||||
-rw-r--r-- | src/interfaces/libpq++/examples/testlibpq3.cc | 131 | ||||
-rw-r--r-- | src/interfaces/libpq++/examples/testlibpq3.sql | 6 | ||||
-rw-r--r-- | src/interfaces/libpq++/examples/testlibpq4.cc | 69 | ||||
-rw-r--r-- | src/interfaces/libpq++/examples/testlo.cc | 63 |
9 files changed, 548 insertions, 0 deletions
diff --git a/src/interfaces/libpq++/examples/Makefile b/src/interfaces/libpq++/examples/Makefile new file mode 100644 index 00000000000..5e51d915876 --- /dev/null +++ b/src/interfaces/libpq++/examples/Makefile @@ -0,0 +1,70 @@ +# +# Makefile for example programs +# + +CPP_PROG = true + +MKDIR= ../../mk +include $(MKDIR)/postgres.mk + +CXXFLAGS+= -I$(HEADERDIR) -I$(srcdir)/libpq -I$(srcdir)/backend \ + -I$(srcdir)/backend/include + +LD_ADD+=-L$(LIBDIR) -lpq++ -lpq + +# +# And where libpq goes, so goes the authentication stuff... +# +ifdef KRBVERS +LD_ADD+= $(KRBLIBS) +CXXFLAGS+= $(KRBFLAGS) +endif + +P0_PROG:= testlibpq0 +P0_OBJS:= testlibpq0.o + +$(P0_PROG): $(addprefix $(objdir)/,$(P0_OBJS)) + $(CXX) $(CDEBUG) -o $(objdir)/$(@F) $< $(LD_ADD) + +P1_PROG:= testlibpq1 +P1_OBJS:= testlibpq1.o + +$(P1_PROG): $(addprefix $(objdir)/,$(P1_OBJS)) + $(CXX) $(CDEBUG) -o $(objdir)/$(@F) $< $(LD_ADD) + +P2_PROG:= testlibpq2 +P2_OBJS:= testlibpq2.o + +$(P2_PROG): $(addprefix $(objdir)/,$(P2_OBJS)) + $(CXX) $(CDEBUG) -o $(objdir)/$(@F) $< $(LD_ADD) + +P3_PROG:= testlibpq3 +P3_OBJS:= testlibpq3.o + +$(P3_PROG): $(addprefix $(objdir)/,$(P3_OBJS)) + $(CXX) $(CDEBUG) -o $(objdir)/$(@F) $< $(LD_ADD) + +P4_PROG:= testlibpq4 +P4_OBJS:= testlibpq4.o + +$(P4_PROG): $(addprefix $(objdir)/,$(P4_OBJS)) + $(CXX) $(CDEBUG) -o $(objdir)/$(@F) $< $(LD_ADD) + +P5_PROG:= testlo +P5_OBJS:= testlo.o + +$(P5_PROG): $(addprefix $(objdir)/,$(P5_OBJS)) + $(CXX) $(CDEBUG) -o $(objdir)/$(@F) $< $(LD_ADD) + +OBJS:= $(P0_OBJS) $(P1_OBJS) $(P2_OBJS) $(P3_OBJS) $(P4_OBJS) $(P5_OBJS) +PROGS:= $(P0_PROG) $(P1_PROG) $(P2_PROG) $(P3_PROG) $(P4_PROG) $(P5_PROG) + +CLEANFILES+= $(OBJS) $(PROGS) + +all:: $(PROGS) + +install:: $(PROGS) + @for i in ${PROGS}; do \ + echo "Installing $$i"; \ + $(INSTALL) $(objdir)/$$i $(DESTDIR)$(BINDIR)/$$i;\ + done diff --git a/src/interfaces/libpq++/examples/testlibpq0.cc b/src/interfaces/libpq++/examples/testlibpq0.cc new file mode 100644 index 00000000000..76f3ea80712 --- /dev/null +++ b/src/interfaces/libpq++/examples/testlibpq0.cc @@ -0,0 +1,49 @@ +/*------------------------------------------------------------------------- + * + * testlibpq0.c-- + * small test program for libpq++, + * small interactive loop where queries can be entered interactively + * and sent to the backend + * + * Copyright (c) 1994, Regents of the University of California + * + * + * IDENTIFICATION + * $Header: /cvsroot/pgsql/src/interfaces/libpq++/examples/Attic/testlibpq0.cc,v 1.1.1.1 1996/07/09 06:22:18 scrappy Exp $ + * + *------------------------------------------------------------------------- + */ + +#include <stdio.h> +#include "libpq++.H" + +int +main(int argc, char** argv) +{ + ExecStatusType status; + PGenv env; + PGdatabase* data; + + char buf[10000]; + int done = 0; + + data = new PGdatabase(&env, "template1"); + + if (data->status() == CONNECTION_BAD) + printf("connection was unsuccessful\n%s\n", data->errormessage()); + else + printf("connection successful\n"); + + while (!done) + { + printf("> ");fflush(stdout); + if (gets(buf) && buf[0]!='\0') + if((status = data->exec(buf)) == PGRES_TUPLES_OK) + data->printtuples(stdout, 1, "|", 1, 0); + else + printf("status = %s\nerrorMessage = %s\n", status, + data->errormessage()); + else + done = 1; + } +} diff --git a/src/interfaces/libpq++/examples/testlibpq1.cc b/src/interfaces/libpq++/examples/testlibpq1.cc new file mode 100644 index 00000000000..1d71f795583 --- /dev/null +++ b/src/interfaces/libpq++/examples/testlibpq1.cc @@ -0,0 +1,84 @@ +/* + * testlibpq.cc + * Test the C++ version of LIBPQ, the POSTGRES frontend library. + * + * queries the template1 database for a list of database names + * + */ +#include <stdio.h> +#include "libpq++.H" + +main() +{ + char* dbName; + int nFields; + int i,j; + + /* begin, by creating the parameter environtment for a backend + connection. When no parameters are given then the system will + try to use reasonable defaults by looking up environment variables + or, failing that, using hardwired constants */ + PGenv env; + PGdatabase* data; + + /* Select a database */ + dbName = "template1"; + + /* make a connection to the database */ + data = new PGdatabase(&env, dbName); + + /* check to see that the backend connection was successfully made */ + if (data->status() == CONNECTION_BAD) { + fprintf(stderr,"Connection to database '%s' failed.\n", dbName); + fprintf(stderr,"%s",data->errormessage()); + delete data; + exit(1); + } + + /* start a transaction block */ + if(data->exec("BEGIN") != PGRES_COMMAND_OK) { + fprintf(stderr,"BEGIN command failed\n"); + delete data; + exit(1); + } + + /* fetch instances from the pg_database, the system catalog of databases*/ + if (data->exec("DECLARE myportal CURSOR FOR select * from pg_database") + != PGRES_COMMAND_OK) { + fprintf(stderr,"DECLARE CURSOR command failed\n"); + delete data; + exit(1); + } + + if(data->exec("FETCH ALL in myportal") != PGRES_TUPLES_OK) { + fprintf(stderr,"FETCH ALL command didn't return tuples properly\n"); + delete data; + exit(1); + } + + /* first, print out the attribute names */ + nFields = data->nfields(); + for (i=0; i < nFields; i++) { + printf("%-15s",data->fieldname(i)); + } + printf("\n\n"); + + /* next, print out the instances */ + for (i=0; i < data->ntuples(); i++) { + for (j=0 ; j < nFields; j++) { + printf("%-15s", data->getvalue(i,j)); + } + printf("\n"); + } + + /* close the portal */ + data->exec("CLOSE myportal"); + + /* end the transaction */ + data->exec("END"); + + /* close the connection to the database and cleanup */ + delete data; +} + + diff --git a/src/interfaces/libpq++/examples/testlibpq2.cc b/src/interfaces/libpq++/examples/testlibpq2.cc new file mode 100644 index 00000000000..8eafea00b7a --- /dev/null +++ b/src/interfaces/libpq++/examples/testlibpq2.cc @@ -0,0 +1,71 @@ +/* + * testlibpq2.cc + * Test of the asynchronous notification interface + * + populate a database with the following: + +CREATE TABLE TBL1 (i int4); + +CREATE TABLE TBL2 (i int4); + +CREATE RULE r1 AS ON INSERT TO TBL1 DO [INSERT INTO TBL2 values (new.i); NOTIFY TBL2]; + + * Then start up this program + * After the program has begun, do + +INSERT INTO TBL1 values (10); + + * + * + */ +#include <stdio.h> +#include "libpq++.H" + +main() +{ + char* dbName; + int nFields; + int i,j; + + /* begin, by creating the parameter environtment for a backend + connection. When no parameters are given then the system will + try to use reasonable defaults by looking up environment variables + or, failing that, using hardwired constants */ + PGenv env; + PGdatabase* data; + PGnotify* notify; + + dbName = getenv("USER"); /* change this to the name of your test database */ + + /* make a connection to the database */ + data = new PGdatabase(&env, dbName); + + /* check to see that the backend connection was successfully made */ + if (data->status() == CONNECTION_BAD) { + fprintf(stderr,"Connection to database '%s' failed.\n", dbName); + fprintf(stderr,"%s",data->errormessage()); + delete data; + exit(1); + } + + if (data->exec("LISTEN TBL2") != PGRES_COMMAND_OK) { + fprintf(stderr,"LISTEN command failed\n"); + delete data; + exit(1); + } + + while (1) { + /* check for asynchronous returns */ + notify = data->notifies(); + if (notify) { + fprintf(stderr, + "ASYNC NOTIFY of '%s' from backend pid '%d' received\n", + notify->relname, notify->be_pid); + free(notify); + break; + } + } + + /* close the connection to the database and cleanup */ + delete data; +} diff --git a/src/interfaces/libpq++/examples/testlibpq2.sql b/src/interfaces/libpq++/examples/testlibpq2.sql new file mode 100644 index 00000000000..f9c74109328 --- /dev/null +++ b/src/interfaces/libpq++/examples/testlibpq2.sql @@ -0,0 +1,5 @@ +CREATE TABLE TBL1 (i int4); + +CREATE TABLE TBL2 (i int4); + +CREATE RULE r1 AS ON INSERT TO TBL1 DO [INSERT INTO TBL2 values (new.i); NOTIFY TBL2]; diff --git a/src/interfaces/libpq++/examples/testlibpq3.cc b/src/interfaces/libpq++/examples/testlibpq3.cc new file mode 100644 index 00000000000..1146dffac98 --- /dev/null +++ b/src/interfaces/libpq++/examples/testlibpq3.cc @@ -0,0 +1,131 @@ +/* + * testlibpq3.cc + * Test the C++ version of LIBPQ, the POSTGRES frontend library. + * tests the binary cursor interface + * + * + * + populate a database by doing the following: + +CREATE TABLE test1 (i int4, d float4, p polygon); + +INSERT INTO test1 values (1, 3.567, '(3.0, 4.0, 1.0, 2.0)'::polygon); + +INSERT INTO test1 values (2, 89.05, '(4.0, 3.0, 2.0, 1.0)'::polygon); + + the expected output is: + +tuple 0: got + i = (4 bytes) 1, + d = (4 bytes) 3.567000, + p = (4 bytes) 2 points boundbox = (hi=3.000000/4.000000, lo = 1.000000,2.000000) +tuple 1: got + i = (4 bytes) 2, + d = (4 bytes) 89.050003, + p = (4 bytes) 2 points boundbox = (hi=4.000000/3.000000, lo = 2.000000,1.000000) + + * + */ +#include <stdio.h> +#include "libpq++.H" +extern "C" { +#include "utils/geo-decls.h" /* for the POLYGON type */ +} + +main() +{ + char* dbName; + int nFields; + int i,j; + int i_fnum, d_fnum, p_fnum; + + /* begin, by creating the parameter environtment for a backend + connection. When no parameters are given then the system will + try to use reasonable defaults by looking up environment variables + or, failing that, using hardwired constants */ + PGenv env; + PGdatabase* data; + + dbName = getenv("USER"); /* change this to the name of your test database */ + + /* make a connection to the database */ + data = new PGdatabase(&env, dbName); + + /* check to see that the backend connection was successfully made */ + if (data->status() == CONNECTION_BAD) { + fprintf(stderr,"Connection to database '%s' failed.\n", dbName); + fprintf(stderr,"%s",data->errormessage()); + delete data; + exit(1); + } + + /* start a transaction block */ + if (data->exec("BEGIN") != PGRES_COMMAND_OK) { + fprintf(stderr,"BEGIN command failed\n"); + delete data; + exit(1); + } + + /* fetch instances from the pg_database, the system catalog of databases*/ + if (data->exec("DECLARE mycursor BINARY CURSOR FOR select * from test1") + != PGRES_COMMAND_OK) { + fprintf(stderr,"DECLARE CURSOR command failed\n"); + delete data; + exit(1); + } + + if (data->exec("FETCH ALL in mycursor") != PGRES_TUPLES_OK) { + fprintf(stderr,"FETCH ALL command didn't return tuples properly\n"); + delete data; + exit(1); + } + + i_fnum = data->fieldnum("i"); + d_fnum = data->fieldnum("d"); + p_fnum = data->fieldnum("p"); + +/* + for (i=0;i<3;i++) { + printf("type[%d] = %d, size[%d] = %d\n", + i, data->fieldtype(i), + i, data->fieldsize(i)); + } +*/ + + for (i=0; i < data->ntuples(); i++) { + int *ival; + float *dval; + int plen; + POLYGON* pval; + /* we hard-wire this to the 3 fields we know about */ + ival = (int*)data->getvalue(i,i_fnum); + dval = (float*)data->getvalue(i,d_fnum); + plen = data->getlength(i,p_fnum); + + /* plen doesn't include the length field so need to increment by VARHDSZ*/ + pval = (POLYGON*) malloc(plen + VARHDRSZ); + pval->size = plen; + memmove((char*)&pval->npts, data->getvalue(i,p_fnum), plen); + printf("tuple %d: got\n", i); + printf(" i = (%d bytes) %d,\n", + data->getlength(i,i_fnum), *ival); + printf(" d = (%d bytes) %f,\n", + data->getlength(i,d_fnum), *dval); + printf(" p = (%d bytes) %d points \tboundbox = (hi=%f/%f, lo = %f,%f)\n", + data->getlength(i,d_fnum), + pval->npts, + pval->boundbox.xh, + pval->boundbox.yh, + pval->boundbox.xl, + pval->boundbox.yl); + } + + /* close the portal */ + data->exec("CLOSE mycursor"); + + /* end the transaction */ + data->exec("END"); + + /* close the connection to the database and cleanup */ + delete data; +} diff --git a/src/interfaces/libpq++/examples/testlibpq3.sql b/src/interfaces/libpq++/examples/testlibpq3.sql new file mode 100644 index 00000000000..f024c0b071b --- /dev/null +++ b/src/interfaces/libpq++/examples/testlibpq3.sql @@ -0,0 +1,6 @@ +CREATE TABLE test1 (i int4, d float4, p polygon); + +INSERT INTO test1 values (1, 3.567, '(3.0, 4.0, 1.0, 2.0)'::polygon); + +INSERT INTO test1 values (2, 89.05, '(4.0, 3.0, 2.0, 1.0)'::polygon); + diff --git a/src/interfaces/libpq++/examples/testlibpq4.cc b/src/interfaces/libpq++/examples/testlibpq4.cc new file mode 100644 index 00000000000..9d5ca3ec769 --- /dev/null +++ b/src/interfaces/libpq++/examples/testlibpq4.cc @@ -0,0 +1,69 @@ +/* + * testlibpq4.cc + * Test the C++ version of LIBPQ, the POSTGRES frontend library. + * tests the copy in features + * + */ +#include <stdio.h> +#include "libpq++.H" + +#define DEBUG printf("Got here %d\n", __LINE__); +main() +{ + char* dbName; + int nFields; + int i,j; + + /* begin, by creating the parameter environment for a backend + connection. When no parameters are given then the system will + try to use reasonable defaults by looking up environment variables + or, failing that, using hardwired constants */ + PGenv env; + PGdatabase* data; + + dbName = getenv("USER"); /* change this to the name of your test database */ + + /* make a connection to the database */ + data = new PGdatabase(&env, dbName); + + /* check to see that the backend connection was successfully made */ + if (data->status() == CONNECTION_BAD) { + fprintf(stderr,"Connection to database '%s' failed.\n", dbName); + fprintf(stderr,"%s",data->errormessage()); + delete data; + exit(1); + } + + /* start a transaction block */ + if(data->exec("BEGIN") != PGRES_COMMAND_OK) { + fprintf(stderr,"BEGIN command failed\n"); + delete data; + exit(1); + } + + if (data->exec("CREATE TABLE foo (a int4, b char16, d float8)") != + PGRES_COMMAND_OK) { + fprintf(stderr,"CREATE TABLE foo command failed\n"); + delete data; + exit(1); + } + + if (data->exec("COPY foo FROM STDIN") != PGRES_COMMAND_OK) { + fprintf(stderr,"COPY foo FROM STDIN\n"); + delete data; + exit(1); + } + + data->putline("3\thello world\t4.5\n"); + data->putline("4\tgoodbye word\t7.11\n"); + data->putline(".\n"); + data->endcopy(); + data->exec("SELECT * FROM foo"); + data->printtuples(stdout,1,"|",1,0); + data->exec("DROP TABLE foo"); + // end the transaction + data->exec("END"); + + // close the connection to the database and cleanup + delete data; +} diff --git a/src/interfaces/libpq++/examples/testlo.cc b/src/interfaces/libpq++/examples/testlo.cc new file mode 100644 index 00000000000..be3459794f9 --- /dev/null +++ b/src/interfaces/libpq++/examples/testlo.cc @@ -0,0 +1,63 @@ +/*------------------------------------------------------------------------- + * + * lotest.cc-- + * test using large objects with libpq + * + * Copyright (c) 1994, Regents of the University of California + * + * + * IDENTIFICATION + * $Header: /cvsroot/pgsql/src/interfaces/libpq++/examples/Attic/testlo.cc,v 1.1.1.1 1996/07/09 06:22:19 scrappy Exp $ + * + *------------------------------------------------------------------------- + */ +#include <stdio.h> +#include "libpq++.H" +extern "C" { +#include "libpq/libpq-fs.h" +} + +int +main(int argc, char **argv) +{ + char *in_filename, *out_filename; + char *database; + Oid lobjOid; + PGenv env; + PGlobj *object; + + if (argc < 4 || argc > 5) { + fprintf(stderr, "Usage: %s database_name in_filename out_filename [oid]\n", + argv[0]); + exit(1); + } + + database = argv[1]; + in_filename = argv[2]; + out_filename = argv[3]; + + /* + * set up the connection and create a largeobject for us + */ + if (argc == 4) { + object = new PGlobj(&env, database); + } else { + object = new PGlobj(&env, database, atoi(argv[4])); + } + + /* check to see that the backend connection was successfully made */ + if (object->status() == CONNECTION_BAD) { + fprintf(stderr,"Connection to database '%s' failed.\n", database); + fprintf(stderr,"%s",object->errormessage()); + delete object; + exit(1); + } + + object->exec("BEGIN"); + printf("importing file \"%s\" ...\n", in_filename); + object->import(in_filename); + printf("exporting large object to file \"%s\" ...\n", out_filename); + object->export(out_filename); + object->exec("END"); // WHY DOES IT CORE DUMP HERE ??? + delete object; +} |