LCOV - code coverage report
Current view: top level - contrib/oid2name - oid2name.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 37 244 15.2 %
Date: 2019-11-22 06:06:53 Functions: 3 11 27.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * oid2name, a PostgreSQL app to map OIDs on the filesystem
       3             :  * to table and database names.
       4             :  *
       5             :  * Originally by
       6             :  * B. Palmer, bpalmer@crimelabs.net 1-17-2001
       7             :  *
       8             :  * contrib/oid2name/oid2name.c
       9             :  */
      10             : #include "postgres_fe.h"
      11             : 
      12             : #include "catalog/pg_class_d.h"
      13             : #include "common/logging.h"
      14             : #include "fe_utils/connect.h"
      15             : #include "getopt_long.h"
      16             : #include "libpq-fe.h"
      17             : #include "pg_getopt.h"
      18             : 
      19             : /* an extensible array to keep track of elements to show */
      20             : typedef struct
      21             : {
      22             :     char      **array;
      23             :     int         num;
      24             :     int         alloc;
      25             : } eary;
      26             : 
      27             : /* these are the opts structures for command line params */
      28             : struct options
      29             : {
      30             :     eary       *tables;
      31             :     eary       *oids;
      32             :     eary       *filenodes;
      33             : 
      34             :     bool        quiet;
      35             :     bool        systables;
      36             :     bool        indexes;
      37             :     bool        nodb;
      38             :     bool        extended;
      39             :     bool        tablespaces;
      40             : 
      41             :     char       *dbname;
      42             :     char       *hostname;
      43             :     char       *port;
      44             :     char       *username;
      45             :     const char *progname;
      46             : };
      47             : 
      48             : /* function prototypes */
      49             : static void help(const char *progname);
      50             : void        get_opts(int, char **, struct options *);
      51             : void        add_one_elt(char *eltname, eary *eary);
      52             : char       *get_comma_elts(eary *eary);
      53             : PGconn     *sql_conn(struct options *);
      54             : int         sql_exec(PGconn *, const char *sql, bool quiet);
      55             : void        sql_exec_dumpalldbs(PGconn *, struct options *);
      56             : void        sql_exec_dumpalltables(PGconn *, struct options *);
      57             : void        sql_exec_searchtables(PGconn *, struct options *);
      58             : void        sql_exec_dumpalltbspc(PGconn *, struct options *);
      59             : 
      60             : /* function to parse command line options and check for some usage errors. */
      61             : void
      62           6 : get_opts(int argc, char **argv, struct options *my_opts)
      63             : {
      64             :     static struct option long_options[] = {
      65             :         {"dbname", required_argument, NULL, 'd'},
      66             :         {"host", required_argument, NULL, 'h'},
      67             :         {"host", required_argument, NULL, 'H'}, /* deprecated */
      68             :         {"filenode", required_argument, NULL, 'f'},
      69             :         {"indexes", no_argument, NULL, 'i'},
      70             :         {"oid", required_argument, NULL, 'o'},
      71             :         {"port", required_argument, NULL, 'p'},
      72             :         {"quiet", no_argument, NULL, 'q'},
      73             :         {"tablespaces", no_argument, NULL, 's'},
      74             :         {"system-objects", no_argument, NULL, 'S'},
      75             :         {"table", required_argument, NULL, 't'},
      76             :         {"username", required_argument, NULL, 'U'},
      77             :         {"version", no_argument, NULL, 'V'},
      78             :         {"extended", no_argument, NULL, 'x'},
      79             :         {"help", no_argument, NULL, '?'},
      80             :         {NULL, 0, NULL, 0}
      81             :     };
      82             : 
      83             :     int         c;
      84             :     const char *progname;
      85             :     int         optindex;
      86             : 
      87           6 :     pg_logging_init(argv[0]);
      88           6 :     progname = get_progname(argv[0]);
      89             : 
      90             :     /* set the defaults */
      91           6 :     my_opts->quiet = false;
      92           6 :     my_opts->systables = false;
      93           6 :     my_opts->indexes = false;
      94           6 :     my_opts->nodb = false;
      95           6 :     my_opts->extended = false;
      96           6 :     my_opts->tablespaces = false;
      97           6 :     my_opts->dbname = NULL;
      98           6 :     my_opts->hostname = NULL;
      99           6 :     my_opts->port = NULL;
     100           6 :     my_opts->username = NULL;
     101           6 :     my_opts->progname = progname;
     102             : 
     103           6 :     if (argc > 1)
     104             :     {
     105           6 :         if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
     106             :         {
     107           2 :             help(progname);
     108           2 :             exit(0);
     109             :         }
     110           4 :         if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
     111             :         {
     112           2 :             puts("oid2name (PostgreSQL) " PG_VERSION);
     113           2 :             exit(0);
     114             :         }
     115             :     }
     116             : 
     117             :     /* get opts */
     118           4 :     while ((c = getopt_long(argc, argv, "d:f:h:H:io:p:qsSt:U:x", long_options, &optindex)) != -1)
     119             :     {
     120           2 :         switch (c)
     121             :         {
     122             :                 /* specify the database */
     123             :             case 'd':
     124           0 :                 my_opts->dbname = pg_strdup(optarg);
     125           0 :                 break;
     126             : 
     127             :                 /* specify one filenode to show */
     128             :             case 'f':
     129           0 :                 add_one_elt(optarg, my_opts->filenodes);
     130           0 :                 break;
     131             : 
     132             :                 /* host to connect to */
     133             :             case 'H':           /* deprecated */
     134             :             case 'h':
     135           0 :                 my_opts->hostname = pg_strdup(optarg);
     136           0 :                 break;
     137             : 
     138             :                 /* also display indexes */
     139             :             case 'i':
     140           0 :                 my_opts->indexes = true;
     141           0 :                 break;
     142             : 
     143             :                 /* specify one Oid to show */
     144             :             case 'o':
     145           0 :                 add_one_elt(optarg, my_opts->oids);
     146           0 :                 break;
     147             : 
     148             :                 /* port to connect to on remote host */
     149             :             case 'p':
     150           0 :                 my_opts->port = pg_strdup(optarg);
     151           0 :                 break;
     152             : 
     153             :                 /* don't show headers */
     154             :             case 'q':
     155           0 :                 my_opts->quiet = true;
     156           0 :                 break;
     157             : 
     158             :                 /* dump tablespaces only */
     159             :             case 's':
     160           0 :                 my_opts->tablespaces = true;
     161           0 :                 break;
     162             : 
     163             :                 /* display system tables */
     164             :             case 'S':
     165           0 :                 my_opts->systables = true;
     166           0 :                 break;
     167             : 
     168             :                 /* specify one tablename to show */
     169             :             case 't':
     170           0 :                 add_one_elt(optarg, my_opts->tables);
     171           0 :                 break;
     172             : 
     173             :                 /* username */
     174             :             case 'U':
     175           0 :                 my_opts->username = pg_strdup(optarg);
     176           0 :                 break;
     177             : 
     178             :                 /* display extra columns */
     179             :             case 'x':
     180           0 :                 my_opts->extended = true;
     181           0 :                 break;
     182             : 
     183             :             default:
     184           2 :                 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
     185           2 :                 exit(1);
     186             :         }
     187             :     }
     188             : 
     189           0 :     if (optind < argc)
     190             :     {
     191           0 :         fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
     192           0 :                 progname, argv[optind]);
     193           0 :         fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
     194           0 :         exit(1);
     195             :     }
     196           0 : }
     197             : 
     198             : static void
     199           2 : help(const char *progname)
     200             : {
     201           2 :     printf("%s helps examining the file structure used by PostgreSQL.\n\n"
     202             :            "Usage:\n"
     203             :            "  %s [OPTION]...\n"
     204             :            "\nOptions:\n"
     205             :            "  -f, --filenode=FILENODE    show info for table with given file node\n"
     206             :            "  -i, --indexes              show indexes and sequences too\n"
     207             :            "  -o, --oid=OID              show info for table with given OID\n"
     208             :            "  -q, --quiet                quiet (don't show headers)\n"
     209             :            "  -s, --tablespaces          show all tablespaces\n"
     210             :            "  -S, --system-objects       show system objects too\n"
     211             :            "  -t, --table=TABLE          show info for named table\n"
     212             :            "  -V, --version              output version information, then exit\n"
     213             :            "  -x, --extended             extended (show additional columns)\n"
     214             :            "  -?, --help                 show this help, then exit\n"
     215             :            "\nConnection options:\n"
     216             :            "  -d, --dbname=DBNAME        database to connect to\n"
     217             :            "  -h, --host=HOSTNAME        database server host or socket directory\n"
     218             :            "  -H                         same as -h, deprecated option\n"
     219             :            "  -p, --port=PORT            database server port number\n"
     220             :            "  -U, --username=USERNAME    connect as specified database user\n"
     221             :            "\nThe default action is to show all database OIDs.\n\n"
     222             :            "Report bugs to <pgsql-bugs@lists.postgresql.org>.\n",
     223             :            progname, progname);
     224           2 : }
     225             : 
     226             : /*
     227             :  * add_one_elt
     228             :  *
     229             :  * Add one element to a (possibly empty) eary struct.
     230             :  */
     231             : void
     232           0 : add_one_elt(char *eltname, eary *eary)
     233             : {
     234           0 :     if (eary->alloc == 0)
     235             :     {
     236           0 :         eary      ->alloc = 8;
     237           0 :         eary      ->array = (char **) pg_malloc(8 * sizeof(char *));
     238             :     }
     239           0 :     else if (eary->num >= eary->alloc)
     240             :     {
     241           0 :         eary      ->alloc *= 2;
     242           0 :         eary      ->array = (char **) pg_realloc(eary->array,
     243           0 :                                                  eary->alloc * sizeof(char *));
     244             :     }
     245             : 
     246           0 :     eary      ->array[eary->num] = pg_strdup(eltname);
     247           0 :     eary      ->num++;
     248           0 : }
     249             : 
     250             : /*
     251             :  * get_comma_elts
     252             :  *
     253             :  * Return the elements of an eary as a (freshly allocated) single string, in
     254             :  * single quotes, separated by commas and properly escaped for insertion in an
     255             :  * SQL statement.
     256             :  */
     257             : char *
     258           0 : get_comma_elts(eary *eary)
     259             : {
     260             :     char       *ret,
     261             :                *ptr;
     262             :     int         i,
     263           0 :                 length = 0;
     264             : 
     265           0 :     if (eary->num == 0)
     266           0 :         return pg_strdup("");
     267             : 
     268             :     /*
     269             :      * PQescapeString wants 2 * length + 1 bytes of breath space.  Add two
     270             :      * chars per element for the single quotes and one for the comma.
     271             :      */
     272           0 :     for (i = 0; i < eary->num; i++)
     273           0 :         length += strlen(eary->array[i]);
     274             : 
     275           0 :     ret = (char *) pg_malloc(length * 2 + 4 * eary->num);
     276           0 :     ptr = ret;
     277             : 
     278           0 :     for (i = 0; i < eary->num; i++)
     279             :     {
     280           0 :         if (i != 0)
     281           0 :             sprintf(ptr++, ",");
     282           0 :         sprintf(ptr++, "'");
     283           0 :         ptr += PQescapeString(ptr, eary->array[i], strlen(eary->array[i]));
     284           0 :         sprintf(ptr++, "'");
     285             :     }
     286             : 
     287           0 :     return ret;
     288             : }
     289             : 
     290             : /* establish connection with database. */
     291             : PGconn *
     292           0 : sql_conn(struct options *my_opts)
     293             : {
     294             :     PGconn     *conn;
     295           0 :     bool        have_password = false;
     296             :     char        password[100];
     297             :     bool        new_pass;
     298             :     PGresult   *res;
     299             : 
     300             :     /*
     301             :      * Start the connection.  Loop until we have a password if requested by
     302             :      * backend.
     303             :      */
     304             :     do
     305             :     {
     306             : #define PARAMS_ARRAY_SIZE   7
     307             : 
     308             :         const char *keywords[PARAMS_ARRAY_SIZE];
     309             :         const char *values[PARAMS_ARRAY_SIZE];
     310             : 
     311           0 :         keywords[0] = "host";
     312           0 :         values[0] = my_opts->hostname;
     313           0 :         keywords[1] = "port";
     314           0 :         values[1] = my_opts->port;
     315           0 :         keywords[2] = "user";
     316           0 :         values[2] = my_opts->username;
     317           0 :         keywords[3] = "password";
     318           0 :         values[3] = have_password ? password : NULL;
     319           0 :         keywords[4] = "dbname";
     320           0 :         values[4] = my_opts->dbname;
     321           0 :         keywords[5] = "fallback_application_name";
     322           0 :         values[5] = my_opts->progname;
     323           0 :         keywords[6] = NULL;
     324           0 :         values[6] = NULL;
     325             : 
     326           0 :         new_pass = false;
     327           0 :         conn = PQconnectdbParams(keywords, values, true);
     328             : 
     329           0 :         if (!conn)
     330             :         {
     331           0 :             pg_log_error("could not connect to database %s",
     332             :                          my_opts->dbname);
     333           0 :             exit(1);
     334             :         }
     335             : 
     336           0 :         if (PQstatus(conn) == CONNECTION_BAD &&
     337           0 :             PQconnectionNeedsPassword(conn) &&
     338           0 :             !have_password)
     339             :         {
     340           0 :             PQfinish(conn);
     341           0 :             simple_prompt("Password: ", password, sizeof(password), false);
     342           0 :             have_password = true;
     343           0 :             new_pass = true;
     344             :         }
     345           0 :     } while (new_pass);
     346             : 
     347             :     /* check to see that the backend connection was successfully made */
     348           0 :     if (PQstatus(conn) == CONNECTION_BAD)
     349             :     {
     350           0 :         pg_log_error("could not connect to database %s: %s",
     351             :                      my_opts->dbname, PQerrorMessage(conn));
     352           0 :         PQfinish(conn);
     353           0 :         exit(1);
     354             :     }
     355             : 
     356           0 :     res = PQexec(conn, ALWAYS_SECURE_SEARCH_PATH_SQL);
     357           0 :     if (PQresultStatus(res) != PGRES_TUPLES_OK)
     358             :     {
     359           0 :         pg_log_error("could not clear search_path: %s",
     360             :                      PQerrorMessage(conn));
     361           0 :         PQclear(res);
     362           0 :         PQfinish(conn);
     363           0 :         exit(-1);
     364             :     }
     365           0 :     PQclear(res);
     366             : 
     367             :     /* return the conn if good */
     368           0 :     return conn;
     369             : }
     370             : 
     371             : /*
     372             :  * Actual code to make call to the database and print the output data.
     373             :  */
     374             : int
     375           0 : sql_exec(PGconn *conn, const char *todo, bool quiet)
     376             : {
     377             :     PGresult   *res;
     378             : 
     379             :     int         nfields;
     380             :     int         nrows;
     381             :     int         i,
     382             :                 j,
     383             :                 l;
     384             :     int        *length;
     385             :     char       *pad;
     386             : 
     387             :     /* make the call */
     388           0 :     res = PQexec(conn, todo);
     389             : 
     390             :     /* check and deal with errors */
     391           0 :     if (!res || PQresultStatus(res) > 2)
     392             :     {
     393           0 :         pg_log_error("query failed: %s", PQerrorMessage(conn));
     394           0 :         pg_log_error("query was: %s", todo);
     395             : 
     396           0 :         PQclear(res);
     397           0 :         PQfinish(conn);
     398           0 :         exit(-1);
     399             :     }
     400             : 
     401             :     /* get the number of fields */
     402           0 :     nrows = PQntuples(res);
     403           0 :     nfields = PQnfields(res);
     404             : 
     405             :     /* for each field, get the needed width */
     406           0 :     length = (int *) pg_malloc(sizeof(int) * nfields);
     407           0 :     for (j = 0; j < nfields; j++)
     408           0 :         length[j] = strlen(PQfname(res, j));
     409             : 
     410           0 :     for (i = 0; i < nrows; i++)
     411             :     {
     412           0 :         for (j = 0; j < nfields; j++)
     413             :         {
     414           0 :             l = strlen(PQgetvalue(res, i, j));
     415           0 :             if (l > length[j])
     416           0 :                 length[j] = strlen(PQgetvalue(res, i, j));
     417             :         }
     418             :     }
     419             : 
     420             :     /* print a header */
     421           0 :     if (!quiet)
     422             :     {
     423           0 :         for (j = 0, l = 0; j < nfields; j++)
     424             :         {
     425           0 :             fprintf(stdout, "%*s", length[j] + 2, PQfname(res, j));
     426           0 :             l += length[j] + 2;
     427             :         }
     428           0 :         fprintf(stdout, "\n");
     429           0 :         pad = (char *) pg_malloc(l + 1);
     430           0 :         MemSet(pad, '-', l);
     431           0 :         pad[l] = '\0';
     432           0 :         fprintf(stdout, "%s\n", pad);
     433           0 :         free(pad);
     434             :     }
     435             : 
     436             :     /* for each row, dump the information */
     437           0 :     for (i = 0; i < nrows; i++)
     438             :     {
     439           0 :         for (j = 0; j < nfields; j++)
     440           0 :             fprintf(stdout, "%*s", length[j] + 2, PQgetvalue(res, i, j));
     441           0 :         fprintf(stdout, "\n");
     442             :     }
     443             : 
     444             :     /* cleanup */
     445           0 :     PQclear(res);
     446           0 :     free(length);
     447             : 
     448           0 :     return 0;
     449             : }
     450             : 
     451             : /*
     452             :  * Dump all databases.  There are no system objects to worry about.
     453             :  */
     454             : void
     455           0 : sql_exec_dumpalldbs(PGconn *conn, struct options *opts)
     456             : {
     457             :     char        todo[1024];
     458             : 
     459             :     /* get the oid and database name from the system pg_database table */
     460           0 :     snprintf(todo, sizeof(todo),
     461             :              "SELECT d.oid AS \"Oid\", datname AS \"Database Name\", "
     462             :              "spcname AS \"Tablespace\" FROM pg_catalog.pg_database d JOIN pg_catalog.pg_tablespace t ON "
     463             :              "(dattablespace = t.oid) ORDER BY 2");
     464             : 
     465           0 :     sql_exec(conn, todo, opts->quiet);
     466           0 : }
     467             : 
     468             : /*
     469             :  * Dump all tables, indexes and sequences in the current database.
     470             :  */
     471             : void
     472           0 : sql_exec_dumpalltables(PGconn *conn, struct options *opts)
     473             : {
     474             :     char        todo[1024];
     475           0 :     char       *addfields = ",c.oid AS \"Oid\", nspname AS \"Schema\", spcname as \"Tablespace\" ";
     476             : 
     477           0 :     snprintf(todo, sizeof(todo),
     478             :              "SELECT pg_catalog.pg_relation_filenode(c.oid) as \"Filenode\", relname as \"Table Name\" %s "
     479             :              "FROM pg_catalog.pg_class c "
     480             :              " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace "
     481             :              " LEFT JOIN pg_catalog.pg_database d ON d.datname = pg_catalog.current_database(),"
     482             :              " pg_catalog.pg_tablespace t "
     483             :              "WHERE relkind IN (" CppAsString2(RELKIND_RELATION) ","
     484             :              CppAsString2(RELKIND_MATVIEW) "%s%s) AND "
     485             :              " %s"
     486             :              "     t.oid = CASE"
     487             :              "         WHEN reltablespace <> 0 THEN reltablespace"
     488             :              "         ELSE dattablespace"
     489             :              "     END "
     490             :              "ORDER BY relname",
     491           0 :              opts->extended ? addfields : "",
     492           0 :              opts->indexes ? "," CppAsString2(RELKIND_INDEX) "," CppAsString2(RELKIND_SEQUENCE) : "",
     493           0 :              opts->systables ? "," CppAsString2(RELKIND_TOASTVALUE) : "",
     494           0 :              opts->systables ? "" : "n.nspname NOT IN ('pg_catalog', 'information_schema') AND n.nspname !~ '^pg_toast' AND");
     495             : 
     496           0 :     sql_exec(conn, todo, opts->quiet);
     497           0 : }
     498             : 
     499             : /*
     500             :  * Show oid, filenode, name, schema and tablespace for each of the
     501             :  * given objects in the current database.
     502             :  */
     503             : void
     504           0 : sql_exec_searchtables(PGconn *conn, struct options *opts)
     505             : {
     506             :     char       *todo;
     507             :     char       *qualifiers,
     508             :                *ptr;
     509             :     char       *comma_oids,
     510             :                *comma_filenodes,
     511             :                *comma_tables;
     512           0 :     bool        written = false;
     513           0 :     char       *addfields = ",c.oid AS \"Oid\", nspname AS \"Schema\", spcname as \"Tablespace\" ";
     514             : 
     515             :     /* get tables qualifiers, whether names, filenodes, or OIDs */
     516           0 :     comma_oids = get_comma_elts(opts->oids);
     517           0 :     comma_tables = get_comma_elts(opts->tables);
     518           0 :     comma_filenodes = get_comma_elts(opts->filenodes);
     519             : 
     520             :     /* 80 extra chars for SQL expression */
     521           0 :     qualifiers = (char *) pg_malloc(strlen(comma_oids) + strlen(comma_tables) +
     522           0 :                                     strlen(comma_filenodes) + 80);
     523           0 :     ptr = qualifiers;
     524             : 
     525           0 :     if (opts->oids->num > 0)
     526             :     {
     527           0 :         ptr += sprintf(ptr, "c.oid IN (%s)", comma_oids);
     528           0 :         written = true;
     529             :     }
     530           0 :     if (opts->filenodes->num > 0)
     531             :     {
     532           0 :         if (written)
     533           0 :             ptr += sprintf(ptr, " OR ");
     534           0 :         ptr += sprintf(ptr, "pg_catalog.pg_relation_filenode(c.oid) IN (%s)", comma_filenodes);
     535           0 :         written = true;
     536             :     }
     537           0 :     if (opts->tables->num > 0)
     538             :     {
     539           0 :         if (written)
     540           0 :             ptr += sprintf(ptr, " OR ");
     541           0 :         sprintf(ptr, "c.relname ~~ ANY (ARRAY[%s])", comma_tables);
     542             :     }
     543           0 :     free(comma_oids);
     544           0 :     free(comma_tables);
     545           0 :     free(comma_filenodes);
     546             : 
     547             :     /* now build the query */
     548           0 :     todo = psprintf(
     549             :                     "SELECT pg_catalog.pg_relation_filenode(c.oid) as \"Filenode\", relname as \"Table Name\" %s\n"
     550             :                     "FROM pg_catalog.pg_class c\n"
     551             :                     "  LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
     552             :                     "  LEFT JOIN pg_catalog.pg_database d ON d.datname = pg_catalog.current_database(),\n"
     553             :                     "  pg_catalog.pg_tablespace t\n"
     554             :                     "WHERE relkind IN (" CppAsString2(RELKIND_RELATION) ","
     555             :                     CppAsString2(RELKIND_MATVIEW) ","
     556             :                     CppAsString2(RELKIND_INDEX) ","
     557             :                     CppAsString2(RELKIND_SEQUENCE) ","
     558             :                     CppAsString2(RELKIND_TOASTVALUE) ") AND\n"
     559             :                     "      t.oid = CASE\n"
     560             :                     "          WHEN reltablespace <> 0 THEN reltablespace\n"
     561             :                     "          ELSE dattablespace\n"
     562             :                     "      END AND\n"
     563             :                     "  (%s)\n"
     564             :                     "ORDER BY relname\n",
     565           0 :                     opts->extended ? addfields : "",
     566             :                     qualifiers);
     567             : 
     568           0 :     free(qualifiers);
     569             : 
     570           0 :     sql_exec(conn, todo, opts->quiet);
     571           0 : }
     572             : 
     573             : void
     574           0 : sql_exec_dumpalltbspc(PGconn *conn, struct options *opts)
     575             : {
     576             :     char        todo[1024];
     577             : 
     578           0 :     snprintf(todo, sizeof(todo),
     579             :              "SELECT oid AS \"Oid\", spcname as \"Tablespace Name\"\n"
     580             :              "FROM pg_catalog.pg_tablespace");
     581             : 
     582           0 :     sql_exec(conn, todo, opts->quiet);
     583           0 : }
     584             : 
     585             : int
     586           6 : main(int argc, char **argv)
     587             : {
     588             :     struct options *my_opts;
     589             :     PGconn     *pgconn;
     590             : 
     591           6 :     my_opts = (struct options *) pg_malloc(sizeof(struct options));
     592             : 
     593           6 :     my_opts->oids = (eary *) pg_malloc(sizeof(eary));
     594           6 :     my_opts->tables = (eary *) pg_malloc(sizeof(eary));
     595           6 :     my_opts->filenodes = (eary *) pg_malloc(sizeof(eary));
     596             : 
     597           6 :     my_opts->oids->num = my_opts->oids->alloc = 0;
     598           6 :     my_opts->tables->num = my_opts->tables->alloc = 0;
     599           6 :     my_opts->filenodes->num = my_opts->filenodes->alloc = 0;
     600             : 
     601             :     /* parse the opts */
     602           6 :     get_opts(argc, argv, my_opts);
     603             : 
     604           0 :     if (my_opts->dbname == NULL)
     605             :     {
     606           0 :         my_opts->dbname = "postgres";
     607           0 :         my_opts->nodb = true;
     608             :     }
     609           0 :     pgconn = sql_conn(my_opts);
     610             : 
     611             :     /* display only tablespaces */
     612           0 :     if (my_opts->tablespaces)
     613             :     {
     614           0 :         if (!my_opts->quiet)
     615           0 :             printf("All tablespaces:\n");
     616           0 :         sql_exec_dumpalltbspc(pgconn, my_opts);
     617             : 
     618           0 :         PQfinish(pgconn);
     619           0 :         exit(0);
     620             :     }
     621             : 
     622             :     /* display the given elements in the database */
     623           0 :     if (my_opts->oids->num > 0 ||
     624           0 :         my_opts->tables->num > 0 ||
     625           0 :         my_opts->filenodes->num > 0)
     626             :     {
     627           0 :         if (!my_opts->quiet)
     628           0 :             printf("From database \"%s\":\n", my_opts->dbname);
     629           0 :         sql_exec_searchtables(pgconn, my_opts);
     630             : 
     631           0 :         PQfinish(pgconn);
     632           0 :         exit(0);
     633             :     }
     634             : 
     635             :     /* no elements given; dump the given database */
     636           0 :     if (my_opts->dbname && !my_opts->nodb)
     637             :     {
     638           0 :         if (!my_opts->quiet)
     639           0 :             printf("From database \"%s\":\n", my_opts->dbname);
     640           0 :         sql_exec_dumpalltables(pgconn, my_opts);
     641             : 
     642           0 :         PQfinish(pgconn);
     643           0 :         exit(0);
     644             :     }
     645             : 
     646             :     /* no database either; dump all databases */
     647           0 :     if (!my_opts->quiet)
     648           0 :         printf("All databases:\n");
     649           0 :     sql_exec_dumpalldbs(pgconn, my_opts);
     650             : 
     651           0 :     PQfinish(pgconn);
     652           0 :     return 0;
     653             : }

Generated by: LCOV version 1.13