2 #-------------------------------------------------------------------------
5 # Install a procedural language in a database
7 # Copyright (c) 1994, Regents of the University of California
11 # $Header: /cvsroot/pgsql/src/bin/scripts/Attic/createlang.sh,v 1.22 2001/01/21 05:16:45 momjian Exp $
13 #-------------------------------------------------------------------------
16 PATHNAME=`echo $0 | sed "s,$CMDNAME\$,,"`
23 # Check for echo -n vs echo \c
25 if echo '\c' | grep -s c >/dev/null 2>&1
37 # Get options, language name and dbname
49 # options passed on to psql
51 PSQLOPT="$PSQLOPT -h $2"
57 PSQLOPT="$PSQLOPT -h "`echo $1 | sed 's/^--host=//'`
60 PSQLOPT="$PSQLOPT -p $2"
66 PSQLOPT="$PSQLOPT -p "`echo $1 | sed 's/^--port=//'`
69 PSQLOPT="$PSQLOPT -U $2"
75 PSQLOPT="$PSQLOPT -U "`echo $1 | sed 's/^--username=//'`
84 dbname=`echo "$1" | sed 's/^-d//'`
87 dbname=`echo "$1" | sed 's/^--dbname=//'`
94 PGLIB=`echo "$1" | sed 's/^-L//'`
97 PGLIB=`echo "$1" | sed 's/^--pglib=//'`
101 echo "$CMDNAME: invalid option: $1" 1>&2
102 echo "Try '$CMDNAME --help' for more information." 1>&2
106 if [ "$list" != "t" ]
120 if [ "$usage" ]; then
121 echo "$CMDNAME installs a procedural language into a PostgreSQL database."
124 echo " $CMDNAME [options] [langname] dbname"
127 echo " -h, --host=HOSTNAME Database server host"
128 echo " -p, --port=PORT Database server port"
129 echo " -U, --username=USERNAME Username to connect as"
130 echo " -W, --password Prompt for password"
131 echo " -d, --dbname=DBNAME Database to install language in"
132 echo " -L, --pglib=DIRECTORY Find language interpreter file in DIRECTORY"
133 echo " -l, --list Show a list of currently installed languages"
135 echo "If 'langname' is not specified, you will be prompted interactively."
136 echo "A database name must be specified."
138 echo "Report bugs to <pgsql-bugs@postgresql.org>."
144 # Check that we have a database
146 if [ -z "$dbname" ]; then
147 echo "$CMDNAME: missing required argument database name" 1>&2
148 echo "Try '$CMDNAME -?' for help." 1>&2
157 ${PATHNAME}psql $PSQLOPT -d "$dbname" -P 'title=Procedural languages' -c "SELECT lanname as \"Name\", lanpltrusted as \"Trusted?\", lancompiler as \"Compiler\" FROM pg_language WHERE lanispl = 't'"
163 # Check that we have PGLIB
165 if [ -z "$PGLIB" ]; then
170 # If not given on the command line, ask for the language
172 if [ -z "$langname" ]; then
173 $ECHO_N "Language to install in database $dbname: "$ECHO_C
178 # Check if supported and set related values
184 handler="plpgsql_call_handler"
190 handler="pltcl_call_handler"
194 lancomp="PL/Tcl (untrusted)"
196 handler="pltclu_call_handler"
202 handler="plperl_call_handler"
206 echo "$CMDNAME: unsupported language '$langname'" 1>&2
207 echo "Supported languages are 'plpgsql', 'pltcl', 'pltclu', and 'plperl'." 1>&2
212 DLSUFFIX='@DLSUFFIX@'
215 # Check that the shared object for the call handler is installed
218 if [ ! -f "$PGLIB/$object$DLSUFFIX" ]; then
220 echo "$CMDNAME: cannot find the file '$PGLIB/$langname$DLSUFFIX'"
222 echo "This file contains the call handler for $lancomp. By default,"
223 echo "only PL/pgSQL is built and installed; other languages must be"
224 echo "explicitly enabled at configure time."
226 echo "To install PL/Tcl, make sure the option --with-tcl is given to"
227 echo "configure, then recompile and install. To install PL/Perl use"
234 PSQL="${PATHNAME}psql -A -t -q $PSQLOPT -d $dbname -c"
237 # Make sure the language isn't already installed
239 res=`$PSQL "SELECT oid FROM pg_language WHERE lanname = '$langname'"`
240 if [ $? -ne 0 ]; then
241 echo "$CMDNAME: external error" 1>&2
245 echo "$CMDNAME: '$langname' is already installed in database $dbname" 1>&2
246 # separate exit status for "already installed"
251 # Check that there is no function named as the call handler
253 res=`$PSQL "SELECT oid FROM pg_proc WHERE proname = '$handler'"`
254 if [ ! -z "$res" ]; then
255 echo "$CMDNAME: A function named '$handler' already exists. Installation aborted." 1>&2
260 # Create the call handler and the language
262 $PSQL "CREATE FUNCTION $handler () RETURNS OPAQUE AS '$PGLIB/${object}$DLSUFFIX' LANGUAGE 'C'"
263 if [ $? -ne 0 ]; then
264 echo "$CMDNAME: language installation failed" 1>&2
268 $PSQL "CREATE ${trusted}PROCEDURAL LANGUAGE '$langname' HANDLER $handler LANCOMPILER '$lancomp'"
269 if [ $? -ne 0 ]; then
270 echo "$CMDNAME: language installation failed" 1>&2