?? sql2stru.sh
字號:
#!/bin/ksh
# luojun,2006-8-15
# 從 sql 建庫腳本生成 dbbuf struct 結構定義
# sql腳本格式:
# create table t_abc
# ( // 1、前 ( 在 create table 后面一行
# tlr_no char(4) not null , {柜員號} // 2、字段名與類型定義必須在一行
# draw_box_flag char(1) {領用標志} // 3、字段名與類型定義必須在一行
# default '0' not null, // 4、其他約束條件可以另起行
# csh_attr char(1) // 5、每個字段的定義最多兩行
# default '0' not null {現(xiàn)金性質(zhì)} // 6、注釋可以在不同的行
# ) ; // 7、后 ) 必須另起一行
# revoke all on "cbs20".t_clm_box_mgmt
# from "public";
#
# create unique index
# "cbs20".idx_clm_box_mgmt on
# "cbs20".t_clm_box_mgmt
# (inst_no,box_no); // 8、revoke、index等其他定義忽略
# // 9、可處理一個文件多個表的定義
if [ $# -ne 2 ]
then
echo "sql2stru.sh sql腳本文件 生成的頭文件名稱"
exit 0
fi
SQL_FILE=$1
INCL_FILE=$2
gen_date=`date +"%Y-%m-%d %T"`
echo "/* 從文件 " $SQL_FILE " 生成開始 */ " > $INCL_FILE
echo "/* 生成時間:\c" >> $INCL_FILE
echo $gen_date "*/" >> $INCL_FILE
awk '
BEGIN {
printf "/* 以下是根據(jù)sql腳本自動建立的, 如果有錯誤請檢查建庫腳本 */ \n";
# printf "$include \"datetime.h\";\n\n";
# printf "#ifndef DBBUF_H\n";
# printf "#define DBBUF_H\n";
# printf "#define MAX_HIS_LEN 1000 /*歷史備份字串最大長度*/\n\n";
one_row = 0;
n_lines = 0;
n_tables = 0;
in_flag = 0;
}
{
n_lines = n_lines + 1;
if ( tolower($1) == "create" )
{
if ( tolower($2) == "table" )
{
if ( in_flag != 0 )
{
printf " ERROR line %d \n",n_lines;
}
in_flag = 1;
sub_idx = index($3,".") + 1;
struct_name = tolower(substr($3,sub_idx));
struct_name = "s" substr(struct_name,2);
printf "$struct %s {\n",struct_name;
n_tables = n_tables + 1;
if ( $4 == "(" && in_flag == 1 )
in_flag = 2;
}
}
if ( $1 == "(" && in_flag == 1 )
in_flag = 2;
if ( one_row == 1 && in_flag == 2 )
{
if ( cmt_str == "" )
printf " %-8s%-25s\n",fld_type,fld_name ";";
else
printf " %-8s%-25s/* %s */\n",fld_type,fld_name ";",cmt_str;
if ( next_table == 1 )
{
printf "};\n\n"
next_table = 0;
in_flag = 0;
}
one_row = 0;
}
if ( NF > 0 && in_flag == 3 )
{
if ( substr($1,1,1) == ")" )
{
in_flag = 2;
one_row = 1;
next_table = 1;
}
else
{
test_str = $0;
cmt = tolower($NF);
if ( index(cmt,"{") > 0 )
{
cmt_str1 = substr(cmt,index(cmt,"{") + 1);
cmt_str = substr(cmt_str1,1,index(cmt_str1,"}") - 1);
}
else
{
cmt_str = "";
}
if ( index (test_str , ",") == 0 )
{
in_flag = 3;
}
else
{
in_flag = 2;
}
one_row = 1;
}
}
else if ( NF > 1 && in_flag == 2 && tolower($1) != "create" )
{
fld_name = tolower($1);
fldtype = tolower($2);
test_str = $0;
if ( tolower(substr(fldtype,1,4)) == "date" )
{
fld_type = "long";
}
if ( tolower(substr(fldtype,1,8)) == "datetime" )
{
fld_type = "dtime_t";
}
if ( tolower(substr(fldtype,1,7)) == "int" )
{
fld_type = "long";
}
if ( tolower(substr(fldtype,1,7)) == "integer" )
{
fld_type = "long";
}
if ( tolower(substr(fldtype,1,7)) == "serial" )
{
fld_type = "long";
}
if ( tolower(substr(fldtype,1,7)) == "decimal" )
{
fld_type = "double";
test_str = substr($0,index($0,",") + 1);
}
if ( tolower(substr(fldtype,1,4)) == "char" || tolower(substr(fldtype,1,7)) == "varchar")
{
fld_type = "char";
len = substr(tolower(fldtype),index(tolower(fldtype),"(") + 1,index(tolower(fldtype),")") - index(tolower(fldtype),"(") - 1);
# if ( substr(fld_name,length(fld_name) - 2,3) == "tlr" && len != 6 )
# {
# printf "ERROR line %d TLR length is not 6\n",n_lines;
# }
# else
# if ( substr(fld_name,1,3) == "tlr" && len != 6 )
# {
# printf "ERROR line %d TLR length is not 6\n",n_lines;
# }
fld_name = fld_name "[" len "+1]";
}
cmt = tolower($NF);
if ( index(cmt,"{") > 0 )
{
cmt_str1 = substr(cmt,index(cmt,"{") + 1);
cmt_str = substr(cmt_str1,1,index(cmt_str1,"}") - 1);
}
else
{
cmt_str = "";
}
if ( index (test_str , ",") == 0 )
{
in_flag = 3;
}
else
{
one_row = 1;
}
}
}
END {
if ( in_flag != 0 && one_row != 0 )
{
printf " ERROR line %d \n",n_lines;
}
# printf "#endif\n";
printf "/* 以上是根據(jù)sql腳本自動建立的, 如果有錯誤請檢查建庫腳本 */ \n";
printf "/* 一共處理 %d 行腳本,生成 %d 個結構 */\n",n_lines,n_tables;
# printf "/* 自動生成完成 */\n";
}' $SQL_FILE >> $INCL_FILE
echo "/* 從文件 " $SQL_FILE " 生成結束 */ " >> $INCL_FILE
echo "EXEC SQL INCLUDE "$INCL_FILE " ;\n" > $INCL_FILE.ec
esql -e $INCL_FILE.ec
if [ $? -eq 0 ]
then
echo "No Error!\n"
else
echo "Error!\n"
fi
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -