]> granicus.if.org Git - postgresql/commitdiff
TODO marked as done:
authorBruce Momjian <bruce@momjian.us>
Thu, 12 Dec 2002 21:02:25 +0000 (21:02 +0000)
committerBruce Momjian <bruce@momjian.us>
Thu, 12 Dec 2002 21:02:25 +0000 (21:02 +0000)
* Add schema, cast, and conversion backslash commands to psql

I had to create a new publically available function,
pg_conversion_is_visible, as it seemed to be missing from the catalogs.
This required me to do no small amount of hacking around in namespace.c

I have updated the \? help and sgml docs.

\dc - list conversions [PATTERN]
\dC - list casts
\dn list schemas

I didn't support patterns with casts as there's nothing obvious to match
against.

Catalog version incremented --- initdb required.

Christopher Kings-Lynne

doc/src/sgml/ref/psql-ref.sgml
src/backend/catalog/namespace.c
src/bin/psql/command.c
src/bin/psql/describe.c
src/bin/psql/describe.h
src/bin/psql/help.c
src/include/catalog/catversion.h
src/include/catalog/namespace.h
src/include/catalog/pg_proc.h

index bb8d8f5eb41cc6890c2cd9317cb35c57b1558515..66a92802baea090dc1a71062e62e800630ad2625 100644 (file)
@@ -1,5 +1,5 @@
 <!--
-$Header: /cvsroot/pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.80 2002/11/08 19:12:21 momjian Exp $
+$Header: /cvsroot/pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.81 2002/12/12 21:02:19 momjian Exp $
 PostgreSQL documentation
 -->
 
@@ -851,6 +851,29 @@ testdb=>
       </varlistentry>
 
 
+      <varlistentry>
+        <term><literal>\dc</literal> [ <replaceable class="parameter">pattern</replaceable> ]</term>
+        <listitem>
+        <para>
+        Lists all available conversions (between encodings). If <replaceable
+        class="parameter">pattern</replaceable>
+        is specified, only matching conversions are shown.
+        </para>
+        </listitem>
+      </varlistentry>
+
+
+      <varlistentry>
+        <term><literal>\dC</literal></term>
+        <listitem>
+        <para>
+        Lists all available type casts.  Casts can be explicit, explicit and assignment
+       or implicit, and are used to change a variable from one type to another.
+        </para>
+        </listitem>
+      </varlistentry>
+
+
       <varlistentry>
         <term><literal>\df [ <replaceable class="parameter">pattern</replaceable> ]</literal></term>
 
index e7fd1c1e5c016a221880fb0401bf7429b85bd0d6..fa0d20af307da781da711944fb9e5b6675a02888 100644 (file)
@@ -13,7 +13,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *       $Header: /cvsroot/pgsql/src/backend/catalog/namespace.c,v 1.41 2002/12/04 05:18:31 momjian Exp $
+ *       $Header: /cvsroot/pgsql/src/backend/catalog/namespace.c,v 1.42 2002/12/12 21:02:19 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -143,7 +143,7 @@ Datum               pg_type_is_visible(PG_FUNCTION_ARGS);
 Datum          pg_function_is_visible(PG_FUNCTION_ARGS);
 Datum          pg_operator_is_visible(PG_FUNCTION_ARGS);
 Datum          pg_opclass_is_visible(PG_FUNCTION_ARGS);
-
+Datum          pg_conversion_is_visible(PG_FUNCTION_ARGS);
 
 /*
  * RangeVarGetRelid
@@ -1035,6 +1035,87 @@ OpclassIsVisible(Oid opcid)
        return visible;
 }
 
+/*
+ * ConversionGetConid
+ *             Try to resolve an unqualified conversion name.
+ *             Returns OID if conversion found in search path, else InvalidOid.
+ *
+ * This is essentially the same as RelnameGetRelid.
+ */
+Oid
+ConversionGetConid(const char *conname)
+{
+       Oid             conid;
+       List       *lptr;
+
+       recomputeNamespacePath();
+
+       foreach(lptr, namespaceSearchPath)
+       {
+               Oid                     namespaceId = (Oid) lfirsti(lptr);
+
+               conid = GetSysCacheOid(CONNAMENSP,
+                                                          PointerGetDatum(conname),
+                                                          ObjectIdGetDatum(namespaceId),
+                                                          0, 0);
+               if (OidIsValid(conid))
+                       return conid;
+       }
+
+       /* Not found in path */
+       return InvalidOid;
+}
+
+/*
+ * ConversionIsVisible
+ *             Determine whether a conversion (identified by OID) is visible in the
+ *             current search path.  Visible means "would be found by searching
+ *             for the unqualified conversion name".
+ */
+bool
+ConversionIsVisible(Oid conid)
+{
+       HeapTuple       contup;
+       Form_pg_conversion conform;
+       Oid                     connamespace;
+       bool            visible;
+
+       contup = SearchSysCache(CONOID,
+                                                       ObjectIdGetDatum(conid),
+                                                       0, 0, 0);
+       if (!HeapTupleIsValid(contup))
+               elog(ERROR, "Cache lookup failed for converions %u", conid);
+       conform = (Form_pg_conversion) GETSTRUCT(contup);
+
+       recomputeNamespacePath();
+
+       /*
+        * Quick check: if it ain't in the path at all, it ain't visible.
+        * Items in the system namespace are surely in the path and so we
+        * needn't even do intMember() for them.
+        */
+       connamespace = conform->connamespace;
+       if (connamespace != PG_CATALOG_NAMESPACE &&
+               !intMember(connamespace, namespaceSearchPath))
+               visible = false;
+       else
+       {
+               /*
+                * If it is in the path, it might still not be visible; it could
+                * be hidden by another conversion of the same name earlier in the
+                * path. So we must do a slow check to see if this conversion would
+                * be found by ConvnameGetConid.
+                */
+               char       *conname = NameStr(conform->conname);
+               
+               visible = (ConversionGetConid(conname) == conid);
+       }
+
+       ReleaseSysCache(contup);
+
+       return visible;
+}
+
 /*
  * DeconstructQualifiedName
  *             Given a possibly-qualified name expressed as a list of String nodes,
@@ -1854,3 +1935,11 @@ pg_opclass_is_visible(PG_FUNCTION_ARGS)
 
        PG_RETURN_BOOL(OpclassIsVisible(oid));
 }
+
+Datum
+pg_conversion_is_visible(PG_FUNCTION_ARGS)
+{
+       Oid                     oid = PG_GETARG_OID(0);
+
+       PG_RETURN_BOOL(ConversionIsVisible(oid));
+}
index bfe5b45e1efcd08709e4a601eb85b85becac33c0..06d31598566207bd8883216979a8e30c61ec7fb5 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright 2000-2002 by PostgreSQL Global Development Group
  *
- * $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.85 2002/11/08 19:12:21 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.86 2002/12/12 21:02:21 momjian Exp $
  */
 #include "postgres_fe.h"
 #include "command.h"
@@ -381,7 +381,13 @@ exec_command(const char *cmd,
                        case 'D':
                                success = listDomains(pattern);
                                break;
-
+                       case 'c':
+                               success = listConversions(pattern);
+                               break;
+                       case 'C':
+                               success = listCasts(pattern);
+                               break;
+                               
                        default:
                                status = CMD_UNKNOWN;
                }
index dae66b22d0babd81ed33999b6007553e4a041835..f5a2c895eeb38cb0a9e5272f191dd6d0a3f964de 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright 2000-2002 by PostgreSQL Global Development Group
  *
- * $Header: /cvsroot/pgsql/src/bin/psql/describe.c,v 1.71 2002/10/19 20:50:44 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/bin/psql/describe.c,v 1.72 2002/12/12 21:02:24 momjian Exp $
  */
 #include "postgres_fe.h"
 #include "describe.h"
@@ -1389,6 +1389,106 @@ listDomains(const char *pattern)
        return true;
 }
 
