Skip to content

Commit 40c4472

Browse files
committed
TODO marked as done:
* 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
1 parent 8ac39d0 commit 40c4472

File tree

9 files changed

+243
-12
lines changed

9 files changed

+243
-12
lines changed

doc/src/sgml/ref/psql-ref.sgml

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.80 2002/11/08 19:12:21 momjian Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.81 2002/12/12 21:02:19 momjian Exp $
33
PostgreSQL documentation
44
-->
55

@@ -851,6 +851,29 @@ testdb=>
851851
</varlistentry>
852852

853853

854+
<varlistentry>
855+
<term><literal>\dc</literal> [ <replaceable class="parameter">pattern</replaceable> ]</term>
856+
<listitem>
857+
<para>
858+
Lists all available conversions (between encodings). If <replaceable
859+
class="parameter">pattern</replaceable>
860+
is specified, only matching conversions are shown.
861+
</para>
862+
</listitem>
863+
</varlistentry>
864+
865+
866+
<varlistentry>
867+
<term><literal>\dC</literal></term>
868+
<listitem>
869+
<para>
870+
Lists all available type casts. Casts can be explicit, explicit and assignment
871+
or implicit, and are used to change a variable from one type to another.
872+
</para>
873+
</listitem>
874+
</varlistentry>
875+
876+
854877
<varlistentry>
855878
<term><literal>\df [ <replaceable class="parameter">pattern</replaceable> ]</literal></term>
856879

src/backend/catalog/namespace.c

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* Portions Copyright (c) 1994, Regents of the University of California
1414
*
1515
* IDENTIFICATION
16-
* $Header: /cvsroot/pgsql/src/backend/catalog/namespace.c,v 1.41 2002/12/04 05:18:31 momjian Exp $
16+
* $Header: /cvsroot/pgsql/src/backend/catalog/namespace.c,v 1.42 2002/12/12 21:02:19 momjian Exp $
1717
*
1818
*-------------------------------------------------------------------------
1919
*/
@@ -143,7 +143,7 @@ Datum pg_type_is_visible(PG_FUNCTION_ARGS);
143143
Datum pg_function_is_visible(PG_FUNCTION_ARGS);
144144
Datum pg_operator_is_visible(PG_FUNCTION_ARGS);
145145
Datum pg_opclass_is_visible(PG_FUNCTION_ARGS);
146-
146+
Datum pg_conversion_is_visible(PG_FUNCTION_ARGS);
147147

