LCOV - code coverage report
Current view: top level - src/bin/scripts - dropdb.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 0.0 % 102 0
Test Date: 2026-01-26 10:56:24 Functions: 0.0 % 2 0
Legend: Lines:     hit not hit

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

Generated by: LCOV version 2.3.2-1