1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
/*-------------------------------------------------------------------------
*
* droplang
*
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $PostgreSQL: pgsql/src/bin/scripts/droplang.c,v 1.11 2004/06/03 00:07:38 momjian Exp $
*
*-------------------------------------------------------------------------
*/
#include "postgres_fe.h"
#include "common.h"
#include "print.h"
#define atooid(x) ((Oid) strtoul((x), NULL, 10))
static void help(const char *progname);
int
main(int argc, char *argv[])
{
static struct option long_options[] = {
{"list", no_argument, NULL, 'l'},
{"host", required_argument, NULL, 'h'},
{"port", required_argument, NULL, 'p'},
{"username", required_argument, NULL, 'U'},
{"password", no_argument, NULL, 'W'},
{"dbname", required_argument, NULL, 'd'},
{"echo", no_argument, NULL, 'e'},
{NULL, 0, NULL, 0}
};
const char *progname;
int optindex;
int c;
bool listlangs = false;
const char *dbname = NULL;
char *host = NULL;
char *port = NULL;
char *username = NULL;
bool password = false;
bool echo = false;
char *langname = NULL;
char *p;
Oid lanplcallfoid;
Oid lanvalidator;
char *handler;
char *validator;
bool keephandler;
bool keepvalidator;
PQExpBufferData sql;
PGconn *conn;
PGresult *result;
progname = get_progname(argv[0]);
set_pglocale_pgservice(argv[0], "pgscripts");
handle_help_version_opts(argc, argv, "droplang", help);
while ((c = getopt_long(argc, argv, "lh:p:U:Wd:e", long_options, &optindex)) != -1)
{
switch (c)
{
case 'l':
listlangs = true;
break;
case 'h':
host = optarg;
break;
case 'p':
port = optarg;
break;
case 'U':
username = optarg;
break;
case 'W':
password = true;
break;
case 'd':
dbname = optarg;
break;
case 'e':
echo = true;
break;
default:
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
exit(1);
}
}
if (argc - optind > 0)
{
if (listlangs)
dbname = argv[optind++];
else
{
langname = argv[optind++];
if (argc - optind > 0)
dbname = argv[optind++];
}
}
if (argc - optind > 0)
{
fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
progname, argv[optind]);
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
exit(1);
}
if (dbname == NULL)
{
if (getenv("PGDATABASE"))
dbname = getenv("PGDATABASE");
else if (getenv("PGUSER"))
dbname = getenv("PGUSER");
else
dbname = get_user_name(progname);
}
initPQExpBuffer(&sql);
/*
* List option
*/
if (listlangs)
{
printQueryOpt popt;
conn = connectDatabase(dbname, host, port, username, password, progname);
printfPQExpBuffer(&sql, "SELECT lanname as \"%s\", (CASE WHEN lanpltrusted THEN '%s' ELSE '%s' END) as \"%s\" FROM pg_language WHERE lanispl IS TRUE;", _("Name"), _("yes"), _("no"), _("Trusted?"));
result = executeQuery(conn, sql.data, progname, echo);
memset(&popt, 0, sizeof(popt));
popt.topt.format = PRINT_ALIGNED;
popt.topt.border = 1;
popt.topt.encoding = PQclientEncoding(conn);
popt.title = _("Procedural Languages");
printQuery(result, &popt, stdout);
PQfinish(conn);
exit(0);
}
if (langname == NULL)
{
fprintf(stderr, _("%s: missing required argument language name\n"), progname);
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
exit(1);
}
for (p = langname; *p; p++)
if (*p >= 'A' && *p <= 'Z')
*p += ('a' - 'A');
conn = connectDatabase(dbname, host, port, username, password, progname);
/*
* Make sure the language is installed and find the OIDs of the handler
* and validator functions
*/
printfPQExpBuffer(&sql, "SELECT lanplcallfoid, lanvalidator FROM pg_language WHERE lanname = '%s' AND lanispl;", langname);
result = executeQuery(conn, sql.data, progname, echo);
if (PQntuples(result) == 0)
{
PQfinish(conn);
fprintf(stderr, _("%s: language \"%s\" is not installed in database \"%s\"\n"),
progname, langname, dbname);
exit(1);
}
lanplcallfoid = atooid(PQgetvalue(result, 0, 0));
lanvalidator = atooid(PQgetvalue(result, 0, 1));
PQclear(result);
/*
* Check that there are no functions left defined in that language
*/
printfPQExpBuffer(&sql, "SELECT count(proname) FROM pg_proc P, pg_language L WHERE P.prolang = L.oid AND L.lanname = '%s';", langname);
result = executeQuery(conn, sql.data, progname, echo);
if (strcmp(PQgetvalue(result, 0, 0), "0") != 0)
{
PQfinish(conn);
fprintf(stderr,
_("%s: still %s functions declared in language \"%s\"; language not removed\n"),
progname, PQgetvalue(result, 0, 0), langname);
exit(1);
}
PQclear(result);
/*
* Check that the handler function isn't used by some other language
*/
printfPQExpBuffer(&sql, "SELECT count(*) FROM pg_language WHERE lanplcallfoid = %u AND lanname <> '%s';", lanplcallfoid, langname);
result = executeQuery(conn, sql.data, progname, echo);
if (strcmp(PQgetvalue(result, 0, 0), "0") == 0)
keephandler = false;
else
keephandler = true;
PQclear(result);
/*
* Find the handler name
*/
if (!keephandler)
{
printfPQExpBuffer(&sql, "SELECT proname FROM pg_proc WHERE oid = %u;", lanplcallfoid);
result = executeQuery(conn, sql.data, progname, echo);
handler = strdup(PQgetvalue(result, 0, 0));
PQclear(result);
}
else
handler = NULL;
/*
* Check that the validator function isn't used by some other language
*/
if (OidIsValid(lanvalidator))
{
printfPQExpBuffer(&sql, "SELECT count(*) FROM pg_language WHERE lanvalidator = %u AND lanname <> '%s';", lanvalidator, langname);
result = executeQuery(conn, sql.data, progname, echo);
if (strcmp(PQgetvalue(result, 0, 0), "0") == 0)
keepvalidator = false;
else
keepvalidator = true;
PQclear(result);
}
else
keepvalidator = true; /* don't try to delete it */
/*
* Find the validator name
*/
if (!keepvalidator)
{
printfPQExpBuffer(&sql, "SELECT proname FROM pg_proc WHERE oid = %u;", lanvalidator);
result = executeQuery(conn, sql.data, progname, echo);
validator = strdup(PQgetvalue(result, 0, 0));
PQclear(result);
}
else
validator = NULL;
/*
* Drop the language and the functions
*/
printfPQExpBuffer(&sql, "DROP LANGUAGE \"%s\";\n", langname);
if (!keephandler)
appendPQExpBuffer(&sql, "DROP FUNCTION \"%s\" ();\n", handler);
if (!keepvalidator)
appendPQExpBuffer(&sql, "DROP FUNCTION \"%s\" (oid);\n", validator);
if (echo)
printf("%s", sql.data);
result = PQexec(conn, sql.data);
if (PQresultStatus(result) != PGRES_COMMAND_OK)
{
fprintf(stderr, _("%s: language removal failed: %s"),
progname, PQerrorMessage(conn));
PQfinish(conn);
exit(1);
}
PQfinish(conn);
exit(0);
}
static void
help(const char *progname)
{
printf(_("%s removes a procedural language from a database.\n\n"), progname);
printf(_("Usage:\n"));
printf(_(" %s [OPTION]... LANGNAME [DBNAME]\n"), progname);
printf(_("\nOptions:\n"));
printf(_(" -d, --dbname=DBNAME database from which to remove the language\n"));
printf(_(" -e, --echo show the commands being sent to the server\n"));
printf(_(" -l, --list show a list of currently installed languages\n"));
printf(_(" -h, --host=HOSTNAME database server host or socket directory\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"));
}
|