From 8bb60b6423f21de03bdb438d8666dd86824c6266 Mon Sep 17 00:00:00 2001 From: Bruce Momjian Date: Mon, 1 Dec 2003 22:21:54 +0000 Subject: [PATCH] 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 --- doc/src/sgml/ref/psql-ref.sgml | 13 +++++++++- src/bin/psql/command.c | 5 +++- src/bin/psql/describe.c | 47 +++++++++++++++++++++++++++++++--- src/bin/psql/describe.h | 5 +++- src/bin/psql/help.c | 3 ++- src/bin/psql/tab-complete.c | 4 +-- 6 files changed, 68 insertions(+), 9 deletions(-) diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml index cae763af0b..b398251745 100644 --- a/doc/src/sgml/ref/psql-ref.sgml +++ b/doc/src/sgml/ref/psql-ref.sgml @@ -1,5 +1,5 @@ @@ -904,6 +904,17 @@ testdb=> + + \dg [ pattern ] + + + Lists all database groups or only those that match pattern. + + + + + \distvS [ pattern ] diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index fda770f9dc..67d0ad7b3b 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -3,7 +3,7 @@ * * Copyright (c) 2000-2003, PostgreSQL Global Development Group * - * $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.107 2003/12/01 22:14:40 momjian Exp $ + * $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.108 2003/12/01 22:21:54 momjian Exp $ */ #include "postgres_fe.h" #include "command.h" @@ -359,6 +359,9 @@ exec_command(const char *cmd, case 'f': success = describeFunctions(pattern, show_verbose); break; + case 'g': + success = describeGroups(pattern); + break; case 'l': success = do_lo_list(); break; diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c index 327dd69a3c..66a7d7b5d2 100644 --- a/src/bin/psql/describe.c +++ b/src/bin/psql/describe.c @@ -3,7 +3,7 @@ * * Copyright (c) 2000-2003, PostgreSQL Global Development Group * - * $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.89 2003/12/01 22:11:06 momjian Exp $ + * $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.90 2003/12/01 22:21:54 momjian Exp $ */ #include "postgres_fe.h" #include "describe.h" @@ -1276,12 +1276,13 @@ describeUsers(const char *pattern) " WHEN u.usesuper THEN CAST('%s' AS pg_catalog.text)\n" " WHEN u.usecreatedb THEN CAST('%s' AS pg_catalog.text)\n" " ELSE CAST('' AS pg_catalog.text)\n" - " END AS \"%s\"\n" + " END AS \"%s\",\n" + " ARRAY(SELECT g.groname FROM pg_catalog.pg_group g WHERE u.usesysid = ANY(g.grolist)) as \"%s\"\n" "FROM pg_catalog.pg_user u\n", _("User name"), _("User ID"), _("superuser, create database"), _("superuser"), _("create database"), - _("Attributes")); + _("Attributes"), _("Groups")); processNamePattern(&buf, pattern, false, false, NULL, "u.usename", NULL, NULL); @@ -1303,6 +1304,46 @@ describeUsers(const char *pattern) } +/* + * \dg + * + * Describes groups. + */ +bool +describeGroups(const char *pattern) +{ + PQExpBufferData buf; + PGresult *res; + printQueryOpt myopt = pset.popt; + + initPQExpBuffer(&buf); + + printfPQExpBuffer(&buf, + "SELECT g.groname AS \"%s\",\n" + " g.grosysid AS \"%s\"\n" + "FROM pg_catalog.pg_group g\n", + _("Group name"), _("Group ID")); + + processNamePattern(&buf, pattern, false, false, + NULL, "g.groname", NULL, NULL); + + appendPQExpBuffer(&buf, "ORDER BY 1;"); + + res = PSQLexec(buf.data, false); + termPQExpBuffer(&buf); + if (!res) + return false; + + myopt.nullPrint = NULL; + myopt.title = _("List of database groups"); + + printQuery(res, &myopt, pset.queryFout); + + PQclear(res); + return true; +} + + /* * listTables() * diff --git a/src/bin/psql/describe.h b/src/bin/psql/describe.h index e5c9460e9f..acc841a02b 100644 --- a/src/bin/psql/describe.h +++ b/src/bin/psql/describe.h @@ -3,7 +3,7 @@ * * Copyright (c) 2000-2003, PostgreSQL Global Development Group * - * $PostgreSQL: pgsql/src/bin/psql/describe.h,v 1.22 2003/11/29 19:52:06 pgsql Exp $ + * $PostgreSQL: pgsql/src/bin/psql/describe.h,v 1.23 2003/12/01 22:21:54 momjian Exp $ */ #ifndef DESCRIBE_H #define DESCRIBE_H @@ -25,6 +25,9 @@ bool describeOperators(const char *pattern); /* \du */ bool describeUsers(const char *pattern); +/* \dg */ +bool describeGroups(const char *pattern); + /* \z (or \dp) */ bool permissionsList(const char *pattern); diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c index 168ddf3716..f36c71a56d 100644 --- a/src/bin/psql/help.c +++ b/src/bin/psql/help.c @@ -3,7 +3,7 @@ * * Copyright (c) 2000-2003, PostgreSQL Global Development Group * - * $PostgreSQL: pgsql/src/bin/psql/help.c,v 1.82 2003/11/29 19:52:06 pgsql Exp $ + * $PostgreSQL: pgsql/src/bin/psql/help.c,v 1.83 2003/12/01 22:21:54 momjian Exp $ */ #include "postgres_fe.h" #include "common.h" @@ -216,6 +216,7 @@ slashUsage(unsigned short int pager) fprintf(output, _(" \\dd [PATTERN] show comment for object\n")); fprintf(output, _(" \\dD [PATTERN] list domains\n")); fprintf(output, _(" \\df [PATTERN] list functions (add \"+\" for more detail)\n")); + fprintf(output, _(" \\dg [PATTERN] list groups\n")); fprintf(output, _(" \\dn [PATTERN] list schemas\n")); fprintf(output, _(" \\do [NAME] list operators\n")); fprintf(output, _(" \\dl list large objects, same as \\lo_list\n")); diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index b6ae2ce865..955e70deb8 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -3,7 +3,7 @@ * * Copyright (c) 2000-2003, PostgreSQL Global Development Group * - * $PostgreSQL: pgsql/src/bin/psql/tab-complete.c,v 1.96 2003/12/01 22:14:40 momjian Exp $ + * $PostgreSQL: pgsql/src/bin/psql/tab-complete.c,v 1.97 2003/12/01 22:21:54 momjian Exp $ */ /*---------------------------------------------------------------------- @@ -576,7 +576,7 @@ psql_completion(char *text, int start, int end) static const char * const backslash_commands[] = { "\\a", "\\connect", "\\C", "\\cd", "\\copy", "\\copyright", - "\\d", "\\da", "\\dc", "\\dC", "\\dd", "\\dD", "\\df", "\\di", + "\\d", "\\da", "\\dc", "\\dC", "\\dd", "\\dD", "\\df", "\\dg", "\\di", "\\dl", "\\dn", "\\do", "\\dp", "\\ds", "\\dS", "\\dt", "\\dT", "\\dv", "\\du", "\\e", "\\echo", "\\encoding",