2 #-------------------------------------------------------------------------
5 # Install a procedural language in a database
7 # Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
8 # Portions Copyright (c) 1994, Regents of the University of California
10 # $Header: /cvsroot/pgsql/src/bin/scripts/Attic/createlang.sh,v 1.34 2002/02/18 23:11:30 petere Exp $
12 #-------------------------------------------------------------------------
14 CMDNAME=`basename "$0"`
15 PATHNAME=`echo $0 | sed "s,$CMDNAME\$,,"`
24 # Check for echo -n vs echo \c
26 if echo '\c' | grep -s c >/dev/null 2>&1
38 # Get options, language name and dbname
50 # options passed on to psql
52 PSQLOPT="$PSQLOPT -h $2"
58 PSQLOPT="$PSQLOPT -h "`echo $1 | sed 's/^--host=//'`
61 PSQLOPT="$PSQLOPT -p $2"
67 PSQLOPT="$PSQLOPT -p "`echo $1 | sed 's/^--port=//'`
70 PSQLOPT="$PSQLOPT -U $2"
76 PSQLOPT="$PSQLOPT -U "`echo $1 | sed 's/^--username=//'`
85 dbname=`echo "$1" | sed 's/^-d//'`
88 dbname=`echo "$1" | sed 's/^--dbname=//'`
95 PGLIB=`echo "$1" | sed 's/^-L//'`
98 PGLIB=`echo "$1" | sed 's/^--pglib=//'`
105 echo "$CMDNAME: invalid option: $1" 1>&2
106 echo "Try '$CMDNAME --help' for more information." 1>&2
110 if [ "$list" != "t" ]
119 if [ "$#" -ne 1 ]; then
120 echo "$CMDNAME: invalid option: $2" 1>&2
121 echo "Try '$CMDNAME --help' for more information." 1>&2
129 if [ -n "$usage" ]; then
130 echo "$CMDNAME installs a procedural language into a PostgreSQL database."
133 echo " $CMDNAME [options] langname [dbname]"
136 echo " -h, --host=HOSTNAME Database server host"
137 echo " -p, --port=PORT Database server port"
138 echo " -U, --username=USERNAME Username to connect as"
139 echo " -W, --password Prompt for password"
140 echo " -d, --dbname=DBNAME Database to install language in"
141 echo " -L, --pglib=DIRECTORY Find language interpreter file in DIRECTORY"
142 echo " -l, --list Show a list of currently installed languages"
144 echo "Report bugs to <pgsql-bugs@postgresql.org>."
149 if [ -z "$dbname" ]; then
150 if [ "$PGUSER" ]; then
153 dbname=`${PATHNAME}pg_id -u -n`
155 [ "$?" -ne 0 ] && exit 1
160 # List option, doesn't need langname
163 sqlcmd="SELECT lanname as \"Name\", lanpltrusted as \"Trusted?\" FROM pg_language WHERE lanispl = TRUE;"
164 if [ "$showsql" = yes ]; then
167 ${PATHNAME}psql $PSQLOPT -d "$dbname" -P 'title=Procedural languages' -c "$sqlcmd"
173 # We can't go any farther without a langname
175 if [ -z "$langname" ]; then
176 echo "$CMDNAME: missing required argument language name" 1>&2
177 echo "Try '$CMDNAME --help' for help." 1>&2
182 # Check that we have PGLIB
184 if [ -z "$PGLIB" ]; then
189 # Check if supported and set related values
192 langname=`echo "$langname" | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz'`
197 handler="plpgsql_call_handler"
202 handler="pltcl_call_handler"
207 handler="pltclu_call_handler"
212 handler="plperl_call_handler"
217 handler="plperl_call_handler"
222 handler="plpython_call_handler"
226 echo "$CMDNAME: unsupported language \"$langname\"" 1>&2
227 echo "Supported languages are plpgsql, pltcl, pltclu, plperl, plperlu, and plpython." 1>&2
233 PSQL="${PATHNAME}psql -A -t -q $PSQLOPT -d $dbname -c"
236 # Make sure the language isn't already installed
238 sqlcmd="SELECT oid FROM pg_language WHERE lanname = '$langname';"
239 if [ "$showsql" = yes ]; then
242 res=`$PSQL "$sqlcmd"`
243 if [ "$?" -ne 0 ]; then
244 echo "$CMDNAME: external error" 1>&2
247 if [ -n "$res" ]; then
248 echo "$CMDNAME: language \"$langname\" is already installed in database $dbname" 1>&2
249 # separate exit status for "already installed"
254 # Check whether the call handler exists
256 sqlcmd="SELECT oid FROM pg_proc WHERE proname = '$handler' AND prorettype = 0 AND pronargs = 0;"
257 if [ "$showsql" = yes ]; then
260 res=`$PSQL "$sqlcmd"`
261 if [ -n "$res" ]; then
268 # Create the call handler and the language
270 if [ "$handlerexists" = no ]; then
271 sqlcmd="CREATE FUNCTION \"$handler\" () RETURNS OPAQUE AS '$PGLIB/${object}' LANGUAGE C;"
272 if [ "$showsql" = yes ]; then
276 if [ "$?" -ne 0 ]; then
277 echo "$CMDNAME: language installation failed" 1>&2
282 sqlcmd="CREATE ${trusted}LANGUAGE \"$langname\" HANDLER \"$handler\";"
283 if [ "$showsql" = yes ]; then
287 if [ "$?" -ne 0 ]; then
288 echo "$CMDNAME: language installation failed" 1>&2
292 if test -n "$trusted"; then
293 sqlcmd="GRANT USAGE ON LANGUAGE \"$langname\" TO PUBLIC;"
294 if [ "$showsql" = yes ]; then
298 if [ "$?" -ne 0 ]; then
299 echo "$CMDNAME: language installation failed" 1>&2