148148
/*
149149
* RangeVarGetRelid
@@ -1035,6 +1035,87 @@ OpclassIsVisible(Oid opcid)
10351035
return visible;
10361036
}
10371037

1038+
/*
1039+
* ConversionGetConid
1040+
* Try to resolve an unqualified conversion name.
1041+
* Returns OID if conversion found in search path, else InvalidOid.
1042+
*
1043+
* This is essentially the same as RelnameGetRelid.
1044+
*/
1045+
Oid
1046+
ConversionGetConid(const char *conname)
1047+
{
1048+
Oid conid;
1049+
List *lptr;
1050+
1051+
recomputeNamespacePath();
1052+
1053+
foreach(lptr, namespaceSearchPath)
1054+
{
1055+
Oid namespaceId = (Oid) lfirsti(lptr);
1056+
1057+
conid = GetSysCacheOid(CONNAMENSP,
1058+
PointerGetDatum(conname),
1059+
ObjectIdGetDatum(namespaceId),
1060+
0, 0);
1061+
if (OidIsValid(conid))
1062+
return conid;
1063+
}
1064+
1065+
/* Not found in path */
1066+
return InvalidOid;
1067+
}
1068+
1069+
/*
1070+
* ConversionIsVisible
1071+
* Determine whether a conversion (identified by OID) is visible in the
1072+
* current search path. Visible means "would be found by searching
1073+
* for the unqualified conversion name".
1074+
*/
1075+
bool
1076+
ConversionIsVisible(Oid conid)
1077+
{
1078+
HeapTuple contup;
1079+
Form_pg_conversion conform;
1080+
Oid connamespace;
1081+
bool visible;
1082+
1083+
contup = SearchSysCache(CONOID,
1084+
ObjectIdGetDatum(conid),
1085+
0, 0, 0);
1086+
if (!HeapTupleIsValid(contup))
1087+
elog(ERROR, "Cache lookup failed for converions %u", conid);
1088+
conform = (Form_pg_conversion) GETSTRUCT(contup);
1089+
1090+
recomputeNamespacePath();
1091+
1092+
/*
1093+
* Quick check: if it ain't in the path at all, it ain't visible.
1094+
* Items in the system namespace are surely in the path and so we
1095+
* needn't even do intMember() for them.
1096+
*/
1097+
connamespace = conform->connamespace;
1098+
if (connamespace != PG_CATALOG_NAMESPACE &&
1099+
!intMember(connamespace, namespaceSearchPath))
1100+
visible = false;
1101+
else
1102+
{
1103+
/*
1104+
* If it is in the path, it might still not be visible; it could
1105+
* be hidden by another conversion of the same name earlier in the
1106+
* path. So we must do a slow check to see if this conversion would
1107+
* be found by ConvnameGetConid.
1108+
*/
1109+
char *conname = NameStr(conform->conname);
1110+
1111+
visible = (ConversionGetConid(conname) == conid);
1112+
}
1113+
1114+
ReleaseSysCache(contup);
1115+
1116+
return visible;
1117+
}
1118+
10381119
/*
10391120
* DeconstructQualifiedName
10401121
* Given a possibly-qualified name expressed as a list of String nodes,
@@ -1854,3 +1935,11 @@ pg_opclass_is_visible(PG_FUNCTION_ARGS)
18541935

18551936
PG_RETURN_BOOL(OpclassIsVisible(oid));
18561937
}
1938+
1939+
Datum
1940+
pg_conversion_is_visible(PG_FUNCTION_ARGS)
1941+
{
1942+
Oid oid = PG_GETARG_OID(0);
1943+
1944+
PG_RETURN_BOOL(ConversionIsVisible(oid));
1945+
}

src/bin/psql/command.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000-2002 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.85 2002/11/08 19:12:21 momjian Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.86 2002/12/12 21:02:21 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "command.h"
@@ -381,7 +381,13 @@ exec_command(const char *cmd,
381381
case 'D':
382382
success = listDomains(pattern);
383383
break;
384-
384+
case 'c':
385+
success = listConversions(pattern);
386+
break;
387+
case 'C':
388+
success = listCasts(pattern);
389+
break;
390+
385391
default:
386392
status = CMD_UNKNOWN;
387393
}

src/bin/psql/describe.c

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000-2002 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/describe.c,v 1.71 2002/10/19 20:50:44 tgl Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/describe.c,v 1.72 2002/12/12 21:02:24 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "describe.h"
@@ -1389,6 +1389,106 @@ listDomains(const char *pattern)
13891389
return true;
13901390
}
13911391

1392+
/*
1393+
* \dc
1394+
*
1395+
* Describes conversions.
1396+
*/
1397+
bool
1398+
listConversions(const char *pattern)
1399+
{
1400+
PQExpBufferData buf;
1401+
PGresult *res;
1402+
printQueryOpt myopt = pset.popt;
1403+
1404+
initPQExpBuffer(&buf);
1405+
1406+
printfPQExpBuffer(&buf,
1407+
"SELECT n.nspname AS \"%s\",\n"
1408+
" c.conname AS \"%s\",\n"
1409+
" pg_catalog.pg_encoding_to_char(c.conforencoding) AS \"%s\",\n"
1410+
" pg_catalog.pg_encoding_to_char(c.contoencoding) AS \"%s\",\n"
1411+
" CASE WHEN c.condefault THEN '%s'\n"
1412+
" ELSE NULL END AS \"%s\"\n"
1413+
"FROM pg_catalog.pg_conversion c, pg_catalog.pg_namespace n\n"
1414+
"WHERE n.oid = c.connamespace\n",
1415+
_("Schema"),
1416+
_("Name"),
1417+
_("Source"),
1418+
_("Dest"),
1419+
_("default"),
1420+
_("Modifier"));
1421+
1422+
processNamePattern(&buf, pattern, true, false,
1423+
"n.nspname", "c.conname", NULL,
1424+
"pg_catalog.pg_conversion_is_visible(c.oid)");
1425+
1426+
appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
1427+
1428+
res = PSQLexec(buf.data, false);
1429+
termPQExpBuffer(&buf);
1430+
if (!res)
1431+
return false;
1432+
1433+
myopt.nullPrint = NULL;
1434+
myopt.title = _("List of conversions");
1435+
1436+
printQuery(res, &myopt, pset.queryFout);
1437+
1438+
PQclear(res);
1439+
return true;
1440+
}
1441+
1442+
/*
1443+
* \dC
1444+
*
1445+
* Describes casts.
1446+
*/
1447+
bool
1448+
listCasts(const char *pattern)
1449+
{
1450+
PQExpBufferData buf;
1451+
PGresult *res;
1452+
printQueryOpt myopt = pset.popt;
1453+
1454+
initPQExpBuffer(&buf);
1455+
/* NEED LEFT JOIN FOR BINARY CASTS */
1456+
printfPQExpBuffer(&buf,
1457+
"SELECT t1.typname AS \"%s\",\n"
1458+
" t2.typname AS \"%s\",\n"
1459+
" CASE WHEN p.proname IS NULL THEN '%s'\n"
1460+
" ELSE p.proname\n"
1461+
" END as \"%s\",\n"
1462+
" CASE WHEN c.castcontext = 'e' THEN '%s'\n"
1463+
" WHEN c.castcontext = 'a' THEN '%s'\n"
1464+
" ELSE '%s'\n"
1465+
" END as \"%s\"\n"
1466+
"FROM pg_catalog.pg_cast c LEFT JOIN pg_catalog.pg_proc p\n"
1467+
" ON c.castfunc=p.oid, pg_catalog.pg_type t1, pg_catalog.pg_type t2\n"
1468+
"WHERE c.castsource=t1.oid AND c.casttarget=t2.oid ORDER BY 1, 2",
1469+
_("Source"),
1470+
_("Target"),
1471+
_("BINARY"),
1472+
_("Function"),
1473+
_("explicit"),
1474+
_("assignment explicit"),
1475+
_("implicit"),
1476+
_("Context"));
1477+
1478+
res = PSQLexec(buf.data, false);
1479+
termPQExpBuffer(&buf);
1480+
if (!res)
1481+
return false;
1482+
1483+
myopt.nullPrint = NULL;
1484+
myopt.title = _("List of casts");
1485+
1486+
printQuery(res, &myopt, pset.queryFout);
1487+
1488+
PQclear(res);
1489+
return true;
1490+
}
1491+
13921492
/*
13931493
* processNamePattern
13941494
*

src/bin/psql/describe.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000-2002 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/describe.h,v 1.18 2002/08/27 18:28:29 momjian Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/describe.h,v 1.19 2002/12/12 21:02:24 momjian Exp $
77
*/
88
#ifndef DESCRIBE_H
99
#define DESCRIBE_H
@@ -43,4 +43,11 @@ bool listTables(const char *tabtypes, const char *pattern, bool verbose);
4343
/* \dD */
4444
bool listDomains(const char *pattern);
4545

