Rework option set of oid2name
oid2name has done little effort to keep an interface consistent with other binary utilities: - -H was used instead of -h/-host. This option is now marked as deprecated, still its output is accepted to be backward-compatible. - -P has been removed from the code, and was still documented. - All options gain long aliases, making connection options more similar to other binaries. - Document environment variables which could be used: PGHOST, PGPORT and PGUSER. A basic set of TAP tests is added on the way, and documentation is cleaned up to be more consistent with other things. Author: Tatsuro Yamada Reviewed-by: Michael Paquier Discussion: https://postgr.es/m/c7e7f25c-1747-cd0f-9335-390bc97b2db5@lab.ntt.co.jp
This commit is contained in:
parent
c8ea87e4bd
commit
1aaf532dea
2
contrib/oid2name/.gitignore
vendored
2
contrib/oid2name/.gitignore
vendored
@ -1 +1,3 @@
|
|||||||
/oid2name
|
/oid2name
|
||||||
|
|
||||||
|
/tmp_check/
|
||||||
|
@ -19,3 +19,9 @@ top_builddir = ../..
|
|||||||
include $(top_builddir)/src/Makefile.global
|
include $(top_builddir)/src/Makefile.global
|
||||||
include $(top_srcdir)/contrib/contrib-global.mk
|
include $(top_srcdir)/contrib/contrib-global.mk
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
check:
|
||||||
|
$(prove_check)
|
||||||
|
|
||||||
|
installcheck:
|
||||||
|
$(prove_installcheck)
|
||||||
|
@ -14,6 +14,8 @@
|
|||||||
#include "fe_utils/connect.h"
|
#include "fe_utils/connect.h"
|
||||||
#include "libpq-fe.h"
|
#include "libpq-fe.h"
|
||||||
#include "pg_getopt.h"
|
#include "pg_getopt.h"
|
||||||
|
#include "getopt_long.h"
|
||||||
|
|
||||||
|
|
||||||
/* an extensible array to keep track of elements to show */
|
/* an extensible array to keep track of elements to show */
|
||||||
typedef struct
|
typedef struct
|
||||||
@ -60,8 +62,28 @@ void sql_exec_dumpalltbspc(PGconn *, struct options *);
|
|||||||
void
|
void
|
||||||
get_opts(int argc, char **argv, struct options *my_opts)
|
get_opts(int argc, char **argv, struct options *my_opts)
|
||||||
{
|
{
|
||||||
|
static struct option long_options[] = {
|
||||||
|
{"dbname", required_argument, NULL, 'd'},
|
||||||
|
{"host", required_argument, NULL, 'h'},
|
||||||
|
{"host", required_argument, NULL, 'H'}, /* deprecated */
|
||||||
|
{"filenode", required_argument, NULL, 'f'},
|
||||||
|
{"indexes", no_argument, NULL, 'i'},
|
||||||
|
{"oid", required_argument, NULL, 'o'},
|
||||||
|
{"port", required_argument, NULL, 'p'},
|
||||||
|
{"quiet", no_argument, NULL, 'q'},
|
||||||
|
{"tablespaces", no_argument, NULL, 's'},
|
||||||
|
{"system-objects", no_argument, NULL, 'S'},
|
||||||
|
{"table", required_argument, NULL, 't'},
|
||||||
|
{"username", required_argument, NULL, 'U'},
|
||||||
|
{"version", no_argument, NULL, 'V'},
|
||||||
|
{"extended", no_argument, NULL, 'x'},
|
||||||
|
{"help", no_argument, NULL, '?'},
|
||||||
|
{NULL, 0, NULL, 0}
|
||||||
|
};
|
||||||
|
|
||||||
int c;
|
int c;
|
||||||
const char *progname;
|
const char *progname;
|
||||||
|
int optindex;
|
||||||
|
|
||||||
progname = get_progname(argv[0]);
|
progname = get_progname(argv[0]);
|
||||||
|
|
||||||
@ -93,7 +115,7 @@ get_opts(int argc, char **argv, struct options *my_opts)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* get opts */
|
/* get opts */
|
||||||
while ((c = getopt(argc, argv, "H:p:U:d:t:o:f:qSxish")) != -1)
|
while ((c = getopt_long(argc, argv, "d:f:h:H:io:p:qsSt:U:x", long_options, &optindex)) != -1)
|
||||||
{
|
{
|
||||||
switch (c)
|
switch (c)
|
||||||
{
|
{
|
||||||
@ -102,54 +124,35 @@ get_opts(int argc, char **argv, struct options *my_opts)
|
|||||||
my_opts->dbname = pg_strdup(optarg);
|
my_opts->dbname = pg_strdup(optarg);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* specify one tablename to show */
|
|
||||||
case 't':
|
|
||||||
add_one_elt(optarg, my_opts->tables);
|
|
||||||
break;
|
|
||||||
|
|
||||||
/* specify one Oid to show */
|
|
||||||
case 'o':
|
|
||||||
add_one_elt(optarg, my_opts->oids);
|
|
||||||
break;
|
|
||||||
|
|
||||||
/* specify one filenode to show */
|
/* specify one filenode to show */
|
||||||
case 'f':
|
case 'f':
|
||||||
add_one_elt(optarg, my_opts->filenodes);
|
add_one_elt(optarg, my_opts->filenodes);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* don't show headers */
|
|
||||||
case 'q':
|
|
||||||
my_opts->quiet = true;
|
|
||||||
break;
|
|
||||||
|
|
||||||
/* host to connect to */
|
/* host to connect to */
|
||||||
case 'H':
|
case 'H': /* deprecated */
|
||||||
|
case 'h':
|
||||||
my_opts->hostname = pg_strdup(optarg);
|
my_opts->hostname = pg_strdup(optarg);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* port to connect to on remote host */
|
|
||||||
case 'p':
|
|
||||||
my_opts->port = pg_strdup(optarg);
|
|
||||||
break;
|
|
||||||
|
|
||||||
/* username */
|
|
||||||
case 'U':
|
|
||||||
my_opts->username = pg_strdup(optarg);
|
|
||||||
break;
|
|
||||||
|
|
||||||
/* display system tables */
|
|
||||||
case 'S':
|
|
||||||
my_opts->systables = true;
|
|
||||||
break;
|
|
||||||
|
|
||||||
/* also display indexes */
|
/* also display indexes */
|
||||||
case 'i':
|
case 'i':
|
||||||
my_opts->indexes = true;
|
my_opts->indexes = true;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* display extra columns */
|
/* specify one Oid to show */
|
||||||
case 'x':
|
case 'o':
|
||||||
my_opts->extended = true;
|
add_one_elt(optarg, my_opts->oids);
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* port to connect to on remote host */
|
||||||
|
case 'p':
|
||||||
|
my_opts->port = pg_strdup(optarg);
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* don't show headers */
|
||||||
|
case 'q':
|
||||||
|
my_opts->quiet = true;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* dump tablespaces only */
|
/* dump tablespaces only */
|
||||||
@ -157,9 +160,24 @@ get_opts(int argc, char **argv, struct options *my_opts)
|
|||||||
my_opts->tablespaces = true;
|
my_opts->tablespaces = true;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'h':
|
/* display system tables */
|
||||||
help(progname);
|
case 'S':
|
||||||
exit(0);
|
my_opts->systables = true;
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* specify one tablename to show */
|
||||||
|
case 't':
|
||||||
|
add_one_elt(optarg, my_opts->tables);
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* username */
|
||||||
|
case 'U':
|
||||||
|
my_opts->username = pg_strdup(optarg);
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* display extra columns */
|
||||||
|
case 'x':
|
||||||
|
my_opts->extended = true;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@ -176,20 +194,22 @@ help(const char *progname)
|
|||||||
"Usage:\n"
|
"Usage:\n"
|
||||||
" %s [OPTION]...\n"
|
" %s [OPTION]...\n"
|
||||||
"\nOptions:\n"
|
"\nOptions:\n"
|
||||||
" -d DBNAME database to connect to\n"
|
" -f, --filenode=FILENODE show info for table with given file node\n"
|
||||||
" -f FILENODE show info for table with given file node\n"
|
" -i, --indexes show indexes and sequences too\n"
|
||||||
" -H HOSTNAME database server host or socket directory\n"
|
" -o, --oid=OID show info for table with given OID\n"
|
||||||
" -i show indexes and sequences too\n"
|
" -q, --quiet quiet (don't show headers)\n"
|
||||||
" -o OID show info for table with given OID\n"
|
" -s, --tablespaces show all tablespaces\n"
|
||||||
" -p PORT database server port number\n"
|
" -S, --system-objects show system objects too\n"
|
||||||
" -q quiet (don't show headers)\n"
|
" -t, --table=TABLE show info for named table\n"
|
||||||
" -s show all tablespaces\n"
|
|
||||||
" -S show system objects too\n"
|
|
||||||
" -t TABLE show info for named table\n"
|
|
||||||
" -U NAME connect as specified database user\n"
|
|
||||||
" -V, --version output version information, then exit\n"
|
" -V, --version output version information, then exit\n"
|
||||||
" -x extended (show additional columns)\n"
|
" -x, --extended extended (show additional columns)\n"
|
||||||
" -?, --help show this help, then exit\n"
|
" -?, --help show this help, then exit\n"
|
||||||
|
"\nConnection options:\n"
|
||||||
|
" -d, --dbname=DBNAME database to connect to\n"
|
||||||
|
" -h, --host=HOSTNAME database server host or socket directory\n"
|
||||||
|
" -H same as -h, deprecated option\n"
|
||||||
|
" -p, --port=PORT database server port number\n"
|
||||||
|
" -U, --username=USERNAME connect as specified database user\n"
|
||||||
"\nThe default action is to show all database OIDs.\n\n"
|
"\nThe default action is to show all database OIDs.\n\n"
|
||||||
"Report bugs to <pgsql-bugs@postgresql.org>.\n",
|
"Report bugs to <pgsql-bugs@postgresql.org>.\n",
|
||||||
progname, progname);
|
progname, progname);
|
||||||
|
12
contrib/oid2name/t/001_basic.pl
Normal file
12
contrib/oid2name/t/001_basic.pl
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
use strict;
|
||||||
|
use warnings;
|
||||||
|
|
||||||
|
use TestLib;
|
||||||
|
use Test::More tests => 8;
|
||||||
|
|
||||||
|
#########################################
|
||||||
|
# Basic checks
|
||||||
|
|
||||||
|
program_help_ok('oid2name');
|
||||||
|
program_version_ok('oid2name');
|
||||||
|
program_options_handling_ok('oid2name');
|
@ -60,41 +60,48 @@
|
|||||||
<variablelist>
|
<variablelist>
|
||||||
|
|
||||||
<varlistentry>
|
<varlistentry>
|
||||||
<term><option>-f</option> <replaceable>filenode</replaceable></term>
|
<term><option>-f <replaceable class="parameter">filenode</replaceable></option></term>
|
||||||
<listitem><para>show info for table with filenode <replaceable>filenode</replaceable></para></listitem>
|
<term><option>--filenode=<replaceable class="parameter">filenode</replaceable></option></term>
|
||||||
|
<listitem><para>show info for table with filenode <replaceable>filenode</replaceable>.</para></listitem>
|
||||||
</varlistentry>
|
</varlistentry>
|
||||||
|
|
||||||
<varlistentry>
|
<varlistentry>
|
||||||
<term><option>-i</option></term>
|
<term><option>-i</option></term>
|
||||||
<listitem><para>include indexes and sequences in the listing</para></listitem>
|
<term><option>--indexes</option></term>
|
||||||
|
<listitem><para>include indexes and sequences in the listing.</para></listitem>
|
||||||
</varlistentry>
|
</varlistentry>
|
||||||
|
|
||||||
<varlistentry>
|
<varlistentry>
|
||||||
<term><option>-o</option> <replaceable>oid</replaceable></term>
|
<term><option>-o <replaceable class="parameter">oid</replaceable></option></term>
|
||||||
<listitem><para>show info for table with OID <replaceable>oid</replaceable></para></listitem>
|
<term><option>--oid=<replaceable class="parameter">oid</replaceable></option></term>
|
||||||
|
<listitem><para>show info for table with OID <replaceable>oid</replaceable>.</para></listitem>
|
||||||
</varlistentry>
|
</varlistentry>
|
||||||
|
|
||||||
<varlistentry>
|
<varlistentry>
|
||||||
<term><option>-q</option></term>
|
<term><option>-q</option></term>
|
||||||
<listitem><para>omit headers (useful for scripting)</para></listitem>
|
<term><option>--quiet</option></term>
|
||||||
|
<listitem><para>omit headers (useful for scripting).</para></listitem>
|
||||||
</varlistentry>
|
</varlistentry>
|
||||||
|
|
||||||
<varlistentry>
|
<varlistentry>
|
||||||
<term><option>-s</option></term>
|
<term><option>-s</option></term>
|
||||||
<listitem><para>show tablespace OIDs</para></listitem>
|
<term><option>--tablespaces</option></term>
|
||||||
|
<listitem><para>show tablespace OIDs.</para></listitem>
|
||||||
</varlistentry>
|
</varlistentry>
|
||||||
|
|
||||||
<varlistentry>
|
<varlistentry>
|
||||||
<term><option>-S</option></term>
|
<term><option>-S</option></term>
|
||||||
|
<term><option>--system-objects</option></term>
|
||||||
<listitem><para>include system objects (those in
|
<listitem><para>include system objects (those in
|
||||||
<option>information_schema</option>, <option>pg_toast</option>
|
<option>information_schema</option>, <option>pg_toast</option>
|
||||||
and <option>pg_catalog</option> schemas)
|
and <option>pg_catalog</option> schemas).
|
||||||
</para></listitem>
|
</para></listitem>
|
||||||
</varlistentry>
|
</varlistentry>
|
||||||
|
|
||||||
<varlistentry>
|
<varlistentry>
|
||||||
<term><option>-t</option> <replaceable>tablename_pattern</replaceable></term>
|
<term><option>-t <replaceable class="parameter">tablename_pattern</replaceable></option></term>
|
||||||
<listitem><para>show info for table(s) matching <replaceable>tablename_pattern</replaceable></para></listitem>
|
<term><option>--table=<replaceable class="parameter">tablename_pattern</replaceable></option></term>
|
||||||
|
<listitem><para>show info for table(s) matching <replaceable class="parameter">tablename_pattern</replaceable>.</para></listitem>
|
||||||
</varlistentry>
|
</varlistentry>
|
||||||
|
|
||||||
<varlistentry>
|
<varlistentry>
|
||||||
@ -109,8 +116,9 @@
|
|||||||
|
|
||||||
<varlistentry>
|
<varlistentry>
|
||||||
<term><option>-x</option></term>
|
<term><option>-x</option></term>
|
||||||
|
<term><option>--extended</option></term>
|
||||||
<listitem><para>display more information about each object shown: tablespace name,
|
<listitem><para>display more information about each object shown: tablespace name,
|
||||||
schema name, and OID
|
schema name, and OID.
|
||||||
</para></listitem>
|
</para></listitem>
|
||||||
</varlistentry>
|
</varlistentry>
|
||||||
|
|
||||||
@ -133,29 +141,34 @@
|
|||||||
|
|
||||||
<variablelist>
|
<variablelist>
|
||||||
<varlistentry>
|
<varlistentry>
|
||||||
<term><option>-d</option> <replaceable>database</replaceable></term>
|
<term><option>-d <replaceable class="parameter">database</replaceable></option></term>
|
||||||
<listitem><para>database to connect to</para></listitem>
|
<term><option>--dbname=<replaceable class="parameter">database</replaceable></option></term>
|
||||||
|
<listitem><para>database to connect to.</para></listitem>
|
||||||
</varlistentry>
|
</varlistentry>
|
||||||
|
|
||||||
<varlistentry>
|
<varlistentry>
|
||||||
<term><option>-H</option> <replaceable>host</replaceable></term>
|
<term><option>-h <replaceable class="parameter">host</replaceable></option></term>
|
||||||
<listitem><para>database server's host</para></listitem>
|
<term><option>--host=<replaceable class="parameter">host</replaceable></option></term>
|
||||||
|
<listitem><para>database server's host.</para></listitem>
|
||||||
</varlistentry>
|
</varlistentry>
|
||||||
|
|
||||||
<varlistentry>
|
<varlistentry>
|
||||||
<term><option>-p</option> <replaceable>port</replaceable></term>
|
<term><option>-H <replaceable class="parameter">host</replaceable></option></term>
|
||||||
<listitem><para>database server's port</para></listitem>
|
<listitem><para>database server's host. Use of this parameter is
|
||||||
|
<emphasis>deprecated</emphasis> as of
|
||||||
|
<productname>PostgreSQL</productname> 12.</para></listitem>
|
||||||
</varlistentry>
|
</varlistentry>
|
||||||
|
|
||||||
<varlistentry>
|
<varlistentry>
|
||||||
<term><option>-U</option> <replaceable>username</replaceable></term>
|
<term><option>-p <replaceable class="parameter">port</replaceable></option></term>
|
||||||
<listitem><para>user name to connect as</para></listitem>
|
<term><option>--port=<replaceable class="parameter">port</replaceable></option></term>
|
||||||
|
<listitem><para>database server's port.</para></listitem>
|
||||||
</varlistentry>
|
</varlistentry>
|
||||||
|
|
||||||
<varlistentry>
|
<varlistentry>
|
||||||
<term><option>-P</option> <replaceable>password</replaceable></term>
|
<term><option>-U <replaceable class="parameter">username</replaceable></option></term>
|
||||||
<listitem><para>password (deprecated — putting this on the command line
|
<term><option>--username=<replaceable class="parameter">username</replaceable></option></term>
|
||||||
is a security hazard)</para></listitem>
|
<listitem><para>user name to connect as.</para></listitem>
|
||||||
</varlistentry>
|
</varlistentry>
|
||||||
|
|
||||||
</variablelist>
|
</variablelist>
|
||||||
@ -188,6 +201,30 @@
|
|||||||
</para>
|
</para>
|
||||||
</refsect1>
|
</refsect1>
|
||||||
|
|
||||||
|
<refsect1>
|
||||||
|
<title>Environment</title>
|
||||||
|
|
||||||
|
<variablelist>
|
||||||
|
<varlistentry>
|
||||||
|
<term><envar>PGHOST</envar></term>
|
||||||
|
<term><envar>PGPORT</envar></term>
|
||||||
|
<term><envar>PGUSER</envar></term>
|
||||||
|
|
||||||
|
<listitem>
|
||||||
|
<para>
|
||||||
|
Default connection parameters.
|
||||||
|
</para>
|
||||||
|
</listitem>
|
||||||
|
</varlistentry>
|
||||||
|
</variablelist>
|
||||||
|
|
||||||
|
<para>
|
||||||
|
This utility, like most other <productname>PostgreSQL</productname>
|
||||||
|
utilities, also uses the environment variables supported by
|
||||||
|
<application>libpq</application> (see <xref linkend="libpq-envars"/>).
|
||||||
|
</para>
|
||||||
|
</refsect1>
|
||||||
|
|
||||||
<refsect1>
|
<refsect1>
|
||||||
<title>Notes</title>
|
<title>Notes</title>
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user