33 *
44 * Copyright 2000 by PostgreSQL Global Development Group
55 *
6- * $Header: /cvsroot/pgsql/src/bin/psql/describe.c,v 1.32 2001/05/27 21:50:50 petere Exp $
6+ * $Header: /cvsroot/pgsql/src/bin/psql/describe.c,v 1.33 2001/05/28 02:01:22 momjian Exp $
77 */
88#include "postgres_fe.h"
99#include "describe.h"
@@ -735,14 +735,18 @@ describeTableDetails(const char *name, bool desc)
735735 /* Information about the table */
736736 else if (tableinfo .relkind == 'r' )
737737 {
738- PGresult * result1 = NULL ,
739- * result2 = NULL ,
740- * result3 = NULL ,
741- * result4 = NULL ;
738+ PGresult * result1 = NULL ,
739+ * result2 = NULL ,
740+ * result3 = NULL ,
741+ * result4 = NULL ,
742+ * result5 = NULL ,
743+ * result6 = NULL ;
742744 int index_count = 0 ,
743- constr_count = 0 ,
744- rule_count = 0 ,
745- trigger_count = 0 ;
745+ primary_count = 0 ,
746+ unique_count = 0 ,
747+ constr_count = 0 ,
748+ rule_count = 0 ,
749+ trigger_count = 0 ;
746750 int count_footers = 0 ;
747751
748752 /* count indices */
@@ -751,7 +755,7 @@ describeTableDetails(const char *name, bool desc)
751755 sprintf (buf , "SELECT c2.relname\n"
752756 "FROM pg_class c, pg_class c2, pg_index i\n"
753757 "WHERE c.relname = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n"
754- " ORDER BY c2.relname" ,
758+ "AND NOT i.indisunique ORDER BY c2.relname" ,
755759 name );
756760 result1 = PSQLexec (buf );
757761 if (!result1 )
@@ -760,10 +764,40 @@ describeTableDetails(const char *name, bool desc)
760764 index_count = PQntuples (result1 );
761765 }
762766
767+ /* count primary keys */
768+ if (!error && tableinfo .hasindex )
769+ {
770+ sprintf (buf , "SELECT c2.relname\n"
771+ "FROM pg_class c, pg_class c2, pg_index i\n"
772+ "WHERE c.relname = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n"
773+ "AND i.indisprimary AND i.indisunique ORDER BY c2.relname" ,
774+ name );
775+ result5 = PSQLexec (buf );
776+ if (!result5 )
777+ error = true;
778+ else
779+ primary_count = PQntuples (result5 );
780+ }
781+
782+ /* count unique constraints */
783+ if (!error && tableinfo .hasindex )
784+ {
785+ sprintf (buf , "SELECT c2.relname\n"
786+ "FROM pg_class c, pg_class c2, pg_index i\n"
787+ "WHERE c.relname = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n"
788+ "AND NOT i.indisprimary AND i.indisunique ORDER BY c2.relname" ,
789+ name );
790+ result6 = PSQLexec (buf );
791+ if (!result6 )
792+ error = true;
793+ else
794+ unique_count = PQntuples (result6 );
795+ }
796+
763797 /* count table (and column) constraints */
764798 if (!error && tableinfo .checks )
765799 {
766- sprintf (buf , "SELECT rcsrc\n"
800+ sprintf (buf , "SELECT rcsrc, rcname \n"
767801 "FROM pg_relcheck r, pg_class c\n"
768802 "WHERE c.relname='%s' AND c.oid = r.rcrelid" ,
769803 name );
@@ -804,8 +838,9 @@ describeTableDetails(const char *name, bool desc)
804838 trigger_count = PQntuples (result4 );
805839 }
806840
807- footers = xmalloc ((index_count + constr_count + rule_count + trigger_count + 1 )
808- * sizeof (* footers ));
841+ footers = xmalloc ((index_count + primary_count + unique_count +
842+ constr_count + rule_count + trigger_count + 1 )
843+ * sizeof (* footers ));
809844
810845 /* print indices */
811846 for (i = 0 ; i < index_count ; i ++ )
@@ -820,12 +855,39 @@ describeTableDetails(const char *name, bool desc)
820855 footers [count_footers ++ ] = xstrdup (buf );
821856 }
822857
823- /* print contraints */
824- for (i = 0 ; i < constr_count ; i ++ )
858+ /* print primary keys */
859+ for (i = 0 ; i < primary_count ; i ++ )
860+ {
861+ sprintf (buf , "%s %s" ,
862+ primary_count == 1 ? "Primary Key:" : (i == 0 ? "Primary Keys:" : " " ),
863+ PQgetvalue (result5 , i , 0 )
864+ );
865+ if (i < primary_count - 1 )
866+ strcat (buf , "," );
867+
868+ footers [count_footers ++ ] = xstrdup (buf );
869+ }
870+
871+ /* print unique constraints */
872+ for (i = 0 ; i < unique_count ; i ++ )
825873 {
826874 sprintf (buf , "%s %s" ,
875+ unique_count == 1 ? "Unique Key:" : (i == 0 ? "Unique Keys:" : " " ),
876+ PQgetvalue (result6 , i , 0 )
877+ );
878+ if (i < unique_count - 1 )
879+ strcat (buf , "," );
880+
881+ footers [count_footers ++ ] = xstrdup (buf );
882+ }
883+
884+ /* print constraints */
885+ for (i = 0 ; i < constr_count ; i ++ )
886+ {
887+ sprintf (buf , "%s \"%s\" %s" ,
827888 constr_count == 1 ? "Constraint:" : (i == 0 ? "Constraints:" : " " ),
828- PQgetvalue (result2 , i , 0 )
889+ PQgetvalue (result2 , i , 1 ),
890+ PQgetvalue (result2 , i , 0 )
829891 );
830892 footers [count_footers ++ ] = xstrdup (buf );
831893 }
@@ -863,6 +925,8 @@ describeTableDetails(const char *name, bool desc)
863925 PQclear (result2 );
864926 PQclear (result3 );
865927 PQclear (result4 );
928+ PQclear (result5 );
929+ PQclear (result6 );
866930 }
867931
868932 if (!error )
0 commit comments