1
/*-------------------------------------------------------------------------
5
* Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
6
* Portions Copyright (c) 1994, Regents of the University of California
8
* src/bin/scripts/droplang.c
10
*-------------------------------------------------------------------------
12
#include "postgres_fe.h"
17
#define atooid(x) ((Oid) strtoul((x), NULL, 10))
20
static void help(const char *progname);
24
main(int argc, char *argv[])
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'},
41
bool listlangs = false;
42
const char *dbname = NULL;
45
char *username = NULL;
46
enum trivalue prompt_password = TRI_DEFAULT;
48
char *langname = NULL;
54
progname = get_progname(argv[0]);
55
set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
57
handle_help_version_opts(argc, argv, "droplang", help);
59
while ((c = getopt_long(argc, argv, "lh:p:U:wWd:e", long_options, &optindex)) != -1)
76
prompt_password = TRI_NO;
79
prompt_password = TRI_YES;
88
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
93
if (argc - optind > 0)
96
dbname = argv[optind++];
99
langname = argv[optind++];
100
if (argc - optind > 0)
101
dbname = argv[optind++];
105
if (argc - optind > 0)
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);
115
if (getenv("PGDATABASE"))
116
dbname = getenv("PGDATABASE");
117
else if (getenv("PGUSER"))
118
dbname = getenv("PGUSER");
120
dbname = get_user_name(progname);
123
initPQExpBuffer(&sql);
131
static const bool translate_columns[] = {false, true};
133
conn = connectDatabase(dbname, host, port, username, prompt_password,
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);
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);
159
if (langname == NULL)
161
fprintf(stderr, _("%s: missing required argument language name\n"),
163
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
168
for (p = langname; *p; p++)
169
if (*p >= 'A' && *p <= 'Z')
172
conn = connectDatabase(dbname, host, port, username, prompt_password, progname);
175
* Force schema search path to be just pg_catalog, so that we don't have
176
* to be paranoid about search paths below.
178
executeCommand(conn, "SET search_path = pg_catalog;", progname, echo);
181
* Make sure the language is installed
183
printfPQExpBuffer(&sql, "SELECT oid "
184
"FROM pg_language WHERE lanname = '%s' AND lanispl;",
186
result = executeQuery(conn, sql.data, progname, echo);
187
if (PQntuples(result) == 0)
190
fprintf(stderr, _("%s: language \"%s\" is not installed in "
191
"database \"%s\"\n"),
192
progname, langname, dbname);
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.
201
printfPQExpBuffer(&sql, "DROP EXTENSION \"%s\";\n", langname);
204
printf("%s", sql.data);
205
result = PQexec(conn, sql.data);
206
if (PQresultStatus(result) != PGRES_COMMAND_OK)
208
fprintf(stderr, _("%s: language removal failed: %s"),
209
progname, PQerrorMessage(conn));
221
help(const char *progname)
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"));