OSDN Git Service

20ec9cd4b3f6a701a19d9cacf8b7b51d5cb23a87
[pg-rex/syncrep.git] / src / bin / scripts / createlang.sh
1 #!/bin/sh
2 #-------------------------------------------------------------------------
3 #
4 # createlang.sh--
5 #    Install a procedural language in a database
6 #
7 # Copyright (c) 1994, Regents of the University of California
8 #
9 #
10 # IDENTIFICATION
11 #    $Header: /cvsroot/pgsql/src/bin/scripts/Attic/createlang.sh,v 1.22 2001/01/21 05:16:45 momjian Exp $
12 #
13 #-------------------------------------------------------------------------
14
15 CMDNAME=`basename $0`
16 PATHNAME=`echo $0 | sed "s,$CMDNAME\$,,"`
17
18 PSQLOPT=
19 dbname=
20 langname=
21 list=
22
23 # Check for echo -n vs echo \c
24
25 if echo '\c' | grep -s c >/dev/null 2>&1
26 then
27     ECHO_N="echo -n"
28     ECHO_C=""
29 else
30     ECHO_N="echo"
31     ECHO_C='\c'
32 fi
33
34
35
36 # ----------
37 # Get options, language name and dbname
38 # ----------
39 while [ $# -gt 0 ]
40 do
41     case "$1" in 
42         --help|-\?)
43                 usage=t
44                 break
45                 ;;
46         --list|-l)
47                 list=t
48                 ;;
49 # options passed on to psql
50         --host|-h)
51                 PSQLOPT="$PSQLOPT -h $2"
52                 shift;;
53         -h*)
54                 PSQLOPT="$PSQLOPT $1"
55                 ;;
56         --host=*)
57                 PSQLOPT="$PSQLOPT -h "`echo $1 | sed 's/^--host=//'`
58                 ;;
59         --port|-p)
60                 PSQLOPT="$PSQLOPT -p $2"
61                 shift;;
62         -p*)
63                 PSQLOPT="$PSQLOPT $1"
64                 ;;
65         --port=*)
66                 PSQLOPT="$PSQLOPT -p "`echo $1 | sed 's/^--port=//'`
67                 ;;
68         --username|-U)
69                 PSQLOPT="$PSQLOPT -U $2"
70                 shift;;
71         -U*)
72                 PSQLOPT="$PSQLOPT $1"
73                 ;;
74         --username=*)
75                 PSQLOPT="$PSQLOPT -U "`echo $1 | sed 's/^--username=//'`
76                 ;;
77         --password|-W)
78                 PSQLOPT="$PSQLOPT -W"
79                 ;;
80         --dbname|-d)
81                 dbname="$2"
82                 shift;;
83         -d*)
84                 dbname=`echo "$1" | sed 's/^-d//'`
85                 ;;
86         --dbname=*)
87                 dbname=`echo "$1" | sed 's/^--dbname=//'`
88                 ;;
89 # misc options
90         --pglib|-L)
91                 PGLIB="$2"
92                 shift;;
93         -L*)
94                 PGLIB=`echo "$1" | sed 's/^-L//'`
95                 ;;
96         --pglib=*)
97                 PGLIB=`echo "$1" | sed 's/^--pglib=//'`
98                 ;;
99
100         -*)
101                 echo "$CMDNAME: invalid option: $1" 1>&2
102                 echo "Try '$CMDNAME --help' for more information." 1>&2
103                 exit 1
104                 ;;
105          *)
106                 if [ "$list" != "t" ]
107                 then    langname="$1"
108                         if [ "$2" ]
109                         then
110                                 shift
111                                 dbname="$1"
112                         fi
113                 else    dbname="$1"
114                 fi
115                 ;;
116     esac
117     shift
118 done
119
120 if [ "$usage" ]; then   
121         echo "$CMDNAME installs a procedural language into a PostgreSQL database."
122         echo
123         echo "Usage:"
124         echo "  $CMDNAME [options] [langname] dbname"
125         echo
126         echo "Options:"
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"
134         echo
135         echo "If 'langname' is not specified, you will be prompted interactively."
136         echo "A database name must be specified."
137         echo
138         echo "Report bugs to <pgsql-bugs@postgresql.org>."
139         exit 0
140 fi
141
142
143 # ----------
144 # Check that we have a database
145 # ----------
146 if [ -z "$dbname" ]; then
147         echo "$CMDNAME: missing required argument database name" 1>&2
148         echo "Try '$CMDNAME -?' for help." 1>&2
149         exit 1
150 fi
151
152
153 # ----------
154 # List option
155 # ----------
156 if [ "$list" ]; then
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'"
158         exit $?
159 fi
160
161
162 # ----------
163 # Check that we have PGLIB
164 # ----------
165 if [ -z "$PGLIB" ]; then
166         PGLIB='@libdir@'
167 fi
168
169 # ----------
170 # If not given on the command line, ask for the language
171 # ----------
172 if [ -z "$langname" ]; then
173         $ECHO_N "Language to install in database $dbname: "$ECHO_C
174         read langname
175 fi
176
177 # ----------
178 # Check if supported and set related values
179 # ----------
180 case "$langname" in
181         plpgsql)
182                 lancomp="PL/pgSQL"
183                 trusted="TRUSTED "
184                 handler="plpgsql_call_handler"
185                 object="plpgsql"
186                 ;;
187         pltcl)
188                 lancomp="PL/Tcl"
189                 trusted="TRUSTED "
190                 handler="pltcl_call_handler"
191                 object="pltcl"
192                 ;;
193         pltclu)
194                 lancomp="PL/Tcl (untrusted)"
195                 trusted=""
196                 handler="pltclu_call_handler"
197                 object="pltcl"
198                 ;;
199         plperl)
200                 lancomp="PL/Perl"
201                 trusted="TRUSTED "
202                 handler="plperl_call_handler"
203                 object="plperl"
204                 ;;
205         *)
206                 echo "$CMDNAME: unsupported language '$langname'" 1>&2
207                 echo "Supported languages are 'plpgsql', 'pltcl', 'pltclu', and 'plperl'." 1>&2
208                 exit 1
209         ;;
210 esac
211
212 DLSUFFIX='@DLSUFFIX@'
213
214 # ----------
215 # Check that the shared object for the call handler is installed
216 # in PGLIB
217 # ----------
218 if [ ! -f "$PGLIB/$object$DLSUFFIX" ]; then
219       (
220         echo "$CMDNAME: cannot find the file '$PGLIB/$langname$DLSUFFIX'"
221         echo ""
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."
225         echo ""
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"
228         echo "--with-perl."
229       ) 1>&2
230         exit 1
231 fi
232
233
234 PSQL="${PATHNAME}psql -A -t -q $PSQLOPT -d $dbname -c"
235
236 # ----------
237 # Make sure the language isn't already installed
238 # ----------
239 res=`$PSQL "SELECT oid FROM pg_language WHERE lanname = '$langname'"`
240 if [ $? -ne 0 ]; then
241         echo "$CMDNAME: external error" 1>&2
242         exit 1
243 fi
244 if [ "$res" ]; then
245         echo "$CMDNAME: '$langname' is already installed in database $dbname" 1>&2
246         # separate exit status for "already installed"
247         exit 2
248 fi
249
250 # ----------
251 # Check that there is no function named as the call handler
252 # ----------
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
256         exit 1
257 fi
258
259 # ----------
260 # Create the call handler and the language
261 # ----------
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
265         exit 1
266 fi
267
268 $PSQL "CREATE ${trusted}PROCEDURAL LANGUAGE '$langname' HANDLER $handler LANCOMPILER '$lancomp'"
269 if [ $? -ne 0 ]; then
270         echo "$CMDNAME: language installation failed" 1>&2
271         exit 1
272 fi
273
274 exit 0