createlang.sh 6.29 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
#!/bin/sh
#-------------------------------------------------------------------------
#
# createlang.sh--
#    Install a procedural language in a database
#
# Copyright (c) 1994, Regents of the University of California
#
#
# IDENTIFICATION
11
#    $Header: /cvsroot/pgsql/src/bin/scripts/Attic/createlang.sh,v 1.9 2000/05/15 16:12:39 momjian Exp $
12 13 14 15
#
#-------------------------------------------------------------------------

CMDNAME=`basename $0`
16
PATHNAME=`echo $0 | sed "s,$CMDNAME\$,,"`
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

PSQLOPT=
dbname=
langname=
list=

# Check for echo -n vs echo \c

if echo '\c' | grep -s c >/dev/null 2>&1
then
    ECHO_N="echo -n"
    ECHO_C=""
else
    ECHO_N="echo"
    ECHO_C='\c'
fi



# ----------
# Get options, language name and dbname
# ----------
while [ $# -gt 0 ]
do
    case "$1" in 
	--help|-\?)
		usage=t
44
                break
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
		;;
        --list|-l)
                list=t
                ;;
# options passed on to psql
	--host|-h)
		PSQLOPT="$PSQLOPT -h $2"
		shift;;
        -h*)
                PSQLOPT="$PSQLOPT $1"
                ;;
        --host=*)
                PSQLOPT="$PSQLOPT -h "`echo $1 | sed 's/^--host=//'`
                ;;
	--port|-p)
		PSQLOPT="$PSQLOPT -p $2"
		shift;;
        -p*)
                PSQLOPT="$PSQLOPT $1"
                ;;
        --port=*)
                PSQLOPT="$PSQLOPT -p "`echo $1 | sed 's/^--port=//'`
                ;;
68 69
	--username|-U)
		PSQLOPT="$PSQLOPT -U $2"
70 71 72 73 74 75 76 77 78 79
		shift;;
        -U*)
                PSQLOPT="$PSQLOPT $1"
                ;;
        --username=*)
                PSQLOPT="$PSQLOPT -U "`echo $1 | sed 's/^--username=//'`
                ;;
	--password|-W)
		PSQLOPT="$PSQLOPT -W"
		;;
80
	--dbname|-d)
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
		dbname="$2"
		shift;;
        -d*)
                dbname=`echo $1 | sed 's/^-d//'`
                ;;
        --dbname=*)
                dbname=`echo $1 | sed 's/^--dbname=//'`
                ;;
# misc options
	--pglib|-L)
                PGLIB="$2"
                shift;;
        -L*)
                PGLIB=`echo $1 | sed 's/^-L//'`
                ;;
        --pglib=*)
                PGLIB=`echo $1 | sed 's/^--pglib=//'`
                ;;

100
	-*)
101
		echo "$CMDNAME: invalid option: $1"
102 103 104
                echo "Try -? for help."
		exit 1
		;;
105
	 *)
106 107 108 109 110 111 112 113
 		if [ "$list" = 't' ]
		then	dbname="$1"
		else	langname="$1"
                	if [ "$2" ]
			then
	                        shift
				dbname="$1"
			fi
114 115 116 117 118 119
		fi
                ;;
    esac
    shift
done

Bruce Momjian's avatar
Hi,  
Bruce Momjian committed
120
if [ "$usage" ]; then	
121 122 123 124 125 126
        echo "$CMDNAME installs a procedural language into a PostgreSQL database."
	echo
	echo "Usage:"
        echo "  $CMDNAME [options] [langname [dbname]]"
        echo
	echo "Options:"
127 128 129 130 131 132 133
	echo "  -h, --host=HOSTNAME             Database server host"
	echo "  -p, --port=PORT                 Database server port"
	echo "  -U, --username=USERNAME         Username to connect as"
	echo "  -W, --password                  Prompt for password"
	echo "  -d, --dbname=DBNAME             Database to install language in"
	echo "  -L, --pglib=PGLIB               Find language interpreter in directory PGLIB"
	echo "  -l, --list                      Show a list of currently installed languages"
