+
/*-------------------------------------------------------------------------
*
* libpq++.H
*
* Copyright (c) 1994, Regents of the University of California
*
- * IDENTIFICATION
- *
- * $Id: libpq++.H,v 1.3 1996/11/12 11:42:27 bryanh Exp $
*
*-------------------------------------------------------------------------
*/
#include
#include
+#include
extern "C" {
#include "config.h"
#include "postgres.h"
#include "libpq-fe.h"
-#include "fe-auth.h"
}
+static char rcsid[] = "$Id: libpq++.H,v 1.4 1999/05/23 01:03:58 momjian Exp $";
+
+
// ****************************************************************
//
-// PGenv - the environment for setting up a connection to postgres
+// PgConnection - a connection made to a postgres backend
//
// ****************************************************************
-class PGenv {
- friend class PGconnection;
- char* pgauth;
- char* pghost;
- char* pgport;
- char* pgoption;
- char* pgtty;
+class PgConnection {
+protected:
+ PGconn* pgConn; // Connection Structures
+ PGresult* pgResult; // Query Result
+ int pgCloseConnection; // Flag indicating whether the connection should be closed
+ ConnStatusType Connect(const char* conninfo);
+ string IntToString(int);
+ PgConnection();
+
public:
- PGenv(); // default ctor will use reasonable defaults
- // will use environment variables PGHOST, PGPORT,
- // PGOPTION, PGTTY
- PGenv(char* auth, char* host, char* port, char* option, char* tty);
- void setValues(char* auth, char* host, char* port, char* option, char* tty);
- ~PGenv();
+ PgConnection(const char* conninfo); // use reasonable and environment defaults
+ ~PgConnection(); // close connection and clean up
+
+ ConnStatusType Status();
+ int ConnectionBad();
+ const char* ErrorMessage();
+
+ // returns the database name of the connection
+ const char* DBName();
+
+ ExecStatusType Exec(const char* query); // send a query to the backend
+ int ExecCommandOk(const char* query); // send a command and check if it's
+ int ExecTuplesOk(const char* query); // send a command and check if tuple
+ PGnotify* Notifies();
};
// ****************************************************************
//
-// PGconnection - a connection made to a postgres backend
+// PgDatabase - a class for accessing databases
//
// ****************************************************************
-class PGconnection {
- friend class PGdatabase;
- friend class PGlobj;
- PGenv* env;
- PGconn* conn;
- PGresult* result;
-
- char errorMessage[ERROR_MSG_LENGTH];
+class PgDatabase : public PgConnection {
+protected:
+ PgDatabase() : PgConnection() {} // Do not connect
+
public:
- PGconnection(); // use reasonable defaults
- PGconnection(PGenv* env, char* dbName); // connect to the database with
- // given environment and database name
- ConnStatusType status();
- char* errormessage() {return errorMessage;};
-
- // returns the database name of the connection
- char* dbName() {return PQdb(conn);};
+ // connect to the database with conninfo
+ PgDatabase(const char *conninfo) : PgConnection(conninfo) {};
+ ~PgDatabase() {}; // close connection and clean up
+ // query result access
+ int Tuples();
+ int Fields();
+ const char* FieldName(int field_num);
+ int FieldNum(const char *field_name);
+ Oid FieldType(int field_num);
+ Oid FieldType(const char *field_name);
+ short FieldSize(int field_num);
+ short FieldSize(const char *field_name);
+ const char* GetValue(int tup_num, int field_num);
+ const char* GetValue(int tup_num, const char *field_name);
+ int GetLength(int tup_num, int field_num);
+ int GetLength(int tup_num, const char* field_name);
+ void DisplayTuples(FILE *out = 0, int fillAlign = 1,
+ const char* fieldSep = "|",int printHeader = 1, int quiet = 0) ;
+ void PrintTuples(FILE *out = 0, int printAttName = 1,
+ int terseOutput = 0, int width = 0) ;
- ExecStatusType exec(char* query); // send a query to the backend
- PGnotify* notifies() {exec(" "); return PQnotifies(conn);};
- ~PGconnection(); // close connection and clean up
-protected:
- ConnStatusType connect(PGenv* env, char* dbName);
+ // copy command related access
+ int GetLine(char* string, int length);
+ void PutLine(const char* string);
+ const char *OidStatus();
+ int EndCopy();
};
+
+
// ****************************************************************
//
-// PGdatabase - a class for accessing databases
+// PGLargeObject - a class for accessing Large Object in a database
//
// ****************************************************************
-class PGdatabase : public PGconnection {
+class PgLargeObject : public PgConnection {
public:
- PGdatabase() : PGconnection() {}; // use reasonable defaults
- // connect to the database with
- PGdatabase(PGenv* env, char* dbName) : PGconnection(env, dbName) {};
- // query result access
- int ntuples()
- {return PQntuples(result);};
- int nfields()
- {return PQnfields(result);};
- char* fieldname(int field_num)
- {return PQfname(result, field_num);};
- int fieldnum(char* field_name)
- {return PQfnumber(result, field_name);};
- Oid fieldtype(int field_num)
- {return PQftype(result, field_num);};
- Oid fieldtype(char* field_name)
- {return PQftype(result, fieldnum(field_name));};
- int2 fieldsize(int field_num)
- {return PQfsize(result, field_num);};
- int2 fieldsize(char* field_name)
- {return PQfsize(result, fieldnum(field_name));};
- char* getvalue(int tup_num, int field_num)
- {return PQgetvalue(result, tup_num, field_num);};
- char* getvalue(int tup_num, char* field_name)
- {return PQgetvalue(result, tup_num, fieldnum(field_name));};
- int getlength(int tup_num, int field_num)
- {return PQgetlength(result, tup_num, field_num);};
- int getlength(int tup_num, char* field_name)
- {return PQgetlength(result, tup_num, fieldnum(field_name));};
- void printtuples(FILE *out, int fillAlign, char *fieldSep,
- int printHeader, int quiet)
- {PQdisplayTuples(result, out, fillAlign, fieldSep, printHeader, quiet);};
- // copy command related access
- int getline(char* string, int length)
- {return PQgetline(conn, string, length);};
- void putline(char* string)
- {PQputline(conn, string);};
- const char *OidStatus()
- {
- return PQoidStatus(result);
- }
- int endcopy()
- {return PQendcopy(conn);};
- ~PGdatabase() {}; // close connection and clean up
+ PgLargeObject(const char* conninfo = 0); // use reasonable defaults and create large object
+ 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();
+ int Unlink();
+ Oid LOid();
+ Oid Import(const char* filename);
+ int Export(const char* filename);
+ string Status();
};
+
// ****************************************************************
//
-// PGlobj - a class for accessing Large Object in a database
+// PgTransaction - a class for running transactions against databases
//
// ****************************************************************
-class PGlobj : public PGconnection {
- int fd;
- Oid object;
+class PgTransaction : public PgDatabase {
+protected:
+ ExecStatusType BeginTransaction();
+ ExecStatusType EndTransaction();
+ PgTransaction() : PgDatabase() {} // Do not connect
+
public:
- PGlobj(); // use reasonable defaults and create large object
- PGlobj(Oid lobjId); // use reasonable defaults and open large object
- PGlobj(PGenv* env, char* dbName); // create large object
- PGlobj(PGenv* env, char* dbName, Oid lobjId); // open large object
- int read(char* buf, int len)
- {return lo_read(conn, fd, buf, len);};
- int write(char* buf, int len)
- {return lo_write(conn, fd, buf, len);};
- int lseek(int offset, int whence)
- {return lo_lseek(conn, fd, offset, whence);};
- int tell()
- {return lo_tell(conn, fd);};
- int unlink();
- int import(char* filename);
- int export(char* filename);
- ~PGlobj(); // close connection and clean up
+ PgTransaction(const char* conninfo); // use reasonable & environment defaults
+ // connect to the database with given environment and database name
+ PgTransaction(const PgConnection&);
+ virtual ~PgTransaction(); // close connection and clean up
+
};
+
+// ****************************************************************
//
-// these are the environment variables used for getting defaults
+// PgCursor - a class for querying databases using a cursor
//
+// ****************************************************************
+class PgCursor : public PgTransaction {
+protected:
+ int Fetch(const string& num, const string& dir);
+ string pgCursor;
+ PgCursor() : PgTransaction() {} // Do not connect
+
+public:
+ PgCursor(const char* dbName, const char* cursor); // use reasonable & environment defaults
+ // connect to the database with given environment and database name
+ PgCursor(const PgConnection&, const char* cursor);
+ virtual ~PgCursor(); // close connection and clean up
+
+ // Commands associated with cursor interface
+ int Declare(const string& query, int binary = 0); // 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();
+ void Cursor(const string& cursor);
+};
+
-#define ENV_DEFAULT_AUTH "PGAUTH"
-#define ENV_DEFAULT_DBASE "PGDATABASE"
-#define ENV_DEFAULT_HOST "PGHOST"
-#define ENV_DEFAULT_OPTION "PGOPTION"
-#define ENV_DEFAULT_PORT "PGPORT"
-#define ENV_DEFAULT_TTY "PGTTY"
// buffer size
#define BUFSIZE 1024
#endif /* LIBPQXX_H */
+
+
*
* Copyright (c) 1994, Regents of the University of California
*
- * IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/interfaces/libpq++/Attic/pgconnection.cc,v 1.4 1999/05/16 14:34:59 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#include
#include
-#include
#include "pgconnection.h"
extern "C" {
#include "fe-auth.h"
}
+static char rcsid[] = "$Id: pgconnection.cc,v 1.5 1999/05/23 01:04:00 momjian Exp $";
// ****************************************************************
//
: pgConn(NULL), pgResult(NULL), pgCloseConnection(0)
{}
-// copy constructor -- copy the pointers; no deep copy required
-PgConnection::PgConnection(const PgConnection& conn)
- : pgEnv(conn.pgEnv), pgConn(conn.pgConn), pgResult(conn.pgResult),
- pgCloseConnection(conn.pgCloseConnection)
-{}
// constructor -- checks environment variable for database name
-PgConnection::PgConnection(const char* dbName)
+// Now uses PQconnectdb
+PgConnection::PgConnection(const char* conninfo)
: pgConn(NULL), pgResult(NULL), pgCloseConnection(1)
{
- // Get a default database name to connect to
- char* defDB = (char*)dbName;
- if ( !dbName )
- if ( !(defDB = getenv(ENV_DEFAULT_DBASE)) )
- return;
// Connect to the database
- Connect( defDB );
+ Connect( conninfo );
}
-// constructor -- for given environment and database name
-PgConnection::PgConnection(const PgEnv& env, const char* dbName)
- : pgEnv(env), pgConn(NULL), pgResult(NULL), pgCloseConnection(1)
-{
- Connect( dbName );
-}
// destructor - closes down the connection and cleanup
PgConnection::~PgConnection()
// This feature will most probably be used by the derived classes that
// need not close the connection after they are destructed.
if ( pgCloseConnection ) {
- if (pgResult) PQclear(pgResult);
- if (pgConn) PQfinish(pgConn);
+ if(pgResult) PQclear(pgResult);
+ if(pgConn) PQfinish(pgConn);
}
}
// PgConnection::connect
// establish a connection to a backend
-ConnStatusType PgConnection::Connect(const char* dbName)
+ConnStatusType PgConnection::Connect(const char* conninfo)
{
- // Turn the trace on
- #if defined(DEBUG)
- FILE *debug = fopen("/tmp/trace.out","w");
- PQtrace(pgConn, debug);
- #endif
-
- // Connect to the database
- ostrstream conninfo;
- conninfo << "dbname="<
- conninfo << pgEnv;
- pgConn=PQconnectdb(conninfo.str());
- conninfo.freeze(0);
-
- if(ConnectionBad()) {
- SetErrorMessage( PQerrorMessage(pgConn) );
- }
+ConnStatusType cst;
+ // Turn the trace on
+#if defined(DEBUG)
+ FILE *debug = fopen("/tmp/trace.out","w");
+ PQtrace(pgConn, debug);
+#endif
+
+ // Connect to the database
+ pgConn = PQconnectdb(conninfo);
+
+ // Status will return either CONNECTION_OK or CONNECTION_BAD
+ cst = Status();
+ if(CONNECTION_OK == cst) pgCloseConnection = (ConnStatusType)1;
+ else pgCloseConnection = (ConnStatusType)0;
- return Status();
+return cst;
}
// PgConnection::status -- return connection or result status
ConnStatusType PgConnection::Status()
{
- return PQstatus(pgConn);
+ return PQstatus(pgConn);
}
// PgConnection::exec -- send a query to the backend
// Return the status
if (pgResult)
- return PQresultStatus(pgResult);
- else {
- SetErrorMessage( PQerrorMessage(pgConn) );
- return PGRES_FATAL_ERROR;
- }
+ return PQresultStatus(pgResult);
+ else
+ return PGRES_FATAL_ERROR;
}
// Return true if the Postgres command was executed OK
} // End ExecTuplesOk()
+
+// Don't know why these next two need to be part of Connection
+
// PgConnection::notifies() -- returns a notification from a list of unhandled notifications
PGnotify* PgConnection::Notifies()
{
return PQnotifies(pgConn);
}
-// PgConnection::SetErrorMessage
-// sets error message to the given string
-void PgConnection::SetErrorMessage(const string& msg, int append)
-{
- if ( append )
- pgErrorMessage += msg;
- else
- pgErrorMessage = msg;
-}
// From Integer To String Conversion Function
string PgConnection::IntToString(int n)
sprintf(buffer, "%d", n);
return buffer;
}
+
+
+
+int PgConnection::ConnectionBad()
+{
+return Status() == CONNECTION_BAD;
+}
+
+
+const char* PgConnection::ErrorMessage()
+{
+return (const char *)PQerrorMessage(pgConn);
+}
+
+
+const char* PgConnection::DBName()
+{
+return (const char *)PQdb(pgConn);
+}
+
+
* Currently under construction.
*
* Copyright (c) 1994, Regents of the University of California
+ *
+ * $Id: pgconnection.h,v 1.2 1999/05/23 01:04:00 momjian Exp $
*
*-------------------------------------------------------------------------
*/
#define PGCONN_H
#include
-#include "pgenv.h"
+#include
extern "C" {
#include "libpq-fe.h"
// derived from this class to obtain the connection interface.
class PgConnection {
protected:
- PgEnv pgEnv; // Current connection environment
PGconn* pgConn; // Connection Structures
PGresult* pgResult; // Query Result
- string pgErrorMessage; // Error messages container
int pgCloseConnection; // Flag indicating whether the connection should be closed or not
public:
- PgConnection(const char* dbName); // use reasonable defaults
- PgConnection(const PgEnv& env, const char* dbName); // connect to the database with
- // given environment and database name
- virtual ~PgConnection(); // close connection and clean up
+ PgConnection(const char* conninfo); // use reasonable & environment defaults
+ ~PgConnection(); // close connection and clean up
// Connection status and error messages
ConnStatusType Status();
- int ConnectionBad() { return Status() == CONNECTION_BAD; }
- const char* ErrorMessage() const { return pgErrorMessage.c_str(); }
+ int ConnectionBad();
+ const char* ErrorMessage();
// returns the database name of the connection
- const char* DBName() const { return PQdb(pgConn); }
+ const char* DBName();
// Query Execution interface
ExecStatusType Exec(const char* query); // send a query to the backend
PGnotify* Notifies();
protected:
- ConnStatusType Connect(const char* dbName);
- void SetErrorMessage(const string&, int append = 0);
+ ConnStatusType Connect(const char* conninfo);
string IntToString(int);
protected:
PgConnection();
- PgConnection(const PgConnection&);
};
#endif // PGCONN_H
*
* Copyright (c) 1994, Regents of the University of California
*
- * IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/interfaces/libpq++/Attic/pgcursordb.cc,v 1.1 1997/02/13 10:00:30 scrappy Exp $
*
*-------------------------------------------------------------------------
*/
#include "pgcursordb.h"
+
+static char rcsid[] = "$Id: pgcursordb.cc,v 1.2 1999/05/23 01:04:01 momjian Exp $";
+
// ****************************************************************
//
// ****************************************************************
// Make a connection to the specified database with default environment
-PgCursor::PgCursor(const char* dbName, const char* cursor)
- : PgTransaction(dbName), pgCursor(cursor)
-{}
-
-// Make a connection to the specified database with the given environment
-PgCursor::PgCursor(const PgEnv& env, const char* dbName, const char* cursor)
- : PgTransaction(env, dbName), pgCursor(cursor)
+// See PQconnectdb() for conninfo usage
+PgCursor::PgCursor(const char* conninfo, const char* cursor)
+ : PgTransaction(conninfo), pgCursor(cursor)
{}
// Do not make a connection to the backend -- just query
*
* Copyright (c) 1994, Regents of the University of California
*
+ *
+ * $Id: pgcursordb.h,v 1.2 1999/05/23 01:04:01 momjian Exp $
+ *
*-------------------------------------------------------------------------
*/
#include "pgtransdb.h"
+
// ****************************************************************
//
// PgCursor - a class for querying databases using a cursor
// operations, like fetch, forward, etc.
class PgCursor : public PgTransaction {
public:
- PgCursor(const char* dbName, const char* cursor); // use reasonable defaults
+ PgCursor(const char* conninfo, const char* cursor); // use reasonable & environment defaults
// connect to the database with given environment and database name
- PgCursor(const PgEnv& env, const char* dbName, const char* cursor);
PgCursor(const PgConnection&, const char* cursor);
- virtual ~PgCursor(); // close connection and clean up
+ ~PgCursor(); // close connection and clean up
// Commands associated with cursor interface
int Declare(const string& query, int binary = 0); // Declare a cursor with given name
--- /dev/null
+/*-------------------------------------------------------------------------
+ *
+ * FILE
+ * pgdatabase.cpp
+ *
+ * DESCRIPTION
+ * implementation of the PgDatabase class.
+ * PgDatabase encapsulates some utility routines
+ *
+ * Copyright (c) 1994, Regents of the University of California
+ *
+ * IDENTIFICATION
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "pgdatabase.h"
+
+static char rcsid[] = "$Id: pgdatabase.cc,v 1.1 1999/05/23 01:04:01 momjian Exp $";
+
+void PgDatabase::DisplayTuples(FILE *out = 0, int fillAlign = 1,
+ const char* fieldSep = "|",int printHeader = 1, int quiet = 0)
+{
+PQprintOpt po;
+
+ memset(&po,0,sizeof(po));
+
+ po.align = (pqbool)fillAlign;
+ po.fieldSep = (char *)fieldSep;
+ po.header = (pqbool)printHeader;
+
+ PQprint(out,pgResult,&po);
+
+}
+
+
+
+
+void PgDatabase::PrintTuples(FILE *out = 0, int printAttName = 1, int terseOutput = 0, int width = 0)
+{
+PQprintOpt po;
+
+ memset(&po,0,sizeof(po));
+
+ po.align = (pqbool)width;
+
+ if(terseOutput) po.fieldSep = strdup("|");
+ else po.fieldSep = "";
+
+ po.header = (pqbool)printAttName;
+
+ PQprint(out,pgResult,&po);
+
+}
+
+
+
+int PgDatabase::Tuples()
+{
+return PQntuples(pgResult);
+}
+
+
+int PgDatabase::Fields()
+{
+return PQnfields(pgResult);
+}
+
+
+const char* PgDatabase::FieldName(int field_num)
+{
+return PQfname(pgResult, field_num);
+}
+
+
+int PgDatabase::FieldNum(const char* field_name)
+{
+return PQfnumber(pgResult, field_name);
+}
+
+
+Oid PgDatabase::FieldType(int field_num)
+{
+return PQftype(pgResult, field_num);
+}
+
+
+Oid PgDatabase::FieldType(const char* field_name)
+{
+return PQftype(pgResult, FieldNum(field_name));
+}
+
+
+short PgDatabase::FieldSize(int field_num)
+{
+return PQfsize(pgResult, field_num);
+}
+
+
+short PgDatabase::FieldSize(const char* field_name)
+{
+return PQfsize(pgResult, FieldNum(field_name));
+}
+
+
+const char* PgDatabase::GetValue(int tup_num, int field_num)
+{
+return PQgetvalue(pgResult, tup_num, field_num);
+}
+
+
+const char* PgDatabase::GetValue(int tup_num, const char* field_name)
+{
+return PQgetvalue(pgResult, tup_num, FieldNum(field_name));
+}
+
+
+int PgDatabase::GetLength(int tup_num, int field_num)
+{
+return PQgetlength(pgResult, tup_num, field_num);
+}
+
+
+int PgDatabase::GetLength(int tup_num, const char* field_name)
+{
+return PQgetlength(pgResult, tup_num, FieldNum(field_name));
+}
+
+int PgDatabase::GetLine(char* string, int length)
+{
+return PQgetline(pgConn, string, length);
+}
+
+
+void PgDatabase::PutLine(const char* string)
+{
+PQputline(pgConn, string);
+}
+
+
+const char* PgDatabase::OidStatus()
+{
+return PQoidStatus(pgResult);
+}
+
+
+int PgDatabase::EndCopy()
+{
+return PQendcopy(pgConn);
+}
+
+
*
* Copyright (c) 1994, Regents of the University of California
*
+ *
+ * $Id: pgdatabase.h,v 1.2 1999/05/23 01:04:01 momjian Exp $
+ *
*-------------------------------------------------------------------------
*/
// results are being received.
class PgDatabase : public PgConnection {
public:
- PgDatabase(const char* dbName) : PgConnection(dbName) {} // use reasonable defaults
- // connect to the database with given environment and database name
- PgDatabase(const PgEnv& env, const char* dbName) : PgConnection(env, dbName) {}
- PgDatabase(const PgConnection& conn) : PgConnection(conn) {pgCloseConnection = 0;}
- ~PgDatabase() {} // close connection and clean up
+ PgDatabase(const char* conninfo) : PgConnection(conninfo) {} // use reasonable defaults
+ ~PgDatabase() {} ; // close connection and clean up
// query result access
- int Tuples()
- { return PQntuples(pgResult); }
- int Fields()
- { return PQnfields(pgResult); }
- const char* FieldName(int field_num)
- { return PQfname(pgResult, field_num); }
- int FieldNum(const char* field_name)
- { return PQfnumber(pgResult, field_name); }
- Oid FieldType(int field_num)
- { return PQftype(pgResult, field_num); }
- Oid FieldType(const char* field_name)
- { return PQftype(pgResult, FieldNum(field_name)); }
- short FieldSize(int field_num)
- { return PQfsize(pgResult, field_num); }
- short FieldSize(const char* field_name)
- { return PQfsize(pgResult, FieldNum(field_name)); }
- const char* GetValue(int tup_num, int field_num)
- { return PQgetvalue(pgResult, tup_num, field_num); }
- const char* GetValue(int tup_num, const char* field_name)
- { return PQgetvalue(pgResult, tup_num, FieldNum(field_name)); }
- int GetLength(int tup_num, int field_num)
- { return PQgetlength(pgResult, tup_num, field_num); }
- int GetLength(int tup_num, const char* field_name)
- { return PQgetlength(pgResult, tup_num, FieldNum(field_name)); }
- void DisplayTuples(FILE *out = 0, int fillAlign = 1, const char* fieldSep = "|",
- int printHeader = 1, int quiet = 0)
- { PQdisplayTuples(pgResult, (out ? out : stdout), fillAlign, fieldSep, printHeader, quiet); }
- void PrintTuples(FILE *out = 0, int printAttName = 1, int terseOutput = 0, int width = 0)
- { PQprintTuples(pgResult, (out ? out : stdout), printAttName, terseOutput, width); }
+ int Tuples();
+ int Fields();
+ const char* FieldName(int field_num);
+ int FieldNum(const char* field_name);
+ Oid FieldType(int field_num);
+ Oid FieldType(const char* field_name);
+ short FieldSize(int field_num);
+ short FieldSize(const char* field_name);
+ const char* GetValue(int tup_num, int field_num);
+ const char* GetValue(int tup_num, const char* field_name);
+ int GetLength(int tup_num, int field_num);
+ int GetLength(int tup_num, const char* field_name);
+ void DisplayTuples(FILE *out = 0, int fillAlign = 1,
+ const char* fieldSep = "|",int printHeader = 1, int quiet = 0) ;
+ void PrintTuples(FILE *out = 0, int printAttName = 1,
+ int terseOutput = 0, int width = 0) ;
// copy command related access
- int GetLine(char* string, int length)
- { return PQgetline(pgConn, string, length); }
- void PutLine(const char* string)
- { PQputline(pgConn, string); }
- const char* OidStatus()
- { return PQoidStatus(pgResult); }
- int EndCopy()
- { return PQendcopy(pgConn); }
+ int GetLine(char* string, int length);
+ void PutLine(const char* string);
+ const char* OidStatus();
+ int EndCopy();
protected:
PgDatabase() : PgConnection() {} // Do not connect
+++ /dev/null
-/*-------------------------------------------------------------------------
- *
- * FILE
- * PgEnv.cc
- *
- * DESCRIPTION
- * PgEnv is the environment for setting up a connection to a
- * postgres backend, captures the host, port, tty, options and
- * authentication type.
- *
- * NOTES
- * Currently under construction.
- *
- * Copyright (c) 1994, Regents of the University of California
- *
- * IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/interfaces/libpq++/Attic/pgenv.cc,v 1.4 1999/05/10 15:27:19 momjian Exp $
- *
- *-------------------------------------------------------------------------
- */
-
-#include
-#include "pgenv.h"
-
-
-#define DefaultAuth DEFAULT_CLIENT_AUTHSVC
-#define DefaultPort POSTPORT
-
-
-// ****************************************************************
-//
-// PgEnv Implementation
-//
-// ****************************************************************
-// Default constructor for PgEnv
-// checks the environment variables
-PgEnv::PgEnv()
-{
- SetValues(getenv(ENV_DEFAULT_AUTH), getenv(ENV_DEFAULT_HOST),
- getenv(ENV_DEFAULT_PORT), getenv(ENV_DEFAULT_OPTION),
- getenv(ENV_DEFAULT_TTY));
-}
-
-// constructor for given environment
-PgEnv::PgEnv(const string& auth, const string& host, const string& port,
- const string& option, const string& tty)
-{
- SetValues(auth, host, port, option, tty);
-}
-
-// allocate memory and set internal structures to match
-// required environment
-void PgEnv::SetValues(const string& auth, const string& host, const string& port,
- const string& option, const string& tty)
-{
- Auth( auth );
- Host( host );
- Port( port );
- Option( option );
- TTY( tty );
-}
-
-// read a string from the environment and convert it to string
-string PgEnv::getenv(const char* name)
-{
- char* env = ::getenv(name);
- return (env ? env : "");
-}
-
-
-// Extract the PgEnv contents into a form suitable for PQconnectdb
-// which happens to be readable, hence choice of <<
-ostream& operator << (ostream &s, const PgEnv& a)
-{
- s<<' '; // surround with whitespace, just in case
- if(a.pgHost.length() !=0)s<<" host=" <
- if(a.pgPort.length() !=0)s<<" port=" <
- // deprecated: if(a.pgAuth.length()!=0)s<<" authtype="<
- if(a.pgOption.length()!=0)s<<" options="<
- if(a.pgTty.length() !=0)s<<" tty=" <
- s<<' ';
-
- return s;
-}
+++ /dev/null
-/*-------------------------------------------------------------------------
- *
- * pgenv.h
- *
- *
- * DESCRIPTION
- * Postgres Environment Class: manages and stores all the required
- * connection variables.
- *
- * NOTES
- * Currently under construction.
- *
- * Copyright (c) 1994, Regents of the University of California
- *
- *-------------------------------------------------------------------------
- */
-
-#ifndef PGENV_H
-#define PGENV_H
-
-#include
-#include
-
-#ifdef __sun__
-#ifndef __GNUC__
-using namespace std;
-#endif
-#endif
-
-//
-// these are the environment variables used for getting defaults
-//
-
-#define ENV_DEFAULT_AUTH "PGAUTH"
-#define ENV_DEFAULT_DBASE "PGDATABASE"
-#define ENV_DEFAULT_HOST "PGHOST"
-#define ENV_DEFAULT_OPTION "PGOPTION"
-#define ENV_DEFAULT_PORT "PGPORT"
-#define ENV_DEFAULT_TTY "PGTTY"
-
-
-// ****************************************************************
-//
-// PgEnv - the environment for setting up a connection to postgres
-//
-// ****************************************************************
-class PgEnv {
-private:
- string pgAuth;
- string pgHost;
- string pgPort;
- string pgOption;
- string pgTty;
-
-public:
- PgEnv(); // default ctor will use reasonable defaults
- // will use environment variables PGHOST, PGPORT,
- // PGOPTION, PGTTY
- PgEnv(const string& auth, const string& host, const string& port,
- const string& option, const string& tty);
-
- // Access methods to all the environment variables
- const char* Auth() { return pgAuth.c_str(); }
- void Auth(const string& auth) { pgAuth = auth; }
-
- const char* Host() { return pgHost.c_str(); }
- void Host(const string& host) { pgHost = host; }
-
- const char* Port() { return pgPort.c_str(); }
- void Port(const string& port) { pgPort = port; }
-
- const char* Option() { return pgOption.c_str(); }
- void Option(const string& option) { pgOption = option; }
-
- const char* TTY() { return pgTty.c_str(); }
- void TTY(const string& tty) { pgTty = tty; }
-
- void SetValues(const string& auth, const string& host, const string& port,
- const string& option, const string& tty);
-
-protected:
- string getenv(const char*);
- friend ostream& operator << (ostream &, const PgEnv&);
-};
-
-#endif // PGENV_H
*
* Copyright (c) 1994, Regents of the University of California
*
- * IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/interfaces/libpq++/Attic/pglobject.cc,v 1.3 1997/02/13 10:00:34 scrappy Exp $
*
*-------------------------------------------------------------------------
*/
#include "pglobject.h"
-
+static char rcsid[] = "$Id: pglobject.cc,v 1.4 1999/05/23 01:04:03 momjian Exp $";
// ****************************************************************
//
// ****************************************************************
// default constructor
// creates a large object in the default database
-PgLargeObject::PgLargeObject(const char* dbName)
- : PgConnection(dbName)
+// See PQconnectdb() for conninfo usage
+PgLargeObject::PgLargeObject(const char* conninfo)
+ : PgConnection(conninfo)
{
Init();
Create();
// constructor
// open an existing large object in the default database
-PgLargeObject::PgLargeObject(Oid lobjId, const char* dbName)
- : PgConnection(dbName)
+// See PQconnectdb() for conninfo usage
+PgLargeObject::PgLargeObject(Oid lobjId, const char* conninfo)
+ : PgConnection(conninfo)
{
- Init(lobjId);
- if ( !pgObject )
- Create();
- Open();
-}
-
-// constructor
-// create a large object in the given database
-PgLargeObject::PgLargeObject(const PgEnv& env, const char* dbName)
- : PgConnection(env, dbName)
-{
- Init();
- Create();
- Open();
-}
-// constructor
-// open an existing large object in the given database
-PgLargeObject::PgLargeObject(const PgEnv& env, const char* dbName, Oid lobjId)
- : PgConnection(env, dbName)
-{
Init(lobjId);
- if ( !pgObject )
- Create();
+ if ( !pgObject ) {
+ Create();
+ }
Open();
}
// Check for possible errors
if (!pgObject)
- SetErrorMessage( "PgLargeObject: can't create large object" );
+ loStatus = "PgLargeObject: can't create large object" ;
+ else
+ loStatus = "PgLargeObject: created large object" ;
}
// PgLargeObject::open
// Check for possible errors
string objStr( IntToString(pgObject) );
if (pgFd < 0)
- SetErrorMessage( "PgLargeObject: can't open large object " + objStr );
+ loStatus = "PgLargeObject: can't open large object " + objStr ;
else
- SetErrorMessage( "PgLargeObject: created and opened large object " + objStr );
+ loStatus = "PgLargeObject: created and opened large object " + objStr ;
}
// PgLargeObject::unlink
// Return the status
return temp;
}
+
+
+
+void PgLargeObject::Close()
+{
+ if (pgFd >= 0) lo_close(pgConn, pgFd);
+}
+
+
+int PgLargeObject::Read(char* buf, int len)
+{
+ return lo_read(pgConn, pgFd, buf, len);
+}
+
+
+int PgLargeObject::Write(const char* buf, int len)
+{
+ return lo_write(pgConn, pgFd, (char*)buf, len);
+}
+
+
+int PgLargeObject::LSeek(int offset, int whence)
+{
+ return lo_lseek(pgConn, pgFd, offset, whence);
+}
+
+
+int PgLargeObject::Tell()
+{
+ return lo_tell(pgConn, pgFd);
+}
+
+
+Oid PgLargeObject::Import(const char* filename)
+{
+ return pgObject = lo_import(pgConn, (char*)filename);
+}
+
+
+int PgLargeObject::Export(const char* filename)
+{
+ return lo_export(pgConn, pgObject, (char*)filename);
+}
+
+
+string PgLargeObject::Status()
+{
+ return loStatus;
+}
+
*
* Copyright (c) 1994, Regents of the University of California
*
- * IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/interfaces/libpq++/Attic/pglobject.h,v 1.1 1997/02/13 10:00:35 scrappy Exp $
+ *
+ * $Id: pglobject.h,v 1.2 1999/05/23 01:04:03 momjian Exp $
*
*-------------------------------------------------------------------------
*/
private:
int pgFd;
Oid pgObject;
+ string loStatus;
public:
- PgLargeObject(const char* dbName = 0); // use reasonable defaults and create large object
- PgLargeObject(Oid lobjId, const char* dbName = 0); // use reasonable defaults and open large object
- PgLargeObject(const PgEnv& env, const char* dbName); // create large object
- PgLargeObject(const PgEnv& env, const char* dbName, Oid lobjId); // open large object
+ PgLargeObject(const char* conninfo = 0); // use reasonable defaults and create large object
+ 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()
- { if (pgFd >= 0) lo_close(pgConn, pgFd); }
- int Read(char* buf, int len)
- { return lo_read(pgConn, pgFd, buf, len); }
- int Write(const char* buf, int len)
- { return lo_write(pgConn, pgFd, (char*)buf, len); }
- int LSeek(int offset, int whence)
- { return lo_lseek(pgConn, pgFd, offset, whence); }
- int Tell()
- { return lo_tell(pgConn, pgFd); }
+ void Close();
+ int Read(char* buf, int len);
+ int Write(const char* buf, int len);
+ int LSeek(int offset, int whence);
+ int Tell();
int Unlink();
- Oid Import(const char* filename) { return pgObject = lo_import(pgConn, (char*)filename); }
- int Export(const char* filename) { return lo_export(pgConn, pgObject, (char*)filename); }
+ Oid LOid();
+ Oid Import(const char* filename);
+ int Export(const char* filename);
+ string Status();
private:
void Init(Oid lobjId = 0);
};
#endif // PGLOBJ_H
+
+// sig 11's if the filename points to a binary file.
*
* Copyright (c) 1994, Regents of the University of California
*
- * IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/interfaces/libpq++/Attic/pgtransdb.cc,v 1.1 1997/02/13 10:00:36 scrappy Exp $
*
*-------------------------------------------------------------------------
*/
#include "pgtransdb.h"
+static char rcsid[] = "$Id: pgtransdb.cc,v 1.2 1999/05/23 01:04:03 momjian Exp $";
// ****************************************************************
//
//
// ****************************************************************
// Make a connection to the specified database with default environment
-PgTransaction::PgTransaction(const char* dbName)
- : PgDatabase(dbName)
-{
- BeginTransaction();
-}
-
-// Make a connection to the specified database with the given environment
-PgTransaction::PgTransaction(const PgEnv& env, const char* dbName)
- : PgDatabase(env, dbName)
-{
- BeginTransaction();
-}
-
-// 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
-PgTransaction::PgTransaction(const PgConnection& conn)
- : PgDatabase(conn)
+// See PQconnectdb() for conninfo usage.
+PgTransaction::PgTransaction(const char* conninfo)
+ : PgDatabase(conninfo)
{
BeginTransaction();
}
*
* Copyright (c) 1994, Regents of the University of California
*
+ *
+ * $Id: pgtransdb.h,v 1.2 1999/05/23 01:04:03 momjian Exp $
+ *
*-------------------------------------------------------------------------
*/
#include "pgdatabase.h"
-
// ****************************************************************
//
// PgTransaction - a class for running transactions against databases
// the object is destroyed.
class PgTransaction : public PgDatabase {
public:
- PgTransaction(const char* dbName); // use reasonable defaults
+ PgTransaction(const char* conninfo); // use reasonable & environment defaults
// connect to the database with given environment and database name
- PgTransaction(const PgEnv& env, const char* dbName);
PgTransaction(const PgConnection&);
- virtual ~PgTransaction(); // close connection and clean up
+ ~PgTransaction(); // close connection and clean up
protected:
ExecStatusType BeginTransaction();