mirror of
https://github.com/postgres/postgres.git
synced 2025-09-02 04:21:28 +03:00
Bring in Leo's <lsh@lubrizol.com> massive changes to libpq++
This commit is contained in:
@@ -1,66 +1,58 @@
|
||||
/*
|
||||
* testlibpq4.cc
|
||||
* Test the C++ version of LIBPQ, the POSTGRES frontend library.
|
||||
* tests the copy in features
|
||||
* Test of the asynchronous notification interface
|
||||
*
|
||||
populate a test database with the following (use testlibpq4.sql):
|
||||
|
||||
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"
|
||||
#include <iostream.h>
|
||||
#include <libpq++.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
main()
|
||||
{
|
||||
char* dbName;
|
||||
// Begin, by connecting to the backend using hardwired constants
|
||||
// and a test database created by the user prior to the invokation
|
||||
// of this test program.
|
||||
char* dbName = getenv("USER"); // change this to the name of your test database
|
||||
PgDatabase data(dbName);
|
||||
|
||||
/* 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;
|
||||
// Check to see that the backend connection was successfully made
|
||||
if ( data.ConnectionBad() ) {
|
||||
cerr << "Connection to database '" << dbName << "' failed." << endl
|
||||
<< data.ErrorMessage() << endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* start a transaction block */
|
||||
if(data->exec("BEGIN") != PGRES_COMMAND_OK) {
|
||||
fprintf(stderr,"BEGIN command failed\n");
|
||||
delete data;
|
||||
// Listen to a table
|
||||
if ( !data.ExecCommandOk("LISTEN TBL2") ) {
|
||||
cerr << "LISTEN command failed" << endl;
|
||||
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);
|
||||
// Test asynchronous notification
|
||||
while (1) {
|
||||
// check for asynchronous returns
|
||||
PGnotify* notify = data.Notifies();
|
||||
if (notify) {
|
||||
cerr << "ASYNC NOTIFY of '" << notify->relname
|
||||
<< "' from backend pid '" << notify->be_pid
|
||||
<< "' received" << endl;
|
||||
free(notify);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user