#!/bin/sh
if [ $# -ne 2 ]; then
exit 1
fi
if [ ! -f "$1" ]; then
exit 2
fi
#clear the temp files
rm -rf $1.tmp
rm -rf $2.tmp
rm -rf $2.tmp0
rm -rf $2.tmp1
rm -rf $2.tmp2
rm -rf $2.tmp3
rm -rf $2
touch $2.tmp;
touch $2.tmp0;
touch $2.tmp1;
touch $2.tmp2;
touch $2.tmp3;
#translate the binary file into ascii file
strings $1 > $1.tmp
#get the primary key of the table.
primaryKey=`grep "ALTER TABLE " $1.tmp | grep "ADD PRIMARY KEY (" | sed 's/)/)#/g' | sed 's/PRIMARY KEY/#PRIMARY KEY/g' | cut -f2 -d '#' -s | sed 's/"//g' | awk -F# '{ for (i = 1; i <= NF; i++) { if (i == NF) { print $i } else { print $i","} } }'`
#get the SQL sentence head - Create table
grep "CREATE TABLE " $1.tmp > $2.tmp
cut -f1-3 -d ' ' -s $2.tmp | sed 's/"//g' > $2.tmp0
echo '(' >> $2.tmp0
#get the field string
cut -f2- -d '(' -s $2.tmp | sed 's/) PCTFREE/#/' | cut -f1 -d '#' -s | sed 's/, "/#/g' | sed 's/"//g' | awk -F# '{ for (i = 1; i <= NF; i++) { print " "$i"," } }' >> $2.tmp1
#get the default values for the fields in the table.
grep 'ALTER TABLE ' $1.tmp | grep ' MODIFY (' | sed 's/(/# /g' | sed 's/)/,#/g' | cut -f2 -d '#' | sed 's/"//g' > $2.tmp2
#join the field names with the default values
#*** END *** AP3D00784 2008-4-15 mod for ENIP V100R001C51B030
#join "$2.tmp1" "$2.tmp2" | sed 's/, DEFAULT/# DEFAULT/g' >> $2.tmp3
field=`cat $2.tmp1 | awk '{print $1}'`
for line in $field
do
#*** BEGIN 2008-09-19 ENIP V00R001C01B155SPC011 AP3D02225 zhangshixiong modify***
#te=`cat $2.tmp2 |grep "^ $line" | sed 's/'"$line"'//g'`
#te2=`cat $2.tmp1 |grep "^ $line"`
te=`cat $2.tmp2 |grep "^ $line " | sed 's/'"$line"'//g'`
te2=`cat $2.tmp1 |grep "^ $line "`
#*** END 2008-09-19 ENIP V00R001C01B155SPC011 AP3D02225 zhangshixiong modify***
echo " $te2 $te " | sed 's/, DEFAULT/# DEFAULT/g'>>$2.tmp3
done
#*** END *** AP3D00784 2008-4-15 mod for ENIP V100R001C51B030
tr -s "#" "\n" < $2.tmp3 >>$2.tmp0
if [ "x${primaryKey}" = "x" ]; then
sed $p 's/,//' $2.tmp0
fi
echo ${primaryKey} >> $2.tmp0
echo ')' >> $2.tmp0
echo "" >> $2.tmp0
#get the index of the table
grep 'CREATE ' $1.tmp | grep ' INDEX '| sed 's/)/);#/g' | sed 's/"//g' | cut -f1 -d '#' >> $2.tmp0
###***BEGIN***AP3D01843 liuyi Modified for ENIP V100R001C51B060
#translate all the character into the lower character
#tr "[A-Z]" "[a-z]" < $2.tmp0 > $2
cat $2.tmp0 > $2
###***END***AP3D01843 liuyi Modified for ENIP V100R001C51B060
#clear the temp files
rm -rf $1.tmp
rm -rf $2.tmp
rm -rf $2.tmp0
rm -rf $2.tmp1
rm -rf $2.tmp2
rm -rf $2.tmp3