+/*
+ * \dc
+ *
+ * Describes conversions.
+ */
+bool
+listConversions(const char *pattern)
+{
+       PQExpBufferData buf;
+       PGresult   *res;
+       printQueryOpt myopt = pset.popt;
+
+       initPQExpBuffer(&buf);
+
+       printfPQExpBuffer(&buf,
+                                         "SELECT n.nspname AS \"%s\",\n"
+                                         "       c.conname AS \"%s\",\n"
+                                         "       pg_catalog.pg_encoding_to_char(c.conforencoding) AS \"%s\",\n"
+                                         "       pg_catalog.pg_encoding_to_char(c.contoencoding) AS \"%s\",\n"
+                                         "       CASE WHEN c.condefault THEN '%s'\n"
+                                         "       ELSE NULL END AS \"%s\"\n"
+                                         "FROM pg_catalog.pg_conversion c, pg_catalog.pg_namespace n\n"
+                                         "WHERE n.oid = c.connamespace\n",
+                                         _("Schema"),
+                                         _("Name"),
+                                         _("Source"),
+                                         _("Dest"),
+                                         _("default"),
+                                         _("Modifier"));
+
+       processNamePattern(&buf, pattern, true, false,
+                                          "n.nspname", "c.conname", NULL,
+                                          "pg_catalog.pg_conversion_is_visible(c.oid)");
+
+       appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
+
+       res = PSQLexec(buf.data, false);
+       termPQExpBuffer(&buf);
+       if (!res)
+               return false;
+
+       myopt.nullPrint = NULL;
+       myopt.title = _("List of conversions");
+
+       printQuery(res, &myopt, pset.queryFout);
+
+       PQclear(res);
+       return true;
+}
+
+/*
+ * \dC
+ *
+ * Describes casts.
+ */
+bool
+listCasts(const char *pattern)
+{
+       PQExpBufferData buf;
+       PGresult   *res;
+       printQueryOpt myopt = pset.popt;
+
+       initPQExpBuffer(&buf);
+/* NEED LEFT JOIN FOR BINARY CASTS */
+       printfPQExpBuffer(&buf,
+                                         "SELECT t1.typname AS \"%s\",\n"
+                                         "       t2.typname AS \"%s\",\n"
+                                         "       CASE WHEN p.proname IS NULL THEN '%s'\n"
+                                         "            ELSE p.proname\n"
+                                         "       END as \"%s\",\n"
+                                         "       CASE WHEN c.castcontext = 'e' THEN '%s'\n"
+                                         "            WHEN c.castcontext = 'a' THEN '%s'\n"
+                                         "            ELSE '%s'\n"
+                                         "       END as \"%s\"\n"
+                                         "FROM pg_catalog.pg_cast c LEFT JOIN pg_catalog.pg_proc p\n"
+                                         "       ON c.castfunc=p.oid, pg_catalog.pg_type t1, pg_catalog.pg_type t2\n"
+                                         "WHERE c.castsource=t1.oid AND c.casttarget=t2.oid ORDER BY 1, 2",
+                                         _("Source"),
+                                         _("Target"),
+                                         _("BINARY"),
+                                         _("Function"),
+                                         _("explicit"),
+                                         _("assignment explicit"),
+                                         _("implicit"),
+                                         _("Context"));
+
+       res = PSQLexec(buf.data, false);
+       termPQExpBuffer(&buf);
+       if (!res)
+               return false;
+
+       myopt.nullPrint = NULL;
+       myopt.title = _("List of casts");
+
+       printQuery(res, &myopt, pset.queryFout);
+
+       PQclear(res);
+       return true;
+}
+
 /*
  * processNamePattern
  *
index 0c22cafcbc86f7f7310dc6e50e832301d5000af1..6461b41d303c311628728d7d6de96fae025c8b7c 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright 2000-2002 by PostgreSQL Global Development Group
  *
- * $Header: /cvsroot/pgsql/src/bin/psql/describe.h,v 1.18 2002/08/27 18:28:29 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/bin/psql/describe.h,v 1.19 2002/12/12 21:02:24 momjian Exp $
  */
 #ifndef DESCRIBE_H
 #define DESCRIBE_H
