版权所有,请勿转载,
GreenPlum存储过程的源码导出
发布时间:2012-03-23 来源:原创 作者:场之作坊
#!/bin/bash
source /usr/local/greenplum-db/greenplum_path.sh
USAGE ()
{
echo "Usage: `basename $0` [-d database] [-p port] [-h hostip] [ -U username ] {-f procname} [-o output]";
echo "ExpFunToFile Parameter Infomation:";
echo " Parameter -f must be specified [必须指定函数名 例如:public.test]";
echo " Parameter -d: Database Name,Default Value:PGDATABASE [数据库名]";
echo " Parameter -p: Database Port,Default Value:5432 [数据库端口]";
echo " Parameter -h: Database Host,Defualt Value:PGHOST [主机IP地址]";
echo " Parameter -U: User Name,Default Value:PGUSER [连接数据库名]";
echo " Parameter -o: Output Path,Default Value: pwd [目标目录]";
echo ""
exit 1;
}
if [ $# -lt 2 ]
then
USAGE
fi
dbname=""
procname=""
dbport=""
dbhost=""
outpath=""
dbuser=""
#***************************Judge Parameter Begin
while getopts :d:f:p:h:U:o: OPTION ;
do
case "$OPTION" in
d) dbname="$OPTARG" ;;
f) procname="$OPTARG" ;;
p) dbport="$OPTARG" ;;
h) dbhost="$OPTARG" ;;
o) outpath="$OPTARG" ;;
U) dbuser="$OPTARG" ;;
\?) #usage statement
USAGE;
;;
esac
done
if [ "$procname" = "" ]
then
echo "Parameter -f must be specified "
USAGE;
else
if [ `expr index $procname .` -le 0 ]
then
echo "The Function Name is the same as schema.funname."
echo "Eg: public.test "
exit 1
fi
fi
strdb=""
if [ "$dbname" != "" ]
then
strdb=" -d $dbname "
fi
if [ "$dbport" != "" ]
then
strdb=$strdb" -p $dbport "
fi
if [ "$dbhost" != "" ]
then
strdb=$strdb" -h $dbhost "
fi
if [ "$dbuser" != "" ]
then
strdb=$strdb" -U $dbuser "
fi
if [ "$outpath" != "" ]
then
outfile=$outpath/$procname.sql
else
outfile=$procname.sql
fi
#获取函数名以及相关的参数
sqlstr="select nspname||'.'|| proname procname,usename,lanname,prorettype::regtype,proargtypes,proallargtypes,proargmodes,proargnames \
from pg_catalog.pg_proc a \
left join pg_catalog.pg_namespace b on a.pronamespace=b.oid \
left join pg_catalog.pg_user c on a.proowner=c.usesysid \
left join pg_catalog.pg_language d on a.prolang=d.oid \
where nspname||'.'||proname=lower('$procname');"
#取出返回值
line=`psql -X -t -c "$sqlstr" -A -F#@# $strdb `
#echo $line,$strdb,$sqlstr
#分解返回值
vusername=`echo $line|awk -F#@# '{print $2}'`
vlanname=`echo $line|awk -F#@# '{print $3}'`
vretvalue=`echo $line|awk -F#@# '{print $4}'`
vargtypes=`echo $line|awk -F#@# '{print $5}'|sed 's/[{}]//g'`
vallargtypes=`echo $line|awk -F#@# '{print $6}'|sed 's/[{}]//g'`
vargmodes=`echo $line|awk -F#@# '{print $7}'|sed 's/[{}]//g'`
vargnames=`echo $line|awk -F#@# '{print $8}'|sed 's/[{}]//g'`
vfuntxt=`echo $line|awk -F#@# '{print $9}'`
#判断参数个数
fnum=`echo $vargnames|awk -F, '{print NF}'`
#echo $strarg
#重组函数的参数串
for((i=1;i<=$fnum;i++))
do
typeid=`echo $vargtypes|cut -d' ' -f$i`
atypeid=`echo $vallargtypes|cut -d, -f$i`
amodes=`echo $vargmodes|cut -d, -f$i`
argnames=`echo $vargnames|cut -d, -f$i`
echo $typeid
if [ "$atypeid" = "" ]
then
atypeid=$typeid
fi
atype=`psql -X -t -c "select $atypeid::regtype" $strdb `
if [ "$amodes" != "o" ]
then
strarg="${strarg},in $argnames $atype"
else
strarg="${strarg},out $argnames $atype"
fi
done
vstrarg=`echo $strarg|cut -c 2-`
#获取函数体
sqlstr=" select prosrc from pg_catalog.pg_proc a \
left join pg_catalog.pg_namespace b on a.pronamespace=b.oid \
where nspname||'.'||proname=lower('$procname'); "
#生成文件
echo "CREATE OR REPLACE FUNCTION ${procname}($vstrarg)" >$outfile.sql
echo "RETURNS $vretvalue AS " >>$outfile.sql
echo "\$BODY\$" >>$outfile.sql
psql -X -t -c "$sqlstr" $strdb >>$outfile.sql
echo "\$BODY\$" >>$outfile.sql
echo "LANGUAGE $vlanname VOLATILE;" >>$outfile.sql
#程序结束