Skip to content

Commit 8bb60b6

Browse files
committed
attached is a patch that adds display of the groups a user belongs to to
\du and a \dg command to psql. It's against 7.4beta5. Markus Bertheau <twanger@bluetwanger.de>
1 parent 5e2b99d commit 8bb60b6

File tree

6 files changed

+68
-9
lines changed

6 files changed

+68
-9
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.100 2003/11/29 19:51:39 pgsql Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.101 2003/12/01 22:21:54 momjian Exp $
33
PostgreSQL documentation
44
-->
55

@@ -904,6 +904,17 @@ testdb=>
904904
</varlistentry>
905905

906906

907+
<varlistentry>
908+
<term><literal>\dg [ <replaceable class="parameter">pattern</replaceable> ]</literal></term>
909+
<listitem>
910+
<para>
911+
Lists all database groups or only those that match <replaceable
912+
class="parameter">pattern</replaceable>.
913+
</para>
914+
</listitem>
915+
</varlistentry>
916+
917+
907918
<varlistentry>
908919
<term><literal>\distvS [ <replaceable class="parameter">pattern</replaceable> ]</literal></term>
909920

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 (c) 2000-2003, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.107 2003/12/01 22:14:40 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.108 2003/12/01 22:21:54 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "command.h"
@@ -359,6 +359,9 @@ exec_command(const char *cmd,
359359
case 'f':
360360
success = describeFunctions(pattern, show_verbose);
361361
break;
362+
case 'g':
363+
success = describeGroups(pattern);
364+
break;
362365
case 'l':
363366
success = do_lo_list();
364367
break;

src/bin/psql/describe.c

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2003, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.89 2003/12/01 22:11:06 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.90 2003/12/01 22:21:54 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "describe.h"
@@ -1276,12 +1276,13 @@ describeUsers(const char *pattern)
12761276
" WHEN u.usesuper THEN CAST('%s' AS pg_catalog.text)\n"
12771277
" WHEN u.usecreatedb THEN CAST('%s' AS pg_catalog.text)\n"
12781278
" ELSE CAST('' AS pg_catalog.text)\n"
1279-
" END AS \"%s\"\n"
1279+
" END AS \"%s\",\n"
1280+
" ARRAY(SELECT g.groname FROM pg_catalog.pg_group g WHERE u.usesysid = ANY(g.grolist)) as \"%s\"\n"
12801281
"FROM pg_catalog.pg_user u\n",
12811282
_("User name"), _("User ID"),
12821283
_("superuser, create database"),
12831284
_("superuser"), _("create database"),
1284-
_("Attributes"));
1285+
_("Attributes"), _("Groups"));
12851286

12861287
processNamePattern(&buf, pattern, false, false,
12871288
NULL, "u.usename", NULL, NULL);
@@ -1303,6 +1304,46 @@ describeUsers(const char *pattern)
13031304
}
13041305

13051306

1307+
/*
1308+
* \dg
1309+
*
1310+
* Describes groups.
1311+
*/
1312+
bool
1313+
describeGroups(const char *pattern)
1314+
{
1315+
PQExpBufferData buf;
1316+
PGresult *res;
1317+
printQueryOpt myopt = pset.popt;
1318+
1319+
initPQExpBuffer(&buf);
1320+
1321+
printfPQExpBuffer(&buf,
1322+
"SELECT g.groname AS \"%s\",\n"
1323+
" g.grosysid AS \"%s\"\n"
1324+
"FROM pg_catalog.pg_group g\n",
1325+
_("Group name"), _("Group ID"));
1326+
1327+
processNamePattern(&buf, pattern, false, false,
1328+
NULL, "g.groname", NULL, NULL);
1329+
1330+
appendPQExpBuffer(&buf, "ORDER BY 1;");
1331+
1332+
res = PSQLexec(buf.data, false);
1333+
termPQExpBuffer(&buf);
1334+
if (!res)
1335+
return false;
1336+
1337+
myopt.nullPrint = NULL;
1338+
myopt.title = _("List of database groups");
1339+
1340+
printQuery(res, &myopt, pset.queryFout);
1341+
1342+
PQclear(res);
1343+
return true;
1344+
}
1345+
1346+
13061347
/*
13071348
* listTables()
13081349
*

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 (c) 2000-2003, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/describe.h,v 1.22 2003/11/29 19:52:06 pgsql Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/describe.h,v 1.23 2003/12/01 22:21:54 momjian Exp $
77
*/
88
#ifndef DESCRIBE_H
99
#define DESCRIBE_H
@@ -25,6 +25,9 @@ bool describeOperators(const char *pattern);
2525
/* \du */
2626
bool describeUsers(const char *pattern);
2727

28+
/* \dg */
29+
bool describeGroups(const char *pattern);
30+
2831
/* \z (or \dp) */
2932
bool permissionsList(const char *pattern);
3033

src/bin/psql/help.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2003, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/help.c,v 1.82 2003/11/29 19:52:06 pgsql Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/help.c,v 1.83 2003/12/01 22:21:54 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "common.h"
@@ -216,6 +216,7 @@ slashUsage(unsigned short int pager)
216216
fprintf(output, _(" \\dd [PATTERN] show comment for object\n"));
217217
fprintf(output, _(" \\dD [PATTERN] list domains\n"));
218218
fprintf(output, _(" \\df [PATTERN] list functions (add \"+\" for more detail)\n"));
219+
fprintf(output, _(" \\dg [PATTERN] list groups\n"));
219220
fprintf(output, _(" \\dn [PATTERN] list schemas\n"));
220221
fprintf(output, _(" \\do [NAME] list operators\n"));
221222
fprintf(output, _(" \\dl list large objects, same as \\lo_list\n"));

src/bin/psql/tab-complete.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2003, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/tab-complete.c,v 1.96 2003/12/01 22:14:40 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/tab-complete.c,v 1.97 2003/12/01 22:21:54 momjian Exp $
77
*/
88

99
/*----------------------------------------------------------------------
@@ -576,7 +576,7 @@ psql_completion(char *text, int start, int end)
576576

577577
static const char * const backslash_commands[] = {
578578
"\\a", "\\connect", "\\C", "\\cd", "\\copy", "\\copyright",
579-
"\\d", "\\da", "\\dc", "\\dC", "\\dd", "\\dD", "\\df", "\\di",
579+
"\\d", "\\da", "\\dc", "\\dC", "\\dd", "\\dD", "\\df", "\\dg", "\\di",
580580
"\\dl", "\\dn", "\\do", "\\dp", "\\ds", "\\dS", "\\dt", "\\dT",
581581
"\\dv", "\\du",
582582
"\\e", "\\echo", "\\encoding",

0 commit comments

Comments
 (0)