abstable脚本程序
时间:2010-04-13 来源:whan
|
|
# 该程序根据数据库名、表名、输出类型和模式生成表的字段
# 组成的结构、数据库宿主变量、字段列表、字段初始化等,
# 分别存放到文件<表名>.[stt | dlr | fld | initfld]中.
# 目的是为了更好更快的书写与数据库表相关的程序.
# if [ $# -eq 4 ]
then
DBNAME=$1
TABNAME=$2
OUTTYPE=$3
SCHEMA=$4
else
echo "Usage: $0 <dbname> <tabname> <outtype> <schema>"
echo "dbname: 数据库名"
echo "tabname: 表名"
echo "outtype: 输出类型"
echo " dlr(declare)--变量定义"
echo " stt(struct)--结构体定义"
echo " all--变量定义和结构体定义"
echo "schema: 模式名(必须与数据库的describe table输出一致, 注意区分大小写)"
echo ""
exit 4
fi DLRNAME=$TABNAME.DLR
STTNAME=$TABNAME.STT
FLDNAME=$TABNAME.FLD
INITFLDNAME=$TABNAME.INITFLD #全部转换成小写
DLRNAME=`echo $DLRNAME | tr A-Z a-z`
STTNAME=`echo $STTNAME | tr A-Z a-z`
FLDNAME=`echo $FLDNAME | tr A-Z a-z`
INITFLDNAME=`echo $INITFLDNAME | tr A-Z a-z`
#变量进行大小写转换
TABNAME=`echo $TABNAME | tr A-Z a-z`
UPTABNAME=`echo $TABNAME | tr a-z A-Z`
DBNAME=`echo $DBNAME | tr A-Z a-z`
OUTTYPE=`echo $OUTTYPE | tr A-Z a-z`
SCHEMA=`echo $SCHEMA | tr a-z A-Z`
#连接数据库
db2 connect to $DBNAME >/dev/null
if [ $? -ne 0 ]
then
echo " 数据库($DBNAME)连接失败,退出程序!"
echo ""
exit 1
fi if [ -z "$SCHEMA" ]
then
echo "模式不能为空 !"
exit 2
else
if [ "$OUTTYPE" == "dlr" ]
then
echo "EXEC SQL BEGIN DECLARE SECTION;" > "$DLRNAME"
elif [ "$OUTTYPE" == "stt" ]
then
echo "typedef struct stt_$TABNAME {" > "$STTNAME"
elif [ "$OUTTYPE" == "fld" ]
then
echo "" > "$FLDNAME"
elif [ "$OUTTYPE" == "initfld" ]
then
echo "" > "$INITFLDNAME"
elif [ "$OUTTYPE" == "all" ]
then
echo "EXEC SQL BEGIN DECLARE SECTION;" > "$DLRNAME"
echo "typedef struct stt_$TABNAME {" > "$STTNAME"
fi db2 describe table $TABNAME \
| grep $SCHEMA \
| awk '{ \
if ( "'$OUTTYPE'" == "fld" ) { print "\tBEGIN-"tolower($1)"-END" }
else if( "'$OUTTYPE'" == "initfld" ) { print "\tbzero( "tolower($1)",\tsizeof("tolower($1)") );" }
else if( $3 == "INTEGER" ) { print "\tlong\t"tolower($1)";" } \
else if( $3 == "SMALLINT" ) { print "\tshort\t"tolower($1)";" } \
else if( $3 == "CHARACTER" ) { print "\tchar\t"tolower($1)"["$4"];"} \
else if( $3 == "VARCHAR" ) { print "\tchar\t"tolower($1)"["$4"];" } \
else if( $3 == "TIMESTAMP" ) { print "\tchar\t"tolower($1)"[""26""];" } \
else if( $3 == "DATE" ) { print "\tchar\t"tolower($1)"[""10""];" } \
else if( $3 == "DECIMAL" ) { print "\tdouble\t"tolower($1)";" } \
else if( $3 == "TIME" ) { print "\tchar\t"tolower($1)"[""10""];" } \
else if( $3 == "BLOB" ) { print "\tSQL TYPE is BLOB("$4")\t"tolower($1)";" } \
else if( $3 == "CLOB" ) { print "\tSQL TYPE is CLOB("$4")\t"tolower($1)";" }; \
}' \
| awk '{ \
if ( "'$OUTTYPE'"=="dlr" ) { print $0 >> "'$DLRNAME'" } \
else if( "'$OUTTYPE'"=="stt" ) { print $0 >> "'$STTNAME'" } \
else if( "'$OUTTYPE'"=="fld" ) { print $0 >> "'$FLDNAME'" } \
else if( "'$OUTTYPE'"=="initfld" ) { print $0 >> "'$INITFLDNAME'" } \
else { print $0 >> "'$DLRNAME'" ; print $0 >> "'$STTNAME'" } \
}' if [ "$OUTTYPE" == "dlr" ]
then
echo "EXEC SQL END DECLARE SECTION;" >> "$DLRNAME"
elif [ $OUTTYPE == "stt" ]
then
echo "} $UPTABNAME;" >> "$STTNAME"
elif [ "$OUTTYPE" == "all" ]
then
echo "EXEC SQL END DECLARE SECTION;" >> "$DLRNAME"
echo "} $UPTABNAME;" >> "$STTNAME"
fi
fi #断开数据库连接
db2 connect reset >/dev/null
相关阅读 更多 +