@@ -43,4 +43,11 @@ bool         listTables(const char *tabtypes, const char *pattern, bool verbose);
 /* \dD */
 bool           listDomains(const char *pattern);
 
+/* \dc */
+bool           listConversions(const char *pattern);
+
+/* \dC */
+bool           listCasts(const char *pattern);
+
+
 #endif   /* DESCRIBE_H */
index e99f6f17204b4ec48d19314c848cbf53f4330c04..ecbc28617005ecd54afc3964b074fcf91d8c430a 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright 2000 by PostgreSQL Global Development Group
  *
- * $Header: /cvsroot/pgsql/src/bin/psql/help.c,v 1.66 2002/12/11 23:07:06 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/bin/psql/help.c,v 1.67 2002/12/12 21:02:24 momjian Exp $
  */
 #include "postgres_fe.h"
 #include "common.h"
@@ -206,6 +206,8 @@ slashUsage(unsigned short int pager)
        fprintf(output, _(" \\d{t|i|s|v|S} [PATTERN] (add \"+\" for more detail)\n"
                                          "                list tables/indexes/sequences/views/system tables\n"));
        fprintf(output, _(" \\da [PATTERN]  list aggregate functions\n"));
+       fprintf(output, _(" \\dc [PATTERN]  list conversions\n"));
+       fprintf(output, _(" \\dC            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"));
index fa3131873300e5f530cf0a44e8ebeacebac84da9..710b5c71ac888ab3c4fb755981189a6bb213185f 100644 (file)
@@ -37,7 +37,7 @@
  * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $Id: catversion.h,v 1.169 2002/12/12 15:49:40 tgl Exp $
+ * $Id: catversion.h,v 1.170 2002/12/12 21:02:25 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -53,6 +53,6 @@
  */
 
 /*                                                     yyyymmddN */
-#define CATALOG_VERSION_NO     200212101
+#define CATALOG_VERSION_NO     200212121
 
 #endif
index 793a84b7c60cf6e10102066d75ca70c650342707..27e3e75168f861c5aaafd9aabc7c7b598e6bf712 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $Id: namespace.h,v 1.22 2002/11/02 18:41:22 tgl Exp $
+ * $Id: namespace.h,v 1.23 2002/12/12 21:02:25 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -65,6 +65,8 @@ extern bool OperatorIsVisible(Oid oprid);
 extern OpclassCandidateList OpclassGetCandidates(Oid amid);
 extern Oid     OpclassnameGetOpcid(Oid amid, const char *opcname);
 extern bool OpclassIsVisible(Oid opcid);
+extern bool ConversionIsVisible(Oid opcid);
+extern Oid     ConversionGetConid(const char *conname);
 
 extern void DeconstructQualifiedName(List *names,
                                                 char **nspname_p,
index 348646c549097b0b661daf2e47924670058f165c..7558206a65744dad7c123eab1f79fd9fdceca1d5 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $Id: pg_proc.h,v 1.279 2002/12/06 05:20:26 momjian Exp $
+ * $Id: pg_proc.h,v 1.280 2002/12/12 21:02:25 momjian Exp $
  *
  * NOTES
  *       The script catalog/genbki.sh reads this file and generates .bki
@@ -2930,6 +2930,8 @@ DATA(insert OID = 2082 (  pg_operator_is_visible  PGNSP PGUID 12 f f t f s 1 16 "
 DESCR("is operator visible in search path?");
 DATA(insert OID = 2083 (  pg_opclass_is_visible                PGNSP PGUID 12 f f t f s 1 16 "26"      pg_opclass_is_visible - _null_ ));
 DESCR("is opclass visible in search path?");
+DATA(insert OID = 2093 (  pg_conversion_is_visible             PGNSP PGUID 12 f f t f s 1 16 "26"      pg_conversion_is_visible - _null_ ));
+DESCR("is conversion visible in search path?");
 
 
 /* Aggregates (moved here from pg_aggregate for 7.3) */