Fix a few errors in comments. Patch by Fujii Masao, plus the one in
[PostgreSQL.git] / src / bin / scripts / createlang.c
blobaa29bc74ea9127e277678aaff6587073e7817db5
1 /*-------------------------------------------------------------------------
3 * createlang
5 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
6 * Portions Copyright (c) 1994, Regents of the University of California
8 * $PostgreSQL$
10 *-------------------------------------------------------------------------
12 #include "postgres_fe.h"
14 #include "common.h"
15 #include "print.h"
17 static void help(const char *progname);
20 int
21 main(int argc, char *argv[])
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 {"no-password", no_argument, NULL, 'w'},
29 {"password", no_argument, NULL, 'W'},
30 {"dbname", required_argument, NULL, 'd'},
31 {"echo", no_argument, NULL, 'e'},
32 {NULL, 0, NULL, 0}
35 const char *progname;
36 int optindex;
37 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;
48 char *p;
50 PQExpBufferData sql;
52 PGconn *conn;
53 PGresult *result;
55 progname = get_progname(argv[0]);
56 set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
58 handle_help_version_opts(argc, argv, "createlang", help);
60 while ((c = getopt_long(argc, argv, "lh:p:U:wWd:e", long_options, &optindex)) != -1)
62 switch (c)
64 case 'l':
65 listlangs = true;
66 break;
67 case 'h':
68 host = optarg;
69 break;
70 case 'p':
71 port = optarg;
72 break;
73 case 'U':
74 username = optarg;
75 break;
76 case 'w':
77 prompt_password = TRI_NO;
78 break;
79 case 'W':
80 prompt_password = TRI_YES;
81 break;
82 case 'd':
83 dbname = optarg;
84 break;
85 case 'e':
86 echo = true;
87 break;
88 default:
89 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
90 exit(1);
94 if (argc - optind > 0)
96 if (listlangs)
97 dbname = argv[optind++];
98 else
100 langname = argv[optind++];
101 if (argc - optind > 0)
102 dbname = argv[optind++];
106 if (argc - optind > 0)
108 fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
109 progname, argv[optind]);
110 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
111 exit(1);
114 if (dbname == NULL)
116 if (getenv("PGDATABASE"))
117 dbname = getenv("PGDATABASE");
118 else if (getenv("PGUSER"))
119 dbname = getenv("PGUSER");
120 else
121 dbname = get_user_name(progname);
124 initPQExpBuffer(&sql);
127 * List option
129 if (listlangs)
131 printQueryOpt popt;
132 static const bool translate_columns[] = {false, true};
134 conn = connectDatabase(dbname, host, port, username, prompt_password,
135 progname);
137 printfPQExpBuffer(&sql, "SELECT lanname as \"%s\", "
138 "(CASE WHEN lanpltrusted THEN '%s' ELSE '%s' END) as \"%s\" "
139 "FROM pg_catalog.pg_language WHERE lanispl;",
140 gettext_noop("Name"),
141 gettext_noop("yes"), gettext_noop("no"),
142 gettext_noop("Trusted?"));
143 result = executeQuery(conn, sql.data, progname, echo);
145 memset(&popt, 0, sizeof(popt));
146 popt.topt.format = PRINT_ALIGNED;
147 popt.topt.border = 1;
148 popt.topt.start_table = true;
149 popt.topt.stop_table = true;
150 popt.topt.encoding = PQclientEncoding(conn);
151 popt.title = _("Procedural Languages");
152 popt.translate_header = true;
153 popt.translate_columns = translate_columns;
154 printQuery(result, &popt, stdout, NULL);
156 PQfinish(conn);
157 exit(0);
160 if (langname == NULL)
162 fprintf(stderr, _("%s: missing required argument language name\n"), progname);
163 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
164 exit(1);
167 for (p = langname; *p; p++)
168 if (*p >= 'A' && *p <= 'Z')
169 *p += ('a' - 'A');
171 conn = connectDatabase(dbname, host, port, username, prompt_password, progname);
174 * Make sure the language isn't already installed
176 printfPQExpBuffer(&sql,
177 "SELECT oid FROM pg_catalog.pg_language WHERE lanname = '%s';",
178 langname);
179 result = executeQuery(conn, sql.data, progname, echo);
180 if (PQntuples(result) > 0)
182 PQfinish(conn);
183 fprintf(stderr,
184 _("%s: language \"%s\" is already installed in database \"%s\"\n"),
185 progname, langname, dbname);
186 /* separate exit status for "already installed" */
187 exit(2);
189 PQclear(result);
191 printfPQExpBuffer(&sql, "CREATE LANGUAGE \"%s\";\n", langname);
193 if (echo)
194 printf("%s", sql.data);
195 result = PQexec(conn, sql.data);
196 if (PQresultStatus(result) != PGRES_COMMAND_OK)
198 fprintf(stderr, _("%s: language installation failed: %s"),
199 progname, PQerrorMessage(conn));
200 PQfinish(conn);
201 exit(1);
204 PQclear(result);
205 PQfinish(conn);
206 exit(0);
211 static void
212 help(const char *progname)
214 printf(_("%s installs a procedural language into a PostgreSQL database.\n\n"), progname);
215 printf(_("Usage:\n"));
216 printf(_(" %s [OPTION]... LANGNAME [DBNAME]\n"), progname);
217 printf(_("\nOptions:\n"));
218 printf(_(" -d, --dbname=DBNAME database to install language in\n"));
219 printf(_(" -e, --echo show the commands being sent to the server\n"));
220 printf(_(" -l, --list show a list of currently installed languages\n"));
221 printf(_(" --help show this help, then exit\n"));
222 printf(_(" --version output version information, then exit\n"));
223 printf(_("\nConnection options:\n"));
224 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
225 printf(_(" -p, --port=PORT database server port\n"));
226 printf(_(" -U, --username=USERNAME user name to connect as\n"));
227 printf(_(" -w, --no-password never prompt for password\n"));
228 printf(_(" -W, --password force password prompt\n"));
229 printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));