Indent libpq++ as mentioned in email. Format was terrible, and this

will make fixing things easier.
This commit is contained in:
Bruce Momjian 2002-07-02 16:32:19 +00:00
parent c9a7345217
commit a4485ea894
19 changed files with 969 additions and 902 deletions

View File

@ -1,52 +1,53 @@
/*-------------------------------------------------------------------------
*
* 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.6 2000/05/29 21:25:04 momjian Exp $
*
*-------------------------------------------------------------------------
*/
*
* 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.7 2002/07/02 16:32:19 momjian Exp $
*
*-------------------------------------------------------------------------
*/
#include <iostream.h>
#include "libpq++.h"
int main()
{
// Open the connection to the database and make sure it's OK
PgDatabase data("dbname=template1");
if ( data.ConnectionBad() ) {
cout << "Connection was unsuccessful..." << endl
<< "Error message returned: " << data.ErrorMessage() << endl;
return 1;
}
else
cout << "Connection successful... Enter queries below:" << endl;
// Interactively obtain and execute queries
ExecStatusType status;
string buf;
int done = 0;
while (!done)
{
cout << "> ";
cout.flush();
getline(cin, buf);
if ( buf != "" )
if ( (status = data.Exec( buf.c_str() )) == PGRES_TUPLES_OK )
data.DisplayTuples();
else
cout << "No tuples returned..." << endl
<< "status = " << status << endl
<< "Error returned: " << data.ErrorMessage() << endl;
else
done = 1;
}
return 0;
// Open the connection to the database and make sure it's OK
PgDatabase data("dbname=template1");
if ( data.ConnectionBad() )
{
cout << "Connection was unsuccessful..." << endl
<< "Error message returned: " << data.ErrorMessage() << endl;
return 1;
}
else
cout << "Connection successful... Enter queries below:" << endl;
// Interactively obtain and execute queries
ExecStatusType status;
string buf;
int done = 0;
while (!done)
{
cout << "> ";
cout.flush();
getline(cin, buf);
if ( buf != "" )
if ( (status = data.Exec( buf.c_str() )) == PGRES_TUPLES_OK )
data.DisplayTuples();
else
cout << "No tuples returned..." << endl
<< "status = " << status << endl
<< "Error returned: " << data.ErrorMessage() << endl;
else
done = 1;
}
return 0;
} // End main()

View File

@ -1,10 +1,10 @@
/*
* testlibpq1.cc
* Test the C++ version of LIBPQ, the POSTGRES frontend library.
*
* queries the template1 database for a list of database names
*
*/
* testlibpq1.cc
* Test the C++ version of LIBPQ, the POSTGRES frontend library.
*
* queries the template1 database for a list of database names
*
*/
#include <iostream.h>
#include <iomanip.h>
@ -12,57 +12,62 @@
int main()
{
// Begin, by establishing a connection to the backend.
// 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
const char* dbName = "dbname=template1";
PgDatabase data(dbName);
// Begin, by establishing a connection to the backend.
// 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
const char* dbName = "dbname=template1";
PgDatabase data(dbName);
// check to see that the backend connection was successfully made
if ( data.ConnectionBad() ) {
cerr << "Connection to database '" << dbName << "' failed." << endl
<< "Error returned: " << data.ErrorMessage() << endl;
exit(1);
}
// check to see that the backend connection was successfully made
if ( data.ConnectionBad() )
{
cerr << "Connection to database '" << dbName << "' failed." << endl
<< "Error returned: " << data.ErrorMessage() << endl;
exit(1);
}
// start a transaction block
if ( !data.ExecCommandOk("BEGIN") ) {
cerr << "BEGIN command failed" << endl;
exit(1);
}
// start a transaction block
if ( !data.ExecCommandOk("BEGIN") )
{
cerr << "BEGIN command failed" << endl;
exit(1);
}
// submit command to the backend
if ( !data.ExecCommandOk("DECLARE myportal CURSOR FOR select * from pg_database") ) {
cerr << "DECLARE CURSOR command failed" << endl;
exit(1);
}
// submit command to the backend
if ( !data.ExecCommandOk("DECLARE myportal CURSOR FOR select * from pg_database") )
{
cerr << "DECLARE CURSOR command failed" << endl;
exit(1);
}
// fetch instances from the pg_database, the system catalog of databases
if ( !data.ExecTuplesOk("FETCH ALL in myportal") ) {
cerr << "FETCH ALL command didn't return tuples properly" << endl;
exit(1);
}
// first, print out the attribute names
int nFields = data.Fields();
for (int i=0; i < nFields; i++)
cout << setiosflags(ios::right) << setw(15) << data.FieldName(i);
cout << endl << endl;
// fetch instances from the pg_database, the system catalog of databases
if ( !data.ExecTuplesOk("FETCH ALL in myportal") )
{
cerr << "FETCH ALL command didn't return tuples properly" << endl;
exit(1);
}
// next, print out the instances
for (int i=0; i < data.Tuples(); i++) {
for (int j=0; j < nFields; j++)
cout << setiosflags(ios::right) << setw(15) << data.GetValue(i,j);
cout << endl;
}
// first, print out the attribute names
int nFields = data.Fields();
for (int i = 0; i < nFields; i++)
cout << setiosflags(ios::right) << setw(15) << data.FieldName(i);
cout << endl << endl;
// Close the portal
data.Exec("CLOSE myportal");
// next, print out the instances
for (int i = 0; i < data.Tuples(); i++)
{
for (int j = 0; j < nFields; j++)
cout << setiosflags(ios::right) << setw(15) << data.GetValue(i, j);
cout << endl;
}
// End the transaction
data.Exec("END");
return 0;
// Close the portal
data.Exec("CLOSE myportal");
// End the transaction
data.Exec("END");
return 0;
}

View File

@ -1,10 +1,10 @@
/*
* testlibpq2.cc
* Test the C++ version of LIBPQ, the POSTGRES frontend library.
*
* queries the template1 database for a list of database names using transaction block
*
*/
* testlibpq2.cc
* Test the C++ version of LIBPQ, the POSTGRES frontend library.
*
* queries the template1 database for a list of database names using transaction block
*
*/
#include <iostream.h>
#include <iomanip.h>
@ -12,46 +12,50 @@
int main()
{
// Begin, by establishing a connection to the backend.
// 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
const char* dbName = "dbname=template1";
PgTransaction data(dbName);
// Begin, by establishing a connection to the backend.
// 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
const char* dbName = "dbname=template1";
PgTransaction data(dbName);
// check to see that the backend connection was successfully made
if ( data.ConnectionBad() ) {
cerr << "Connection to database '" << dbName << "' failed." << endl
<< "Error returned: " << data.ErrorMessage() << endl;
exit(1);
}
// check to see that the backend connection was successfully made
if ( data.ConnectionBad() )
{
cerr << "Connection to database '" << dbName << "' failed." << endl
<< "Error returned: " << data.ErrorMessage() << endl;
exit(1);
}
// submit command to the backend
if ( !data.ExecCommandOk("DECLARE myportal CURSOR FOR select * from pg_database") ) {
cerr << "DECLARE CURSOR command failed" << endl;
exit(1);
}
// submit command to the backend
if ( !data.ExecCommandOk("DECLARE myportal CURSOR FOR select * from pg_database") )
{
cerr << "DECLARE CURSOR command failed" << endl;
exit(1);
}
// fetch instances from the pg_database, the system catalog of databases
if ( !data.ExecTuplesOk("FETCH ALL in myportal") ) {
cerr << "FETCH ALL command didn't return tuples properly" << endl;
exit(1);
}
// first, print out the attribute names
int nFields = data.Fields();
for (int i=0; i < nFields; i++)
cout << setiosflags(ios::right) << setw(15) << data.FieldName(i);
cout << endl << endl;
// fetch instances from the pg_database, the system catalog of databases
if ( !data.ExecTuplesOk("FETCH ALL in myportal") )
{
cerr << "FETCH ALL command didn't return tuples properly" << endl;
exit(1);
}
// next, print out the instances
for (int i=0; i < data.Tuples(); i++) {
for (int j=0; j < nFields; j++)
cout << setiosflags(ios::right) << setw(15) << data.GetValue(i,j);
cout << endl;
}
// first, print out the attribute names
int nFields = data.Fields();
for (int i = 0; i < nFields; i++)
cout << setiosflags(ios::right) << setw(15) << data.FieldName(i);
cout << endl << endl;
// close the portal
data.Exec("CLOSE myportal");
return 0;
// next, print out the instances
for (int i = 0; i < data.Tuples(); i++)
{
for (int j = 0; j < nFields; j++)
cout << setiosflags(ios::right) << setw(15) << data.GetValue(i, j);
cout << endl;
}
// close the portal
data.Exec("CLOSE myportal");
return 0;
}

