]> granicus.if.org Git - postgresql/blob - src/bin/scripts/dropdb.c
Remove cvs keywords from all files.
[postgresql] / src / bin / scripts / dropdb.c
1 /*-------------------------------------------------------------------------
2  *
3  * dropdb
4  *
5  * Portions Copyright (c) 1996-2010, 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 "dumputils.h"
16
17
18 static void help(const char *progname);
19
20
21 int
22 main(int argc, char *argv[])
23 {
24         static struct option long_options[] = {
25                 {"host", required_argument, NULL, 'h'},
26                 {"port", required_argument, NULL, 'p'},
27                 {"username", required_argument, NULL, 'U'},
28                 {"no-password", no_argument, NULL, 'w'},
29                 {"password", no_argument, NULL, 'W'},
30                 {"echo", no_argument, NULL, 'e'},
31                 {"interactive", no_argument, NULL, 'i'},
32                 {NULL, 0, NULL, 0}
33         };
34
35         const char *progname;
36         int                     optindex;
37         int                     c;
38
39         char       *dbname = NULL;
40         char       *host = NULL;
41         char       *port = NULL;
42         char       *username = NULL;
43         enum trivalue prompt_password = TRI_DEFAULT;
44         bool            echo = false;
45         bool            interactive = false;
46
47         PQExpBufferData sql;
48
49         PGconn     *conn;
50         PGresult   *result;
51
52         progname = get_progname(argv[0]);
53         set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
54
55         handle_help_version_opts(argc, argv, "dropdb", help);
56
57         while ((c = getopt_long(argc, argv, "h:p:U:wWei", long_options, &optindex)) != -1)
58         {
59                 switch (c)
60                 {
61                         case 'h':
62                                 host = optarg;
63                                 break;
64                         case 'p':
65                                 port = optarg;
66                                 break;
67                         case 'U':
68                                 username = optarg;
69                                 break;
70                         case 'w':
71                                 prompt_password = TRI_NO;
72                                 break;
73                         case 'W':
74                                 prompt_password = TRI_YES;
75                                 break;
76                         case 'e':
77                                 echo = true;
78                                 break;
79                         case 'i':
80                                 interactive = true;
81                                 break;
82                         default:
83                                 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
84                                 exit(1);
85                 }
86         }
87
88         switch (argc - optind)
89         {
90                 case 0:
91                         fprintf(stderr, _("%s: missing required argument database name\n"), progname);
92                         fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
93                         exit(1);
94                 case 1:
95                         dbname = argv[optind];
96                         break;
97                 default:
98                         fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
99                                         progname, argv[optind + 1]);
100                         fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
101                         exit(1);
102         }
103
104         if (interactive)
105         {
106                 printf(_("Database \"%s\" will be permanently removed.\n"), dbname);
107                 if (!yesno_prompt("Are you sure?"))
108                         exit(0);
109         }
110
111         initPQExpBuffer(&sql);
112
113         appendPQExpBuffer(&sql, "DROP DATABASE %s;\n",
114                                           fmtId(dbname));
115
116         conn = connectDatabase(strcmp(dbname, "postgres") == 0 ? "template1" : "postgres",
117                                                    host, port, username, prompt_password, progname);
118
119         if (echo)
120                 printf("%s", sql.data);
121         result = PQexec(conn, sql.data);
122         if (PQresultStatus(result) != PGRES_COMMAND_OK)
123         {
124                 fprintf(stderr, _("%s: database removal failed: %s"),
125                                 progname, PQerrorMessage(conn));
126                 PQfinish(conn);
127                 exit(1);
128         }
129
130         PQclear(result);
131         PQfinish(conn);
132         exit(0);
133 }
134
135
136 static void
137 help(const char *progname)
138 {
139         printf(_("%s removes a PostgreSQL database.\n\n"), progname);
140         printf(_("Usage:\n"));
141         printf(_("  %s [OPTION]... DBNAME\n"), progname);
142         printf(_("\nOptions:\n"));
143         printf(_("  -e, --echo                show the commands being sent to the server\n"));
144         printf(_("  -i, --interactive         prompt before deleting anything\n"));
145         printf(_("  --help                    show this help, then exit\n"));
146         printf(_("  --version                 output version information, then exit\n"));
147         printf(_("\nConnection options:\n"));
148         printf(_("  -h, --host=HOSTNAME       database server host or socket directory\n"));
149         printf(_("  -p, --port=PORT           database server port\n"));
150         printf(_("  -U, --username=USERNAME   user name to connect as\n"));
151         printf(_("  -w, --no-password         never prompt for password\n"));
152         printf(_("  -W, --password            force password prompt\n"));
153         printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
154 }