Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a79b698

Browse files
committed
Here's a version of my suggested diffs transplanted to 7.1 beta 5. I'm still looking at the best way to integrate Tom Vijlbrief's fixes (insofar as they're still needed); would 7.2 be a suitable time for incompatible API changes? Jeroen Changes: (*) Introduced bool, true, false (replacing some int, 1, 0) (*) Made some member functions const (*) Documented GetIsNull() (*) Marked DisplayTuples() and PrintTuples() as obsolescent; fixed possible portability problem (assumed that NULL pointer equals all-zero bit pattern) (*) PrintTuples(): renamed width parameter to fillAlign to conform with other usage; fixed memory leak and compile issue w.r.t. field separator (should also slightly improve performance) (*) Fixed some minor compilation issues (*) Moved "using namespace std;" out of headers, where they didn't belong; used new (temporary) preprocessor macro PGSTD to do this (*) Made ToString() static, removed unneeded memset(), made buffer size adapt to sizeof(int) (*) Made some constructors explicit (*) Changed some const std::string & parameters to plain std::string (*) Marked PgCursor::Cursor(std::string) as obsolescent (setter with same name as getter--bad style) (*) Renamed some paramaters previously named "string" (*) Introduced size_type typedef for number of tuples in result set (*) PgTransaction now supports re-opening after closing, and aborts if not explicitly committed prior to destruction J. T. Vermeulen
1 parent c8db55f commit a79b698

File tree

18 files changed

+338
-146
lines changed

18 files changed

+338
-146
lines changed

doc/src/sgml/libpq++.sgml

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/Attic/libpq++.sgml,v 1.28 2001/05/01 10:48:57 petere Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/Attic/libpq++.sgml,v 1.29 2001/05/09 17:29:09 momjian Exp $
33
-->
44

