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