Skip to content

Commit abd5d75

Browse files
committed
This patch finishes off the work that I did with making view
definitions use pretty printing. It does: * Pretty index predicates * Pretty rule definitions * Uppercases PRIMARY KEY and UNIQUE to be consistent with CHECK and FOREIGN KEY * View rules are improved to match table rules: Christopher Kings-Lynne
1 parent 7ce9b7c commit abd5d75

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

src/bin/psql/describe.c

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2003, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.88 2003/11/29 19:52:06 pgsql Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.89 2003/12/01 22:11:06 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "describe.h"
@@ -857,7 +857,7 @@ describeOneTableDetails(const char *schemaname,
857857

858858
printfPQExpBuffer(&buf,
859859
"SELECT i.indisunique, i.indisprimary, a.amname, c2.relname,\n"
860-
" pg_catalog.pg_get_expr(i.indpred, i.indrelid)\n"
860+
" pg_catalog.pg_get_expr(i.indpred, i.indrelid, true)\n"
861861
"FROM pg_catalog.pg_index i, pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_am a\n"
862862
"WHERE i.indexrelid = c.oid AND c.oid = '%s' AND c.relam = a.oid\n"
863863
"AND i.indrelid = c2.oid",
@@ -880,9 +880,9 @@ describeOneTableDetails(const char *schemaname,
880880
char *indpred = PQgetvalue(result, 0, 4);
881881

882882
if (strcmp(indisprimary, "t") == 0)
883-
printfPQExpBuffer(&tmpbuf, _("primary key, "));
883+
printfPQExpBuffer(&tmpbuf, _("PRIMARY KEY, "));
884884
else if (strcmp(indisunique, "t") == 0)
885-
printfPQExpBuffer(&tmpbuf, _("unique, "));
885+
printfPQExpBuffer(&tmpbuf, _("UNIQUE, "));
886886
else
887887
resetPQExpBuffer(&tmpbuf);
888888
appendPQExpBuffer(&tmpbuf, "%s, ", indamname);
@@ -892,7 +892,7 @@ describeOneTableDetails(const char *schemaname,
892892
schemaname, indtable);
893893

894894
if (strlen(indpred))
895-
appendPQExpBuffer(&tmpbuf, ", predicate %s", indpred);
895+
appendPQExpBuffer(&tmpbuf, ", predicate (%s)", indpred);
896896

897897
footers = xmalloczero(2 * sizeof(*footers));
898898
footers[0] = xstrdup(tmpbuf.data);
@@ -911,7 +911,7 @@ describeOneTableDetails(const char *schemaname,
911911
if (tableinfo.hasrules)
912912
{
913913
printfPQExpBuffer(&buf,
914-
"SELECT r.rulename\n"
914+
"SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true))\n"
915915
"FROM pg_catalog.pg_rewrite r\n"
916916
"WHERE r.ev_class = '%s' AND r.rulename != '_RETURN'",
917917
oid);
@@ -923,27 +923,31 @@ describeOneTableDetails(const char *schemaname,
923923
}
924924

925925
/* Footer information about a view */
926-
footers = xmalloczero((rule_count + 2) * sizeof(*footers));
926+
footers = xmalloczero((rule_count + 3) * sizeof(*footers));
927927
footers[count_footers] = xmalloc(64 + strlen(view_def));
928928
snprintf(footers[count_footers], 64 + strlen(view_def),
929929
_("View definition:\n%s"), view_def);
930930
count_footers++;
931931

932932
/* print rules */
933-
for (i = 0; i < rule_count; i++)
933+
if (rule_count > 0)
934934
{
935-
char *s = _("Rules");
935+
printfPQExpBuffer(&buf, _("Rules:"));
936+
footers[count_footers++] = xstrdup(buf.data);
937+
for (i = 0; i < rule_count; i++)
938+
{
939+
const char *ruledef;
936940

937-
if (i == 0)
938-
printfPQExpBuffer(&buf, "%s: %s", s, PQgetvalue(result, i, 0));
939-
else
940-
printfPQExpBuffer(&buf, "%*s %s", (int) strlen(s), "", PQgetvalue(result, i, 0));
941-
if (i < rule_count - 1)
942-
appendPQExpBuffer(&buf, ",");
941+
/* Everything after "CREATE RULE" is echoed verbatim */
942+
ruledef = PQgetvalue(result, i, 1);
943+
ruledef += 12;
943944

944-
footers[count_footers++] = xstrdup(buf.data);
945+
printfPQExpBuffer(&buf, " %s", ruledef);
946+
947+
footers[count_footers++] = xstrdup(buf.data);
948+
}
949+
PQclear(result);
945950
}
946-
PQclear(result);
947951

948952
footers[count_footers] = NULL;
949953

@@ -970,7 +974,7 @@ describeOneTableDetails(const char *schemaname,
970974
{
971975
printfPQExpBuffer(&buf,
972976
"SELECT c2.relname, i.indisprimary, i.indisunique, "
973-
"pg_catalog.pg_get_indexdef(i.indexrelid)\n"
977+
"pg_catalog.pg_get_indexdef(i.indexrelid, 0, true)\n"
974978
"FROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i\n"
975979
"WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n"
976980
"ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname",
@@ -1006,7 +1010,7 @@ describeOneTableDetails(const char *schemaname,
10061010
if (tableinfo.hasrules)
10071011
{
10081012
printfPQExpBuffer(&buf,
1009-
"SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid))\n"
1013+
"SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true))\n"
10101014
"FROM pg_catalog.pg_rewrite r\n"
10111015
"WHERE r.ev_class = '%s'",
10121016
oid);
@@ -1051,7 +1055,7 @@ describeOneTableDetails(const char *schemaname,
10511055
{
10521056
printfPQExpBuffer(&buf,
10531057
"SELECT conname,\n"
1054-
" pg_catalog.pg_get_constraintdef(oid) as condef\n"
1058+
" pg_catalog.pg_get_constraintdef(oid, true) as condef\n"
10551059
"FROM pg_catalog.pg_constraint r\n"
10561060
"WHERE r.conrelid = '%s' AND r.contype = 'f'",
10571061
oid);
@@ -1097,9 +1101,9 @@ describeOneTableDetails(const char *schemaname,
10971101
/* Label as primary key or unique (but not both) */
10981102
appendPQExpBuffer(&buf,
10991103
strcmp(PQgetvalue(result1, i, 1), "t") == 0
1100-
? _(" primary key,") :
1104+
? _(" PRIMARY KEY,") :
11011105
(strcmp(PQgetvalue(result1, i, 2), "t") == 0
1102-
? _(" unique,")
1106+
? _(" UNIQUE,")
11031107
: ""));
11041108

11051109
/* Everything after "USING" is echoed verbatim */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy