~ubuntu-branches/ubuntu/precise/postgresql-9.1/precise-security

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-05-11 10:41:53 UTC
  • Revision ID: james.westby@ubuntu.com-20110511104153-psbh2o58553fv1m0
Tags: upstream-9.1~beta1
ImportĀ upstreamĀ versionĀ 9.1~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-------------------------------------------------------------------------
 
2
 *
 
3
 * droplang
 
4
 *
 
5
 * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
 
6
 * Portions Copyright (c) 1994, Regents of the University of California
 
7
 *
 
8
 * src/bin/scripts/droplang.c
 
9
 *
 
10
 *-------------------------------------------------------------------------
 
11
 */
 
12
#include "postgres_fe.h"
 
13
 
 
14
#include "common.h"
 
15
#include "print.h"
 
16
 
 
17
#define atooid(x)  ((Oid) strtoul((x), NULL, 10))
 
18
 
 
19
 
 
20
static void help(const char *progname);
 
21
 
 
22
 
 
23
int
 
24
main(int argc, char *argv[])
 
25
{
 
26
        static struct option long_options[] = {
 
27
                {"list", no_argument, NULL, 'l'},
 
28
                {"host", required_argument, NULL, 'h'},
 
29
                {"port", required_argument, NULL, 'p'},
 
30
                {"username", required_argument, NULL, 'U'},
 
31
                {"no-password", no_argument, NULL, 'w'},
 
32
                {"password", no_argument, NULL, 'W'},
 
33
                {"dbname", required_argument, NULL, 'd'},
 
34
                {"echo", no_argument, NULL, 'e'},
 
35
                {NULL, 0, NULL, 0}
 
36
        };
 
37
 
 
38
        const char *progname;
 
39
        int                     optindex;
 
40
        int                     c;
 
41
        bool            listlangs = false;
 
42
        const char *dbname = NULL;
 
43
        char       *host = NULL;
 
44
        char       *port = NULL;
 
45
        char       *username = NULL;
 
46
        enum trivalue prompt_password = TRI_DEFAULT;
 
47
        bool            echo = false;
 
48
        char       *langname = NULL;
 
49
        char       *p;
 
50
        PQExpBufferData sql;
 
51
        PGconn     *conn;
 
52
        PGresult   *result;
 
53
 
 
54
        progname = get_progname(argv[0]);
 
55
        set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
 
56
 
 
57
        handle_help_version_opts(argc, argv, "droplang", help);
 
58
 
 
59
        while ((c = getopt_long(argc, argv, "lh:p:U:wWd:e", long_options, &optindex)) != -1)
 
60
        {
 
61
                switch (c)
 
62
                {
 
63
                        case 'l':
 
64
                                listlangs = true;
 
65
                                break;
 
66
                        case 'h':
 
67
                                host = optarg;
 
68
                                break;
 
69
                        case 'p':
 
70
                                port = optarg;
 
71
                                break;
 
72
                        case 'U':
 
73
                                username = optarg;
 
74
                                break;
 
75
                        case 'w':
 
76
                                prompt_password = TRI_NO;
 
77
                                break;
 
78
                        case 'W':
 
79
                                prompt_password = TRI_YES;
 
80
                                break;
 
81
                        case 'd':
 
82
                                dbname = optarg;
 
83
                                break;
 
84
                        case 'e':
 
85
                                echo = true;
 
86
                                break;
 
87
                        default:
 
88
                                fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
 
89
                                exit(1);
 
90
                }
 
91
        }
 
92
 
 
93
        if (argc - optind > 0)
 
94
        {
 
95
                if (listlangs)
 
96
                        dbname = argv[optind++];
 
97
                else
 
98
                {
 
99
                        langname = argv[optind++];
 
100
                        if (argc - optind > 0)
 
101
                                dbname = argv[optind++];
 
102
                }
 
103
        }
 
104
 
 
105
        if (argc - optind > 0)
 
106
        {
 
107
                fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
 
108
                                progname, argv[optind]);
 
109
                fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
 
110
                exit(1);
 
111
        }
 
112
 
 
113
        if (dbname == NULL)
 
114
        {
 
115
                if (getenv("PGDATABASE"))
 
116
                        dbname = getenv("PGDATABASE");
 
117
                else if (getenv("PGUSER"))
 
118
                        dbname = getenv("PGUSER");
 
119
                else
 
120
                        dbname = get_user_name(progname);
 
121
        }
 
122
 
 
123
        initPQExpBuffer(&sql);
 
124
 
 
125
        /*
 
126
         * List option
 
127
         */
 
128
        if (listlangs)
 
