Commit 8516ce09 authored by Bruce Momjian's avatar Bruce Momjian

Have psql \d show the value of sequence columns.

Dickson S. Guedes
parent da0a9f1d
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* Copyright (c) 2000-2008, PostgreSQL Global Development Group * Copyright (c) 2000-2008, PostgreSQL Global Development Group
* *
* $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.179 2008/07/14 23:13:04 momjian Exp $ * $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.180 2008/07/15 03:16:03 momjian Exp $
*/ */
#include "postgres_fe.h" #include "postgres_fe.h"
...@@ -811,7 +811,8 @@ describeOneTableDetails(const char *schemaname, ...@@ -811,7 +811,8 @@ describeOneTableDetails(const char *schemaname,
printTableContent cont; printTableContent cont;
int i; int i;
char *view_def = NULL; char *view_def = NULL;
char *headers[5]; char *headers[6];
char **seq_values = NULL;
char **modifiers = NULL; char **modifiers = NULL;
char **ptr; char **ptr;
PQExpBufferData title; PQExpBufferData title;
...@@ -870,6 +871,35 @@ describeOneTableDetails(const char *schemaname, ...@@ -870,6 +871,35 @@ describeOneTableDetails(const char *schemaname,
atooid(PQgetvalue(res, 0, 6)) : 0; atooid(PQgetvalue(res, 0, 6)) : 0;
PQclear(res); PQclear(res);
/*
* This is used to get the values of a sequence and store it in an
* array that will be used later.
*/
if (tableinfo.relkind == 'S')
{
PGresult *result;
#define SEQ_NUM_COLS 10
printfPQExpBuffer(&buf,
"SELECT sequence_name, last_value, \n"
" start_value, increment_by, \n"
" max_value, min_value, cache_value, \n"
" log_cnt, is_cycled, is_called \n"
"FROM \"%s\"",
relationname);
result = PSQLexec(buf.data, false);
if (!result)
goto error_return;
seq_values = pg_malloc_zero((SEQ_NUM_COLS+1) * sizeof(*seq_values));
for (i = 0; i < SEQ_NUM_COLS; i++)
seq_values[i] = pg_strdup(PQgetvalue(result, 0, i));
PQclear(result);
}
/* Get column info (index requires additional checks) */ /* Get column info (index requires additional checks) */
printfPQExpBuffer(&buf, "SELECT a.attname,"); printfPQExpBuffer(&buf, "SELECT a.attname,");
appendPQExpBuffer(&buf, "\n pg_catalog.format_type(a.atttypid, a.atttypmod)," appendPQExpBuffer(&buf, "\n pg_catalog.format_type(a.atttypid, a.atttypmod),"
...@@ -932,7 +962,7 @@ describeOneTableDetails(const char *schemaname, ...@@ -932,7 +962,7 @@ describeOneTableDetails(const char *schemaname,
} }
/* Set the number of columns, and their names */ /* Set the number of columns, and their names */
cols = 2; cols += 2;
headers[0] = gettext_noop("Column"); headers[0] = gettext_noop("Column");
headers[1] = gettext_noop("Type"); headers[1] = gettext_noop("Type");
...@@ -943,6 +973,9 @@ describeOneTableDetails(const char *schemaname, ...@@ -943,6 +973,9 @@ describeOneTableDetails(const char *schemaname,
modifiers = pg_malloc_zero((numrows + 1) * sizeof(*modifiers)); modifiers = pg_malloc_zero((numrows + 1) * sizeof(*modifiers));
} }
if (tableinfo.relkind == 'S')
headers[cols++] = gettext_noop("Value");
if (verbose) if (verbose)
{ {
headers[cols++] = gettext_noop("Storage"); headers[cols++] = gettext_noop("Storage");
...@@ -981,6 +1014,10 @@ describeOneTableDetails(const char *schemaname, ...@@ -981,6 +1014,10 @@ describeOneTableDetails(const char *schemaname,
/* Type */ /* Type */
printTableAddCell(&cont, PQgetvalue(res, i, 1), false); printTableAddCell(&cont, PQgetvalue(res, i, 1), false);
/* A special 'Value' column for sequences */
if (tableinfo.relkind == 'S')
printTableAddCell(&cont, seq_values[i], false);
/* Extra: not null and default */ /* Extra: not null and default */
if (show_modifiers) if (show_modifiers)
{ {
...@@ -1544,6 +1581,13 @@ error_return: ...@@ -1544,6 +1581,13 @@ error_return:
termPQExpBuffer(&title); termPQExpBuffer(&title);
termPQExpBuffer(&tmpbuf); termPQExpBuffer(&tmpbuf);
if (tableinfo.relkind == 'S')
{
for (ptr = seq_values; *ptr; ptr++)
free(*ptr);
free(seq_values);
}
if (show_modifiers) if (show_modifiers)
{ {
for (ptr = modifiers; *ptr; ptr++) for (ptr = modifiers; *ptr; ptr++)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment