88 *
99 * Copyright (c) 2000-2009, PostgreSQL Global Development Group
1010 *
11- * $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.211 2009/05/04 17:31:35 heikki Exp $
11+ * $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.212 2009/05/05 02:29:06 tgl Exp $
1212 */
1313#include "postgres_fe.h"
1414
@@ -197,14 +197,15 @@ describeTablespaces(const char *pattern, bool verbose)
197197bool
198198describeFunctions (const char * functypes , const char * pattern , bool verbose , bool showSystem )
199199{
200- bool showAggregate = strchr (functypes , 'a' ) != NULL ;
201- bool showNormal = strchr (functypes , 'n' ) != NULL ;
202- bool showTrigger = strchr (functypes , 't' ) != NULL ;
203- bool showWindow = strchr (functypes , 'w' ) != NULL ;
204-
200+ bool showAggregate = strchr (functypes , 'a' ) != NULL ;
201+ bool showNormal = strchr (functypes , 'n' ) != NULL ;
202+ bool showTrigger = strchr (functypes , 't' ) != NULL ;
203+ bool showWindow = strchr (functypes , 'w' ) != NULL ;
204+ bool have_where ;
205205 PQExpBufferData buf ;
206206 PGresult * res ;
207207 printQueryOpt myopt = pset .popt ;
208+ static const bool translate_columns [] = {false, false, false, false, true, true, false, false, false, false};
208209
209210 if (strlen (functypes ) != strspn (functypes , "antwS+" ))
210211 {
@@ -241,7 +242,7 @@ describeFunctions(const char *functypes, const char *pattern, bool verbose, bool
241242 " CASE\n"
242243 " WHEN p.proisagg THEN '%s'\n"
243244 " WHEN p.proiswindow THEN '%s'\n"
244- " WHEN pg_catalog.pg_get_function_result(p.oid) = 'trigger' THEN '%s'\n"
245+ " WHEN p.prorettype = 'pg_catalog. trigger'::pg_catalog.regtype THEN '%s'\n"
245246 " ELSE '%s'\n"
246247 "END as \"%s\"" ,
247248 gettext_noop ("Result data type" ),
@@ -287,7 +288,7 @@ describeFunctions(const char *functypes, const char *pattern, bool verbose, bool
287288 " END AS \"%s\",\n"
288289 " CASE\n"
289290 " WHEN p.proisagg THEN '%s'\n"
290- " WHEN 'trigger' = pg_catalog.format_type(p.prorettype, NULL) THEN '%s'\n"
291+ " WHEN p.prorettype = ' pg_catalog.trigger'::pg_catalog.regtype THEN '%s'\n"
291292 " ELSE '%s'\n"
292293 " END AS \"%s\"" ,
293294 gettext_noop ("Result data type" ),
@@ -304,7 +305,7 @@ describeFunctions(const char *functypes, const char *pattern, bool verbose, bool
304305 " pg_catalog.oidvectortypes(p.proargtypes) as \"%s\",\n"
305306 " CASE\n"
306307 " WHEN p.proisagg THEN '%s'\n"
307- " WHEN 'trigger' = pg_catalog.format_type(p.prorettype, NULL) THEN '%s'\n"
308+ " WHEN p.prorettype = ' pg_catalog.trigger'::pg_catalog.regtype THEN '%s'\n"
308309 " ELSE '%s'\n"
309310 " END AS \"%s\"" ,
310311 gettext_noop ("Result data type" ),
@@ -318,14 +319,17 @@ describeFunctions(const char *functypes, const char *pattern, bool verbose, bool
318319 if (verbose )
319320 appendPQExpBuffer (& buf ,
320321 ",\n CASE\n"
321- " WHEN p.provolatile = 'i' THEN 'immutable '\n"
322- " WHEN p.provolatile = 's' THEN 'stable '\n"
323- " WHEN p.provolatile = 'v' THEN 'volatile '\n"
322+ " WHEN p.provolatile = 'i' THEN '%s '\n"
323+ " WHEN p.provolatile = 's' THEN '%s '\n"
324+ " WHEN p.provolatile = 'v' THEN '%s '\n"
324325 "END as \"%s\""
325326 ",\n pg_catalog.pg_get_userbyid(p.proowner) as \"%s\",\n"
326327 " l.lanname as \"%s\",\n"
327328 " p.prosrc as \"%s\",\n"
328329 " pg_catalog.obj_description(p.oid, 'pg_proc') as \"%s\"" ,
330+ gettext_noop ("immutable" ),
331+ gettext_noop ("stable" ),
332+ gettext_noop ("volatile" ),
329333 gettext_noop ("Volatility" ),
330334 gettext_noop ("Owner" ),
331335 gettext_noop ("Language" ),
@@ -340,33 +344,54 @@ describeFunctions(const char *functypes, const char *pattern, bool verbose, bool
340344 appendPQExpBuffer (& buf ,
341345 " LEFT JOIN pg_catalog.pg_language l ON l.oid = p.prolang\n" );
342346
343- processSQLNamePattern (pset .db , & buf , pattern , false, true,
344- "n.nspname" , "p.proname" , NULL ,
345- "pg_catalog.pg_function_is_visible(p.oid)" );
347+ have_where = false;
346348
349+ /* filter by function type, if requested */
347350 if (showNormal && showAggregate && showTrigger && showWindow )
348351 /* Do nothing */ ;
349352 else if (showNormal )
350353 {
351- if (!showWindow && pset .sversion >= 80400 )
352- appendPQExpBuffer (& buf , " AND NOT p.proiswindow\n" );
353354 if (!showAggregate )
354- appendPQExpBuffer (& buf , " AND NOT p.proisagg\n" );
355+ {
356+ if (have_where )
357+ appendPQExpBuffer (& buf , " AND " );
358+ else
359+ {
360+ appendPQExpBuffer (& buf , "WHERE " );
361+ have_where = true;
362+ }
363+ appendPQExpBuffer (& buf , "NOT p.proisagg\n" );
364+ }
355365 if (!showTrigger )
356366 {
357- if (pset .sversion >= 80400 )
358- appendPQExpBuffer (& buf ,
359- " AND pg_catalog.pg_get_function_result(p.oid) <> 'trigger'\n" );
367+ if (have_where )
368+ appendPQExpBuffer (& buf , " AND " );
360369 else
361- appendPQExpBuffer (& buf ,
362- " AND pg_catalog.format_type(p.prorettype, NULL) <> 'trigger'\n" );
370+ {
371+ appendPQExpBuffer (& buf , "WHERE " );
372+ have_where = true;
373+ }
374+ appendPQExpBuffer (& buf , "p.prorettype <> 'pg_catalog.trigger'::pg_catalog.regtype\n" );
375+ }
376+ if (!showWindow && pset .sversion >= 80400 )
377+ {
378+ if (have_where )
379+ appendPQExpBuffer (& buf , " AND " );
380+ else
381+ {
382+ appendPQExpBuffer (& buf , "WHERE " );
383+ have_where = true;
384+ }
385+ appendPQExpBuffer (& buf , "NOT p.proiswindow\n" );
363386 }
364387 }
365388 else
366389 {
367390 bool needs_or = false;
368391
369- appendPQExpBuffer (& buf , " AND (\n " );
392+ appendPQExpBuffer (& buf , "WHERE (\n " );
393+ have_where = true;
394+ /* Note: at least one of these must be true ... */
370395 if (showAggregate )
371396 {
372397 appendPQExpBuffer (& buf ,"p.proisagg\n" );
@@ -375,24 +400,25 @@ describeFunctions(const char *functypes, const char *pattern, bool verbose, bool
375400 if (showTrigger )
376401 {
377402 if (needs_or )
378- appendPQExpBuffer (& buf , " OR " );
379- if (pset .sversion >= 80400 )
380- appendPQExpBuffer (& buf ,
381- "pg_catalog.pg_get_function_result(p.oid) = 'trigger'\n" );
382- else
383- appendPQExpBuffer (& buf ,
384- "'trigger' <> pg_catalog.format_type(p.prorettype, NULL)\n" );
403+ appendPQExpBuffer (& buf , " OR " );
404+ appendPQExpBuffer (& buf ,
405+ "p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype\n" );
385406 needs_or = true;
386407 }
387408 if (showWindow )
388409 {
389410 if (needs_or )
390- appendPQExpBuffer (& buf , " OR " );
411+ appendPQExpBuffer (& buf , " OR " );
391412 appendPQExpBuffer (& buf , "p.proiswindow\n" );
413+ needs_or = true;
392414 }
393415 appendPQExpBuffer (& buf , " )\n" );
394416 }
395417
418+ processSQLNamePattern (pset .db , & buf , pattern , have_where , true,
419+ "n.nspname" , "p.proname" , NULL ,
420+ "pg_catalog.pg_function_is_visible(p.oid)" );
421+
396422 if (!showSystem && !pattern )
397423 appendPQExpBuffer (& buf , " AND n.nspname <> 'pg_catalog'\n"
398424 " AND n.nspname <> 'information_schema'\n" );
@@ -407,6 +433,7 @@ describeFunctions(const char *functypes, const char *pattern, bool verbose, bool
407433 myopt .nullPrint = NULL ;
408434 myopt .title = _ ("List of functions" );
409435 myopt .translate_header = true;
436+ myopt .translate_columns = translate_columns ;
410437
411438 printQuery (res , & myopt , pset .queryFout , pset .logfile );
412439
0 commit comments