88 *
99 * Copyright (c) 2000-2009, PostgreSQL Global Development Group
1010 *
11- * $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.220 2009/07/06 17:01:42 petere Exp $
11+ * $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.221 2009/07/07 16:28:38 tgl Exp $
1212 */
1313#include "postgres_fe.h"
1414
@@ -1028,7 +1028,7 @@ describeOneTableDetails(const char *schemaname,
10281028char * * ptr ;
10291029PQExpBufferData title ;
10301030PQExpBufferData tmpbuf ;
1031- int cols = 0 ;
1031+ int cols ;
10321032int numrows = 0 ;
10331033struct
10341034{
@@ -1156,23 +1156,19 @@ describeOneTableDetails(const char *schemaname,
11561156PQclear (result );
11571157}
11581158
1159- /* Get column info(index requires additional checks) */
1159+ /* Get column info */
11601160printfPQExpBuffer (& buf ,"SELECT a.attname," );
11611161appendPQExpBuffer (& buf ,"\n pg_catalog.format_type(a.atttypid, a.atttypmod),"
11621162"\n (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)"
11631163"\n FROM pg_catalog.pg_attrdef d"
11641164"\n WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef),"
11651165"\n a.attnotnull, a.attnum" );
11661166if (tableinfo .relkind == 'i' )
1167- appendPQExpBuffer (& buf ,", pg_get_indexdef(i.indexrelid, a.attnum, TRUE) AS indexdef" );
1167+ appendPQExpBuffer (& buf ,",\n pg_catalog. pg_get_indexdef(a.attrelid, a.attnum, TRUE) AS indexdef" );
11681168if (verbose )
1169- appendPQExpBuffer (& buf ,", a.attstorage, pg_catalog.col_description(a.attrelid, a.attnum)" );
1169+ appendPQExpBuffer (& buf ,",\n a.attstorage, pg_catalog.col_description(a.attrelid, a.attnum)" );
11701170appendPQExpBuffer (& buf ,"\nFROM pg_catalog.pg_attribute a" );
1171- if (tableinfo .relkind == 'i' )
1172- appendPQExpBuffer (& buf ,", pg_catalog.pg_index i" );
11731171appendPQExpBuffer (& buf ,"\nWHERE a.attrelid = '%s' AND a.attnum > 0 AND NOT a.attisdropped" ,oid );
1174- if (tableinfo .relkind == 'i' )
1175- appendPQExpBuffer (& buf ," AND a.attrelid = i.indexrelid" );
11761172appendPQExpBuffer (& buf ,"\nORDER BY a.attnum" );
11771173
11781174res = PSQLexec (buf .data , false);
@@ -1220,9 +1216,9 @@ describeOneTableDetails(const char *schemaname,
12201216}
12211217
12221218/* Set the number of columns, and their names */
1223- cols += 2 ;
12241219headers [0 ]= gettext_noop ("Column" );
12251220headers [1 ]= gettext_noop ("Type" );
1221+ cols = 2 ;
12261222
12271223if (tableinfo .relkind == 'r' || tableinfo .relkind == 'v' )
12281224{
@@ -1302,15 +1298,15 @@ describeOneTableDetails(const char *schemaname,
13021298if (tableinfo .relkind == 'S' )
13031299printTableAddCell (& cont ,seq_values [i ], false);
13041300
1305- /* Expression for index */
1301+ /* Expression for indexcolumn */
13061302if (tableinfo .relkind == 'i' )
13071303printTableAddCell (& cont ,PQgetvalue (res ,i ,5 ), false);
13081304
13091305/* Storage and Description */
13101306if (verbose )
13111307{
1312- int fnum = (tableinfo .relkind == 'i' ?6 :5 );
1313- char * storage = PQgetvalue (res ,i ,fnum );
1308+ int firstvcol = (tableinfo .relkind == 'i' ?6 :5 );
1309+ char * storage = PQgetvalue (res ,i ,firstvcol );
13141310
13151311/* these strings are literal in our syntax, so not translated. */
13161312printTableAddCell (& cont , (storage [0 ]== 'p' ?"plain" :
@@ -1319,7 +1315,7 @@ describeOneTableDetails(const char *schemaname,
13191315(storage [0 ]== 'e' ?"external" :
13201316"???" )))),
13211317 false);
1322- printTableAddCell (& cont ,PQgetvalue (res ,i ,fnum + 1 ), false);
1318+ printTableAddCell (& cont ,PQgetvalue (res ,i ,firstvcol + 1 ), false);
13231319}
13241320}
13251321
@@ -1844,20 +1840,23 @@ describeOneTableDetails(const char *schemaname,
18441840}
18451841else
18461842{
1847- /* display the list of child tables*/
1843+ /* display the list of child tables */
1844+ const char * ct = _ ("Child tables" );
1845+
18481846for (i = 0 ;i < tuples ;i ++ )
1849- {
1850- const char * ct = _ ("Child tables" );
1851-
1852- if (i == 0 )
1853- printfPQExpBuffer (& buf ,"%s: %s" ,ct ,PQgetvalue (result ,i ,0 ));
1854- else
1855- printfPQExpBuffer (& buf ,"%*s %s" , (int )strlen (ct ),"" ,PQgetvalue (result ,i ,0 ));
1856- if (i < tuples - 1 )
1857- appendPQExpBuffer (& buf ,"," );
1858-
1859- printTableAddFooter (& cont ,buf .data );
1860- }
1847+ {
1848+ if (i == 0 )
1849+ printfPQExpBuffer (& buf ,"%s: %s" ,
1850+ ct ,PQgetvalue (result ,i ,0 ));
1851+ else
1852+ printfPQExpBuffer (& buf ,"%*s %s" ,
1853+ (int )strlen (ct ),"" ,
1854+ PQgetvalue (result ,i ,0 ));
1855+ if (i < tuples - 1 )
1856+ appendPQExpBuffer (& buf ,"," );
1857+
1858+ printTableAddFooter (& cont ,buf .data );
1859+ }
18611860}
18621861PQclear (result );
18631862