Index: describe.c =================================================================== RCS file: /home/projects/pgsql/cvsroot/pgsql/src/bin/psql/describe.c,v retrieving revision 1.31 diff -c -r1.31 describe.c *** describe.c 2001/05/09 17:57:42 1.31 --- describe.c 2001/05/22 01:56:04 *************** *** 735,748 **** /* Information about the table */ else if (tableinfo.relkind == 'r') { ! PGresult *result1 = NULL, ! *result2 = NULL, ! *result3 = NULL, ! *result4 = NULL; int index_count = 0, ! constr_count = 0, ! rule_count = 0, ! trigger_count = 0; int count_footers = 0; /* count indices */ --- 735,752 ---- /* Information about the table */ else if (tableinfo.relkind == 'r') { ! PGresult *result1 = NULL, ! *result2 = NULL, ! *result3 = NULL, ! *result4 = NULL, ! *result5 = NULL, ! *result6 = NULL; int index_count = 0, ! primary_count = 0, ! unique_count = 0, ! constr_count = 0, ! rule_count = 0, ! trigger_count = 0; int count_footers = 0; /* count indices */ *************** *** 751,757 **** sprintf(buf, "SELECT c2.relname\n" "FROM pg_class c, pg_class c2, pg_index i\n" "WHERE c.relname = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n" ! "ORDER BY c2.relname", name); result1 = PSQLexec(buf); if (!result1) --- 755,761 ---- sprintf(buf, "SELECT c2.relname\n" "FROM pg_class c, pg_class c2, pg_index i\n" "WHERE c.relname = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n" ! "AND NOT i.indisunique ORDER BY c2.relname", name); result1 = PSQLexec(buf); if (!result1) *************** *** 760,769 **** index_count = PQntuples(result1); } /* count table (and column) constraints */ if (!error && tableinfo.checks) { ! sprintf(buf, "SELECT rcsrc\n" "FROM pg_relcheck r, pg_class c\n" "WHERE c.relname='%s' AND c.oid = r.rcrelid", name); --- 764,803 ---- index_count = PQntuples(result1); } + /* count primary keys */ + if (!error && tableinfo.hasindex) + { + sprintf(buf, "SELECT c2.relname\n" + "FROM pg_class c, pg_class c2, pg_index i\n" + "WHERE c.relname = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n" + "AND i.indisprimary AND i.indisunique ORDER BY c2.relname", + name); + result5 = PSQLexec(buf); + if (!result5) + error = true; + else + primary_count = PQntuples(result5); + } + + /* count unique constraints */ + if (!error && tableinfo.hasindex) + { + sprintf(buf, "SELECT c2.relname\n" + "FROM pg_class c, pg_class c2, pg_index i\n" + "WHERE c.relname = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n" + "AND NOT i.indisprimary AND i.indisunique ORDER BY c2.relname", + name); + result6 = PSQLexec(buf); + if (!result6) + error = true; + else + unique_count = PQntuples(result6); + } + /* count table (and column) constraints */ if (!error && tableinfo.checks) { ! sprintf(buf, "SELECT rcsrc, rcname\n" "FROM pg_relcheck r, pg_class c\n" "WHERE c.relname='%s' AND c.oid = r.rcrelid", name); *************** *** 804,811 **** trigger_count = PQntuples(result4); } ! footers = xmalloc((index_count + constr_count + rule_count + trigger_count + 1) ! * sizeof(*footers)); /* print indices */ for (i = 0; i < index_count; i++) --- 838,846 ---- trigger_count = PQntuples(result4); } ! footers = xmalloc((index_count + primary_count + unique_count + ! constr_count + rule_count + trigger_count + 1) ! * sizeof(*footers)); /* print indices */ for (i = 0; i < index_count; i++) *************** *** 820,831 **** footers[count_footers++] = xstrdup(buf); } ! /* print contraints */ ! for (i = 0; i < constr_count; i++) { sprintf(buf, "%s %s", constr_count == 1 ? "Constraint:" : (i == 0 ? "Constraints:" : " "), ! PQgetvalue(result2, i, 0) ); footers[count_footers++] = xstrdup(buf); } --- 855,893 ---- footers[count_footers++] = xstrdup(buf); } ! /* print primary keys */ ! for (i = 0; i < primary_count; i++) ! { ! sprintf(buf, "%s %s", ! primary_count == 1 ? "Primary Key:" : (i == 0 ? "Primary Keys:" : " "), ! PQgetvalue(result5, i, 0) ! ); ! if (i < primary_count - 1) ! strcat(buf, ","); ! ! footers[count_footers++] = xstrdup(buf); ! } ! ! /* print unique constraints */ ! for (i = 0; i < unique_count; i++) { sprintf(buf, "%s %s", + unique_count == 1 ? "Unique Key:" : (i == 0 ? "Unique Keys:" : " "), + PQgetvalue(result6, i, 0) + ); + if (i < unique_count - 1) + strcat(buf, ","); + + footers[count_footers++] = xstrdup(buf); + } + + /* print constraints */ + for (i = 0; i < constr_count; i++) + { + sprintf(buf, "%s \"%s\" %s", constr_count == 1 ? "Constraint:" : (i == 0 ? "Constraints:" : " "), ! PQgetvalue(result2, i, 1), ! PQgetvalue(result2, i, 0) ); footers[count_footers++] = xstrdup(buf); } *************** *** 863,868 **** --- 925,932 ---- PQclear(result2); PQclear(result3); PQclear(result4); + PQclear(result5); + PQclear(result6); } if (!error)