diff options
Diffstat (limited to 'src/interfaces/libpq++/pgconnection.cc')
-rw-r--r-- | src/interfaces/libpq++/pgconnection.cc | 183 |
1 files changed, 129 insertions, 54 deletions
diff --git a/src/interfaces/libpq++/pgconnection.cc b/src/interfaces/libpq++/pgconnection.cc index 777a12e8e7a..96ef215700f 100644 --- a/src/interfaces/libpq++/pgconnection.cc +++ b/src/interfaces/libpq++/pgconnection.cc @@ -1,94 +1,169 @@ /*------------------------------------------------------------------------- * * FILE - * pgconnection.cc + * pgconnection.cpp * * DESCRIPTION - * implementation of the PGconnection class. - * PGconnection encapsulates a frontend to backend connection + * 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.1.1.1 1996/07/09 06:22:18 scrappy Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpq++/Attic/pgconnection.cc,v 1.2 1997/02/13 10:00:27 scrappy Exp $ * *------------------------------------------------------------------------- */ -#include "libpq++.H" +#include <stdlib.h> +#include <string.h> +#include "pgconnection.h" + +extern "C" { +#include "fe-auth.h" +} -// default constructor -// checks environment variable for database name -PGconnection::PGconnection() -{ - char* name; - PGenv* newenv; - conn = NULL; - result = NULL; - errorMessage[0] = '\0'; +// **************************************************************** +// +// PgConnection Implementation +// +// **************************************************************** +// default constructor -- initialize everything +PgConnection::PgConnection() + : pgConn(NULL), pgResult(NULL), pgCloseConnection(0) +{} - newenv = new PGenv(); // use reasonable defaults for the environment - if (!(name = getenv(ENV_DEFAULT_DBASE))) - return; - connect(newenv, name); +// 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(PGenv* env, char* dbName) +PgConnection::PgConnection(const PgEnv& env, const char* dbName) + : pgEnv(env), pgConn(NULL), pgResult(NULL), pgCloseConnection(1) { - conn = NULL; - result = NULL; - errorMessage[0] = '\0'; - connect(env, dbName); + Connect( dbName ); } // destructor - closes down the connection and cleanup -PGconnection::~PGconnection() +PgConnection::~PgConnection() { - if (result) PQclear(result); - if (conn) PQfinish(conn); + // 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 +// PgConnection::connect // establish a connection to a backend -ConnStatusType -PGconnection::connect(PGenv* newenv, char* dbName) +ConnStatusType PgConnection::Connect(const char* dbName) { -#if 0 - FILE *debug; - debug = fopen("/tmp/trace.out","w"); - PQtrace(conn, debug); -#endif - - env = newenv; - fe_setauthsvc(env->pgauth, errorMessage); - conn = PQsetdb(env->pghost, env->pgport, env->pgoption, env->pgtty, dbName); - if(strlen(errorMessage)) - return CONNECTION_BAD; + // Turn the trace on + #if defined(DEBUG) + FILE *debug = fopen("/tmp/trace.out","w"); + PQtrace(pgConn, debug); + #endif + + // Set Host Authentication service + char errorMessage[ERROR_MSG_LENGTH]; + memset(errorMessage, 0, sizeof(errorMessage)); + fe_setauthsvc(pgEnv.Auth(), errorMessage); + + // Connect to the database + pgConn = PQsetdb(pgEnv.Host(), pgEnv.Port(), pgEnv.Option(), pgEnv.TTY(), dbName); + + // Return the connection status + if (errorMessage) { + SetErrorMessage( errorMessage ); + return CONNECTION_BAD; + } else - return status(); + return Status(); } -// PGconnection::status -- return connection or result status -ConnStatusType -PGconnection::status() +// PgConnection::status -- return connection or result status +ConnStatusType PgConnection::Status() { - return PQstatus(conn); + return PQstatus(pgConn); } -// PGconnection::exec -- send a query to the backend -ExecStatusType -PGconnection::exec(char* query) +// PgConnection::exec -- send a query to the backend +ExecStatusType PgConnection::Exec(const char* query) { - if (result) - PQclear(result); + // Clear the Result Stucture if needed + if (pgResult) + PQclear(pgResult); - result = PQexec(conn, query); - if (result) - return PQresultStatus(result); + // Execute the given query + pgResult = PQexec(pgConn, query); + + // Return the status + if (pgResult) + return PQresultStatus(pgResult); else { - strcpy(errorMessage, PQerrorMessage(conn)); + SetErrorMessage( PQerrorMessage(pgConn) ); return PGRES_FATAL_ERROR; } } + +// Return true if the Postgres command was executed OK +int PgConnection::ExecCommandOk(const char* query) +{ + return Exec(query) == PGRES_COMMAND_OK; +} // End ExecCommandOk() + +int PgConnection::ExecTuplesOk(const char* query) +{ + return Exec(query) == PGRES_TUPLES_OK; +} // End ExecTuplesOk() + + +// PgConnection::notifies() -- returns a notification from a list of unhandled notifications +PGnotify* PgConnection::Notifies() +{ + Exec(" "); + 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) +{ + char buffer [32]; + memset(buffer, 0, sizeof(buffer)); + sprintf(buffer, "%d", n); + return buffer; +} |