/*------------------------------------------------------------------------- * * FILE * pgconnection.cpp * * 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.3 1999/05/10 15:27:18 momjian Exp $ * *------------------------------------------------------------------------- */ #include #include #include #include "pgconnection.h" extern "C" { #include "fe-auth.h" } // **************************************************************** // // PgConnection Implementation // // **************************************************************** // default constructor -- initialize everything PgConnection::PgConnection() : 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) : 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 ); } // 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() { // Terminate the debugging output if it was turned on #if defined(DEBUG) PQuntrace(pgConn); #endif // Close the conneciton only if needed // 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); } } // PgConnection::connect // establish a connection to a backend ConnStatusType PgConnection::Connect(const char* dbName) { // 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="<