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