diff --git a/doc/src/sgml/libpq++.sgml b/doc/src/sgml/libpq++.sgml index ad782013a3..d4b53557dd 100644 --- a/doc/src/sgml/libpq++.sgml +++ b/doc/src/sgml/libpq++.sgml @@ -1,5 +1,5 @@ @@ -227,9 +227,9 @@ $Header: /cvsroot/pgsql/doc/src/sgml/Attic/libpq++.sgml,v 1.28 2001/05/01 10:48: returns whether or not the connection to the backend server succeeded or failed. - int PgConnection::ConnectionBad() + bool PgConnection::ConnectionBad() const - Returns TRUE if the connection failed. + Returns true if the connection failed. @@ -368,7 +368,7 @@ $Header: /cvsroot/pgsql/doc/src/sgml/Attic/libpq++.sgml,v 1.28 2001/05/01 10:48: Tuples Returns the number of tuples (rows) in the query result. - int PgDatabase::Tuples() + int PgDatabase::Tuples() const @@ -387,7 +387,7 @@ $Header: /cvsroot/pgsql/doc/src/sgml/Attic/libpq++.sgml,v 1.28 2001/05/01 10:48: Returns the field (attribute) name associated with the given field index. Field indices start at 0. - const char *PgDatabase::FieldName(int field_num) + const char *PgDatabase::FieldName(int field_num) const @@ -397,7 +397,7 @@ $Header: /cvsroot/pgsql/doc/src/sgml/Attic/libpq++.sgml,v 1.28 2001/05/01 10:48: PQfnumber Returns the field (attribute) index associated with the given field name. - int PgDatabase::FieldNum(const char* field_name) + int PgDatabase::FieldNum(const char* field_name) const -1 is returned if the given name does not match any field. @@ -409,7 +409,7 @@ $Header: /cvsroot/pgsql/doc/src/sgml/Attic/libpq++.sgml,v 1.28 2001/05/01 10:48: integer returned is an internal coding of the type. Field indices start at 0. - Oid PgDatabase::FieldType(int field_num) + Oid PgDatabase::FieldType(int field_num) const @@ -420,7 +420,7 @@ $Header: /cvsroot/pgsql/doc/src/sgml/Attic/libpq++.sgml,v 1.28 2001/05/01 10:48: integer returned is an internal coding of the type. Field indices start at 0. - Oid PgDatabase::FieldType(const char* field_name) + Oid PgDatabase::FieldType(const char* field_name) const @@ -430,7 +430,7 @@ $Header: /cvsroot/pgsql/doc/src/sgml/Attic/libpq++.sgml,v 1.28 2001/05/01 10:48: Returns the size in bytes of the field associated with the given field index. Field indices start at 0. - short PgDatabase::FieldSize(int field_num) + short PgDatabase::FieldSize(int field_num) const Returns the space allocated for this field in a database tuple given the field number. In other words the size of the server's binary @@ -444,7 +444,7 @@ $Header: /cvsroot/pgsql/doc/src/sgml/Attic/libpq++.sgml,v 1.28 2001/05/01 10:48: Returns the size in bytes of the field associated with the given field index. Field indices start at 0. - short PgDatabase::FieldSize(const char *field_name) + short PgDatabase::FieldSize(const char *field_name) const Returns the space allocated for this field in a database tuple given the field name. In other words the size of the server's binary @@ -466,7 +466,7 @@ $Header: /cvsroot/pgsql/doc/src/sgml/Attic/libpq++.sgml,v 1.28 2001/05/01 10:48: Returns a single field (attribute) value of one tuple of a PGresult. Tuple and field indices start at 0. - const char *PgDatabase::GetValue(int tup_num, int field_num) + const char *PgDatabase::GetValue(int tup_num, int field_num) const For most queries, the value returned by GetValue is a null-terminated ASCII string representation of the attribute value. But if BinaryTuples() @@ -486,7 +486,7 @@ $Header: /cvsroot/pgsql/doc/src/sgml/Attic/libpq++.sgml,v 1.28 2001/05/01 10:48: Returns a single field (attribute) value of one tuple of a PGresult. Tuple and field indices start at 0. - const char *PgDatabase::GetValue(int tup_num, const char *field_name) + const char *PgDatabase::GetValue(int tup_num, const char *field_name) const For most queries, the value returned by GetValue is a null-terminated ASCII string representation of the attribute value. But if BinaryTuples() @@ -506,7 +506,7 @@ $Header: /cvsroot/pgsql/doc/src/sgml/Attic/libpq++.sgml,v 1.28 2001/05/01 10:48: Returns the length of a field (attribute) in bytes. Tuple and field indices start at 0. - int PgDatabase::GetLength(int tup_num, int field_num) + int PgDatabase::GetLength(int tup_num, int field_num) const This is the actual data length for the particular data value, that is the size of the object pointed to by GetValue. Note that for @@ -520,7 +520,7 @@ $Header: /cvsroot/pgsql/doc/src/sgml/Attic/libpq++.sgml,v 1.28 2001/05/01 10:48: Returns the length of a field (attribute) in bytes. Tuple and field indices start at 0. - int PgDatabase::GetLength(int tup_num, const char* field_name) + int PgDatabase::GetLength(int tup_num, const char* field_name) const This is the actual data length for the particular data value, that is the size of the object pointed to by GetValue. Note that for @@ -528,25 +528,47 @@ $Header: /cvsroot/pgsql/doc/src/sgml/Attic/libpq++.sgml,v 1.28 2001/05/01 10:48: size reported by PQfsize. + + + GetIsNull + Returns whether a field has the null value. + + bool GetIsNull(int tup_num, int field_num) const + + Note that GetValue will return the empty string for null fields, not + the NULL pointer. + + + + + GetIsNull + Returns whether a field has the null value. + + bool GetIsNull(int tup_num, const char *field_name) const + + Note that GetValue will return the empty string for null fields, not + the NULL pointer. + + DisplayTuples - Prints out all the tuples and, optionally, the attribute names to the + OBSOLESCENT: Prints out all the tuples and, optionally, the attribute names to the specified output stream. - void PgDatabase::DisplayTuples(FILE *out = 0, int fillAlign = 1, - const char* fieldSep = "|",int printHeader = 1, int quiet = 0) + void PgDatabase::DisplayTuples(FILE *out = 0, bool fillAlign = true, + const char* fieldSep = "|",bool printHeader = true, bool quiet = false) const PrintTuples - Prints out all the tuples and, optionally, the attribute names to the + OBSOLESCENT: Prints out all the tuples and, optionally, the attribute names to the specified output stream. - void PgDatabase::PrintTuples(FILE *out = 0, int printAttName = 1, - int terseOutput = 0, int width = 0) + void PgDatabase::PrintTuples(FILE *out = 0, bool printAttName = true, + bool terseOutput = false, bool fillAlign = false) const @@ -563,7 +585,7 @@ $Header: /cvsroot/pgsql/doc/src/sgml/Attic/libpq++.sgml,v 1.28 2001/05/01 10:48: Returns the number of rows affected after an INSERT, UPDATE or DELETE. If the command was anything else, it returns -1. - int PgDatabase::CmdTuples() + int PgDatabase::CmdTuples() const @@ -572,7 +594,7 @@ $Header: /cvsroot/pgsql/doc/src/sgml/Attic/libpq++.sgml,v 1.28 2001/05/01 10:48: OidStatus - const char *PgDatabase::OidStatus() + const char *PgDatabase::OidStatus() const @@ -650,8 +672,8 @@ $Header: /cvsroot/pgsql/doc/src/sgml/Attic/libpq++.sgml,v 1.28 2001/05/01 10:48: to see if any notification data is currently available from the backend. PgDatabase::Notifies returns the notification from a list of unhandled notifications from the - backend. The function eturns NULL if there is no pending notifications from the - backend. + backend. The function returns NULL if there are no pending notifications + from the backend. PgDatabase::Notifies behaves like the popping of a stack. Once a notification is returned from PgDatabase::Notifies, diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml index c1f405f3f3..a5a1dc8ac0 100644 --- a/doc/src/sgml/ref/psql-ref.sgml +++ b/doc/src/sgml/ref/psql-ref.sgml @@ -1,5 +1,5 @@ @@ -494,6 +494,16 @@ testdb=> + + \du [ pattern ] + + + Lists all configured users or only those that match pattern. + + + + + \edit (or \e) [ filename ] diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index fd3c9f8d92..966fbaafe4 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -3,7 +3,7 @@ * * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.51 2001/05/07 19:31:33 petere Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.52 2001/05/09 17:29:10 momjian Exp $ */ #include "postgres_fe.h" #include "command.h" @@ -357,6 +357,9 @@ exec_command(const char *cmd, case 'S': success = listTables(&cmd[1], name, show_verbose); break; + case 'u': + success = describeUsers(name); + break; default: status = CMD_UNKNOWN; } diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c index d31d68bbbe..f7e544bc78 100644 --- a/src/bin/psql/describe.c +++ b/src/bin/psql/describe.c @@ -3,7 +3,7 @@ * * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/describe.c,v 1.28 2001/03/22 04:00:19 momjian Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/describe.c,v 1.29 2001/05/09 17:29:10 momjian Exp $ */ #include "postgres_fe.h" #include "describe.h" @@ -890,6 +890,103 @@ describeTableDetails(const char *name, bool desc) } +/* + * describeUsers() + * + * \du [user] + * + * Describes users, possibly based on a simplistic prefix search on the + * argument. + */ + +bool +describeUsers (const char *name) +{ + char buf[384 + REGEXP_CUTOFF]; + PGresult *res; + printTableOpt myopt = pset.popt.topt; + int i; + char *title; + const char *headers[4]; + char **cells = NULL; + unsigned int cols; + + /* + * All we want to know is the user names and permissions + * for the system. + */ + + title = "List of Users"; + + cols = 0; + headers[cols++] = "User Name"; + headers[cols++] = "User ID"; + headers[cols++] = "Attributes"; + headers[cols] = NULL; + + strcpy(buf, + "SELECT u.usename AS \"User Name\"\n + , u.usesysid AS \"User ID\"\n + , u.usesuper AS \"Super User\"\n + , u.usecreatedb AS \"Create DB\"\n + FROM pg_user u\n"); + if (name) + { + strcat(buf, " WHERE u.usename ~ '^"); + strncat(buf, name, REGEXP_CUTOFF); + strcat(buf, "'\n"); + } + strcat(buf, "ORDER BY \"User Name\"\n"); + + res = PSQLexec(buf); + if (!res) + return false; + + cells = xmalloc((PQntuples(res) * cols + 1) * sizeof(*cells)); + cells[PQntuples(res) * cols] = NULL; + + for (i = 0; i < PQntuples(res); i++) + { + char createuser[2] = ""; + char createdb[2] = ""; + + /* Name */ + cells[i * cols + 0] = PQgetvalue(res, i, 0); + + /* ID */ + cells[i * cols + 1] = PQgetvalue(res, i, 1); + + /* Super */ + strcpy(createuser, PQgetvalue(res, i, 2)); + + /* Create DB */ + strcpy(createdb, PQgetvalue(res, i, 3)); + + cells[i * cols + 2] = xmalloc((strlen("create user, create DB") * sizeof(char)) + 1); + strcpy(cells[i * cols + 2], ""); + + if (strcmp(createuser, "t") == 0) + strcat(cells[i * cols + 2], "create user"); + + if (strcmp(createdb, "t") == 0) { + if (strcmp(createuser, "t") == 0) + strcat(cells[i * cols + 2], ", "); + strcat(cells[i * cols + 2], "create DB"); + } + } + + printTable(title, headers, + (const char **) cells, + NULL, + "lll", &myopt, pset.queryFout); + + /* clean up */ + free(cells); + + PQclear(res); + return true; +} + /* * listTables() diff --git a/src/bin/psql/describe.h b/src/bin/psql/describe.h index 7383ea6fa6..98821fbed0 100644 --- a/src/bin/psql/describe.h +++ b/src/bin/psql/describe.h @@ -3,7 +3,7 @@ * * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/describe.h,v 1.9 2000/04/12 17:16:22 momjian Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/describe.h,v 1.10 2001/05/09 17:29:10 momjian Exp $ */ #ifndef DESCRIBE_H #define DESCRIBE_H @@ -22,6 +22,9 @@ bool describeTypes(const char *name, bool verbose); /* \do */ bool describeOperators(const char *name); +/* \du */ +bool describeUsers(const char *name); + /* \z (or \dp) */ bool permissionsList(const char *name); diff --git a/src/interfaces/libpq++/examples/testlibpq4.cc b/src/interfaces/libpq++/examples/testlibpq4.cc index 98f91686c9..a3248ea258 100644 --- a/src/interfaces/libpq++/examples/testlibpq4.cc +++ b/src/interfaces/libpq++/examples/testlibpq4.cc @@ -22,12 +22,12 @@ INSERT INTO TBL1 values (10); #include "libpq++.h" #include -main() +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. - char* dbName = "dbname=template1"; + const char* dbName = "dbname=template1"; PgDatabase data(dbName); // Check to see that the backend connection was successfully made diff --git a/src/interfaces/libpq++/examples/testlibpq5.cc b/src/interfaces/libpq++/examples/testlibpq5.cc index 9f74d4d530..04e600d531 100644 --- a/src/interfaces/libpq++/examples/testlibpq5.cc +++ b/src/interfaces/libpq++/examples/testlibpq5.cc @@ -34,12 +34,12 @@ extern "C" { #include "utils/geo_decls.h" // for the POLYGON type } -main() +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. - char* dbName = "dbname=template1"; // change this to the name of your test database + 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 diff --git a/src/interfaces/libpq++/examples/testlibpq6.cc b/src/interfaces/libpq++/examples/testlibpq6.cc index 23263d9c36..1f469f145f 100644 --- a/src/interfaces/libpq++/examples/testlibpq6.cc +++ b/src/interfaces/libpq++/examples/testlibpq6.cc @@ -8,12 +8,12 @@ #include "libpq++.h" #include -main() +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. - char* dbName = "dbname=template1"; + const char* dbName = "dbname=template1"; PgTransaction data(dbName); // check to see that the backend connection was successfully made diff --git a/src/interfaces/libpq++/pgconnection.cc b/src/interfaces/libpq++/pgconnection.cc index 80b45685dc..2f10264dea 100644 --- a/src/interfaces/libpq++/pgconnection.cc +++ b/src/interfaces/libpq++/pgconnection.cc @@ -10,13 +10,15 @@ * Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/interfaces/libpq++/Attic/pgconnection.cc,v 1.10 2000/07/27 19:44:01 momjian Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpq++/Attic/pgconnection.cc,v 1.11 2001/05/09 17:29:10 momjian Exp $ * *------------------------------------------------------------------------- */ #include "pgconnection.h" +using namespace std; + // **************************************************************** // @@ -25,14 +27,14 @@ // **************************************************************** // default constructor -- initialize everything PgConnection::PgConnection() - : pgConn(NULL), pgResult(NULL), pgCloseConnection(0) + : 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(1) + : pgConn(NULL), pgResult(NULL), pgCloseConnection(true) { // Connect to the database @@ -60,14 +62,14 @@ void PgConnection::CloseConnection() pgResult=NULL; if(pgConn) PQfinish(pgConn); pgConn=NULL; - pgCloseConnection=0; + pgCloseConnection=false; } } // PgConnection::connect // establish a connection to a backend -ConnStatusType PgConnection::Connect(const char* conninfo) +ConnStatusType PgConnection::Connect(const char conninfo[]) { // if the connection is open, close it first CloseConnection(); @@ -76,14 +78,14 @@ ConnStatusType PgConnection::Connect(const char* conninfo) pgConn = PQconnectdb(conninfo); // Now we have a connection we must close (even if it's bad!) - pgCloseConnection = 1; + pgCloseConnection = true; // Status will return either CONNECTION_OK or CONNECTION_BAD return Status(); } // PgConnection::status -- return connection or result status -ConnStatusType PgConnection::Status() +ConnStatusType PgConnection::Status() const { return PQstatus(pgConn); } @@ -131,27 +133,26 @@ PGnotify* PgConnection::Notifies() // From Integer To String Conversion Function string PgConnection::IntToString(int n) { - char buffer [32]; - memset(buffer, 0, sizeof(buffer)); + char buffer [4*sizeof(n) + 2]; sprintf(buffer, "%d", n); return buffer; } -int PgConnection::ConnectionBad() +bool PgConnection::ConnectionBad() const { return Status() == CONNECTION_BAD; } -const char* PgConnection::ErrorMessage() +const char* PgConnection::ErrorMessage() const { return (const char *)PQerrorMessage(pgConn); } -const char* PgConnection::DBName() +const char* PgConnection::DBName() const { return (const char *)PQdb(pgConn); } diff --git a/src/interfaces/libpq++/pgconnection.h b/src/interfaces/libpq++/pgconnection.h index 4310dac61d..76cb6ec367 100644 --- a/src/interfaces/libpq++/pgconnection.h +++ b/src/interfaces/libpq++/pgconnection.h @@ -13,7 +13,7 @@ * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $Id: pgconnection.h,v 1.10 2001/02/10 02:31:30 tgl Exp $ + * $Id: pgconnection.h,v 1.11 2001/05/09 17:29:10 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -43,7 +43,9 @@ extern "C" { } #ifdef HAVE_NAMESPACE_STD -using namespace std; +#define PGSTD std:: +#else +#define PGSTD #endif @@ -57,21 +59,21 @@ using namespace std; // derived from this class to obtain the connection interface. class PgConnection { protected: - PGconn* pgConn; // Connection Structure + PGconn* pgConn; // Connection Structure PGresult* pgResult; // Current Query Result - int pgCloseConnection; // TRUE if connection should be closed by destructor + bool pgCloseConnection; // true if connection should be closed by destructor public: - PgConnection(const char* conninfo); // use reasonable & environment defaults + explicit PgConnection(const char* conninfo); // use reasonable & environment defaults virtual ~PgConnection(); // close connection and clean up // Connection status and error messages - ConnStatusType Status(); - int ConnectionBad(); - const char* ErrorMessage(); + ConnStatusType Status() const; + bool ConnectionBad() const; + const char* ErrorMessage() const; // returns the database name of the connection - const char* DBName(); + const char* DBName() const; // Query Execution interface ExecStatusType Exec(const char* query); // send a query to the backend @@ -82,7 +84,7 @@ public: protected: ConnStatusType Connect(const char* conninfo); void CloseConnection(); - string IntToString(int); + static PGSTD string IntToString(int); // Default constructor is only available to subclasses PgConnection(); @@ -93,4 +95,10 @@ private: PgConnection& operator= (const PgConnection&); }; + +#ifdef HAVE_NAMESPACE_STD +#undef PGSTD +#endif + + #endif // PGCONNECTION_H diff --git a/src/interfaces/libpq++/pgcursordb.cc b/src/interfaces/libpq++/pgcursordb.cc index e2bfd2480f..369bc15f6e 100644 --- a/src/interfaces/libpq++/pgcursordb.cc +++ b/src/interfaces/libpq++/pgcursordb.cc @@ -10,13 +10,17 @@ * Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/interfaces/libpq++/Attic/pgcursordb.cc,v 1.4 1999/06/01 02:43:37 momjian Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpq++/Attic/pgcursordb.cc,v 1.5 2001/05/09 17:29:10 momjian Exp $ * *------------------------------------------------------------------------- */ #include "pgcursordb.h" + +using namespace std; + + // **************************************************************** // // PgCursor Implementation @@ -48,7 +52,7 @@ PgCursor::~PgCursor() // // **************************************************************** // Declare a cursor: name has already been supplied in the constructor -int PgCursor::Declare(const string& query, int binary) +int PgCursor::Declare(string query, bool binary) { string cmd = "DECLARE " + pgCursor; if ( binary ) @@ -70,7 +74,7 @@ int PgCursor::Fetch(unsigned num, const char* dir) } // End Fetch() // Create and execute the actual fetch command with the given arguments -int PgCursor::Fetch(const string& num, const string& dir) +int PgCursor::Fetch(string num, string dir) { string cmd = "FETCH " + dir + " " + num + " IN " + pgCursor; return ExecTuplesOk( cmd.c_str() ); diff --git a/src/interfaces/libpq++/pgcursordb.h b/src/interfaces/libpq++/pgcursordb.h index 4f8ad4e1db..bbf8757ce4 100644 --- a/src/interfaces/libpq++/pgcursordb.h +++ b/src/interfaces/libpq++/pgcursordb.h @@ -14,7 +14,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * - * $Id: pgcursordb.h,v 1.6 2001/01/24 19:43:32 momjian Exp $ + * $Id: pgcursordb.h,v 1.7 2001/05/09 17:29:10 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -26,6 +26,12 @@ #include "pgtransdb.h" #endif +#ifdef HAVE_NAMESPACE_STD +#define PGSTD std:: +#else +#define PGSTD +#endif + // **************************************************************** // @@ -44,20 +50,22 @@ public: ~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 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(); } - void Cursor(const string& cursor) { pgCursor = cursor; } + // TODO: Setter has same name as getter--ouch! + // OBSOLESCENT + void Cursor(PGSTD string cursor) { pgCursor = cursor; } protected: - int Fetch(const string& num, const string& dir); + int Fetch(PGSTD string num, PGSTD string dir); protected: - string pgCursor; + PGSTD string pgCursor; protected: PgCursor() : PgTransaction() {} // Do not connect @@ -69,4 +77,10 @@ private: PgCursor& operator= (const PgCursor&); }; // End PgCursor Class Declaration + +#ifdef HAVE_NAMESPACE_STD +#undef PGSTD +#endif + #endif // PGCURSORDB_H + diff --git a/src/interfaces/libpq++/pgdatabase.cc b/src/interfaces/libpq++/pgdatabase.cc index 545c71c08d..73eab78862 100644 --- a/src/interfaces/libpq++/pgdatabase.cc +++ b/src/interfaces/libpq++/pgdatabase.cc @@ -10,7 +10,7 @@ * Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/interfaces/libpq++/Attic/pgdatabase.cc,v 1.9 2000/01/29 16:58:52 petere Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpq++/Attic/pgdatabase.cc,v 1.10 2001/05/09 17:29:10 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -18,151 +18,155 @@ #include "pgdatabase.h" -void PgDatabase::DisplayTuples(FILE *out, int fillAlign, - const char* fieldSep, int printHeader, - int /* quiet */) +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 { PQprintOpt po; - memset(&po,0,sizeof(po)); - - po.align = fillAlign; - po.fieldSep = (char *)fieldSep; po.header = printHeader; + po.align = fillAlign; + po.standard = po.html3 = po.expanded = po.pager = 0; + po.fieldSep = const_cast(fieldSep); + po.tableOpt = po.caption = 0; + po.fieldName = 0; PQprint(out,pgResult,&po); } - -void PgDatabase::PrintTuples(FILE *out, int printAttName, int terseOutput, - int width) +// OBSOLESCENT (uses PQprint(), which is no longer being maintained) +void PgDatabase::PrintTuples(FILE *out, + bool printAttName, + bool terseOutput, + bool fillAlign) const { PQprintOpt po; - memset(&po,0,sizeof(po)); - - po.align = width; - - if(terseOutput) po.fieldSep = strdup("|"); - else po.fieldSep = ""; - po.header = printAttName; + po.align = fillAlign; + po.standard = po.html3 = po.expanded = po.pager = 0; + po.tableOpt = po.caption = 0; + po.fieldSep = const_cast(terseOutput ? "" : "|"); + po.fieldName = 0; PQprint(out,pgResult,&po); } -int PgDatabase::Tuples() +int PgDatabase::Tuples() const { return PQntuples(pgResult); } -int PgDatabase::CmdTuples() +int PgDatabase::CmdTuples() const { -char *a; - - a = (char *)PQcmdTuples(pgResult); - if(!a[0]) return -1; - -return atoi(a); +const char *a = PQcmdTuples(pgResult); +return a[0] ? atoi(a) : -1; } +// TODO: Make const? int PgDatabase::Fields() { return PQnfields(pgResult); } -const char* PgDatabase::FieldName(int field_num) +const char* PgDatabase::FieldName(int field_num) const { return PQfname(pgResult, field_num); } -int PgDatabase::FieldNum(const char* field_name) +int PgDatabase::FieldNum(const char* field_name) const { return PQfnumber(pgResult, field_name); } -Oid PgDatabase::FieldType(int field_num) +Oid PgDatabase::FieldType(int field_num) const { return PQftype(pgResult, field_num); } -Oid PgDatabase::FieldType(const char* field_name) +Oid PgDatabase::FieldType(const char* field_name) const { return PQftype(pgResult, FieldNum(field_name)); } -short PgDatabase::FieldSize(int field_num) +short PgDatabase::FieldSize(int field_num) const { return PQfsize(pgResult, field_num); } -short PgDatabase::FieldSize(const char* field_name) +short PgDatabase::FieldSize(const char* field_name) const { return PQfsize(pgResult, FieldNum(field_name)); } -const char* PgDatabase::GetValue(int tup_num, int field_num) +const char* PgDatabase::GetValue(int tup_num, int field_num) const { return PQgetvalue(pgResult, tup_num, field_num); } -const char* PgDatabase::GetValue(int tup_num, const char* field_name) +const char* PgDatabase::GetValue(int tup_num, const char* field_name) const { return PQgetvalue(pgResult, tup_num, FieldNum(field_name)); } -int PgDatabase::GetIsNull(int tup_num, int field_num) +bool PgDatabase::GetIsNull(int tup_num, int field_num) const { return PQgetisnull(pgResult, tup_num, field_num); } -int PgDatabase::GetIsNull(int tup_num, const char* field_name) +bool PgDatabase::GetIsNull(int tup_num, const char* field_name) const { return PQgetisnull(pgResult, tup_num, FieldNum(field_name)); } -int PgDatabase::GetLength(int tup_num, int field_num) +int PgDatabase::GetLength(int tup_num, int field_num) const { return PQgetlength(pgResult, tup_num, field_num); } -int PgDatabase::GetLength(int tup_num, const char* field_name) +int PgDatabase::GetLength(int tup_num, const char* field_name) const { return PQgetlength(pgResult, tup_num, FieldNum(field_name)); } -int PgDatabase::GetLine(char* string, int length) +int PgDatabase::GetLine(char str[], int length) { -return PQgetline(pgConn, string, length); +return PQgetline(pgConn, str, length); } -void PgDatabase::PutLine(const char* string) +void PgDatabase::PutLine(const char str[]) { -PQputline(pgConn, string); +PQputline(pgConn, str); } -const char* PgDatabase::OidStatus() +const char* PgDatabase::OidStatus() const { return PQoidStatus(pgResult); } diff --git a/src/interfaces/libpq++/pgdatabase.h b/src/interfaces/libpq++/pgdatabase.h index 76ed215930..3815a8641e 100644 --- a/src/interfaces/libpq++/pgdatabase.h +++ b/src/interfaces/libpq++/pgdatabase.h @@ -14,7 +14,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * - * $Id: pgdatabase.h,v 1.9 2001/01/24 19:43:32 momjian Exp $ + * $Id: pgdatabase.h,v 1.10 2001/05/09 17:29:10 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -37,35 +37,39 @@ class PgDatabase : public PgConnection { public: // connect to the database with conninfo - PgDatabase(const char* conninfo) : PgConnection(conninfo) {} + explicit PgDatabase(const char* conninfo) : PgConnection(conninfo) {} ~PgDatabase() {} // close connection and clean up + + typedef int size_type; // query result access - int Tuples(); - int CmdTuples(); + size_type Tuples() const; + size_type CmdTuples() const; 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 GetIsNull(int tup_num, int field_num); - int GetIsNull(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) ; + 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; + short FieldSize(int field_num) const; + short 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* string, int length); - void PutLine(const char* string); - const char* OidStatus(); + int GetLine(char str[], int length); + void PutLine(const char str[]); + const char* OidStatus() const; int EndCopy(); protected: diff --git a/src/interfaces/libpq++/pglobject.cc b/src/interfaces/libpq++/pglobject.cc index 5eee243686..0523ec8f91 100644 --- a/src/interfaces/libpq++/pglobject.cc +++ b/src/interfaces/libpq++/pglobject.cc @@ -10,7 +10,7 @@ * Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/interfaces/libpq++/Attic/pglobject.cc,v 1.6 2000/04/22 22:39:15 tgl Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpq++/Attic/pglobject.cc,v 1.7 2001/05/09 17:29:10 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -21,6 +21,10 @@ extern "C" { #include "libpq/libpq-fs.h" } + +using namespace std; + + // **************************************************************** // // PgLargeObject Implementation @@ -142,7 +146,7 @@ int PgLargeObject::LSeek(int offset, int whence) } -int PgLargeObject::Tell() +int PgLargeObject::Tell() const { return lo_tell(pgConn, pgFd); } @@ -160,7 +164,7 @@ int PgLargeObject::Export(const char* filename) } -string PgLargeObject::Status() +string PgLargeObject::Status() const { return loStatus; } diff --git a/src/interfaces/libpq++/pglobject.h b/src/interfaces/libpq++/pglobject.h index 3e3664a1dd..493182e97b 100644 --- a/src/interfaces/libpq++/pglobject.h +++ b/src/interfaces/libpq++/pglobject.h @@ -11,7 +11,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * - * $Id: pglobject.h,v 1.6 2001/01/24 19:43:32 momjian Exp $ + * $Id: pglobject.h,v 1.7 2001/05/09 17:29:10 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -23,6 +23,12 @@ #include "pgconnection.h" #endif +#ifdef HAVE_NAMESPACE_STD +#define PGSTD std:: +#else +#define PGSTD +#endif + // **************************************************************** // @@ -33,12 +39,12 @@ class PgLargeObject : public PgConnection { private: int pgFd; Oid pgObject; - string loStatus; + PGSTD string loStatus; void Init(Oid lobjId = 0); public: - 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 + 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(); @@ -47,12 +53,12 @@ public: int Read(char* buf, int len); int Write(const char* buf, int len); int LSeek(int offset, int whence); - int Tell(); + int Tell() const; int Unlink(); Oid LOid(); Oid Import(const char* filename); int Export(const char* filename); - string Status(); + PGSTD string Status() const; private: // We don't support copying of PgLargeObject objects, @@ -61,4 +67,10 @@ private: PgLargeObject& operator= (const PgLargeObject&); }; + +#ifdef HAVE_NAMESPACE_STD +#undef PGSTD +#endif + + #endif // PGLOBJECT_H diff --git a/src/interfaces/libpq++/pgtransdb.cc b/src/interfaces/libpq++/pgtransdb.cc index 61549d0124..dd7195e1bf 100644 --- a/src/interfaces/libpq++/pgtransdb.cc +++ b/src/interfaces/libpq++/pgtransdb.cc @@ -10,7 +10,7 @@ * Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/interfaces/libpq++/Attic/pgtransdb.cc,v 1.3 1999/05/30 15:17:58 tgl Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpq++/Attic/pgtransdb.cc,v 1.4 2001/05/09 17:29:10 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -25,7 +25,8 @@ // Make a connection to the specified database with default environment // See PQconnectdb() for conninfo usage. PgTransaction::PgTransaction(const char* conninfo) - : PgDatabase(conninfo) + : PgDatabase(conninfo), + pgCommitted(true) { BeginTransaction(); } @@ -33,17 +34,20 @@ PgTransaction::PgTransaction(const char* conninfo) // Destructor: End the transaction block PgTransaction::~PgTransaction() { - EndTransaction(); + if (!pgCommitted) Exec("ABORT"); } // Begin the transaction block ExecStatusType PgTransaction::BeginTransaction() { + pgCommitted = false; return Exec("BEGIN"); } // End BeginTransaction() // Begin the transaction block ExecStatusType PgTransaction::EndTransaction() { + pgCommitted = true; return Exec("END"); } // End EndTransaction() + diff --git a/src/interfaces/libpq++/pgtransdb.h b/src/interfaces/libpq++/pgtransdb.h index d8e616772f..b0f645fd02 100644 --- a/src/interfaces/libpq++/pgtransdb.h +++ b/src/interfaces/libpq++/pgtransdb.h @@ -14,7 +14,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * - * $Id: pgtransdb.h,v 1.6 2001/01/24 19:43:32 momjian Exp $ + * $Id: pgtransdb.h,v 1.7 2001/05/09 17:29:10 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -36,9 +36,9 @@ // the object is destroyed. class PgTransaction : public PgDatabase { public: - PgTransaction(const char* conninfo); // use reasonable & environment defaults + explicit PgTransaction(const char* conninfo); // use reasonable & environment defaults // connect to the database with given environment and database name - // PgTransaction(const PgConnection&); + // explicit PgTransaction(const PgConnection&); ~PgTransaction(); // close connection and clean up protected: @@ -46,9 +46,11 @@ protected: ExecStatusType EndTransaction(); protected: - PgTransaction() : PgDatabase() {} // Do not connect + PgTransaction() : PgDatabase(), pgCommitted(true) {} // Do not connect private: + bool pgCommitted; + // We don't support copying of PgTransaction objects, // so make copy constructor and assignment op private. PgTransaction(const PgTransaction&);