summaryrefslogtreecommitdiff
path: root/src/bin/scripts/dropdb.c
diff options
context:
space:
mode:
authorPeter Eisentraut <peter_e@gmx.net>2003-03-18 22:19:47 +0000
committerPeter Eisentraut <peter_e@gmx.net>2003-03-18 22:19:47 +0000
commit9e0ab7126d106debb3fbdbc0e808368432cf39f0 (patch)
tree63d924e978ce0e50fc57bf44c30bdc8564f9a0f3 /src/bin/scripts/dropdb.c
parentcf1cf8964925cd36dc7bf034a968a4e44ede382a (diff)
Reimplement create and drop scripts in C, to reduce repetitive
connections, increase robustness, add NLS, and prepare for Windows port. (vacuumdb and clusterdb will follow later.)
Diffstat (limited to 'src/bin/scripts/dropdb.c')
-rw-r--r--src/bin/scripts/dropdb.c156
1 files changed, 156 insertions, 0 deletions
diff --git a/src/bin/scripts/dropdb.c b/src/bin/scripts/dropdb.c
new file mode 100644
index 00000000000..784c7c252da
--- /dev/null
+++ b/src/bin/scripts/dropdb.c
@@ -0,0 +1,156 @@
+/*-------------------------------------------------------------------------
+ *
+ * dropdb
+ *
+ * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * $Header: /cvsroot/pgsql/src/bin/scripts/dropdb.c,v 1.1 2003/03/18 22:19:46 petere Exp $
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "postgres_fe.h"
+#include "common.h"
+#include "dumputils.h"
+
+
+static void help(const char *progname);
+
+
+int
+main(int argc, char *argv[])
+{
+ static struct option long_options[] = {
+ {"host", required_argument, NULL, 'h'},
+ {"port", required_argument, NULL, 'p'},
+ {"username", required_argument, NULL, 'U'},
+ {"password", no_argument, NULL, 'W'},
+ {"echo", no_argument, NULL, 'e'},
+ {"quiet", no_argument, NULL, 'q'},
+ {"interactive", no_argument, NULL, 'i'},
+ {NULL, 0, NULL, 0}
+ };
+
+ char *progname;
+ int optindex;
+ int c;
+
+ char *dbname = NULL;
+ char *host = NULL;
+ char *port = NULL;
+ char *username = NULL;
+ bool password = false;
+ bool echo = false;
+ bool quiet = false;
+ bool interactive = false;
+
+ PQExpBufferData sql;
+
+ PGconn *conn;
+ PGresult *result;
+
+ progname = get_progname(argv[0]);
+ init_nls();
+ handle_help_version_opts(argc, argv, "dropdb", help);
+
+ while ((c = getopt_long(argc, argv, "h:p:U:Weqi", long_options, &optindex)) != -1)
+ {
+ switch (c)
+ {
+ case 'h':
+ host = optarg;
+ break;
+ case 'p':
+ port = optarg;
+ break;
+ case 'U':
+ username = optarg;
+ break;
+ case 'W':
+ password = true;
+ break;
+ case 'e':
+ echo = true;
+ break;
+ case 'q':
+ quiet = true;
+ break;
+ case 'i':
+ interactive = true;
+ break;
+ default:
+ fprintf(stderr, _("Try '%s --help' for more information.\n"), progname);
+ exit(1);
+ }
+ }
+
+ switch (argc - optind)
+ {
+ case 0:
+ fprintf(stderr, _("%s: missing required argument database name\n"), progname);
+ fprintf(stderr, _("Try '%s --help' for more information.\n"), progname);
+ exit(1);
+ case 1:
+ dbname = argv[optind];
+ break;
+ default:
+ fprintf(stderr, _("%s: too many command-line arguments (first is '%s')\n"),
+ progname, argv[optind + 1]);
+ fprintf(stderr, _("Try '%s --help' for more information.\n"), progname);
+ exit(1);
+ }
+
+ if (interactive)
+ {
+ char *reply;
+
+ printf(_("Database \"%s\" will be permanently deleted.\n"), dbname);
+ reply = simple_prompt("Are you sure? (y/n) ", 1, true);
+ if (reply[0] != 'y' && reply[0] != 'Y')
+ exit(0);
+ }
+
+ initPQExpBuffer(&sql);
+
+ appendPQExpBuffer(&sql, "SET autocommit TO on;\nDROP DATABASE %s;\n",
+ fmtId(dbname));
+
+ conn = connectDatabase("template1", host, port, username, password, progname);
+
+ if (echo)
+ printf("%s", sql.data);
+ result = PQexec(conn, sql.data);
+ if (PQresultStatus(result) != PGRES_COMMAND_OK)
+ {
+ fprintf(stderr, _("%s: database removal failed: %s"),
+ progname, PQerrorMessage(conn));
+ PQfinish(conn);
+ exit(1);
+ }
+
+ PQfinish(conn);
+ if (!quiet)
+ puts("DROP DATABASE");
+ exit(0);
+}
+
+
+static void
+help(const char *progname)
+{
+ printf(_("%s removes a PostgreSQL database.\n\n"), progname);
+ printf(_("Usage:\n"));
+ printf(_(" %s [OPTION]... DBNAME\n"), progname);
+ printf(_("\nOptions:\n"));
+ printf(_(" -e, --echo show the commands being sent to the server\n"));
+ printf(_(" -i, --interactive prompt before deleting anything\n"));
+ printf(_(" -q, --quiet don't write any messages\n"));
+ printf(_(" -h, --host=HOSTNAME database server host\n"));
+ printf(_(" -p, --port=PORT database server port\n"));
+ printf(_(" -U, --username=USERNAME user name to connect as\n"));
+ printf(_(" -W, --password prompt for password\n"));
+ printf(_(" --help show this help, then exit\n"));
+ printf(_(" --version output version information, then exit\n"));
+ printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
+}