]> granicus.if.org Git - postgresql/blob - src/bin/scripts/createlang.c
f27e0d25425b1aa1dc22d67716c64e05e595288a
[postgresql] / src / bin / scripts / createlang.c
1 /*-------------------------------------------------------------------------
2  *
3  * createlang
4  *
5  * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
6  * Portions Copyright (c) 1994, Regents of the University of California
7  *
8  * $PostgreSQL: pgsql/src/bin/scripts/createlang.c,v 1.30 2008/07/14 22:00:04 momjian Exp $
9  *
10  *-------------------------------------------------------------------------
11  */
12 #include "postgres_fe.h"
13
14 #include "common.h"
15 #include "print.h"
16
17 static void help(const char *progname);
18
19
20 int
21 main(int argc, char *argv[])
22 {
23         static struct option long_options[] = {
24                 {"list", no_argument, NULL, 'l'},
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                 {"dbname", required_argument, NULL, 'd'},
30                 {"echo", no_argument, NULL, 'e'},
31                 {NULL, 0, NULL, 0}
32         };
33
34         const char *progname;
35         int                     optindex;
36         int                     c;
37
38         bool            listlangs = false;
39         const char *dbname = NULL;
40         char       *host = NULL;
41         char       *port = NULL;
42         char       *username = NULL;
43         bool            password = false;
44         bool            echo = false;
45         char       *langname = NULL;
46
47         char       *p;
48
49         PQExpBufferData sql;
50
51         PGconn     *conn;
52         PGresult   *result;
53
54         progname = get_progname(argv[0]);
55         set_pglocale_pgservice(argv[0], "pgscripts");
56
57         handle_help_version_opts(argc, argv, "createlang", help);
58
59         while ((c = getopt_long(argc, argv, "lh:p:U:Wd: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                                 password = true;
77                                 break;
78                         case 'd':
79                                 dbname = optarg;
80                                 break;
81                         case 'e':
82                                 echo = true;
83                                 break;
84                         default:
85                                 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
86                                 exit(1);
87                 }
88         }
89
90         if (argc - optind > 0)
91         {
92                 if (listlangs)
93                         dbname = argv[optind++];
94                 else
95                 {
96                         langname = argv[optind++];
97                         if (argc - optind > 0)
98                                 dbname = argv[optind++];
99                 }
100         }
101
102         if (argc - optind > 0)
103         {
104                 fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
105                                 progname, argv[optind]);
106                 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
107                 exit(1);
108         }
109
110         if (dbname == NULL)
111         {
112                 if (getenv("PGDATABASE"))
113                         dbname = getenv("PGDATABASE");
114                 else if (getenv("PGUSER"))
115                         dbname = getenv("PGUSER");
116                 else
117                         dbname = get_user_name(progname);
118         }
119
120         initPQExpBuffer(&sql);
121
122         /*
123          * List option
124          */
125         if (listlangs)
126         {
127                 printQueryOpt popt;
128                 static const bool translate_columns[] = {false, true};
129
130                 conn = connectDatabase(dbname, host, port, username, password,
131                                                            progname);
132
133                 printfPQExpBuffer(&sql, "SELECT lanname as \"%s\", "
134                                 "(CASE WHEN lanpltrusted THEN '%s' ELSE '%s' END) as \"%s\" "
135                                                   "FROM pg_catalog.pg_language WHERE lanispl;",
136                                                   gettext_noop("Name"),
137                                                   gettext_noop("yes"), gettext_noop("no"),
138                                                   gettext_noop("Trusted?"));
139                 result = executeQuery(conn, sql.data, progname, echo);
140
141                 memset(&popt, 0, sizeof(popt));
142                 popt.topt.format = PRINT_ALIGNED;
143                 popt.topt.border = 1;
144                 popt.topt.start_table = true;
145                 popt.topt.stop_table = true;
146                 popt.topt.encoding = PQclientEncoding(conn);
147                 popt.title = _("Procedural Languages");
148                 popt.translate_header = true;
149                 popt.translate_columns = translate_columns;
150                 printQuery(result, &popt, stdout, NULL);
151
152                 PQfinish(conn);
153                 exit(0);
154         }
155
156         if (langname == NULL)
157         {
158                 fprintf(stderr, _("%s: missing required argument language name\n"), progname);
159                 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
160                 exit(1);
161         }
162
163         for (p = langname; *p; p++)
164                 if (*p >= 'A' && *p <= 'Z')
165                         *p += ('a' - 'A');
166
167         conn = connectDatabase(dbname, host, port, username, password, progname);
168
169         /*
170          * Make sure the language isn't already installed
171          */
172         printfPQExpBuffer(&sql,
173                           "SELECT oid FROM pg_catalog.pg_language WHERE lanname = '%s';",
174                                           langname);
175         result = executeQuery(conn, sql.data, progname, echo);
176         if (PQntuples(result) > 0)
177         {
178                 PQfinish(conn);
179                 fprintf(stderr,
180                   _("%s: language \"%s\" is already installed in database \"%s\"\n"),
181                                 progname, langname, dbname);
182                 /* separate exit status for "already installed" */
183                 exit(2);
184         }
185         PQclear(result);
186
187         printfPQExpBuffer(&sql, "CREATE LANGUAGE \"%s\";\n", langname);
188
189         if (echo)
190                 printf("%s", sql.data);
191         result = PQexec(conn, sql.data);
192         if (PQresultStatus(result) != PGRES_COMMAND_OK)
193         {
194                 fprintf(stderr, _("%s: language installation failed: %s"),
195                                 progname, PQerrorMessage(conn));
196                 PQfinish(conn);
197                 exit(1);
198         }
199
200         PQclear(result);
201         PQfinish(conn);
202         exit(0);
203 }
204
205
206
207 static void
208 help(const char *progname)
209 {
210         printf(_("%s installs a procedural language into a PostgreSQL database.\n\n"), progname);
211         printf(_("Usage:\n"));
212         printf(_("  %s [OPTION]... LANGNAME [DBNAME]\n"), progname);
213         printf(_("\nOptions:\n"));
214         printf(_("  -d, --dbname=DBNAME       database to install language in\n"));
215         printf(_("  -e, --echo                show the commands being sent to the server\n"));
216         printf(_("  -l, --list                show a list of currently installed languages\n"));
217         printf(_("  -h, --host=HOSTNAME       database server host or socket directory\n"));
218         printf(_("  -p, --port=PORT           database server port\n"));
219         printf(_("  -U, --username=USERNAME   user name to connect as\n"));
220         printf(_("  -W, --password            force password prompt\n"));
221         printf(_("  --help                    show this help, then exit\n"));
222         printf(_("  --version                 output version information, then exit\n"));
223         printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
224 }