~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to src/bin/scripts/dropdb.c

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-------------------------------------------------------------------------
 
2
 *
 
3
 * dropdb
 
4
 *
 
5
 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
 
6
 * Portions Copyright (c) 1994, Regents of the University of California
 
7
 *
 
8
 * $PostgreSQL: pgsql/src/bin/scripts/dropdb.c,v 1.14 2004-12-31 22:03:17 pgsql Exp $
 
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
                {"password", no_argument, NULL, 'W'},
 
29
                {"echo", no_argument, NULL, 'e'},
 
30
                {"quiet", no_argument, NULL, 'q'},
 
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
        bool            password = false;
 
44
        bool            echo = false;
 
45
        bool            quiet = false;
 
46
        bool            interactive = false;
 
47
 
 
48
        PQExpBufferData sql;
 
49
 
 
50
        PGconn     *conn;
 
51
        PGresult   *result;
 
52
 
 
53
        progname = get_progname(argv[0]);
 
54
        set_pglocale_pgservice(argv[0], "pgscripts");
 
55
 
 
56
        handle_help_version_opts(argc, argv, "dropdb", help);
 
57
 
 
58
        while ((c = getopt_long(argc, argv, "h:p:U:Weqi", long_options, &optindex)) != -1)
 
59
        {
 
60
                switch (c)
 
61
                {
 
62
                        case 'h':
 
63
                                host = optarg;
 
64
                                break;
 
65
                        case 'p':
 
66
                                port = optarg;
 
67
                                break;
 
68
                        case 'U':
 
69
                                username = optarg;
 
70
                                break;
 
71
                        case 'W':
 
72
                                password = true;
 
73
                                break;
 
74
                        case 'e':
 
75
                                echo = true;
 
76
                                break;
 
77
                        case 'q':
 
78
                                quiet = true;
 
79
                                break;
 
80
                        case 'i':
 
81
                                interactive = true;
 
82
                                break;
 
83
                        default:
 
84
                                fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
 
85
                                exit(1);
 
86
                }
 
87
        }
 
88
 
 
89
        switch (argc - optind)
 
90
        {
 
91
                case 0:
 
92
                        fprintf(stderr, _("%s: missing required argument database name\n"), progname);
 
93
                        fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
 
94
                        exit(1);
 
95
                case 1:
 
96
                        dbname = argv[optind];
 
97
                        break;
 
98
                default:
 
99
                        fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
 
100
                                        progname, argv[optind + 1]);
 
101
                        fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
 
102
                        exit(1);
 
103
        }
 
104
 
 
105
        if (interactive)
 
106
        {
 
107
                char       *reply;
 
108
 
 
109
                printf(_("Database \"%s\" will be permanently removed.\n"), dbname);
 
110
                reply = simple_prompt("Are you sure? (y/n) ", 1, true);
 
111
                if (check_yesno_response(reply) != 1)
 
112
                        exit(0);
 
113
        }
 
114
 
 
115
        initPQExpBuffer(&sql);
 
116
 
 
117
        appendPQExpBuffer(&sql, "DROP DATABASE %s;\n",
 
118
                                          fmtId(dbname));
 
119
 
 
120
        conn = connectDatabase("template1", host, port, username, password, progname);
 
121
 
 
122
        if (echo)
 
123
                printf("%s", sql.data);
 
124
        result = PQexec(conn, sql.data);
 
125
        if (PQresultStatus(result) != PGRES_COMMAND_OK)
 
126
        {
 
127
                fprintf(stderr, _("%s: database removal failed: %s"),
 
128
                                progname, PQerrorMessage(conn));
 
129
                PQfinish(conn);
 
130
                exit(1);
 
131
        }
 
132
 
 
133
        PQfinish(conn);
 
134
        if (!quiet)
 
135
        {
 
136
                puts("DROP DATABASE");
 
137
                fflush(stdout);
 
138
        }
 
139
        exit(0);
 
140
}
 
141
 
 
142
 
 
143
static void
 
144
help(const char *progname)
 
145
{
 
146
        printf(_("%s removes a PostgreSQL database.\n\n"), progname);
 
147
        printf(_("Usage:\n"));
 
148
        printf(_("  %s [OPTION]... DBNAME\n"), progname);
 
149
        printf(_("\nOptions:\n"));
 
150
        printf(_("  -e, --echo                show the commands being sent to the server\n"));
 
151
        printf(_("  -i, --interactive         prompt before deleting anything\n"));
 
152
        printf(_("  -q, --quiet               don't write any messages\n"));
 
153
        printf(_("  -h, --host=HOSTNAME       database server host or socket directory\n"));
 
154
        printf(_("  -p, --port=PORT           database server port\n"));
 
155
        printf(_("  -U, --username=USERNAME   user name to connect as\n"));
 
156
        printf(_("  -W, --password            prompt for password\n"));
 
157
        printf(_("  --help                    show this help, then exit\n"));
 
158
        printf(_("  --version                 output version information, then exit\n"));
 
159
        printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
 
160
}