Patch: psql \whoami option

David Christensen <david@endpoint.com>

From: David Christensen <david@endpoint.com>
To: pgsql-hackers@postgresql.org
Date: 2010-01-26T23:24:14Z
Lists: pgsql-hackers
-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@endpoint.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 */