129
        {
 
130
                printQueryOpt popt;
 
131
                static const bool translate_columns[] = {false, true};
 
132
 
 
133
                conn = connectDatabase(dbname, host, port, username, prompt_password,
 
134
                                                           progname);
 
135
 
 
136
                printfPQExpBuffer(&sql, "SELECT lanname as \"%s\", "
 
137
                                "(CASE WHEN lanpltrusted THEN '%s' ELSE '%s' END) as \"%s\" "
 
138
                                                  "FROM pg_catalog.pg_language WHERE lanispl;",
 
139
                                                  gettext_noop("Name"),
 
140
                                                  gettext_noop("yes"), gettext_noop("no"),
 
141
                                                  gettext_noop("Trusted?"));
 
142
                result = executeQuery(conn, sql.data, progname, echo);
 
143
 
 
144
                memset(&popt, 0, sizeof(popt));
 
145
                popt.topt.format = PRINT_ALIGNED;
 
146
                popt.topt.border = 1;
 
147
                popt.topt.start_table = true;
 
148
                popt.topt.stop_table = true;
 
149
                popt.topt.encoding = PQclientEncoding(conn);
 
150
                popt.title = _("Procedural Languages");
 
151
                popt.translate_header = true;
 
152
                popt.translate_columns = translate_columns;
 
153
                printQuery(result, &popt, stdout, NULL);
 
154
 
 
155
                PQfinish(conn);
 
156
                exit(0);
 
157
        }
 
158
 
 
159
        if (langname == NULL)
 
160
        {
 
161
                fprintf(stderr, _("%s: missing required argument language name\n"),
 
162
                                progname);
 
163
                fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
 
164
                                progname);
 
165
                exit(1);
 
166
        }
 
167
 
 
168
        for (p = langname; *p; p++)
 
169
                if (*p >= 'A' && *p <= 'Z')
 
170
                        *p += ('a' - 'A');
 
171
 
 
172
        conn = connectDatabase(dbname, host, port, username, prompt_password, progname);
 
173
 
 
174
        /*
 
175
         * Force schema search path to be just pg_catalog, so that we don't have
 
176
         * to be paranoid about search paths below.
 
177
         */
 
178
        executeCommand(conn, "SET search_path = pg_catalog;", progname, echo);
 
179
 
 
180
        /*
 
181
         * Make sure the language is installed
 
182
         */
 
183
        printfPQExpBuffer(&sql, "SELECT oid "
 
184
                                          "FROM pg_language WHERE lanname = '%s' AND lanispl;",
 
185
                                          langname);
 
186
        result = executeQuery(conn, sql.data, progname, echo);
 
187
        if (PQntuples(result) == 0)
 
188
        {
 
189
                PQfinish(conn);
 
190
                fprintf(stderr, _("%s: language \"%s\" is not installed in "
 
191
                                                  "database \"%s\"\n"),
 
192
                                progname, langname, dbname);
 
193
                exit(1);
 
194
        }
 
195
        PQclear(result);
 
196
 
 
197
        /*
 
198
         * Attempt to drop the language.  We do not use CASCADE, so that the drop
 
199
         * will fail if there are any functions in the language.
 
200
         */
 
201
        printfPQExpBuffer(&sql, "DROP EXTENSION \"%s\";\n", langname);
 
202
 
 
203
        if (echo)
 
204
                printf("%s", sql.data);
 
205
        result = PQexec(conn, sql.data);
 
206
        if (PQresultStatus(result) != PGRES_COMMAND_OK)
 
207
        {
 
208
                fprintf(stderr, _("%s: language removal failed: %s"),
 
209
                                progname, PQerrorMessage(conn));
 
210
                PQfinish(conn);
 
211
                exit(1);
 
212
        }
 
213
 
 
214
        PQclear(result);
 
215
        PQfinish(conn);
 
216
        exit(0);
 
217
}
 
218
 
 
219
 
 
220
static void
 
221
help(const char *progname)
 
222
{
 
223
        printf(_("%s removes a procedural language from a database.\n\n"), progname);
 
224
        printf(_("Usage:\n"));
 
225
        printf(_("  %s [OPTION]... LANGNAME [DBNAME]\n"), progname);
 
226
        printf(_("\nOptions:\n"));
 
227
        printf(_("  -d, --dbname=DBNAME       database from which to remove the language\n"));
 
228
        printf(_("  -e, --echo                show the commands being sent to the server\n"));
 
229
        printf(_("  -l, --list                show a list of currently installed languages\n"));
 
230
        printf(_("  --help                    show this help, then exit\n"));
 
231
        printf(_("  --version                 output version information, then exit\n"));
 
232
        printf(_("\nConnection options:\n"));
 
233
        printf(_("  -h, --host=HOSTNAME       database server host or socket directory\n"));
 
234
        printf(_("  -p, --port=PORT           database server port\n"));
 
235
        printf(_("  -U, --username=USERNAME   user name to connect as\n"));
 
236
        printf(_("  -w, --no-password         never prompt for password\n"));
 
237
        printf(_("  -W, --password            force password prompt\n"));
 
238
        printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
 
239
}