55
<chapter id="libpqplusplus">
@@ -227,9 +227,9 @@ $Header: /cvsroot/pgsql/doc/src/sgml/Attic/libpq++.sgml,v 1.28 2001/05/01 10:48:
227227
returns whether or not the connection to the backend server succeeded or
228228
failed.
229229
<synopsis>
230-
int PgConnection::ConnectionBad()
230+
bool PgConnection::ConnectionBad() const
231231
</synopsis>
232-
Returns TRUE if the connection failed.
232+
Returns true if the connection failed.
233233
</para>
234234
</listitem>
235235
<listitem>
@@ -368,7 +368,7 @@ $Header: /cvsroot/pgsql/doc/src/sgml/Attic/libpq++.sgml,v 1.28 2001/05/01 10:48:
368368
<function>Tuples</function>
369369
Returns the number of tuples (rows) in the query result.
370370
<synopsis>
371-
int PgDatabase::Tuples()
371+
int PgDatabase::Tuples() const
372372
</synopsis>
373373
</para>
374374
</listitem>
@@ -387,7 +387,7 @@ $Header: /cvsroot/pgsql/doc/src/sgml/Attic/libpq++.sgml,v 1.28 2001/05/01 10:48:
387387
Returns the field (attribute) name associated with the given field index.
388388
Field indices start at 0.
389389
<synopsis>
390-
const char *PgDatabase::FieldName(int field_num)
390+
const char *PgDatabase::FieldName(int field_num) const
391391
</synopsis>
392392
</para>
393393
</listitem>
@@ -397,7 +397,7 @@ $Header: /cvsroot/pgsql/doc/src/sgml/Attic/libpq++.sgml,v 1.28 2001/05/01 10:48:
397397
PQfnumber Returns the field (attribute) index associated with
398398
the given field name.
399399
<synopsis>
400-
int PgDatabase::FieldNum(const char* field_name)
400+
int PgDatabase::FieldNum(const char* field_name) const
401401
</synopsis>
402402
-1 is returned if the given name does not match any field.
403403
</para>
@@ -409,7 +409,7 @@ $Header: /cvsroot/pgsql/doc/src/sgml/Attic/libpq++.sgml,v 1.28 2001/05/01 10:48:
409409
integer returned is an internal coding of the type. Field indices
410410
start at 0.
411411
<synopsis>
412-
Oid PgDatabase::FieldType(int field_num)
412+
Oid PgDatabase::FieldType(int field_num) const
413413
</synopsis>
414414
</para>
415415
</listitem>
@@ -420,7 +420,7 @@ $Header: /cvsroot/pgsql/doc/src/sgml/Attic/libpq++.sgml,v 1.28 2001/05/01 10:48:
420420
integer returned is an internal coding of the type. Field indices
421421
start at 0.
422422
<synopsis>
423-
Oid PgDatabase::FieldType(const char* field_name)
423+
Oid PgDatabase::FieldType(const char* field_name) const
424424
</synopsis>
425425
</para>
426426
</listitem>
@@ -430,7 +430,7 @@ $Header: /cvsroot/pgsql/doc/src/sgml/Attic/libpq++.sgml,v 1.28 2001/05/01 10:48:
430430
Returns the size in bytes of the field associated with the given
431431
field index. Field indices start at 0.
432432
<synopsis>
433-
short PgDatabase::FieldSize(int field_num)
433+
short PgDatabase::FieldSize(int field_num) const
434434
</synopsis>
435435
Returns the space allocated for this field in a database tuple given
436436
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:
444444
Returns the size in bytes of the field associated with the given
445445
field index. Field indices start at 0.
446446
<synopsis>
447-
short PgDatabase::FieldSize(const char *field_name)
447+
short PgDatabase::FieldSize(const char *field_name) const
448448
</synopsis>
449449
Returns the space allocated for this field in a database tuple given
450450
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:
466466
Returns a single field (attribute) value of one tuple of a PGresult.
467467
Tuple and field indices start at 0.
468468
<synopsis>
469-
const char *PgDatabase::GetValue(int tup_num, int field_num)
469+
const char *PgDatabase::GetValue(int tup_num, int field_num) const
470470
</synopsis>
471471
For most queries, the value returned by GetValue is a null-terminated
472472
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:
486486
Returns a single field (attribute) value of one tuple of a PGresult.
487487
Tuple and field indices start at 0.
488488
<synopsis>
489-
const char *PgDatabase::GetValue(int tup_num, const char *field_name)
489+
const char *PgDatabase::GetValue(int tup_num, const char *field_name) const
490490
</synopsis>
491491
For most queries, the value returned by GetValue is a null-terminated
492492
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:
506506
Returns the length of a field (attribute) in bytes. Tuple and field
507507
indices start at 0.
508508
<synopsis>
509-
int PgDatabase::GetLength(int tup_num, int field_num)
509+
int PgDatabase::GetLength(int tup_num, int field_num) const
510510
</synopsis>
511511
This is the actual data length for the particular data value, that
512512
is the size of the object pointed to by GetValue. Note that for
@@ -520,33 +520,55 @@ $Header: /cvsroot/pgsql/doc/src/sgml/Attic/libpq++.sgml,v 1.28 2001/05/01 10:48:
520520
Returns the length of a field (attribute) in bytes. Tuple and field
521521
indices start at 0.
522522
<synopsis>
523-
int PgDatabase::GetLength(int tup_num, const char* field_name)
523+
int PgDatabase::GetLength(int tup_num, const char* field_name) const
524524
</synopsis>
525525
This is the actual data length for the particular data value, that
526526
is the size of the object pointed to by GetValue. Note that for
527527
ASCII-represented values, this size has little to do with the binary
528528
size reported by PQfsize.
529529
</para>
530530
</listitem>
531+
<listitem>
532+
<para>
533+
<function>GetIsNull</function>
534+
Returns whether a field has the null value.
535+
<synopsis>
536+
bool GetIsNull(int tup_num, int field_num) const
537+
</synopsis>
538+
Note that GetValue will return the empty string for null fields, not
539+
the NULL pointer.
540+
</para>
541+
</listitem>
542+
<listitem>
543+
<para>
544+
<function>GetIsNull</function>
545+
Returns whether a field has the null value.
546+
<synopsis>
547+
bool GetIsNull(int tup_num, const char *field_name) const
548+
</synopsis>
549+
Note that GetValue will return the empty string for null fields, not
550+
the NULL pointer.
551+
</para>
552+
</listitem>
531553
<listitem>
532554
<para>
533555
<function>DisplayTuples</function>
534-
Prints out all the tuples and, optionally, the attribute names to the
556+
OBSOLESCENT: Prints out all the tuples and, optionally, the attribute names to the
535557
specified output stream.
536558
<synopsis>
537-
void PgDatabase::DisplayTuples(FILE *out = 0, int fillAlign = 1,
538-
const char* fieldSep = "|",int printHeader = 1, int quiet = 0)
559+
void PgDatabase::DisplayTuples(FILE *out = 0, bool fillAlign = true,
560+
const char* fieldSep = "|",bool printHeader = true, bool quiet = false) const
539561
</synopsis>
540562
</para>
541563
</listitem>
542564
<listitem>
543565
<para>
544566
<function>PrintTuples</function>
545-
Prints out all the tuples and, optionally, the attribute names to the
567+
OBSOLESCENT: Prints out all the tuples and, optionally, the attribute names to the
546568
specified output stream.
547569
<synopsis>
548-
void PgDatabase::PrintTuples(FILE *out = 0, int printAttName = 1,
549-
int terseOutput = 0, int width = 0)
570+
void PgDatabase::PrintTuples(FILE *out = 0, bool printAttName = true,
571+
bool terseOutput = false, bool fillAlign = false) const
550572
</synopsis>
551573
</para>
552574
</listitem>
@@ -563,7 +585,7 @@ $Header: /cvsroot/pgsql/doc/src/sgml/Attic/libpq++.sgml,v 1.28 2001/05/01 10:48:
563585
Returns the number of rows affected after an INSERT, UPDATE or DELETE.
564586
If the command was anything else, it returns -1.
565587
<synopsis>
566-
int PgDatabase::CmdTuples()
588+
int PgDatabase::CmdTuples() const
567589
</synopsis>
568590
</para>
569591
</listitem>
@@ -572,7 +594,7 @@ $Header: /cvsroot/pgsql/doc/src/sgml/Attic/libpq++.sgml,v 1.28 2001/05/01 10:48:
572594
<para>
573595
<function>OidStatus</function>
574596
<synopsis>
575-
const char *PgDatabase::OidStatus()
597+
const char *PgDatabase::OidStatus() const
576598
</synopsis>
577599
</para>
578600
</listitem>
@@ -650,8 +672,8 @@ $Header: /cvsroot/pgsql/doc/src/sgml/Attic/libpq++.sgml,v 1.28 2001/05/01 10:48:
650672
to see if any notification data is currently available from the backend.
651673
<function>PgDatabase::Notifies</function>
652674
returns the notification from a list of unhandled notifications from the
653-
backend. The function eturns NULL if there is no pending notifications from the
654-
backend.
675+
backend. The function returns NULL if there are no pending notifications
676+
from the backend.
655677
<function>PgDatabase::Notifies</function>
656678
behaves like the popping of a stack. Once a notification is returned
657679
from <function>PgDatabase::Notifies</function>,

