Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
summaryrefslogtreecommitdiff
blob: aa5533fa30f2e65a1b763ae27767b7880399c118 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/bin/sh
#-------------------------------------------------------------------------
#
# createlang.sh--
#    Install a procedural language in a database
#
# Copyright (c) 1994, Regents of the University of California
#
#
# IDENTIFICATION
#    $Header: /cvsroot/pgsql/src/bin/createlang/Attic/createlang.sh,v 1.2 1999/07/09 17:57:46 momjian Exp $
#
#-------------------------------------------------------------------------

CMDNAME=`basename $0`

# ----------
# Find the default PGLIB directory
# ----------
postconfig_result="`sh -c postconfig 2>/dev/null`"
if [ ! -z "$postconfig_result" ]; then
    set -a
	eval "$postconfig_result"
	set +a
fi

# ----------
# Determine username
# ----------
if [ -z "$USER" ]; then
    if [ -z "$LOGNAME" ]; then
	if [ -z "`whoami`" ]; then
	    echo "$CMDNAME: cannot determine user name"
	    exit 1
	fi
    else
	USER=$LOGNAME
	export USER
    fi
fi

# ----------
# Get options, language name and dbname
# ----------
dbname=$USER
while [ -n "$1" ]
do
	case $1 in 
		--pglib)	PGLIB=$2; shift;;
		-a) 		AUTHSYS=$2; shift;;
		-h) 		PGHOST=$2; shift;;
		-p) 		PGPORT=$2; shift;;
		 *) 		langname=$1
		 			if [ -n "$2" ]; then
						shift
						dbname=$1
					fi;;
	esac
	shift;
done

# ----------
# Check that we have PGLIB
# ----------
if [ -z "$PGLIB" ]; then
	echo "Cannot determine PostgreSQL lib directory (PGLIB)."
	echo "You must identify the PGLIB either with a --pglib option"
	echo "or by setting the PGLIB environment variable."
	exit 1
fi

# ----------
# If not given on the commandline, ask for the language
# ----------
if [ -z "$langname" ]; then
	echo -n "Language to install in database $dbname: "
	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";;
	*)			echo "$CMDNAME: unsupported language '$langname'"
				echo "          supported languages are plpgsql and pltcl"
				exit 1;;
esac

# ----------
# Check that the shared object for the call handler is installed
# in PGLIB
# ----------
if [ ! -f $PGLIB/${langname}__DLSUFFIX__ ]; then
	echo "Cannot find the file $PGLIB/${langname}__DLSUFFIX__"
	echo "This shared object contains the call handler for $lancomp."
	echo "By default, only PL/pgSQL is built and installed. Other"
	echo "languages must be explicitly enabled at configure."
	echo ""
	echo "To install PL/Tcl make sure the option --with-tcl is"
	echo "given to configure, then recompile and install."
	exit 1
fi

# ----------
# Combine psql with options given
# ----------
if [ -z "$AUTHSYS" ]; then
  AUTHOPT=""
else
  AUTHOPT="-a $AUTHSYS"
fi

if [ -z "$PGHOST" ]; then
  PGHOSTOPT=""
else
  PGHOSTOPT="-h $PGHOST"
fi

if [ -z "$PGPORT" ]; then
  PGPORTOPT=""
else
  PGPORTOPT="-p $PGPORT"
fi

MONITOR="psql -tq $AUTHOPT $PGHOSTOPT $PGPORTOPT -c"

# ----------
# Make sure the language isn't already installed
# ----------
res=`$MONITOR "select oid from pg_language where lanname = '$langname'" $dbname`
if [ $? -ne 0 ]; then
	echo "Cannot install language"
	exit 1
fi
if [ ! -z "$res" ]; then
	echo "The language '$langname' is already installed in database $dbname"
	exit 2
fi

# ----------
# Check that there is no function named as the call handler
# ----------
res=`$MONITOR "select oid from pg_proc where proname = '$handler'" $dbname`
if [ ! -z "$res" ]; then
	echo "The language $lancomp isn't created up to now but there"
	echo "is already a function named '$handler' declared."
	echo "Language installation aborted."
	exit 1
fi

# ----------
# Create the call handler and the language
# ----------
$MONITOR "create function $handler () returns opaque as '$PGLIB/${langname}__DLSUFFIX__' language 'C'" $dbname
if [ $? -ne 0 ]; then
	echo "Language installation failed"
	exit 1
fi
$MONITOR "create $trusted procedural language '$langname' handler $handler lancompiler '$lancomp'" $dbname
if [ $? -ne 0 ]; then
	echo "Language installation failed"
	exit 1
fi


exit 0