Re: Patch: psql \whoami option

From: Bruce Momjian <bruce(at)momjian(dot)us>
To: David Christensen <david(at)endpoint(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Patch: psql \whoami option
Date: 2010-02-05 17:45:09
Message-ID: 201002051745.o15Hj9C10846@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers


I have added this patch to the next commit-fest. Thanks:

https://commitfest.postgresql.org/action/commitfest_view?id=6

---------------------------------------------------------------------------

David Christensen wrote:
> -hackers,
>
> In the spirit of small, but hopefully useful interface improvement
> patches, enclosed for your review is a patch for providing psql with a
> \whoami command (maybe a better name is \conninfo or similar). Its
> purpose is to print information about the current connection, by
> default in a human-readable format. There is also an optional format
> parameter which currently accepts 'dsn' as an option to output the
> current connection information as a DSN.
>
> Example output:
>
> $psql -d postgres -p 8555
> psql (8.5devel)
> You are now connected to database "postgres".
>
> [Tue Jan 26 17:17:31 CST 2010]
> machack:postgres:8555=# \whoami
> Connected to database: "postgres", user: "machack", port: "8555"
> via local domain socket
>
> [Tue Jan 26 17:17:34 CST 2010]
> machack:postgres:8555=# \c - - localhost 8555
> psql (8.5devel)
> You are now connected to database "postgres" on host "localhost".
>
> [Tue Jan 26 17:17:42 CST 2010]
> machack:postgres:8555=# \whoami
> Connected to database: "postgres", user: "machack", host:
> "localhost", port: "8555"
>
> [Tue Jan 26 17:17:46 CST 2010]
> machack:postgres:8555=# \whoami dsn
> dbname=postgres;user=machack;host=localhost;port=8555
>
> [Tue Jan 26 17:19:02 CST 2010]
> machack:postgres:8555=# \q
>
> Regards,
>
> David
> --
> David Christensen
> End Point Corporation
> david(at)endpoint(dot)com
>
>
>
> diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-
> ref.sgml
> index 3ce5996..b58b24d 100644
> *** a/doc/src/sgml/ref/psql-ref.sgml
> --- b/doc/src/sgml/ref/psql-ref.sgml
> *************** lo_import 152801
> *** 2149,2154 ****
> --- 2149,2167 ----
>
>
> <varlistentry>
> + <term><literal>\whoami</literal> [ <replaceable
> class="parameter">default</replaceable> | <replaceable
> class="parameter">dsn</replaceable> ] </term>
> + <listitem>
> + <para>
> + Outputs connection information about the current database
> + connection. When passed parameter <literal>dsn</literal>,
> + outputs as a DSN. If parameter is unspecified or
> + unrecognized, outputs in a human-readable format.
> + </para>
> + </listitem>
> + </varlistentry>
> +
> +
> + <varlistentry>
> <term><literal>\x</literal></term>
> <listitem>
> <para>
> diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
> index 5188b18..21b2468 100644
> *** a/src/bin/psql/command.c
> --- b/src/bin/psql/command.c
> *************** exec_command(const char *cmd,
> *** 1106,1111 ****
> --- 1106,1156 ----
> free(fname);
> }
>
> + /* \whoami -- display information about the current connection */
> + else if (strcmp(cmd, "whoami") == 0)
> + {
> + char *format = psql_scan_slash_option(scan_state,
> + OT_NORMAL, NULL, true);
> + char *host = PQhost(pset.db);
> +
> + if (format && !pg_strcasecmp(format, "dsn")) {
> + if (host) {
> + printf("dbname=%s;user=%s;host=%s;port=%s\n",
> + PQdb(pset.db),
> + PQuser(pset.db),
> + host,
> + PQport(pset.db)
> + );
> + }
> + else {
> + printf("dbname=%s;user=%s;port=%s\n",
> + PQdb(pset.db),
> + PQuser(pset.db),
> + PQport(pset.db)
> + );
> + }
> + }
> + else {
> + /* default case */
> + if (host) {
> + printf("Connected to database: \"%s\", user: \"%s\", host: \"%s
> \", port: \"%s\"\n",
> + PQdb(pset.db),
> + PQuser(pset.db),
> + host,
> + PQport(pset.db)
> + );
> + }
> + else {
> + printf("Connected to database: \"%s\", user: \"%s\", port: \"%s
> \" via local domain socket\n",
> + PQdb(pset.db),
> + PQuser(pset.db),
> + PQport(pset.db)
> + );
> + }
> + }
> + free(format);
> + }
> +
> /* \x -- toggle expanded table representation */
> else if (strcmp(cmd, "x") == 0)
> {
> diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c
> index 6037351..802b76d 100644
> *** a/src/bin/psql/help.c
> --- b/src/bin/psql/help.c
> *************** slashUsage(unsigned short int pager)
> *** 249,254 ****
> --- 249,256 ----
> PQdb(pset.db));
> fprintf(output, _(" \\encoding [ENCODING] show or set client
> encoding\n"));
> fprintf(output, _(" \\password [USERNAME] securely change the
> password for a user\n"));
> + fprintf(output, _(" \\whoami [FORMAT] display information
> about current connection\n"
> + " (FORMAT := {default|
> dsn})\n"));
> fprintf(output, "\n");
>
> fprintf(output, _("Operating System\n"));
> diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
> index cb2ae9a..952d2bc 100644
> *** a/src/bin/psql/tab-complete.c
> --- b/src/bin/psql/tab-complete.c
> *************** psql_completion(char *text, int start, i
> *** 635,641 ****
> "\\lo_import", "\\lo_export", "\\lo_list", "\\lo_unlink",
> "\\o", "\\p", "\\password", "\\prompt", "\\pset", "\\q", "\
> \qecho", "\\r",
> "\\set", "\\t", "\\T",
> ! "\\timing", "\\unset", "\\x", "\\w", "\\z", "\\!", NULL
> };
>
> (void) end; /* not used */
> --- 635,641 ----
> "\\lo_import", "\\lo_export", "\\lo_list", "\\lo_unlink",
> "\\o", "\\p", "\\password", "\\prompt", "\\pset", "\\q", "\
> \qecho", "\\r",
> "\\set", "\\t", "\\T",
> ! "\\timing", "\\unset", "\\x", "\\w", "\\whoami", "\\z", "\\!", NULL
> };
>
> (void) end; /* not used */
>
>
> --
> Sent via pgsql-hackers mailing list (pgsql-hackers(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-hackers

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ If your life is a hard drive, Christ can be your backup. +

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Bruce Momjian 2010-02-05 18:12:51 Re: plperl db access documentation enhancement
Previous Message Markus Wanner 2010-02-05 17:29:53 Re: synchronized snapshots