doc/src/sgml/ref/psql-ref.sgml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.50 2001/05/07 19:31:33 petere Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.51 2001/05/09 17:29:10 momjian Exp $
33
Postgres documentation
44
-->
55

@@ -494,6 +494,16 @@ testdb=>
494494
</varlistentry>
495495

496496

497+
<varlistentry>
498+
<term><literal>\du [ <replaceable class="parameter">pattern</replaceable> ]</literal></term>
499+
<listitem>
500+
<para>
501+
Lists all configured users or only those that match <replaceable class="parameter">pattern</replaceable>.
502+
</para>
503+
</listitem>
504+
</varlistentry>
505+
506+
497507
<varlistentry>
498508
<term><literal>\edit</literal> (or <literal>\e</literal>) [ <replaceable class="parameter">filename</replaceable> ]</term>
499509

src/bin/psql/command.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.51 2001/05/07 19:31:33 petere Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.52 2001/05/09 17:29:10 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "command.h"
@@ -357,6 +357,9 @@ exec_command(const char *cmd,
357357
case 'S':
358358
success = listTables(&cmd[1], name, show_verbose);
359359
break;
360+
case 'u':
361+
success = describeUsers(name);
362+
break;
360363
default:
361364
status = CMD_UNKNOWN;
362365
}

src/bin/psql/describe.c

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/describe.c,v 1.28 2001/03/22 04:00:19 momjian Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/describe.c,v 1.29 2001/05/09 17:29:10 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "describe.h"
@@ -890,6 +890,103 @@ describeTableDetails(const char *name, bool desc)
890890
}
891891

892892

