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 :06pgsql Exp $
6+ * $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.89 2003/12/01 22:11 :06momjian Exp $
77 */
88#include "postgres_fe.h"
99#include "describe.h"
@@ -857,7 +857,7 @@ describeOneTableDetails(const char *schemaname,
857857
858858printfPQExpBuffer (& 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,
880880char * indpred = PQgetvalue (result ,0 ,4 );
881881
882882if (strcmp (indisprimary ,"t" )== 0 )
883- printfPQExpBuffer (& tmpbuf ,_ ("primary key , " ));
883+ printfPQExpBuffer (& tmpbuf ,_ ("PRIMARY KEY , " ));
884884else if (strcmp (indisunique ,"t" )== 0 )
885- printfPQExpBuffer (& tmpbuf ,_ ("unique , " ));
885+ printfPQExpBuffer (& tmpbuf ,_ ("UNIQUE , " ));
886886else
887887resetPQExpBuffer (& tmpbuf );
888888appendPQExpBuffer (& tmpbuf ,"%s, " ,indamname );
@@ -892,7 +892,7 @@ describeOneTableDetails(const char *schemaname,
892892schemaname ,indtable );
893893
894894if (strlen (indpred ))
895- appendPQExpBuffer (& tmpbuf ,", predicate%s " ,indpred );
895+ appendPQExpBuffer (& tmpbuf ,", predicate(%s) " ,indpred );
896896
897897footers = xmalloczero (2 * sizeof (* footers ));
898898footers [0 ]= xstrdup (tmpbuf .data );
@@ -911,7 +911,7 @@ describeOneTableDetails(const char *schemaname,
911911if (tableinfo .hasrules )
912912{
913913printfPQExpBuffer (& 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'" ,
917917oid );
@@ -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 ));
927927footers [count_footers ]= xmalloc (64 + strlen (view_def ));
928928snprintf (footers [count_footers ],64 + strlen (view_def ),
929929_ ("View definition:\n%s" ),view_def );
930930count_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
948952footers [count_footers ]= NULL ;
949953
@@ -970,7 +974,7 @@ describeOneTableDetails(const char *schemaname,
970974{
971975printfPQExpBuffer (& 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,
10061010if (tableinfo .hasrules )
10071011{
10081012printfPQExpBuffer (& 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'" ,
10121016oid );
@@ -1051,7 +1055,7 @@ describeOneTableDetails(const char *schemaname,
10511055{
10521056printfPQExpBuffer (& 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'" ,
10571061oid );
@@ -1097,9 +1101,9 @@ describeOneTableDetails(const char *schemaname,
10971101/* Label as primary key or unique (but not both) */
10981102appendPQExpBuffer (& buf ,
10991103strcmp (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 */