View File

@ -1,11 +1,11 @@
/*
* testlibpq3.cc
* Test the C++ version of LIBPQ, the POSTGRES frontend library.
*
* queries the template1 database for a list of database names using transaction block
* and cursor interface.
*
*/
* testlibpq3.cc
* Test the C++ version of LIBPQ, the POSTGRES frontend library.
*
* queries the template1 database for a list of database names using transaction block
* and cursor interface.
*
*/
#include <iostream.h>
#include <iomanip.h>
@ -13,45 +13,49 @@
int main()
{
// Begin, by establishing a connection to the backend.
// 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.
// Create a cursor database query object.
// All queries using cursor will be performed through this object.
const char* dbName = "dbname=template1";
PgCursor cData(dbName, "myportal");
// Begin, by establishing a connection to the backend.
// 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.
// Create a cursor database query object.
// All queries using cursor will be performed through this object.
const char* dbName = "dbname=template1";
PgCursor cData(dbName, "myportal");
// check to see that the backend connection was successfully made
if ( cData.ConnectionBad() ) {
cerr << "Connection to database '" << dbName << "' failed." << endl
<< "Error returned: " << cData.ErrorMessage() << endl;
exit(1);
}
// submit command to the backend
if ( !cData.Declare("select * from pg_database") ) {
cerr << "DECLARE CURSOR command failed" << endl;
exit(1);
}
// check to see that the backend connection was successfully made
if ( cData.ConnectionBad() )
{
cerr << "Connection to database '" << dbName << "' failed." << endl
<< "Error returned: " << cData.ErrorMessage() << endl;
exit(1);
}
// fetch instances from the pg_cDatabase, the system catalog of cDatabases
if ( !cData.Fetch() ) {
cerr << "FETCH ALL command didn't return tuples properly" << endl;
exit(1);
}
// first, print out the attribute names
int nFields = cData.Fields();
for (int i=0; i < nFields; i++)
cout << setiosflags(ios::right) << setw(15) << cData.FieldName(i);
cout << endl << endl;
// submit command to the backend
if ( !cData.Declare("select * from pg_database") )
{
cerr << "DECLARE CURSOR command failed" << endl;
exit(1);
}
// next, print out the instances
for (int i=0; i < cData.Tuples(); i++) {
for (int j=0; j < nFields; j++)
cout << setiosflags(ios::right) << setw(15) << cData.GetValue(i,j);
cout << endl;
}
return 0;
// fetch instances from the pg_cDatabase, the system catalog of cDatabases
if ( !cData.Fetch() )
{
cerr << "FETCH ALL command didn't return tuples properly" << endl;
exit(1);
}
// first, print out the attribute names
int nFields = cData.Fields();
for (int i = 0; i < nFields; i++)
cout << setiosflags(ios::right) << setw(15) << cData.FieldName(i);
cout << endl << endl;
// next, print out the instances
for (int i = 0; i < cData.Tuples(); i++)
{
for (int j = 0; j < nFields; j++)
cout << setiosflags(ios::right) << setw(15) << cData.GetValue(i, j);
cout << endl;
}
return 0;
}

View File

@ -1,8 +1,8 @@
/*
* testlibpq4.cc
* Test of the asynchronous notification interface
*
populate a test database with the following (use testlibpq4.sql):
* testlibpq4.cc
* Test of the asynchronous notification interface
*
populate a test database with the following (use testlibpq4.sql):
CREATE TABLE TBL1 (i int4);
@ -10,50 +10,54 @@ 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
* Then start up this program
* After the program has begun, do
INSERT INTO TBL1 values (10);
*
*
*/
*
*
*/
#include <iostream.h>
#include "libpq++.h"
#include <stdlib.h>
int main()
{
// 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.
const char* dbName = "dbname=template1";
PgDatabase data(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.
const char* dbName = "dbname=template1";
PgDatabase data(dbName);
// 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);
}
// 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);
}
// Listen to a table
if ( !data.ExecCommandOk("LISTEN TBL2") ) {
cerr << "LISTEN command failed" << endl;
exit(1);
}
// Listen to a table
if ( !data.ExecCommandOk("LISTEN TBL2") )
{
cerr << "LISTEN command failed" << endl;
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;
}
}
return 0;
// 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;
}
}
return 0;
}

View File

@ -1,103 +1,108 @@
/*
* testlibpq5.cc
* Test the C++ version of LIBPQ, the POSTGRES frontend library.
* tests the binary cursor interface
*
*
*
populate a database by doing the following (use testlibpq5.sql):
* testlibpq5.cc
* Test the C++ version of LIBPQ, the POSTGRES frontend library.
* tests the binary cursor interface
*
*
*
populate a database by doing the following (use testlibpq5.sql):
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:
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)
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)
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 <iostream.h>
#include "libpq++.h"
#include <stdlib.h>
extern "C" {
extern "C"
{
#include "postgres.h" // for Postgres types
#include "utils/geo_decls.h" // for the POLYGON type
}
int main()
{
// 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. Connect using cursor interface.
const char* dbName = "dbname=template1"; // change this to the name of your test database
PgCursor data(dbName, "mycursor");
// 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. Connect using cursor interface.
const char* dbName = "dbname=template1"; // change this to the name of your test database
PgCursor data(dbName, "mycursor");
// check to see that the backend connection was successfully made
if ( data.ConnectionBad() ) {
cerr << "Connection to database '" << dbName << "' failed." << endl
<< data.ErrorMessage();
exit(1);
}
// check to see that the backend connection was successfully made
if ( data.ConnectionBad() )
{
cerr << "Connection to database '" << dbName << "' failed." << endl
<< data.ErrorMessage();
exit(1);
}
// Declare a binary cursor for all the tuples in database 'test1'
if ( !data.Declare("select * from test1", 1) ) {
cerr << "DECLARE CURSOR command failed" << endl;
exit(1);
}
// Declare a binary cursor for all the tuples in database 'test1'
if ( !data.Declare("select * from test1", 1) )
{
cerr << "DECLARE CURSOR command failed" << endl;
exit(1);
}
// fetch all instances from the current cursor
if ( !data.Fetch() ) {
cerr << "FETCH ALL command didn't return tuples properly" << endl;
exit(1);
}
// Find the field numbers for the columns 'i', 'd', and 'p'
int i_fnum = data.FieldNum("i");
int d_fnum = data.FieldNum("d");
int 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));
}
*/
// fetch all instances from the current cursor
if ( !data.Fetch() )
{
cerr << "FETCH ALL command didn't return tuples properly" << endl;
exit(1);
}
// Print out the information about the extracted tuple
for (int i=0; i < data.Tuples(); i++) {
// we hard-wire this to the 3 fields we know about
int* ival = (int*)data.GetValue(i,i_fnum);
float* dval = (float*)data.GetValue(i,d_fnum);
int plen = data.GetLength(i,p_fnum);
// Find the field numbers for the columns 'i', 'd', and 'p'
int i_fnum = data.FieldNum("i");
int d_fnum = data.FieldNum("d");
int p_fnum = data.FieldNum("p");
// Allocate correct memory space for the Polygon struct and copy
// the extracted data into it.
// plen doesn't include the length field so need to increment by VARHDSZ
POLYGON* pval = (POLYGON*) malloc(plen + VARHDRSZ);
pval->size = plen;
memmove((char*)&pval->npts, data.GetValue(i,p_fnum), plen);
// Display Polygon Information
cout << "tuple " << i << ": got" << endl
<< " i = (" << data.GetLength(i,i_fnum) << " bytes) " << *ival << "," << endl
<< " d = (" << data.GetLength(i,d_fnum) << " bytes) " << *dval << "," << endl
<< " p = (" << data.GetLength(i,d_fnum) << " bytes) " << pval->npts << " points"
<< "\tboundbox = (hi=" << pval->boundbox.high.x << "/" << pval->boundbox.high.y << ","
<< "lo = " << pval->boundbox.low.x << "," << pval->boundbox.low.y << ")" << endl;
// Deallocate memory allocated for the Polygon structure
free(pval);
}
return 0;
/*
for (i=0;i<3;i++) {
printf("type[%d] = %d, size[%d] = %d\n",
i, data.FieldType(i),
i, data.FieldSize(i));
}
*/
// Print out the information about the extracted tuple
for (int i = 0; i < data.Tuples(); i++)
{
// we hard-wire this to the 3 fields we know about
int* ival = (int*)data.GetValue(i, i_fnum);
float* dval = (float*)data.GetValue(i, d_fnum);
int plen = data.GetLength(i, p_fnum);
// Allocate correct memory space for the Polygon struct and copy
// the extracted data into it.
// plen doesn't include the length field so need to increment by VARHDSZ
POLYGON* pval = (POLYGON*) malloc(plen + VARHDRSZ);
pval->size = plen;
memmove((char*)&pval->npts, data.GetValue(i, p_fnum), plen);
// Display Polygon Information
cout << "tuple " << i << ": got" << endl
<< " i = (" << data.GetLength(i, i_fnum) << " bytes) " << *ival << "," << endl
<< " d = (" << data.GetLength(i, d_fnum) << " bytes) " << *dval << "," << endl
<< " p = (" << data.GetLength(i, d_fnum) << " bytes) " << pval->npts << " points"
<< "\tboundbox = (hi=" << pval->boundbox.high.x << "/" << pval->boundbox.high.y << ","
<< "lo = " << pval->boundbox.low.x << "," << pval->boundbox.low.y << ")" << endl;
// Deallocate memory allocated for the Polygon structure
free(pval);
}
return 0;
}

View File

