From cdc197cf3100359cd436757adc0002dad07e3117 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Thu, 6 Nov 2008 15:18:36 +0000
Subject: [PATCH] Improve psql's \dC command to take a pattern parameter. 
 Casts are shown if their source or target types match the pattern (using the
 same definition of "match" as \dT does).  Per recent discussion.

---
 doc/src/sgml/ref/psql-ref.sgml |  7 +++++--
 src/bin/psql/describe.c        | 38 ++++++++++++++++++++++++++++------
 src/bin/psql/help.c            |  4 ++--
 3 files changed, 39 insertions(+), 10 deletions(-)

diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index 464cf8ec7f2..2d21ff05a58 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -1,5 +1,5 @@
 <!--
-$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.211 2008/09/06 20:18:08 tgl Exp $
+$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.212 2008/11/06 15:18:35 tgl Exp $
 PostgreSQL documentation
 -->
 
@@ -894,10 +894,13 @@ testdb=&gt;
 
 
       <varlistentry>
-        <term><literal>\dC</literal></term>
+        <term><literal>\dC [ <replaceable class="parameter">pattern</replaceable> ]</literal></term>
         <listitem>
         <para>
         Lists all available type casts.
+        If <replaceable class="parameter">pattern</replaceable>
+        is specified, only casts whose source or target types match the
+        pattern are listed.
         </para>
         </listitem>
       </varlistentry>
diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c
index 09e082d81df..d0ca9a034b6 100644
--- a/src/bin/psql/describe.c
+++ b/src/bin/psql/describe.c
@@ -8,7 +8,7 @@
  *
  * Copyright (c) 2000-2008, PostgreSQL Global Development Group
  *
- * $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.186 2008/11/03 19:08:56 tgl Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.187 2008/11/06 15:18:35 tgl Exp $
  */
 #include "postgres_fe.h"
 
@@ -2082,10 +2082,10 @@ listCasts(const char *pattern)
 
 	initPQExpBuffer(&buf);
 	/*
-	 * We need left join here for binary casts.  Also note that we don't
-	 * attempt to localize '(binary coercible)', because there's too much
-	 * risk of gettext translating a function name that happens to match
-	 * some string in the PO database.
+	 * We need a left join to pg_proc for binary casts; the others are just
+	 * paranoia.  Also note that we don't attempt to localize '(binary
+	 * coercible)', because there's too much risk of gettext translating a
+	 * function name that happens to match some string in the PO database.
 	 */
 	printfPQExpBuffer(&buf,
 			   "SELECT pg_catalog.format_type(castsource, NULL) AS \"%s\",\n"
@@ -2099,13 +2099,39 @@ listCasts(const char *pattern)
 					  "       END as \"%s\"\n"
 				 "FROM pg_catalog.pg_cast c LEFT JOIN pg_catalog.pg_proc p\n"
 					  "     ON c.castfunc = p.oid\n"
-					  "ORDER BY 1, 2",
+					  "     LEFT JOIN pg_catalog.pg_type ts\n"
+					  "     ON c.castsource = ts.oid\n"
+					  "     LEFT JOIN pg_catalog.pg_namespace ns\n"
+					  "     ON ns.oid = ts.typnamespace\n"
+					  "     LEFT JOIN pg_catalog.pg_type tt\n"
+					  "     ON c.casttarget = tt.oid\n"
+					  "     LEFT JOIN pg_catalog.pg_namespace nt\n"
+					  "     ON nt.oid = tt.typnamespace\n"
+					  "WHERE (true",
 					  gettext_noop("Source type"),
 					  gettext_noop("Target type"),
 					  gettext_noop("Function"),
 					  gettext_noop("no"), gettext_noop("in assignment"), gettext_noop("yes"),
 					  gettext_noop("Implicit?"));
 
+	/*
+	 * Match name pattern against either internal or external name of either
+	 * castsource or casttarget
+	 */
+	processSQLNamePattern(pset.db, &buf, pattern, true, false,
+						  "ns.nspname", "ts.typname",
+						  "pg_catalog.format_type(ts.oid, NULL)",
+						  "pg_catalog.pg_type_is_visible(ts.oid)");
+
+	appendPQExpBuffer(&buf, ") OR (true");
+
+	processSQLNamePattern(pset.db, &buf, pattern, true, false,
+						  "nt.nspname", "tt.typname",
+						  "pg_catalog.format_type(tt.oid, NULL)",
+						  "pg_catalog.pg_type_is_visible(tt.oid)");
+
+	appendPQExpBuffer(&buf, ")\nORDER BY 1, 2;");
+
 	res = PSQLexec(buf.data, false);
 	termPQExpBuffer(&buf);
 	if (!res)
diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c
index e21fc150ddc..49db83c03d4 100644
--- a/src/bin/psql/help.c
+++ b/src/bin/psql/help.c
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 2000-2008, PostgreSQL Global Development Group
  *
- * $PostgreSQL: pgsql/src/bin/psql/help.c,v 1.130 2008/08/29 15:52:07 alvherre Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/help.c,v 1.131 2008/11/06 15:18:36 tgl Exp $
  */
 #include "postgres_fe.h"
 
@@ -200,7 +200,7 @@ slashUsage(unsigned short int pager)
 	fprintf(output, _("  \\da [PATTERN]  list aggregate functions\n"));
 	fprintf(output, _("  \\db [PATTERN]  list tablespaces (add \"+\" for more detail)\n"));
 	fprintf(output, _("  \\dc [PATTERN]  list conversions\n"));
-	fprintf(output, _("  \\dC            list casts\n"));
+	fprintf(output, _("  \\dC [PATTERN]  list casts\n"));
 	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"));
-- 
GitLab