~ubuntu-branches/ubuntu/natty/postgresql-8.4/natty-updates

« back to all changes in this revision

Viewing changes to src/bin/psql/describe.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-05-05 00:58:06 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090505005806-c19tt7oyqb7kuw49
Tags: 8.4~beta1+cvs20090503-1
New upstream snapshot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
183
183
 
184
184
 
185
185
/* \df
186
 
 * Takes an optional regexp to select particular functions
 
186
 * Takes an optional regexp to select particular functions.
 
187
 *
 
188
 * As with \d, you can specify the kinds of functions you want:
 
189
 *
 
190
 * a for aggregates
 
191
 * n for normal
 
192
 * t for trigger
 
193
 * w for window
 
194
 *
 
195
 * and you can mix and match these in any order.
187
196
 */
188
197
bool
189
 
describeFunctions(const char *pattern, bool verbose, bool showSystem)
 
198
describeFunctions(const char *functypes, const char *pattern, bool verbose, bool showSystem)
190
199
{
 
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
 
191
205
        PQExpBufferData buf;
192
206
        PGresult   *res;
193
207
        printQueryOpt myopt = pset.popt;
194
208
 
 
209
        if (strlen(functypes) != strspn(functypes, "antwS+"))
 
210
        {
 
211
                fprintf(stderr, _("\\df only takes [antwS+] as options\n"));
 
212
                return true;
 
213
        }
 
214
 
 
215
        if (showWindow && pset.sversion < 80400)
 
216
        {
 
217
                fprintf(stderr, _("\\df does not take a \"w\" option in %d.%d.\n"),
 
218
                                pset.sversion / 10000, (pset.sversion / 100) % 100);
 
219
                return true;
 
220
        }
 
221
 
 
222
        if (!showAggregate && !showNormal && !showTrigger && !showWindow)
 
223
        {
 
224
                showAggregate = showNormal = showTrigger = true;
 
225
                if (pset.sversion >= 80400)
 
226
                        showWindow = true;
 
227
        }
 
228
 
195
229
        initPQExpBuffer(&buf);
196
230
 
197
231
        printfPQExpBuffer(&buf,
203
237
    if (pset.sversion >= 80400)
204
238
                appendPQExpBuffer(&buf,
205
239
                                                  "  pg_catalog.pg_get_function_result(p.oid) as \"%s\",\n"
206
 
                                                  "  pg_catalog.pg_get_function_arguments(p.oid) as \"%s\"",
 
240
                                                  "  pg_catalog.pg_get_function_arguments(p.oid) as \"%s\",\n"
 
241
                                                  " CASE\n"
 
242
                                                  "  WHEN p.proisagg THEN '%s'\n"
 
243
                                                  "  WHEN p.proiswindow THEN '%s'\n"
 
244
                                                  "  WHEN pg_catalog.pg_get_function_result(p.oid) = 'trigger' THEN '%s'\n"
 
245
                                                  "  ELSE '%s'\n"
 
246
                                                  "END as \"%s\"",
207
247
                                                  gettext_noop("Result data type"),
208
 
                                                  gettext_noop("Argument data types"));
 
248
                                                          gettext_noop("Argument data types"),
 
249
                                                          /* translator: "agg" is short for "aggregate" */
 
250
                                                          gettext_noop("agg"),
 
251
                                                          gettext_noop("window"),
 
252
                                                          gettext_noop("trigger"),
 
253
                                                          gettext_noop("normal"),
 
254
                                                          gettext_noop("Type"));
209
255
    else if (pset.sversion >= 80100)
210
256
                appendPQExpBuffer(&buf,
211
257
                                          "  CASE WHEN p.proretset THEN 'SETOF ' ELSE '' END ||\n"
238
284
                                          "      FROM\n"
239
285
                                          "        pg_catalog.generate_series(0, pg_catalog.array_upper(p.proargtypes, 1)) AS s(i)\n"
240
286
                                          "    ), ', ')\n"
 
287
                                          "  END AS \"%s\",\n"
 
288
                                          "  CASE\n"
 
289
                                          "    WHEN p.proisagg THEN '%s'\n"
 
290
                                          "    WHEN 'trigger' = pg_catalog.format_type(p.prorettype, NULL) THEN '%s'\n"
 
291
                                          "    ELSE '%s'\n"
241
292
                                          "  END AS \"%s\"",
242
293
                                                  gettext_noop("Result data type"),
243
 
                                                  gettext_noop("Argument data types"));
 
294
                                                  gettext_noop("Argument data types"),
 
295
                                                  /* translator: "agg" is short for "aggregate" */
 
296
                                                  gettext_noop("agg"),
 
297
                                                  gettext_noop("trigger"),
 
298
                                                  gettext_noop("normal"),
 
299
                                                  gettext_noop("Type"));
244
300
        else
245
301
                appendPQExpBuffer(&buf,
246
302
                                          "  CASE WHEN p.proretset THEN 'SETOF ' ELSE '' END ||\n"
247
303
                                  "  pg_catalog.format_type(p.prorettype, NULL) as \"%s\",\n"
248
 
                                          "  pg_catalog.oidvectortypes(p.proargtypes) as \"%s\"",
 
304
                                          "  pg_catalog.oidvectortypes(p.proargtypes) as \"%s\",\n"
 
305
                                          "  CASE\n"
 
306
                                          "    WHEN p.proisagg THEN '%s'\n"
 
307
                                          "    WHEN 'trigger' = pg_catalog.format_type(p.prorettype, NULL) THEN '%s'\n"
 
308
                                          "    ELSE '%s'\n"
 
309
                                          "  END AS \"%s\"",
249
310
                                                  gettext_noop("Result data type"),
250
 
                                                  gettext_noop("Argument data types"));
 
311
                                                  gettext_noop("Argument data types"),
 
312
                                                  /* translator: "agg" is short for "aggregate" */
 
313
                                                  gettext_noop("agg"),
 
314
                                                  gettext_noop("trigger"),
 
315
                                                  gettext_noop("normal"),
 
316
                                                  gettext_noop("Type"));
251
317
 
252
318
        if (verbose)
253
319
                appendPQExpBuffer(&buf,
274
340
                appendPQExpBuffer(&buf,
275
341
                                                  "     LEFT JOIN pg_catalog.pg_language l ON l.oid = p.prolang\n");
276
342
 
277
 
        appendPQExpBuffer(&buf, "WHERE NOT p.proisagg\n");
 
343
        processSQLNamePattern(pset.db, &buf, pattern, false, true,
 
344
                                                  "n.nspname", "p.proname", NULL,
 
345
                                                  "pg_catalog.pg_function_is_visible(p.oid)");
 
346
 
 
347
        if (showNormal && showAggregate && showTrigger && showWindow)
 
348
                /* Do nothing */;
 
349
        else if (showNormal)
 
350
        {
 
351
                if (!showWindow && pset.sversion >= 80400)
 
352
                                appendPQExpBuffer(&buf, "      AND NOT p.proiswindow\n");
 
353
                if (!showAggregate)
 
354
                        appendPQExpBuffer(&buf, "      AND NOT p.proisagg\n");
 
355
                if (!showTrigger)
 
356
                {
 
357
                        if (pset.sversion >= 80400)
 
358
                                appendPQExpBuffer(&buf,
 
359
                                                                  "      AND pg_catalog.pg_get_function_result(p.oid) <> 'trigger'\n");
 
360
                        else
 
361
                                appendPQExpBuffer(&buf,
 
362
                                                                  "      AND pg_catalog.format_type(p.prorettype, NULL) <> 'trigger'\n");
 
363
                }
 
364
        }
 
365
        else
 
366
        {
 
367
                bool    needs_or = false;
 
368
 
 
369
                appendPQExpBuffer(&buf, "      AND (\n         ");
 
370
                if (showAggregate)
 
371
                {
 
372
                        appendPQExpBuffer(&buf,"p.proisagg\n");
 
373
                        needs_or = true;
 
374
                }
 
375
                if (showTrigger)
 
376
                {
 
377
                        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");
 
385
                        needs_or = true;
 
386
                }
 
387
                if (showWindow)
 
388
                {
 
389
                        if (needs_or)
 
390
                                appendPQExpBuffer(&buf, "      OR ");
 
391
                        appendPQExpBuffer(&buf, "p.proiswindow\n");
 
392
                }
 
393
                appendPQExpBuffer(&buf, "      )\n");
 
394
        }
278
395
 
279
396
        if (!showSystem && !pattern)
280
397
                appendPQExpBuffer(&buf, "      AND n.nspname <> 'pg_catalog'\n"
281
398
                                                                "      AND n.nspname <> 'information_schema'\n");
282
399
 
283
 
        processSQLNamePattern(pset.db, &buf, pattern, true, false,
284
 
                                                  "n.nspname", "p.proname", NULL,
285
 
                                                  "pg_catalog.pg_function_is_visible(p.oid)");
286
 
 
287
400
        appendPQExpBuffer(&buf, "ORDER BY 1, 2, 4;");
288
401
 
289
402
        res = PSQLexec(buf.data, false);