@ -1,60 +1,68 @@
/*
* testlibpq4.cc
* Test the C++ version of LIBPQ, the POSTGRES frontend library.
* tests the copy in features
*
*/
* testlibpq4.cc
* Test the C++ version of LIBPQ, the POSTGRES frontend library.
* tests the copy in features
*
*/
#include <iostream.h>
#include "libpq++.h"
#include <stdlib.h>
int main()
{
// 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. Connect using transaction interface.
const char* dbName = "dbname=template1";
PgTransaction data(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. Connect using transaction interface.
const char* dbName = "dbname=template1";
PgTransaction data(dbName);
// check to see that the backend connection was successfully made
if ( data.ConnectionBad() ) {
cerr << "Connection to database '" << dbName << "' failed." << endl
<< data.ErrorMessage();
exit(1);
}
else cout << "Connected to database '" << dbName << "'..." << endl;
// check to see that the backend connection was successfully made
if ( data.ConnectionBad() )
{
cerr << "Connection to database '" << dbName << "' failed." << endl
<< data.ErrorMessage();
exit(1);
}
else
cout << "Connected to database '" << dbName << "'..." << endl;
// Create a new table
if ( !data.ExecCommandOk("CREATE TABLE foo (a int4, b char(16), d float8)") ) {
cerr << "CREATE TABLE foo command failed" << endl;
exit(1);
}
else cout << "CREATEd TABLE foo successfully.." << endl;
// Create a new table
if ( !data.ExecCommandOk("CREATE TABLE foo (a int4, b char(16), d float8)") )
{
cerr << "CREATE TABLE foo command failed" << endl;
exit(1);
}
else
cout << "CREATEd TABLE foo successfully.." << endl;
// Initiate Copy command
if ( data.ExecCommandOk("COPY foo FROM STDIN") ) {
cerr << "COPY foo FROM STDIN" << endl;
exit(1);
}
else cout << "COPY foo FROM STDIN was successful.." << endl;
// Initiate Copy command
if ( data.ExecCommandOk("COPY foo FROM STDIN") )
{
cerr << "COPY foo FROM STDIN" << endl;
exit(1);
}
else
cout << "COPY foo FROM STDIN was successful.." << endl;
// Put some test data into the table
data.PutLine("3\thello world\t4.5\n");
cout << "Line: \"3\thello world\t4.5\" copied..." << endl;
data.PutLine("4\tgoodbye word\t7.11\n");
cout << "Line: \"4\tgoodbye word\t7.11\" copied..." << endl;
data.PutLine("\\.\n");
cout << "Line: \"\\.\" copied..." << endl;
if ( !data.EndCopy() )
cout << "Ended COPY succesfully..." << endl;
else cerr << "End Copy failed..." << endl;
// Print the data that was inserted into the table
if ( data.ExecTuplesOk("SELECT * FROM foo") )
data.PrintTuples();
else cerr << "SELECT * FROM foo failed..." << endl;
// Drop the test table
data.Exec("DROP TABLE foo");
return 0;
// Put some test data into the table
data.PutLine("3\thello world\t4.5\n");
cout << "Line: \"3\thello world\t4.5\" copied..." << endl;
data.PutLine("4\tgoodbye word\t7.11\n");
cout << "Line: \"4\tgoodbye word\t7.11\" copied..." << endl;
data.PutLine("\\.\n");
cout << "Line: \"\\.\" copied..." << endl;
if ( !data.EndCopy() )
cout << "Ended COPY succesfully..." << endl;
else
cerr << "End Copy failed..." << endl;
// Print the data that was inserted into the table
if ( data.ExecTuplesOk("SELECT * FROM foo") )
data.PrintTuples();
else
cerr << "SELECT * FROM foo failed..." << endl;
// Drop the test table
data.Exec("DROP TABLE foo");
return 0;
}

View File

@ -1,50 +1,52 @@
/*-------------------------------------------------------------------------
*
* 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.8 2000/05/29 21:25:04 momjian Exp $
*
*-------------------------------------------------------------------------
*/
*
* 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.9 2002/07/02 16:32:19 momjian Exp $
*
*-------------------------------------------------------------------------
*/
#include <iostream.h>
#include "libpq++.h"
#include <stdlib.h>
int main(int argc, char **argv)
{
// Check if the program was invoked correctly; if not, signal error
if (argc < 4 || argc > 5) {
cerr << "Usage: " << argv[0] << " conninfo_str in_filename out_filename [oid]" << endl;
exit(1);
}
// Check if the program was invoked correctly; if not, signal error
if (argc < 4 || argc > 5)
{
cerr << "Usage: " << argv[0] << " conninfo_str in_filename out_filename [oid]" << endl;
exit(1);
}
// Get the arguments passed to the program
char* conninfo = argv[1];
char* in_filename = argv[2];
char* out_filename = argv[3];
// Get the arguments passed to the program
char* conninfo = argv[1];
char* in_filename = argv[2];
char* out_filename = argv[3];
// Set up the connection and create a large object
int lobjId = ( argc == 4 ? 0 : atoi(argv[4]) );
PgLargeObject object(lobjId, conninfo);
// Set up the connection and create a large object
int lobjId = ( argc == 4 ? 0 : atoi(argv[4]) );
PgLargeObject object(lobjId, conninfo);
// check to see that the backend connection was successfully made
if ( object.ConnectionBad() ) {
cerr << "Connection with conninfo '" << conninfo << "' failed." << endl
<< object.ErrorMessage();
exit(1);
}
// check to see that the backend connection was successfully made
if ( object.ConnectionBad() )
{
cerr << "Connection with conninfo '" << conninfo << "' failed." << endl
<< object.ErrorMessage();
exit(1);
}
// Test the import and export features of the Large Object interface
object.Exec("BEGIN");
cout << "Importing file \"" << in_filename << "\"..." << endl;
object.Import(in_filename);
cout << "Exporting large object to file \"" << out_filename << "\"..." << endl;
object.Export(out_filename);
object.Exec("END"); // WHY DOES IT CORE DUMP HERE ???
return 0;
// Test the import and export features of the Large Object interface
object.Exec("BEGIN");
cout << "Importing file \"" << in_filename << "\"..." << endl;
object.Import(in_filename);
cout << "Exporting large object to file \"" << out_filename << "\"..." << endl;
object.Export(out_filename);
object.Exec("END"); // WHY DOES IT CORE DUMP HERE ???
return 0;
}

View File

@ -1,25 +1,25 @@
/*-------------------------------------------------------------------------
*
* libpq++.h
*
*
* DESCRIPTION
* C++ client interface to Postgres
* used for building front-end applications
*
* NOTES
* This is intended to be included by client applications.
* It will not work as an inclusion in the libpq++ sources, since
* in the build environment the individual include files are not
* yet installed in a subdirectory.
*
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: libpq++.h,v 1.12 2002/06/20 20:29:54 momjian Exp $
*
*-------------------------------------------------------------------------
*/
*
* libpq++.h
*
*
* DESCRIPTION
* C++ client interface to Postgres
* used for building front-end applications
*
* NOTES
* This is intended to be included by client applications.
* It will not work as an inclusion in the libpq++ sources, since
* in the build environment the individual include files are not
* yet installed in a subdirectory.
*
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: libpq++.h,v 1.13 2002/07/02 16:32:19 momjian Exp $
*
*-------------------------------------------------------------------------
*/
#ifndef LIBPQXX_H

View File

@ -1,19 +1,19 @@
/*-------------------------------------------------------------------------
*
* FILE
* pgconnection.cc
*
* DESCRIPTION
* implementation of the PgConnection class.
* PgConnection encapsulates a frontend to backend connection
*
* Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/libpq++/Attic/pgconnection.cc,v 1.14 2002/06/15 18:49:29 momjian Exp $
*
*-------------------------------------------------------------------------
*/
*
* FILE
* pgconnection.cc
*
* DESCRIPTION
* implementation of the PgConnection class.
* PgConnection encapsulates a frontend to backend connection
*
* Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/libpq++/Attic/pgconnection.cc,v 1.15 2002/07/02 16:32:19 momjian Exp $
*
*-------------------------------------------------------------------------
*/
#include "pgconnection.h"
@ -28,44 +28,46 @@ using namespace std;
// ****************************************************************
// default constructor -- initialize everything
PgConnection::PgConnection()
: pgConn(NULL), pgResult(NULL), pgCloseConnection(false)
: pgConn(NULL), pgResult(NULL), pgCloseConnection(false)
{}
// constructor -- checks environment variable for database name
// Now uses PQconnectdb
PgConnection::PgConnection(const char* conninfo)
: pgConn(NULL), pgResult(NULL), pgCloseConnection(true)
: pgConn(NULL), pgResult(NULL), pgCloseConnection(true)
{
// Connect to the database
Connect(conninfo);
// Connect to the database
Connect(conninfo);
}
// destructor - closes down the connection and cleanup
PgConnection::~PgConnection()
{
// Close the connection only if needed
// This feature will most probably be used by the derived classes that
// need not close the connection after they are destructed.
CloseConnection();
// Close the connection only if needed
// This feature will most probably be used by the derived classes that
// need not close the connection after they are destructed.
CloseConnection();
}
// PgConnection::CloseConnection()
// close down the connection if there is one
void PgConnection::CloseConnection()
void PgConnection::CloseConnection()
{
// if the connection is open, close it first
if (pgCloseConnection) {
if (pgResult)
PQclear(pgResult);
pgResult = NULL;
if (pgConn)
PQfinish(pgConn);
pgConn = NULL;
pgCloseConnection = false;
}
// if the connection is open, close it first
if (pgCloseConnection)
{
if (pgResult)
PQclear(pgResult);
pgResult = NULL;
if (pgConn)
PQfinish(pgConn);
pgConn = NULL;
pgCloseConnection = false;
}
}
@ -73,40 +75,40 @@ void PgConnection::CloseConnection()
// establish a connection to a backend
ConnStatusType PgConnection::Connect(const char conninfo[])
{
// if the connection is open, close it first
CloseConnection();
// if the connection is open, close it first
CloseConnection();
// Connect to the database
pgConn = PQconnectdb(conninfo);
// Connect to the database
pgConn = PQconnectdb(conninfo);
// Now we have a connection we must close (even if it's bad!)
pgCloseConnection = true;
// Status will return either CONNECTION_OK or CONNECTION_BAD
return Status();
// Now we have a connection we must close (even if it's bad!)
pgCloseConnection = true;
// Status will return either CONNECTION_OK or CONNECTION_BAD
return Status();
}
// PgConnection::status -- return connection or result status
ConnStatusType PgConnection::Status() const
{
return PQstatus(pgConn);
return PQstatus(pgConn);
}
// PgConnection::exec -- send a query to the backend
ExecStatusType PgConnection::Exec(const char* query)
{
// Clear the result stucture if needed
if (pgResult)
PQclear(pgResult);
// Clear the result stucture if needed
if (pgResult)
PQclear(pgResult);
// Execute the given query
pgResult = PQexec(pgConn, query);
// Return the status
if (pgResult)
return PQresultStatus(pgResult);
else
return PGRES_FATAL_ERROR;
// Execute the given query
pgResult = PQexec(pgConn, query);
// Return the status
if (pgResult)
return PQresultStatus(pgResult);
else
return PGRES_FATAL_ERROR;
}
// Return true if the Postgres command was executed OK
@ -125,34 +127,34 @@ int PgConnection::ExecTuplesOk(const char* query)
// PgConnection::notifies() -- returns a notification from a list of unhandled notifications
PGnotify* PgConnection::Notifies()
{
return PQnotifies(pgConn);
return PQnotifies(pgConn);
}
// From Integer To String Conversion Function
string PgConnection::IntToString(int n)
{
char buffer [4*sizeof(n) + 2];
sprintf(buffer, "%d", n);
return buffer;
char buffer [4*sizeof(n) + 2];
sprintf(buffer, "%d", n);
return buffer;
}
bool PgConnection::ConnectionBad() const
{
return Status() == CONNECTION_BAD;
{
return Status() == CONNECTION_BAD;
}
const char* PgConnection::ErrorMessage() const
{
return (const char *)PQerrorMessage(pgConn);
{
return (const char *)PQerrorMessage(pgConn);
}
const char* PgConnection::DBName() const
{
return (const char *)PQdb(pgConn);
{
return (const char *)PQdb(pgConn);
}
PQnoticeProcessor PgConnection::SetNoticeProcessor(PQnoticeProcessor proc, void *arg)
{
return PQsetNoticeProcessor(pgConn, proc, arg);
return PQsetNoticeProcessor(pgConn, proc, arg);
}

View File

@ -1,27 +1,28 @@
/*-------------------------------------------------------------------------
*
* pgconnection.h
*
*
* DESCRIPTION
* Postgres Connection Class:
* Manage Postgres backend connection
*
* NOTES
* Currently under construction.
*
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: pgconnection.h,v 1.18 2002/06/20 20:29:54 momjian Exp $
*
*-------------------------------------------------------------------------
*/
*
* pgconnection.h
*
*
* DESCRIPTION
* Postgres Connection Class:
* Manage Postgres backend connection
*
* NOTES
* Currently under construction.
*
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: pgconnection.h,v 1.19 2002/07/02 16:32:19 momjian Exp $
*
*-------------------------------------------------------------------------
*/
#ifndef PGCONNECTION_H
#define PGCONNECTION_H
extern "C" {
extern "C"
{
#include "pg_config.h"
}
@ -37,7 +38,8 @@ extern "C" {
#include <string>
#endif
extern "C" {
extern "C"
{
#include "libpq-fe.h"
}
@ -54,47 +56,48 @@ extern "C" {
//
// ****************************************************************
// This class contains all the information about the connection
// to the backend process. All the database classes should be
// to the backend process. All the database classes should be
// derived from this class to obtain the connection interface.
class DLLIMPORT PgConnection {
class DLLIMPORT PgConnection
{
protected:
PGconn* pgConn; // Connection Structure
PGresult* pgResult; // Current Query Result
bool pgCloseConnection; // true if connection should be closed by destructor
public:
explicit PgConnection(const char* conninfo); // use reasonable & environment defaults
virtual ~PgConnection(); // close connection and clean up
// Connection status and error messages
ConnStatusType Status() const;
bool ConnectionBad() const;
const char* ErrorMessage() const;
// returns the database name of the connection
const char* DBName() const;
PGconn* pgConn; // Connection Structure
PGresult* pgResult; // Current Query Result
bool pgCloseConnection; // true if connection should be closed by destructor
public:
explicit PgConnection(const char* conninfo); // use reasonable & environment defaults
virtual ~PgConnection(); // close connection and clean up
// Connection status and error messages
ConnStatusType Status() const;
bool ConnectionBad() const;
const char* ErrorMessage() const;
// returns the database name of the connection
const char* DBName() const;
// Query Execution interface
ExecStatusType Exec(const char* query); // send a query to the backend
int ExecCommandOk(const char* query); // send a command and check if it's OK
int ExecTuplesOk(const char* query); // send a command and check if tuples are returned
PGnotify* Notifies();
// Query Execution interface
ExecStatusType Exec(const char* query); // send a query to the backend
int ExecCommandOk(const char* query); // send a command and check if it's OK
int ExecTuplesOk(const char* query); // send a command and check if tuples are returned
PGnotify* Notifies();
// set the notice processor
PQnoticeProcessor SetNoticeProcessor(PQnoticeProcessor proc, void *arg);
protected:
ConnStatusType Connect(const char* conninfo);
void CloseConnection();
static PGSTD string IntToString(int);
// Default constructor is only available to subclasses
PgConnection();
ConnStatusType Connect(const char* conninfo);
void CloseConnection();
static PGSTD string IntToString(int);
// Default constructor is only available to subclasses
PgConnection();
private:
// We don't support copying of PgConnection objects,
// so make copy constructor and assignment op private.
PgConnection(const PgConnection&);
PgConnection& operator= (const PgConnection&);
// We don't support copying of PgConnection objects,
// so make copy constructor and assignment op private.
PgConnection(const PgConnection&);
PgConnection& operator= (const PgConnection&);
};

View File

@ -1,22 +1,22 @@
/*-------------------------------------------------------------------------
*
* FILE
* pgcursordb.cpp
*
* DESCRIPTION
* implementation of the PgCursor class.
* PgCursor encapsulates a cursor interface to the backend
*
* Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/libpq++/Attic/pgcursordb.cc,v 1.7 2002/06/15 18:49:29 momjian Exp $
*
*-------------------------------------------------------------------------
*/
*
* FILE
* pgcursordb.cpp
*
* DESCRIPTION
* implementation of the PgCursor class.
* PgCursor encapsulates a cursor interface to the backend
*
* Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/libpq++/Attic/pgcursordb.cc,v 1.8 2002/07/02 16:32:19 momjian Exp $
*
*-------------------------------------------------------------------------
*/
#include "pgcursordb.h"
#ifdef HAVE_NAMESPACE_STD
using namespace std;
#endif
@ -30,17 +30,18 @@ using namespace std;
// Make a connection to the specified database with default environment
// See PQconnectdb() for conninfo usage
PgCursor::PgCursor(const char* conninfo, const char* cursor)
: PgTransaction(conninfo), pgCursor(cursor)
: PgTransaction(conninfo), pgCursor(cursor)
{}
// Do not make a connection to the backend -- just query
// Connection should not be closed after the object destructs since some
// other object is using the connection
//PgCursor::PgCursor(const PgConnection& conn, const char* cursor)
// : PgTransaction(conn), pgCursor(cursor)
// : PgTransaction(conn), pgCursor(cursor)
//{}
// Destructor: End the transaction block
PgCursor::~PgCursor()
{
Close();
@ -57,7 +58,7 @@ int PgCursor::Declare(string query, bool binary)
{
string cmd = "DECLARE " + pgCursor;
if ( binary )
cmd += " BINARY";
cmd += " BINARY";
cmd += " CURSOR FOR " + query;
return ExecCommandOk( cmd.c_str() );
}

View File

@ -1,24 +1,24 @@
/*-------------------------------------------------------------------------
*
* pgcursordb.h
*
*
* DESCRIPTION
* Postgres Cursor Database Class:
* Query Postgres backend using a cursor
*
* NOTES
* Currently under construction.
*
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* $Id: pgcursordb.h,v 1.11 2002/06/20 20:29:54 momjian Exp $
*
*-------------------------------------------------------------------------
*/
*
* pgcursordb.h
*
*
* DESCRIPTION
* Postgres Cursor Database Class:
* Query Postgres backend using a cursor
*
* NOTES
* Currently under construction.
*
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* $Id: pgcursordb.h,v 1.12 2002/07/02 16:32:19 momjian Exp $
*
*-------------------------------------------------------------------------
*/
#ifndef PGCURSORDB_H
#define PGCURSORDB_H
@ -41,40 +41,50 @@
// manipulates data through it. The interface will introduce some
// ease of use through the methods that will allow cursor specific
// operations, like fetch, forward, etc.
class DLLIMPORT PgCursor : public PgTransaction {
class DLLIMPORT PgCursor : public PgTransaction
{
public:
PgCursor(const char* conninfo, const char* cursor); // use reasonable & environment defaults
// connect to the database with given environment and database name
// PgCursor(const PgConnection&, const char* cursor);
~PgCursor(); // close connection and clean up
// Commands associated with cursor interface
int Declare(PGSTD string query, bool binary = false); // Declare a cursor with given name
int Fetch(const char* dir = "FORWARD"); // Fetch ALL tuples in given direction
int Fetch(unsigned num, const char* dir = "FORWARD"); // Fetch specified amount of tuples
int Close(); // Close the cursor
// Accessors to the cursor name
const char* Cursor() const { return pgCursor.c_str(); }
// TODO: Setter has same name as getter--ouch!
// OBSOLESCENT
void Cursor(PGSTD string cursor) { pgCursor = cursor; }
PgCursor(const char* conninfo, const char* cursor); // use reasonable & environment defaults
// connect to the database with given environment and database name
// PgCursor(const PgConnection&, const char* cursor);
~PgCursor(); // close connection and clean up
// Commands associated with cursor interface
int Declare(PGSTD string query, bool binary = false); // Declare a cursor with given name
int Fetch(const char* dir = "FORWARD"); // Fetch ALL tuples in given direction
int Fetch(unsigned num, const char* dir = "FORWARD"); // Fetch specified amount of tuples
int Close(); // Close the cursor
// Accessors to the cursor name
const char* Cursor() const
{
return pgCursor.c_str();
}
// TODO: Setter has same name as getter--ouch!
// OBSOLESCENT
void Cursor(PGSTD string cursor)
{
pgCursor = cursor;
}
protected:
int Fetch(PGSTD string num, PGSTD string dir);
int Fetch(PGSTD string num, PGSTD string dir);
protected:
PGSTD string pgCursor;
PGSTD string pgCursor;
protected:
PgCursor() : PgTransaction() {} // Do not connect
PgCursor() : PgTransaction()
{} // Do not connect
private:
// We don't support copying of PgCursor objects,
// so make copy constructor and assignment op private.
PgCursor(const PgCursor&);
PgCursor& operator= (const PgCursor&);
}; // End PgCursor Class Declaration
// We don't support copying of PgCursor objects,
// so make copy constructor and assignment op private.
PgCursor(const PgCursor&);
PgCursor& operator= (const PgCursor&);
}
; // End PgCursor Class Declaration
#undef PGSTD

View File

@ -1,20 +1,20 @@
/*-------------------------------------------------------------------------
*
* FILE
* pgdatabase.cpp
*
* DESCRIPTION
* implementation of the PgDatabase class.
* PgDatabase encapsulates some utility routines
*
* Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/libpq++/Attic/pgdatabase.cc,v 1.12 2001/09/30 22:30:37 tgl Exp $
*
*-------------------------------------------------------------------------
*/
*
* FILE
* pgdatabase.cpp
*
* DESCRIPTION
* implementation of the PgDatabase class.
* PgDatabase encapsulates some utility routines
*
* Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/libpq++/Attic/pgdatabase.cc,v 1.13 2002/07/02 16:32:19 momjian Exp $
*
*-------------------------------------------------------------------------
*/
#include "pgdatabase.h"
#ifdef HAVE_NAMESPACE_STD
@ -23,11 +23,11 @@ using namespace std;
// OBSOLESCENT (uses PQprint(), which is no longer being maintained)
void PgDatabase::DisplayTuples(FILE *out,
bool fillAlign,
const char* fieldSep,
bool printHeader,
bool /* quiet */) const
void PgDatabase::DisplayTuples(FILE *out,
bool fillAlign,
const char* fieldSep,
bool printHeader,
bool /* quiet */) const
{
PQprintOpt po;
@ -38,16 +38,16 @@ void PgDatabase::DisplayTuples(FILE *out,
po.tableOpt = po.caption = 0;
po.fieldName = 0;
PQprint(out,pgResult,&po);
PQprint(out, pgResult, &po);
}
// OBSOLESCENT (uses PQprint(), which is no longer being maintained)
void PgDatabase::PrintTuples(FILE *out,
bool printAttName,
bool terseOutput,
bool fillAlign) const
void PgDatabase::PrintTuples(FILE *out,
bool printAttName,
bool terseOutput,
bool fillAlign) const
{
PQprintOpt po;
@ -58,124 +58,124 @@ void PgDatabase::PrintTuples(FILE *out,
po.fieldSep = (char *) (terseOutput ? "" : "|");
po.fieldName = 0;
PQprint(out,pgResult,&po);
PQprint(out, pgResult, &po);
}
int PgDatabase::Tuples() const
{
return PQntuples(pgResult);
{
return PQntuples(pgResult);
}
int PgDatabase::CmdTuples() const
{
const char *a = PQcmdTuples(pgResult);
return a[0] ? atoi(a) : -1;
const char *a = PQcmdTuples(pgResult);
return a[0] ? atoi(a) : -1;
}
// TODO: Make const?
int PgDatabase::Fields()
{
return PQnfields(pgResult);
{
return PQnfields(pgResult);
}
const char* PgDatabase::FieldName(int field_num) const
{
return PQfname(pgResult, field_num);
{
return PQfname(pgResult, field_num);
}
int PgDatabase::FieldNum(const char* field_name) const
{
return PQfnumber(pgResult, field_name);
{
return PQfnumber(pgResult, field_name);
}
Oid PgDatabase::FieldType(int field_num) const
{
return PQftype(pgResult, field_num);
{
return PQftype(pgResult, field_num);
}
Oid PgDatabase::FieldType(const char* field_name) const
{
return PQftype(pgResult, FieldNum(field_name));
{
return PQftype(pgResult, FieldNum(field_name));
}
int PgDatabase::FieldSize(int field_num) const
{
return PQfsize(pgResult, field_num);
{
return PQfsize(pgResult, field_num);
}
int PgDatabase::FieldSize(const char* field_name) const
{
return PQfsize(pgResult, FieldNum(field_name));
{
return PQfsize(pgResult, FieldNum(field_name));
}
const char* PgDatabase::GetValue(int tup_num, int field_num) const
{
return PQgetvalue(pgResult, tup_num, field_num);
{
return PQgetvalue(pgResult, tup_num, field_num);
}
const char* PgDatabase::GetValue(int tup_num, const char* field_name) const
{
return PQgetvalue(pgResult, tup_num, FieldNum(field_name));
{
return PQgetvalue(pgResult, tup_num, FieldNum(field_name));
}
bool PgDatabase::GetIsNull(int tup_num, int field_num) const
{
return PQgetisnull(pgResult, tup_num, field_num);
{
return PQgetisnull(pgResult, tup_num, field_num);
}
bool PgDatabase::GetIsNull(int tup_num, const char* field_name) const
{
return PQgetisnull(pgResult, tup_num, FieldNum(field_name));
{
return PQgetisnull(pgResult, tup_num, FieldNum(field_name));
}
int PgDatabase::GetLength(int tup_num, int field_num) const
{
return PQgetlength(pgResult, tup_num, field_num);
{
return PQgetlength(pgResult, tup_num, field_num);
}
int PgDatabase::GetLength(int tup_num, const char* field_name) const
{
return PQgetlength(pgResult, tup_num, FieldNum(field_name));
{
return PQgetlength(pgResult, tup_num, FieldNum(field_name));
}
int PgDatabase::GetLine(char str[], int length)
{
return PQgetline(pgConn, str, length);
{
return PQgetline(pgConn, str, length);
}
void PgDatabase::PutLine(const char str[])
{
PQputline(pgConn, str);
{
PQputline(pgConn, str);
}
const char* PgDatabase::OidStatus() const
{
return PQoidStatus(pgResult);
{
return PQoidStatus(pgResult);
}
int PgDatabase::EndCopy()
{
return PQendcopy(pgConn);
{
return PQendcopy(pgConn);
}

View File

@ -1,27 +1,27 @@
/*-------------------------------------------------------------------------
*
* pgdatabase.h
*
*
* DESCRIPTION
* Postgres Database Class:
* Query Postgres backend to obtain query results
*
* NOTES
* Currently under construction.
*
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* $Id: pgdatabase.h,v 1.13 2002/06/20 20:29:54 momjian Exp $
*
*-------------------------------------------------------------------------
*/
*
* pgdatabase.h
*
*
* DESCRIPTION
* Postgres Database Class:
* Query Postgres backend to obtain query results
*
* NOTES
* Currently under construction.
*
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* $Id: pgdatabase.h,v 1.14 2002/07/02 16:32:19 momjian Exp $
*
*-------------------------------------------------------------------------
*/
#ifndef PGDATABASE_H
#define PGDATABASE_H
#ifndef PGCONNECTION_H
#include "pgconnection.h"
#endif
@ -31,55 +31,61 @@
// PgDatabase - a class for accessing databases
//
// ****************************************************************
// This is the basic database access class. Its interface should
// This is the basic database access class. Its interface should
// be used only after a query has been sent to the backend and
// results are being received.
class DLLIMPORT PgDatabase : public PgConnection {
class DLLIMPORT PgDatabase : public PgConnection
{
public:
// connect to the database with conninfo
explicit PgDatabase(const char* conninfo) : PgConnection(conninfo) {}
// connect to the database with conninfo
explicit PgDatabase(const char* conninfo) : PgConnection(conninfo)
{}
~PgDatabase() {} // close connection and clean up
~PgDatabase()
{} // close connection and clean up
typedef int size_type;
// query result access
size_type Tuples() const;
size_type CmdTuples() const;
int Fields();
const char* FieldName(int field_num) const;
int FieldNum(const char* field_name) const;
Oid FieldType(int field_num) const;
Oid FieldType(const char* field_name) const;
int FieldSize(int field_num) const;
int FieldSize(const char* field_name) const;
const char* GetValue(size_type tup_num, int field_num) const;
const char* GetValue(size_type tup_num, const char* field_name) const;
bool GetIsNull(size_type tup_num, int field_num) const;
bool GetIsNull(size_type tup_num, const char* field_name) const;
int GetLength(size_type tup_num, int field_num) const;
int GetLength(size_type tup_num, const char* field_name) const;
// OBSOLESCENT (use PQprint()):
void DisplayTuples(FILE *out=0, bool fillAlign=true,
const char* fieldSep="|", bool printHeader=true, bool quiet=false) const;
void PrintTuples(FILE *out=0, bool printAttName=true,
bool terseOutput=false, bool fillAlign=false) const;
typedef int size_type;
// query result access
size_type Tuples() const;
size_type CmdTuples() const;
int Fields();
const char* FieldName(int field_num) const;
int FieldNum(const char* field_name) const;
Oid FieldType(int field_num) const;
Oid FieldType(const char* field_name) const;
int FieldSize(int field_num) const;
int FieldSize(const char* field_name) const;
const char* GetValue(size_type tup_num, int field_num) const;
const char* GetValue(size_type tup_num, const char* field_name) const;
bool GetIsNull(size_type tup_num, int field_num) const;
bool GetIsNull(size_type tup_num, const char* field_name) const;
int GetLength(size_type tup_num, int field_num) const;
int GetLength(size_type tup_num, const char* field_name) const;
// OBSOLESCENT (use PQprint()):
void DisplayTuples(FILE *out = 0, bool fillAlign = true,
const char* fieldSep = "|", bool printHeader = true, bool quiet = false) const;
void PrintTuples(FILE *out = 0, bool printAttName = true,
bool terseOutput = false, bool fillAlign = false) const;
// copy command related access
int GetLine(char str[], int length);
void PutLine(const char str[]);
const char* OidStatus() const;
int EndCopy();
// copy command related access
int GetLine(char str[], int length);
void PutLine(const char str[]);
const char* OidStatus() const;
int EndCopy();
protected:
PgDatabase() : PgConnection() {} // Do not connect
PgDatabase() : PgConnection()
{} // Do not connect
private:
// We don't support copying of PgDatabase objects,
// so make copy constructor and assignment op private.
PgDatabase(const PgDatabase&);
PgDatabase& operator= (const PgDatabase&);
// We don't support copying of PgDatabase objects,
// so make copy constructor and assignment op private.
PgDatabase(const PgDatabase&);
PgDatabase& operator= (const PgDatabase&);
};
#endif // PGDATABASE_H

View File

@ -1,23 +1,24 @@
/*-------------------------------------------------------------------------
*
* FILE
* pglobject.cc
*
* DESCRIPTION
* implementation of the PgLargeObject class.
* PgLargeObject encapsulates a frontend to backend connection
*
* Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/libpq++/Attic/pglobject.cc,v 1.10 2002/06/15 19:30:40 momjian Exp $
*
*-------------------------------------------------------------------------
*/
*
* FILE
* pglobject.cc
*
* DESCRIPTION
* implementation of the PgLargeObject class.
* PgLargeObject encapsulates a frontend to backend connection
*
* Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/libpq++/Attic/pglobject.cc,v 1.11 2002/07/02 16:32:19 momjian Exp $
*
*-------------------------------------------------------------------------
*/
#include "pglobject.h"
extern "C" {
extern "C"
{
#include "libpq/libpq-fs.h"
}
@ -35,140 +36,145 @@ using namespace std;
// creates a large object in the default database
// See PQconnectdb() for conninfo usage
PgLargeObject::PgLargeObject(const char* conninfo)
: PgConnection(conninfo)
: PgConnection(conninfo)
{
Init();
if (! ConnectionBad()) {
Create();
Open();
}
Init();
if (! ConnectionBad())
{
Create();
Open();
}
}
// constructor
// open an existing large object in the default database
// See PQconnectdb() for conninfo usage
PgLargeObject::PgLargeObject(Oid lobjId, const char* conninfo)
: PgConnection(conninfo)
PgLargeObject::PgLargeObject(Oid lobjId, const char* conninfo)
: PgConnection(conninfo)
{
Init(lobjId);
if (! ConnectionBad()) {
if ( !pgObject )
Create();
Open();
}
Init(lobjId);
if (! ConnectionBad())
{
if ( !pgObject )
Create();
Open();
}
}
// destructor -- closes large object
PgLargeObject::~PgLargeObject()
{
Close();
Close();
}
// PgLargeObject::Init
// Initialize the variables
void PgLargeObject::Init(Oid lobjId)
{
pgFd = -1;
pgObject = lobjId;
pgFd = -1;
pgObject = lobjId;
}
// PgLargeObject::create
// create large object and check for errors
void PgLargeObject::Create()
{
// Create the object
pgObject = lo_creat(pgConn, INV_READ|INV_WRITE);
// Check for possible errors
if (!pgObject)
loStatus = "PgLargeObject: can't create large object" ;
else
loStatus = "PgLargeObject: created large object" ;
// Create the object
pgObject = lo_creat(pgConn, INV_READ | INV_WRITE);
// Check for possible errors
if (!pgObject)
loStatus = "PgLargeObject: can't create large object" ;
else
loStatus = "PgLargeObject: created large object" ;
}
// PgLargeObject::open
// open large object and check for errors
void PgLargeObject::Open()
{
// Close any prior object
Close();
// Open the object
pgFd = lo_open(pgConn, pgObject, INV_READ|INV_WRITE);
// Check for possible errors
string objStr( IntToString(pgObject) );
if (pgFd < 0)
loStatus = "PgLargeObject: can't open large object " + objStr ;
else
loStatus = "PgLargeObject: created and opened large object " + objStr ;
// Close any prior object
Close();
// Open the object
pgFd = lo_open(pgConn, pgObject, INV_READ | INV_WRITE);
// Check for possible errors
string objStr( IntToString(pgObject) );
if (pgFd < 0)
loStatus = "PgLargeObject: can't open large object " + objStr ;
else
loStatus = "PgLargeObject: created and opened large object " + objStr ;
}
// PgLargeObject::unlink
// destroy large object and delete from it from the database
int PgLargeObject::Unlink()
{
// Unlink the object
int temp = lo_unlink(pgConn, pgObject);
// Initialize the large object upon success
if (!temp) {
Close();
Init();
}
// Return the status
return temp;
// Unlink the object
int temp = lo_unlink(pgConn, pgObject);
// Initialize the large object upon success
if (!temp)
{
Close();
Init();
}
// Return the status
return temp;
}
void PgLargeObject::Close()
{
if (pgFd >= 0) lo_close(pgConn, pgFd);
pgFd = -1;
{
if (pgFd >= 0)
lo_close(pgConn, pgFd);
pgFd = -1;
}
int PgLargeObject::Read(char* buf, int len)
{
return lo_read(pgConn, pgFd, buf, len);
{
return lo_read(pgConn, pgFd, buf, len);
}
int PgLargeObject::Write(const char* buf, int len)
{
return lo_write(pgConn, pgFd, (char*)buf, len);
{
return lo_write(pgConn, pgFd, (char*)buf, len);
}
int PgLargeObject::LSeek(int offset, int whence)
{
return lo_lseek(pgConn, pgFd, offset, whence);
{
return lo_lseek(pgConn, pgFd, offset, whence);
}
int PgLargeObject::Tell() const
{
return lo_tell(pgConn, pgFd);
{
return lo_tell(pgConn, pgFd);
}
Oid PgLargeObject::Import(const char* filename)
{
return pgObject = lo_import(pgConn, filename);
Oid PgLargeObject::Import(const char* filename)
{
return pgObject = lo_import(pgConn, filename);
}
int PgLargeObject::Export(const char* filename)
{
return lo_export(pgConn, pgObject, filename);
int PgLargeObject::Export(const char* filename)
{
return lo_export(pgConn, pgObject, filename);
}
string PgLargeObject::Status() const
{
return loStatus;
{
return loStatus;
}
Oid PgLargeObject::LOid(){
return pgObject;
Oid PgLargeObject::LOid()
{
return pgObject;
}

View File

@ -1,24 +1,24 @@
/*-------------------------------------------------------------------------
*
* FILE
* pglobject.h
*
* DESCRIPTION
* declaration of the PGlobj class.
* PGlobj encapsulates a large object interface to Postgres backend
*
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* $Id: pglobject.h,v 1.10 2002/06/20 20:29:54 momjian Exp $
*
*-------------------------------------------------------------------------
*/
*
* FILE
* pglobject.h
*
* DESCRIPTION
* declaration of the PGlobj class.
* PGlobj encapsulates a large object interface to Postgres backend
*
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* $Id: pglobject.h,v 1.11 2002/07/02 16:32:19 momjian Exp $
*
*-------------------------------------------------------------------------
*/
#ifndef PGLOBJECT_H
#define PGLOBJECT_H
#ifndef PGCONNECTION_H
#include "pgconnection.h"
#endif
@ -35,36 +35,37 @@
// PgLargeObject - a class for accessing Large Object in a database
//
// ****************************************************************
class DLLIMPORT PgLargeObject : public PgConnection {
class DLLIMPORT PgLargeObject : public PgConnection
{
private:
int pgFd;
Oid pgObject;
PGSTD string loStatus;
void Init(Oid lobjId = 0);
int pgFd;
Oid pgObject;
PGSTD string loStatus;
void Init(Oid lobjId = 0);
public:
explicit PgLargeObject(const char* conninfo = 0); // use reasonable defaults and create large object
explicit PgLargeObject(Oid lobjId, const char* conninfo = 0); // use reasonable defaults and open large object
~PgLargeObject(); // close connection and clean up
void Create();
void Open();
void Close();
int Read(char* buf, int len);
int Write(const char* buf, int len);
int LSeek(int offset, int whence);
int Tell() const;
int Unlink();
Oid LOid();
Oid Import(const char* filename);
int Export(const char* filename);
PGSTD string Status() const;
explicit PgLargeObject(const char* conninfo = 0); // use reasonable defaults and create large object
explicit PgLargeObject(Oid lobjId, const char* conninfo = 0); // use reasonable defaults and open large object
~PgLargeObject(); // close connection and clean up
void Create();
void Open();
void Close();
int Read(char* buf, int len);
int Write(const char* buf, int len);
int LSeek(int offset, int whence);
int Tell() const;
int Unlink();
Oid LOid();
Oid Import(const char* filename);
int Export(const char* filename);
PGSTD string Status() const;
private:
// We don't support copying of PgLargeObject objects,
// so make copy constructor and assignment op private.
PgLargeObject(const PgLargeObject&);
PgLargeObject& operator= (const PgLargeObject&);
// We don't support copying of PgLargeObject objects,
// so make copy constructor and assignment op private.
PgLargeObject(const PgLargeObject&);
PgLargeObject& operator= (const PgLargeObject&);
};

View File

@ -1,20 +1,20 @@
/*-------------------------------------------------------------------------
*
* FILE
* pgtransdb.cpp
*
* DESCRIPTION
* implementation of the PgTransaction class.
* PgConnection encapsulates a transaction querying to backend
*
* Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/libpq++/Attic/pgtransdb.cc,v 1.4 2001/05/09 17:29:10 momjian Exp $
*
*-------------------------------------------------------------------------
*/
*
* FILE
* pgtransdb.cpp
*
* DESCRIPTION
* implementation of the PgTransaction class.
* PgConnection encapsulates a transaction querying to backend
*
* Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/libpq++/Attic/pgtransdb.cc,v 1.5 2002/07/02 16:32:19 momjian Exp $
*
*-------------------------------------------------------------------------
*/
#include "pgtransdb.h"
// ****************************************************************
@ -23,10 +23,10 @@
//
// ****************************************************************
// Make a connection to the specified database with default environment
// See PQconnectdb() for conninfo usage.
// See PQconnectdb() for conninfo usage.
PgTransaction::PgTransaction(const char* conninfo)
: PgDatabase(conninfo),
pgCommitted(true)
: PgDatabase(conninfo),
pgCommitted(true)
{
BeginTransaction();
}
@ -34,7 +34,8 @@ PgTransaction::PgTransaction(const char* conninfo)
// Destructor: End the transaction block
PgTransaction::~PgTransaction()
{
if (!pgCommitted) Exec("ABORT");
if (!pgCommitted)
Exec("ABORT");
}
// Begin the transaction block

View File

@ -1,24 +1,24 @@
/*-------------------------------------------------------------------------
*
* pgtransdb.h
*
*
* DESCRIPTION
* Postgres Transaction Database Class:
* Query Postgres backend using a transaction block
*
* NOTES
* Currently under construction.
*
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* $Id: pgtransdb.h,v 1.9 2002/06/20 20:29:54 momjian Exp $
*
*-------------------------------------------------------------------------
*/
*
* pgtransdb.h
*
*
* DESCRIPTION
* Postgres Transaction Database Class:
* Query Postgres backend using a transaction block
*
* NOTES
* Currently under construction.
*
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* $Id: pgtransdb.h,v 1.10 2002/07/02 16:32:19 momjian Exp $
*
*-------------------------------------------------------------------------
*/
#ifndef PGTRANSDB_H
#define PGTRANSDB_H
@ -34,27 +34,31 @@
// This is the database access class that keeps an open
// transaction block during its lifetime. The block is ENDed when
// the object is destroyed.
class DLLIMPORT PgTransaction : public PgDatabase {
class DLLIMPORT PgTransaction : public PgDatabase
{
public:
explicit PgTransaction(const char* conninfo); // use reasonable & environment defaults
// connect to the database with given environment and database name
// explicit PgTransaction(const PgConnection&);
~PgTransaction(); // close connection and clean up
explicit PgTransaction(const char* conninfo); // use reasonable & environment defaults
// connect to the database with given environment and database name
// explicit PgTransaction(const PgConnection&);
~PgTransaction(); // close connection and clean up
protected:
ExecStatusType BeginTransaction();
ExecStatusType EndTransaction();
ExecStatusType BeginTransaction();
ExecStatusType EndTransaction();
protected:
PgTransaction() : PgDatabase(), pgCommitted(true) {} // Do not connect
PgTransaction() : PgDatabase(), pgCommitted(true)
{} // Do not connect
private:
bool pgCommitted;
bool pgCommitted;
// We don't support copying of PgTransaction objects,
// so make copy constructor and assignment op private.
PgTransaction(const PgTransaction&);
PgTransaction& operator= (const PgTransaction&);
}; // End PgTransaction Class Declaration
// We don't support copying of PgTransaction objects,
// so make copy constructor and assignment op private.
PgTransaction(const PgTransaction&);
PgTransaction& operator= (const PgTransaction&);
}
; // End PgTransaction Class Declaration
#endif // PGTRANSDB_H