2003-03-18 17:19:47 -05:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
|
*
|
|
|
|
|
* createlang
|
|
|
|
|
*
|
2015-01-06 11:43:47 -05:00
|
|
|
* Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
|
2003-03-18 17:19:47 -05:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
|
*
|
2010-09-20 16:08:53 -04:00
|
|
|
* src/bin/scripts/createlang.c
|
2003-03-18 17:19:47 -05:00
|
|
|
*
|
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
|
*/
|
|
|
|
|
#include "postgres_fe.h"
|
2005-09-05 19:50:49 -04:00
|
|
|
|
2003-03-18 17:19:47 -05:00
|
|
|
#include "common.h"
|
|
|
|
|
#include "print.h"
|
|
|
|
|
|
|
|
|
|
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'},
|
2009-02-26 11:02:39 -05:00
|
|
|
{"no-password", no_argument, NULL, 'w'},
|
2003-03-18 17:19:47 -05:00
|
|
|
{"password", no_argument, NULL, 'W'},
|
|
|
|
|
{"dbname", required_argument, NULL, 'd'},
|
|
|
|
|
{"echo", no_argument, NULL, 'e'},
|
|
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
|
};
|
|
|
|
|
|
2004-05-12 09:38:49 -04:00
|
|
|
const char *progname;
|
2003-03-18 17:19:47 -05:00
|
|
|
int optindex;
|
|
|
|
|
int c;
|
|
|
|
|
|
|
|
|
|
bool listlangs = false;
|
|
|
|
|
const char *dbname = NULL;
|
|
|
|
|
char *host = NULL;
|
|
|
|
|
char *port = NULL;
|
|
|
|
|
char *username = NULL;
|
2009-02-26 11:02:39 -05:00
|
|
|
enum trivalue prompt_password = TRI_DEFAULT;
|
2003-03-18 17:19:47 -05:00
|
|
|
bool echo = false;
|
|
|
|
|
char *langname = NULL;
|
|
|
|
|
|
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
|
|
PQExpBufferData sql;
|
|
|
|
|
|
|
|
|
|
PGconn *conn;
|
|
|
|
|
PGresult *result;
|
|
|
|
|
|
|
|
|
|
progname = get_progname(argv[0]);
|
2008-12-11 02:34:09 -05:00
|
|
|
set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
|
2004-05-31 22:54:09 -04:00
|
|
|
|
2003-03-18 17:19:47 -05:00
|
|
|
handle_help_version_opts(argc, argv, "createlang", help);
|
|
|
|
|
|
2009-02-26 11:02:39 -05:00
|
|
|
while ((c = getopt_long(argc, argv, "lh:p:U:wWd:e", long_options, &optindex)) != -1)
|
2003-03-18 17:19:47 -05:00
|
|
|
{
|
|
|
|
|
switch (c)
|
|
|
|
|
{
|
|
|
|
|
case 'l':
|
|
|
|
|
listlangs = true;
|
|
|
|
|
break;
|
|
|
|
|
case 'h':
|
2012-10-12 13:35:40 -04:00
|
|
|
host = pg_strdup(optarg);
|
2003-03-18 17:19:47 -05:00
|
|
|
break;
|
|
|
|
|
case 'p':
|
2012-10-12 13:35:40 -04:00
|
|
|
port = pg_strdup(optarg);
|
2003-03-18 17:19:47 -05:00
|
|
|
break;
|
|
|
|
|
case 'U':
|
2012-10-12 13:35:40 -04:00
|
|
|
username = pg_strdup(optarg);
|
2003-03-18 17:19:47 -05:00
|
|
|
break;
|
2009-02-26 11:02:39 -05:00
|
|
|
case 'w':
|
|
|
|
|
prompt_password = TRI_NO;
|
|
|
|
|
break;
|
2003-03-18 17:19:47 -05:00
|
|
|
case 'W':
|
2009-02-26 11:02:39 -05:00
|
|
|
prompt_password = TRI_YES;
|
2003-03-18 17:19:47 -05:00
|
|
|
break;
|
|
|
|
|
case 'd':
|
2012-10-12 13:35:40 -04:00
|
|
|
dbname = pg_strdup(optarg);
|
2003-03-18 17:19:47 -05:00
|
|
|
break;
|
|
|
|
|
case 'e':
|
|
|
|
|
echo = true;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2003-07-23 04:47:41 -04:00
|
|
|
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
|
2003-03-18 17:19:47 -05:00
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-17 18:30:34 -04:00
|
|
|
/*
|
2012-06-10 15:20:04 -04:00
|
|
|
* We set dbname from positional arguments if it is not already set by
|
|
|
|
|
* option arguments -d. If not doing listlangs, positional dbname must
|
|
|
|
|
* follow positional langname.
|
2012-04-17 18:30:34 -04:00
|
|
|
*/
|
|
|
|
|
|
2003-03-18 17:19:47 -05:00
|
|
|
if (argc - optind > 0)
|
|
|
|
|
{
|
|
|
|
|
if (listlangs)
|
2012-04-17 18:30:34 -04:00
|
|
|
{
|
|
|
|
|
if (dbname == NULL)
|
|
|
|
|
dbname = argv[optind++];
|
|
|
|
|
}
|
2003-03-18 17:19:47 -05:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
langname = argv[optind++];
|
2012-04-17 18:30:34 -04:00
|
|
|
if (argc - optind > 0 && dbname == NULL)
|
2003-03-18 17:19:47 -05:00
|
|
|
dbname = argv[optind++];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (argc - optind > 0)
|
|
|
|
|
{
|
2003-08-03 20:43:34 -04:00
|
|
|
fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
|
2003-03-18 17:19:47 -05:00
|
|
|
progname, argv[optind]);
|
2003-08-03 20:43:34 -04:00
|
|
|
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
|
2003-03-18 17:19:47 -05:00
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (dbname == NULL)
|
|
|
|
|
{
|
|
|
|
|
if (getenv("PGDATABASE"))
|
|
|
|
|
dbname = getenv("PGDATABASE");
|
|
|
|
|
else if (getenv("PGUSER"))
|
|
|
|
|
dbname = getenv("PGUSER");
|
|
|
|
|
else
|
2013-12-18 12:16:16 -05:00
|
|
|
dbname = get_user_name_or_exit(progname);
|
2003-03-18 17:19:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
initPQExpBuffer(&sql);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* List option
|
|
|
|
|
*/
|
|
|
|
|
if (listlangs)
|
|
|
|
|
{
|
|
|
|
|
printQueryOpt popt;
|
2008-07-14 18:00:04 -04:00
|
|
|
static const bool translate_columns[] = {false, true};
|
2003-03-18 17:19:47 -05:00
|
|
|
|
2015-11-12 16:05:23 -05:00
|
|
|
conn = connectDatabase(dbname, host, port, username, NULL,
|
|
|
|
|
prompt_password, progname, false);
|
2003-03-18 17:19:47 -05:00
|
|
|
|
2005-08-15 17:02:26 -04:00
|
|
|
printfPQExpBuffer(&sql, "SELECT lanname as \"%s\", "
|
2005-10-14 22:49:52 -04:00
|
|
|
"(CASE WHEN lanpltrusted THEN '%s' ELSE '%s' END) as \"%s\" "
|
|
|
|
|
"FROM pg_catalog.pg_language WHERE lanispl;",
|
2007-12-12 16:41:47 -05:00
|
|
|
gettext_noop("Name"),
|
|
|
|
|
gettext_noop("yes"), gettext_noop("no"),
|
|
|
|
|
gettext_noop("Trusted?"));
|
2003-03-18 17:19:47 -05:00
|
|
|
result = executeQuery(conn, sql.data, progname, echo);
|
|
|
|
|
|
|
|
|
|
memset(&popt, 0, sizeof(popt));
|
|
|
|
|
popt.topt.format = PRINT_ALIGNED;
|
|
|
|
|
popt.topt.border = 1;
|
2007-08-09 20:39:31 -04:00
|
|
|
popt.topt.start_table = true;
|
|
|
|
|
popt.topt.stop_table = true;
|
2003-03-18 17:19:47 -05:00
|
|
|
popt.topt.encoding = PQclientEncoding(conn);
|
|
|
|
|
popt.title = _("Procedural Languages");
|
2008-07-14 18:00:04 -04:00
|
|
|
popt.translate_header = true;
|
|
|
|
|
popt.translate_columns = translate_columns;
|
Fix translatability markings in psql, and add defenses against future bugs.
Several previous commits have added columns to various \d queries without
updating their translate_columns[] arrays, leading to potentially incorrect
translations in NLS-enabled builds. Offenders include commit 893686762
(added prosecdef to \df+), c9ac00e6e (added description to \dc+) and
3b17efdfd (added description to \dC+). Fix those cases back to 9.3 or
9.2 as appropriate.
Since this is evidently more easily missed than one would like, in HEAD
also add an Assert that the supplied array is long enough. This requires
an API change for printQuery(), so it seems inappropriate for back
branches, but presumably all future changes will be tested in HEAD anyway.
In HEAD and 9.3, also clean up a whole lot of sloppiness in the emitted
SQL for \dy (event triggers): lack of translatability due to failing to
pass words-to-be-translated through gettext_noop(), inadequate schema
qualification, and sloppy formatting resulting in unnecessarily ugly
-E output.
Peter Eisentraut and Tom Lane, per bug #8702 from Sergey Burladyan
2014-01-04 16:05:16 -05:00
|
|
|
popt.n_translate_columns = lengthof(translate_columns);
|
|
|
|
|
|
Fix behavior of printTable() and friends with externally-invoked pager.
The formatting modes that depend on knowledge of the terminal window width
did not work right when printing a query result that's been fetched in
sections (as a result of FETCH_SIZE). ExecQueryUsingCursor() would force
use of the pager as soon as there's more than one result section, and then
print.c would see an output file pointer that's not stdout and incorrectly
conclude that the terminal window width isn't relevant.
This has been broken all along for non-expanded "wrapped" output format,
and as of 9.5 the issue affects expanded mode as well. The problem also
caused "\pset expanded auto" mode to invariably *not* switch to expanded
output in a segmented result, which seems to me to be exactly backwards.
To fix, we need to pass down an "is_pager" flag to inform the print.c
subroutines that some calling level has already replaced stdout with a
pager pipe, so they should (a) not do that again and (b) nonetheless honor
the window size. (Notably, this makes the first is_pager test in
print_aligned_text() not be dead code anymore.)
This patch is a bit invasive because there are so many existing calls of
printQuery()/printTable(), but fortunately all but a couple can just pass
"false" for the added parameter.
Back-patch to 9.5 but no further. Given the lack of field complaints,
it's not clear that we should change the behavior in stable branches.
Also, the API change for printQuery()/printTable() might possibly break
third-party code, again something we don't like to do in stable branches.
However, it's not quite too late to do this in 9.5, and with the larger
scope of the problem there, it seems worth doing.
2015-12-02 18:20:33 -05:00
|
|
|
printQuery(result, &popt, stdout, false, NULL);
|
2003-03-18 17:19:47 -05:00
|
|
|
|
|
|
|
|
PQfinish(conn);
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (langname == NULL)
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr, _("%s: missing required argument language name\n"), progname);
|
2003-08-03 20:43:34 -04:00
|
|
|
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
|
2003-03-18 17:19:47 -05:00
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-10 16:39:02 -04:00
|
|
|
/* lower case language name */
|
2003-03-18 17:19:47 -05:00
|
|
|
for (p = langname; *p; p++)
|
|
|
|
|
if (*p >= 'A' && *p <= 'Z')
|
|
|
|
|
*p += ('a' - 'A');
|
|
|
|
|
|
2015-11-12 16:05:23 -05:00
|
|
|
conn = connectDatabase(dbname, host, port, username, NULL,
|
|
|
|
|
prompt_password, progname, false);
|
2003-03-18 17:19:47 -05:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Make sure the language isn't already installed
|
|
|
|
|
*/
|
2005-10-14 22:49:52 -04:00
|
|
|
printfPQExpBuffer(&sql,
|
|
|
|
|
"SELECT oid FROM pg_catalog.pg_language WHERE lanname = '%s';",
|
2005-07-10 10:26:30 -04:00
|
|
|
langname);
|
2003-03-18 17:19:47 -05:00
|
|
|
result = executeQuery(conn, sql.data, progname, echo);
|
|
|
|
|
if (PQntuples(result) > 0)
|
|
|
|
|
{
|
|
|
|
|
PQfinish(conn);
|
|
|
|
|
fprintf(stderr,
|
2005-10-14 22:49:52 -04:00
|
|
|
_("%s: language \"%s\" is already installed in database \"%s\"\n"),
|
2003-03-18 17:19:47 -05:00
|
|
|
progname, langname, dbname);
|
|
|
|
|
/* separate exit status for "already installed" */
|
|
|
|
|
exit(2);
|
|
|
|
|
}
|
|
|
|
|
PQclear(result);
|
|
|
|
|
|
2011-03-05 14:03:06 -05:00
|
|
|
/*
|
|
|
|
|
* In 9.1 and up, assume that languages should be installed using CREATE
|
|
|
|
|
* EXTENSION. However, it's possible this tool could be used against an
|
|
|
|
|
* older server, and it's easy enough to continue supporting the old way.
|
|
|
|
|
*/
|
|
|
|
|
if (PQserverVersion(conn) >= 90100)
|
2014-02-10 21:47:19 -05:00
|
|
|
printfPQExpBuffer(&sql, "CREATE EXTENSION \"%s\";", langname);
|
2011-03-05 14:03:06 -05:00
|
|
|
else
|
2014-02-10 21:47:19 -05:00
|
|
|
printfPQExpBuffer(&sql, "CREATE LANGUAGE \"%s\";", langname);
|
2004-03-19 13:58:07 -05:00
|
|
|
|
2003-03-18 17:19:47 -05:00
|
|
|
if (echo)
|
2014-02-10 21:47:19 -05:00
|
|
|
printf("%s\n", sql.data);
|
2003-03-18 17:19:47 -05:00
|
|
|
result = PQexec(conn, sql.data);
|
|
|
|
|
if (PQresultStatus(result) != PGRES_COMMAND_OK)
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr, _("%s: language installation failed: %s"),
|
|
|
|
|
progname, PQerrorMessage(conn));
|
|
|
|
|
PQfinish(conn);
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-29 15:52:46 -04:00
|
|
|
PQclear(result);
|
2003-03-18 17:19:47 -05:00
|
|
|
PQfinish(conn);
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
help(const char *progname)
|
|
|
|
|
{
|
|
|
|
|
printf(_("%s installs a procedural language into a PostgreSQL database.\n\n"), progname);
|
|
|
|
|
printf(_("Usage:\n"));
|
|
|
|
|
printf(_(" %s [OPTION]... LANGNAME [DBNAME]\n"), progname);
|
|
|
|
|
printf(_("\nOptions:\n"));
|
|
|
|
|
printf(_(" -d, --dbname=DBNAME database to install language in\n"));
|
|
|
|
|
printf(_(" -e, --echo show the commands being sent to the server\n"));
|
|
|
|
|
printf(_(" -l, --list show a list of currently installed languages\n"));
|
2012-06-17 19:44:00 -04:00
|
|
|
printf(_(" -V, --version output version information, then exit\n"));
|
|
|
|
|
printf(_(" -?, --help show this help, then exit\n"));
|
2009-02-25 08:03:07 -05:00
|
|
|
printf(_("\nConnection options:\n"));
|
2003-06-11 01:13:12 -04:00
|
|
|
printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
|
2003-03-18 17:19:47 -05:00
|
|
|
printf(_(" -p, --port=PORT database server port\n"));
|
|
|
|
|
printf(_(" -U, --username=USERNAME user name to connect as\n"));
|
2009-02-26 11:02:39 -05:00
|
|
|
printf(_(" -w, --no-password never prompt for password\n"));
|
2007-12-11 14:57:32 -05:00
|
|
|
printf(_(" -W, --password force password prompt\n"));
|
2003-03-18 17:19:47 -05:00
|
|
|
printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
|
|
|
|
|
}
|