134 135
        echo
	echo "Report bugs to <pgsql-bugs@postgresql.org>."
136
	exit 0
137 138 139 140 141 142 143
fi


# ----------
# Check that we have a database
# ----------
if [ -z "$dbname" ]; then
144 145
	echo "$CMDNAME: missing required argument database name"
        echo "Try -? for help."
146 147 148 149
	exit 1
fi


150 151 152 153 154 155 156 157 158
# ----------
# List option
# ----------
if [ "$list" ]; then
        ${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'"
        exit $?
fi


159 160 161 162
# ----------
# Check that we have PGLIB
# ----------
if [ -z "$PGLIB" ]; then
163 164 165 166
	echo "$CMDNAME: missing required argument PGLIB directory"
        echo "(This is the directory where the interpreter for the procedural"
        echo "language is stored. Traditionally, these are installed in whatever"
        echo "'lib' directory was specified at configure time.)"
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
	exit 1
fi

# ----------
# If not given on the command line, ask for the language
# ----------
if [ -z "$langname" ]; then
	$ECHO_N "Language to install in database $dbname: "$ECHO_C
	read langname
fi

# ----------
# Check if supported and set related values
# ----------
case "$langname" in
	plpgsql)
                lancomp="PL/pgSQL"
		trusted="TRUSTED "
		handler="plpgsql_call_handler"
                ;;
	pltcl)
		lancomp="PL/Tcl"
		trusted="TRUSTED "
		handler="pltcl_call_handler";;
191 192 193 194
	plperl)
		lancomp="PL/Perl"
		trusted="TRUSTED "
		handler="plperl_call_handler";;
195
	*)
196
		echo "$CMDNAME: unsupported language '$langname'"
197
		echo "Supported languages are 'plpgsql', 'pltcl', and 'plperl'."
198 199 200 201 202 203 204 205 206 207
		exit 1
        ;;
esac


# ----------
# Check that the shared object for the call handler is installed
# in PGLIB
# ----------
if [ ! -f $PGLIB/${langname}__DLSUFFIX__ ]; then
208
	echo "$CMDNAME: cannot find the file $PGLIB/${langname}__DLSUFFIX__"
209 210 211 212 213 214 215 216 217 218 219
        echo ""
	echo "This file contains the call handler for $lancomp. By default,"
        echo "only PL/pgSQL is built and installed; other languages must be"
        echo "explicitly enabled at configure time."
	echo ""
	echo "To install PL/Tcl, make sure the option --with-tcl is given to"
        echo "configure, then recompile and install."
	exit 1
fi


220
PSQL="${PATHNAME}psql -A -t -q $PSQLOPT -d $dbname -c"
221 222 223 224 225 226

# ----------
# Make sure the language isn't already installed
# ----------
res=`$PSQL "SELECT oid FROM pg_language WHERE lanname = '$langname'"`
if [ $? -ne 0 ]; then
227
	echo "$CMDNAME: external error"
228 229 230
	exit 1
fi
if [ "$res" ]; then
231 232
	echo "$CMDNAME: '$langname' is already installed in database $dbname"
	exit 1
233 234 235 236 237 238 239
fi

# ----------
# Check that there is no function named as the call handler
# ----------
res=`$PSQL "SELECT oid FROM pg_proc WHERE proname = '$handler'"`
if [ ! -z "$res" ]; then
240
	echo "$CMDNAME: A function named '$handler' already exists. Installation aborted."
241 242 243 244 245 246 247 248
	exit 1
fi

# ----------
# Create the call handler and the language
# ----------
$PSQL "CREATE FUNCTION $handler () RETURNS OPAQUE AS '$PGLIB/${langname}__DLSUFFIX__' LANGUAGE 'C'"
if [ $? -ne 0 ]; then
249
	echo "$CMDNAME: language installation failed"
250 251
	exit 1
fi
252

253 254
$PSQL "CREATE ${trusted}PROCEDURAL LANGUAGE '$langname' HANDLER $handler LANCOMPILER '$lancomp'"
if [ $? -ne 0 ]; then
255
	echo "$CMDNAME: language installation failed"
256 257 258 259
	exit 1
fi

exit 0