46+
/* \dc */
47+
bool listConversions(const char *pattern);
48+
49+
/* \dC */
50+
bool listCasts(const char *pattern);
51+
52+
4653
#endif /* DESCRIBE_H */

src/bin/psql/help.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/help.c,v 1.66 2002/12/11 23:07:06 momjian Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/help.c,v 1.67 2002/12/12 21:02:24 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "common.h"
@@ -206,6 +206,8 @@ slashUsage(unsigned short int pager)
206206
fprintf(output, _(" \\d{t|i|s|v|S} [PATTERN] (add \"+\" for more detail)\n"
207207
" list tables/indexes/sequences/views/system tables\n"));
208208
fprintf(output, _(" \\da [PATTERN] list aggregate functions\n"));
209+
fprintf(output, _(" \\dc [PATTERN] list conversions\n"));
210+
fprintf(output, _(" \\dC list casts\n"));
209211
fprintf(output, _(" \\dd [PATTERN] show comment for object\n"));
210212
fprintf(output, _(" \\dD [PATTERN] list domains\n"));
211213
fprintf(output, _(" \\df [PATTERN] list functions (add \"+\" for more detail)\n"));

src/include/catalog/catversion.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
3838
* Portions Copyright (c) 1994, Regents of the University of California
3939
*
40-
* $Id: catversion.h,v 1.169 2002/12/12 15:49:40 tgl Exp $
40+
* $Id: catversion.h,v 1.170 2002/12/12 21:02:25 momjian Exp $
4141
*
4242
*-------------------------------------------------------------------------
4343
*/
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 200212101
56+
#define CATALOG_VERSION_NO 200212121
5757

5858
#endif

src/include/catalog/namespace.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: namespace.h,v 1.22 2002/11/02 18:41:22 tgl Exp $
10+
* $Id: namespace.h,v 1.23 2002/12/12 21:02:25 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -65,6 +65,8 @@ extern bool OperatorIsVisible(Oid oprid);
6565
extern OpclassCandidateList OpclassGetCandidates(Oid amid);
6666
extern Oid OpclassnameGetOpcid(Oid amid, const char *opcname);
6767
extern bool OpclassIsVisible(Oid opcid);
68+
extern bool ConversionIsVisible(Oid opcid);
69+
extern Oid ConversionGetConid(const char *conname);
6870

6971
extern void DeconstructQualifiedName(List *names,
7072
char **nspname_p,

src/include/catalog/pg_proc.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: pg_proc.h,v 1.279 2002/12/06 05:20:26 momjian Exp $
10+
* $Id: pg_proc.h,v 1.280 2002/12/12 21:02:25 momjian Exp $
1111
*
1212
* NOTES
1313
* 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 "
29302930
DESCR("is operator visible in search path?");
29312931
DATA(insert OID = 2083 ( pg_opclass_is_visible PGNSP PGUID 12 f f t f s 1 16 "26" pg_opclass_is_visible - _null_ ));
29322932
DESCR("is opclass visible in search path?");
2933+
DATA(insert OID = 2093 ( pg_conversion_is_visible PGNSP PGUID 12 f f t f s 1 16 "26" pg_conversion_is_visible - _null_ ));
2934+
DESCR("is conversion visible in search path?");
29332935

29342936

29352937
/* Aggregates (moved here from pg_aggregate for 7.3) */

0 commit comments

Comments
 (0)