LCOV - differential code coverage report
Current view: top level - src/bin/scripts - dropdb.c (source / functions) Coverage Total Hit UNC LBC UBC GBC GNC CBC DUB DCB
Current: Differential Code Coverage HEAD vs 15 Lines: 66.4 % 107 71 6 3 27 3 4 64 6 4
Current Date: 2023-04-08 17:13:01 Functions: 100.0 % 2 2 1 1
Baseline: 15 Line coverage date bins:
Baseline Date: 2023-04-08 15:09:40 (60,120] days: 40.0 % 10 4 6 4
Legend: Lines: hit not hit (240..) days: 69.1 % 97 67 3 27 3 64
Function coverage date bins:
(240..) days: 100.0 % 2 2 1 1

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

Generated by: LCOV version v1.16-55-g56c0a2a