LCOV - code coverage report
Current view: top level - src/bin/scripts - dropdb.c (source / functions) Hit Total Coverage
Test: PostgreSQL 12beta2 Lines: 54 82 65.9 %
Date: 2019-06-18 07:06:57 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * dropdb
       4             :  *
       5             :  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
       6             :  * Portions Copyright (c) 1994, Regents of the University of California
       7             :  *
       8             :  * src/bin/scripts/dropdb.c
       9             :  *
      10             :  *-------------------------------------------------------------------------
      11             :  */
      12             : 
      13             : #include "postgres_fe.h"
      14             : #include "common.h"
      15             : #include "common/logging.h"
      16             : #include "fe_utils/string_utils.h"
      17             : 
      18             : 
      19             : static void help(const char *progname);
      20             : 
      21             : 
      22             : int
      23          16 : main(int argc, char *argv[])
      24             : {
      25             :     static int  if_exists = 0;
      26             : 
      27             :     static struct option long_options[] = {
      28             :         {"host", required_argument, NULL, 'h'},
      29             :         {"port", required_argument, NULL, 'p'},
      30             :         {"username", required_argument, NULL, 'U'},
      31             :         {"no-password", no_argument, NULL, 'w'},
      32             :         {"password", no_argument, NULL, 'W'},
      33             :         {"echo", no_argument, NULL, 'e'},
      34             :         {"interactive", no_argument, NULL, 'i'},
      35             :         {"if-exists", no_argument, &if_exists, 1},
      36             :         {"maintenance-db", required_argument, NULL, 2},
      37             :         {NULL, 0, NULL, 0}
      38             :     };
      39             : 
      40             :     const char *progname;
      41             :     int         optindex;
      42             :     int         c;
      43             : 
      44          16 :     char       *dbname = NULL;
      45          16 :     char       *maintenance_db = NULL;
      46          16 :     char       *host = NULL;
      47          16 :     char       *port = NULL;
      48          16 :     char       *username = NULL;
      49          16 :     enum trivalue prompt_password = TRI_DEFAULT;
      50          16 :     bool        echo = false;
      51          16 :     bool        interactive = false;
      52             : 
      53             :     PQExpBufferData sql;
      54             : 
      55             :     PGconn     *conn;
      56             :     PGresult   *result;
      57             : 
      58          16 :     pg_logging_init(argv[0]);
      59          16 :     progname = get_progname(argv[0]);
      60          16 :     set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
      61             : 
      62          16 :     handle_help_version_opts(argc, argv, "dropdb", help);
      63             : 
      64          12 :     while ((c = getopt_long(argc, argv, "h:p:U:wWei", long_options, &optindex)) != -1)
      65             :     {
      66           2 :         switch (c)
      67             :         {
      68             :             case 'h':
      69           0 :                 host = pg_strdup(optarg);
      70           0 :                 break;
      71             :             case 'p':
      72           0 :                 port = pg_strdup(optarg);
      73           0 :                 break;
      74             :             case 'U':
      75           0 :                 username = pg_strdup(optarg);
      76           0 :                 break;
      77             :             case 'w':
      78           0 :                 prompt_password = TRI_NO;
      79           0 :                 break;
      80             :             case 'W':
      81           0 :                 prompt_password = TRI_YES;
      82           0 :                 break;
      83             :             case 'e':
      84           0 :                 echo = true;
      85           0 :                 break;
      86             :             case 'i':
      87           0 :                 interactive = true;
      88           0 :                 break;
      89             :             case 0:
      90             :                 /* this covers the long options */
      91           0 :                 break;
      92             :             case 2:
      93           0 :                 maintenance_db = pg_strdup(optarg);
      94           0 :                 break;
      95             :             default:
      96           2 :                 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
      97           2 :                 exit(1);
      98             :         }
      99             :     }
     100             : 
     101          10 :     switch (argc - optind)
     102             :     {
     103             :         case 0:
     104           0 :             pg_log_error("missing required argument database name");
     105           0 :             fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
     106           0 :             exit(1);
     107             :         case 1:
     108          10 :             dbname = argv[optind];
     109          10 :             break;
     110             :         default:
     111           0 :             pg_log_error("too many command-line arguments (first is \"%s\")",
     112             :                          argv[optind + 1]);
     113           0 :             fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
     114           0 :             exit(1);
     115             :     }
     116             : 
     117          10 :     if (interactive)
     118             :     {
     119           0 :         printf(_("Database \"%s\" will be permanently removed.\n"), dbname);
     120           0 :         if (!yesno_prompt("Are you sure?"))
     121           0 :             exit(0);
     122             :     }
     123             : 
     124          10 :     initPQExpBuffer(&sql);
     125             : 
     126          20 :     appendPQExpBuffer(&sql, "DROP DATABASE %s%s;",
     127          10 :                       (if_exists ? "IF EXISTS " : ""), fmtId(dbname));
     128             : 
     129             :     /* Avoid trying to drop postgres db while we are connected to it. */
     130          10 :     if (maintenance_db == NULL && strcmp(dbname, "postgres") == 0)
     131           0 :         maintenance_db = "template1";
     132             : 
     133          10 :     conn = connectMaintenanceDatabase(maintenance_db,
     134             :                                       host, port, username, prompt_password,
     135             :                                       progname, echo);
     136             : 
     137          10 :     if (echo)
     138           0 :         printf("%s\n", sql.data);
     139          10 :     result = PQexec(conn, sql.data);
     140          10 :     if (PQresultStatus(result) != PGRES_COMMAND_OK)
     141             :     {
     142           2 :         pg_log_error("database removal failed: %s", PQerrorMessage(conn));
     143           2 :         PQfinish(conn);
     144           2 :         exit(1);
     145             :     }
     146             : 
     147           8 :     PQclear(result);
     148           8 :     PQfinish(conn);
     149           8 :     exit(0);
     150             : }
     151             : 
     152             : 
     153             : static void
     154           2 : help(const char *progname)
     155             : {
     156           2 :     printf(_("%s removes a PostgreSQL database.\n\n"), progname);
     157           2 :     printf(_("Usage:\n"));
     158           2 :     printf(_("  %s [OPTION]... DBNAME\n"), progname);
     159           2 :     printf(_("\nOptions:\n"));
     160           2 :     printf(_("  -e, --echo                show the commands being sent to the server\n"));
     161           2 :     printf(_("  -i, --interactive         prompt before deleting anything\n"));
     162           2 :     printf(_("  -V, --version             output version information, then exit\n"));
     163           2 :     printf(_("  --if-exists               don't report error if database doesn't exist\n"));
     164           2 :     printf(_("  -?, --help                show this help, then exit\n"));
     165           2 :     printf(_("\nConnection options:\n"));
     166           2 :     printf(_("  -h, --host=HOSTNAME       database server host or socket directory\n"));
     167           2 :     printf(_("  -p, --port=PORT           database server port\n"));
     168           2 :     printf(_("  -U, --username=USERNAME   user name to connect as\n"));
     169           2 :     printf(_("  -w, --no-password         never prompt for password\n"));
     170           2 :     printf(_("  -W, --password            force password prompt\n"));
     171           2 :     printf(_("  --maintenance-db=DBNAME   alternate maintenance database\n"));
     172           2 :     printf(_("\nReport bugs to <pgsql-bugs@lists.postgresql.org>.\n"));
     173           2 : }

Generated by: LCOV version 1.13