LCOV - code coverage report
Current view: top level - src/bin/scripts - dropdb.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13beta1 Lines: 65 101 64.4 %
Date: 2020-06-03 10:06:28 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13