893+
/*
894+
* describeUsers()
895+
*
896+
* \du [user]
897+
*
898+
* Describes users, possibly based on a simplistic prefix search on the
899+
* argument.
900+
*/
901+
902+
bool
903+
describeUsers (const char *name)
904+
{
905+
char buf[384 + REGEXP_CUTOFF];
906+
PGresult *res;
907+
printTableOpt myopt = pset.popt.topt;
908+
int i;
909+
char *title;
910+
const char *headers[4];
911+
char **cells = NULL;
912+
unsigned int cols;
913+
914+
/*
915+
* All we want to know is the user names and permissions
916+
* for the system.
917+
*/
918+
919+
title = "List of Users";
920+
921+
cols = 0;
922+
headers[cols++] = "User Name";
923+
headers[cols++] = "User ID";
924+
headers[cols++] = "Attributes";
925+
headers[cols] = NULL;
926+
927+
strcpy(buf,
928+
"SELECT u.usename AS \"User Name\"\n
929+
, u.usesysid AS \"User ID\"\n
930+
, u.usesuper AS \"Super User\"\n
931+
, u.usecreatedb AS \"Create DB\"\n
932+
FROM pg_user u\n");
933+
if (name)
934+
{
935+
strcat(buf, " WHERE u.usename ~ '^");
936+
strncat(buf, name, REGEXP_CUTOFF);
937+
strcat(buf, "'\n");
938+
}
939+
strcat(buf, "ORDER BY \"User Name\"\n");
940+
941+
res = PSQLexec(buf);
942+
if (!res)
943+
return false;
944+
945+
cells = xmalloc((PQntuples(res) * cols + 1) * sizeof(*cells));
946+
cells[PQntuples(res) * cols] = NULL;
947+
948+
for (i = 0; i < PQntuples(res); i++)
949+
{
950+
char createuser[2] = "";
951+
char createdb[2] = "";
952+
953+
/* Name */
954+
cells[i * cols + 0] = PQgetvalue(res, i, 0);
955+
956+
/* ID */
957+
cells[i * cols + 1] = PQgetvalue(res, i, 1);
958+
959+
/* Super */
960+
strcpy(createuser, PQgetvalue(res, i, 2));
961+
962+
/* Create DB */
963+
strcpy(createdb, PQgetvalue(res, i, 3));
964+
965+
cells[i * cols + 2] = xmalloc((strlen("create user, create DB") * sizeof(char)) + 1);
966+
strcpy(cells[i * cols + 2], "");
967+
968+
if (strcmp(createuser, "t") == 0)
969+
strcat(cells[i * cols + 2], "create user");
970+
971+
if (strcmp(createdb, "t") == 0) {
972+
if (strcmp(createuser, "t") == 0)
973+
strcat(cells[i * cols + 2], ", ");
974+
strcat(cells[i * cols + 2], "create DB");
975+
}
976+
}
977+
978+
printTable(title, headers,
979+
(const char **) cells,
980+
NULL,
981+
"lll", &myopt, pset.queryFout);
982+
983+
/* clean up */
984+
free(cells);
985+
986+
PQclear(res);
987+
return true;
988+
}
989+
893990

894991
/*
895992
* listTables()

src/bin/psql/describe.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/describe.h,v 1.9 2000/04/12 17:16:22 momjian Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/describe.h,v 1.10 2001/05/09 17:29:10 momjian Exp $
77
*/
88
#ifndef DESCRIBE_H
99
#define DESCRIBE_H
@@ -22,6 +22,9 @@ bool describeTypes(const char *name, bool verbose);
2222
/* \do */
2323
bool describeOperators(const char *name);
2424

25+
/* \du */
26+
bool describeUsers(const char *name);
27+
2528
/* \z (or \dp) */
2629
bool permissionsList(const char *name);
2730

src/interfaces/libpq++/examples/testlibpq4.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ INSERT INTO TBL1 values (10);
2222
#include "libpq++.h"
2323
#include <stdlib.h>
2424

25-
main()
25+
int main()
2626
{
2727
// Begin, by connecting to the backend using hardwired constants
2828
// and a test database created by the user prior to the invokation
2929
// of this test program.
30-
char* dbName = "dbname=template1";
30+
const char* dbName = "dbname=template1";
3131
PgDatabase data(dbName);
3232

3333
// Check to see that the backend connection was successfully made

0 commit comments

Comments
 (0)