33 *
44 * Copyright 2000-2002 by PostgreSQL Global Development Group
55 *
6- * $Header: /cvsroot/pgsql/src/bin/psql/describe.c,v 1.75 2003/02/24 03:54:06 tgl Exp $
6+ * $Header: /cvsroot/pgsql/src/bin/psql/describe.c,v 1.76 2003/03/27 16:57:39 momjian Exp $
77 */
88#include "postgres_fe.h"
99#include "describe.h"
@@ -975,7 +975,7 @@ describeOneTableDetails(const char *schemaname,
975975if (tableinfo .hasrules )
976976{
977977printfPQExpBuffer (& buf ,
978- "SELECT r.rulename\n"
978+ "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid)) \n"
979979"FROM pg_catalog.pg_rewrite r\n"
980980"WHERE r.ev_class = '%s'" ,
981981oid );
@@ -990,7 +990,7 @@ describeOneTableDetails(const char *schemaname,
990990if (tableinfo .triggers )
991991{
992992printfPQExpBuffer (& buf ,
993- "SELECT t.tgname\n"
993+ "SELECT t.tgname, pg_catalog.pg_get_triggerdef(t.oid) \n"
994994"FROM pg_catalog.pg_trigger t\n"
995995"WHERE t.tgrelid = '%s' "
996996"and (not tgisconstraint "
@@ -1022,113 +1022,120 @@ describeOneTableDetails(const char *schemaname,
10221022foreignkey_count = PQntuples (result5 );
10231023}
10241024
1025- footers = xmalloczero ((index_count + check_count + rule_count + trigger_count + foreignkey_count + 1 )
1025+ footers = xmalloczero ((index_count + check_count + rule_count + trigger_count + foreignkey_count + 6 )
10261026* sizeof (* footers ));
10271027
10281028/* print indexes */
1029- for (i = 0 ;i < index_count ;i ++ )
1030- {
1031- char * s = _ ("Indexes" );
1032- const char * indexdef ;
1033- const char * usingpos ;
1029+ if (index_count > 0 ) {
1030+ printfPQExpBuffer (& buf ,_ ("Indexes:" ));
1031+ footers [count_footers ++ ]= xstrdup (buf .data );
1032+ for (i = 0 ;i < index_count ;i ++ )
1033+ {
1034+ const char * indexdef ;
1035+ const char * usingpos ;
10341036
1035- if (i == 0 )
1036- printfPQExpBuffer (& buf ,"%s: %s" ,s ,
1037- PQgetvalue (result1 ,i ,0 ));
1038- else
1039- printfPQExpBuffer (& buf ,"%*s %s" , (int )strlen (s ),"" ,
1037+ /* Output index/constraint name */
1038+ printfPQExpBuffer (& buf ," \"%s\"" ,
10401039PQgetvalue (result1 ,i ,0 ));
10411040
1042- /* Label as primary key or unique (but not both) */
1043- appendPQExpBuffer (& buf ,
1044- strcmp (PQgetvalue (result1 ,i ,1 ),"t" )== 0
1045- ?_ ("primary key " ) :
1046- (strcmp (PQgetvalue (result1 ,i ,2 ),"t" )== 0
1047- ?_ ("unique " )
1048- :"" ));
1041+ /* Label as primary key or unique (but not both) */
1042+ appendPQExpBuffer (& buf ,
1043+ strcmp (PQgetvalue (result1 ,i ,1 ),"t" )== 0
1044+ ?_ ("PRIMARY KEY " ) :
1045+ (strcmp (PQgetvalue (result1 ,i ,2 ),"t" )== 0
1046+ ?_ ("UNIQUE " )
1047+ :"" ));
10491048
1050- /* Everything after "USING" is echoed verbatim */
1051- indexdef = PQgetvalue (result1 ,i ,3 );
1052- usingpos = strstr (indexdef ," USING " );
1053- if (usingpos )
1054- indexdef = usingpos + 7 ;
1049+ /* Everything after "USING" is echoed verbatim */
1050+ indexdef = PQgetvalue (result1 ,i ,3 );
1051+ usingpos = strstr (indexdef ," USING " );
1052+ if (usingpos )
1053+ indexdef = usingpos + 7 ;
10551054
1056- appendPQExpBuffer (& buf ," %s" ,indexdef );
1055+ appendPQExpBuffer (& buf ," %s" ,indexdef );
10571056
1058- if (i < index_count - 1 )
1059- appendPQExpBuffer (& buf ,"," );
1057+ if (i < index_count - 1 )
1058+ appendPQExpBuffer (& buf ,"," );
10601059
1061- footers [count_footers ++ ]= xstrdup (buf .data );
1060+ footers [count_footers ++ ]= xstrdup (buf .data );
1061+ }
10621062}
10631063
1064-
10651064/* print check constraints */
1066- for (i = 0 ;i < check_count ;i ++ )
1067- {
1068- char * s = _ ("Check constraints" );
1069-
1070- if (i == 0 )
1071- printfPQExpBuffer (& buf ,_ ("%s: \"%s\" %s" ),
1072- s ,
1073- PQgetvalue (result2 ,i ,1 ),
1074- PQgetvalue (result2 ,i ,0 ));
1075- else
1076- printfPQExpBuffer (& buf ,_ ("%*s \"%s\" %s" ),
1077- (int )strlen (s ),"" ,
1065+ if (check_count > 0 ) {
1066+ printfPQExpBuffer (& buf ,_ ("Check Constraints:" ));
1067+ footers [count_footers ++ ]= xstrdup (buf .data );
1068+ for (i = 0 ;i < check_count ;i ++ )
1069+ {
1070+ printfPQExpBuffer (& buf ,_ (" \"%s\" CHECK %s" ),
10781071PQgetvalue (result2 ,i ,1 ),
10791072PQgetvalue (result2 ,i ,0 ));
1080- footers [count_footers ++ ]= xstrdup (buf .data );
1073+ if (i < check_count - 1 )
1074+ appendPQExpBuffer (& buf ,"," );
1075+
1076+ footers [count_footers ++ ]= xstrdup (buf .data );
1077+ }
10811078}
10821079
10831080/* print foreign key constraints */
1084- for (i = 0 ;i < foreignkey_count ;i ++ )
1085- {
1086- char * s = _ ("Foreign Key constraints" );
1087-
1088- if (i == 0 )
1089- printfPQExpBuffer (& buf ,_ ("%s: %s %s" ),
1090- s ,
1091- PQgetvalue (result5 ,i ,0 ),
1092- PQgetvalue (result5 ,i ,1 ));
1093- else
1094- printfPQExpBuffer (& buf ,_ ("%*s %s %s" ),
1095- (int )strlen (s ),"" ,
1081+ if (foreignkey_count > 0 ) {
1082+ printfPQExpBuffer (& buf ,_ ("Foreign Key Constraints:" ));
1083+ footers [count_footers ++ ]= xstrdup (buf .data );
1084+ for (i = 0 ;i < foreignkey_count ;i ++ )
1085+ {
1086+ printfPQExpBuffer (& buf ,_ (" \"%s\" %s" ),
10961087PQgetvalue (result5 ,i ,0 ),
10971088PQgetvalue (result5 ,i ,1 ));
1098- if (i < foreignkey_count - 1 )
1099- appendPQExpBuffer (& buf ,"," );
1089+ if (i < foreignkey_count - 1 )
1090+ appendPQExpBuffer (& buf ,"," );
11001091
1101- footers [count_footers ++ ]= xstrdup (buf .data );
1092+ footers [count_footers ++ ]= xstrdup (buf .data );
1093+ }
11021094}
11031095
11041096/* print rules */
1105- for (i = 0 ;i < rule_count ;i ++ )
1106- {
1107- char * s = _ ("Rules" );
1097+ if (rule_count > 0 ) {
1098+ printfPQExpBuffer (& buf ,_ ("Rules:" ));
1099+ footers [count_footers ++ ]= xstrdup (buf .data );
1100+ for (i = 0 ;i < rule_count ;i ++ )
1101+ {
1102+ const char * ruledef ;
11081103
1109- if (i == 0 )
1110- printfPQExpBuffer (& buf ,"%s: %s" ,s ,PQgetvalue (result3 ,i ,0 ));
1111- else
1112- printfPQExpBuffer (& buf ,"%*s %s" , (int )strlen (s ),"" ,PQgetvalue (result3 ,i ,0 ));
1113- if (i < rule_count - 1 )
1114- appendPQExpBuffer (& buf ,"," );
1104+ /* Everything after "CREATE RULE" is echoed verbatim */
1105+ ruledef = PQgetvalue (result3 ,i ,1 );
1106+ ruledef += 12 ;
11151107
1116- footers [count_footers ++ ]= xstrdup (buf .data );
1108+ printfPQExpBuffer (& buf ," %s" ,ruledef );
1109+
1110+ if (i < rule_count - 1 )
1111+ appendPQExpBuffer (& buf ,"," );
1112+
1113+ footers [count_footers ++ ]= xstrdup (buf .data );
1114+ }
11171115}
11181116
11191117/* print triggers */
1120- for (i = 0 ;i < trigger_count ;i ++ )
1121- {
1122- char * s = _ ("Triggers" );
1118+ if (trigger_count > 0 ) {
1119+ printfPQExpBuffer (& buf ,_ ("Triggers:" ));
1120+ footers [count_footers ++ ]= xstrdup (buf .data );
1121+ for (i = 0 ;i < trigger_count ;i ++ )
1122+ {
1123+ const char * tgdef ;
1124+ const char * usingpos ;
11231125
1124- if (i == 0 )
1125- printfPQExpBuffer (& buf ,"%s: %s" ,s ,PQgetvalue (result4 ,i ,0 ));
1126- else
1127- printfPQExpBuffer (& buf ,"%*s %s" , (int )strlen (s ),"" ,PQgetvalue (result4 ,i ,0 ));
1128- if (i < trigger_count - 1 )
1129- appendPQExpBuffer (& buf ,"," );
1126+ /* Everything after "TRIGGER" is echoed verbatim */
1127+ tgdef = PQgetvalue (result4 ,i ,1 );
1128+ usingpos = strstr (tgdef ," TRIGGER " );
1129+ if (usingpos )
1130+ tgdef = usingpos + 9 ;
11301131
1131- footers [count_footers ++ ]= xstrdup (buf .data );
1132+ printfPQExpBuffer (& buf ," %s" ,tgdef );
1133+
1134+ if (i < trigger_count - 1 )
1135+ appendPQExpBuffer (& buf ,"," );
1136+
1137+ footers [count_footers ++ ]= xstrdup (buf .data );
1138+ }
11321139}
11331140
11341141/* end of list marker */