]> andersk Git - moira.git/blobdiff - backup/db2rest.awk
ignore placeholder entries
[moira.git] / backup / db2rest.awk
index 5cf4293e192559a2ff07f0e3c597d548bc1ddb1e..fd07bb68e2a38e3e18ec37754f122dd5e2c5f7c4 100644 (file)
@@ -9,49 +9,49 @@
 BEGIN { print "/* This file automatically generated */";
        print "/* Do not edit */";
        print "#include <stdio.h>";
+       print "EXEC SQL INCLUDE sqlca;";
        print "void parse_nl(), parse_str(), parse_sep();"
-       print "/* This file automatically generated */" > "rest1.qc";
-       print "/* Do not edit */" 
-       print "#include <stdio.h>" >> "rest1.qc"
-       print "FILE *open_file();" >> "rest1.qc"
-       print "do_restores(prefix)\n\tchar *prefix;\n{" >>"rest1.qc"
+       print "/* This file automatically generated */" > "rest1.dc";
+       print "/* Do not edit */" >> "rest1.dc";
+       print "#include <stdio.h>" >> "rest1.dc";
+       print "FILE *open_file();" >> "rest1.dc";
+       print "do_restores(prefix)\n\tchar *prefix;\n{" >>"rest1.dc";
 }
 
-/^create/ { printf "restore_%s(f)\nFILE *f;\n", $2;
-       printf "\trestore_%s(open_file(prefix, \"%s\"));\n", $2, $2 >> "rest1.qc"
-       tablename = $2;
+$1=="#" { next; }
+
+/^create/ { printf "restore_%s(f)\nFILE *f;\n", $3;
+       print "{\n\tEXEC SQL BEGIN DECLARE SECTION;";
+       printf "\trestore_%s(open_file(prefix, \"%s\"));\n", $3, $3 >> "rest1.dc";
+       tablename = $3;
        rangename = substr(tablename, 1, 1);
-       count = 0; }
+       count = 0;
+       next; }
 
-$2 ~ /\=/ {
+NF >= 2 {
        vname[count] = $1; 
        printf "/* %s */\n", $0
-       if ($3 ~ /i[24]/) {
-               printf "##      int     t_%s;\n", vname[count]
+       if ($2 ~ /INTEGER/ || $2 ~ /SMALLINT/ || $2 ~ /INTEGER1/) {
+               printf "\tint\tt_%s;\n", vname[count]
                vtype[count]="int"
-       } else if ($3 ~ /text\([0-9]*\)/) {
-               t = split($3, temp, "(")
-               if (t != 2) printf "Can't parse %s\n", $3;
+       } else if ($2 ~ /CHAR\([0-9]*\)/) {
+               t = split($2, temp, "(")
+               if (t != 2) printf "Can't parse %s\n", $2;
                t = split(temp[2], temp2, ")")
                if (t != 2) printf "Can't parse %s\n", temp[2];
-               printf "##      char    t_%s[%d];\n", vname[count], temp2[1]+1;
+               printf "\tchar\tt_%s[%d];\n", vname[count], temp2[1]+1;
                vsize[count] = temp2[1]+1;
                vtype[count]="str"
-       } else if ($3 ~ /date/) {
-               printf "##      char    t_%s[26];\n", vname[count];
+       } else if ($2 ~ /DATE/) {
+               printf "\tchar\tt_%s[26];\n", vname[count];
                vtype[count]="str";
                vsize[count]=26;
-       } else if ($3 ~ /c[0-9]*/) {
-               t = split($3, temp, ",")
-               vsize[count] = substr(temp[1], 2) + 1
-               printf "##      char    t_%s[%d];\n", vname[count],vsize[count]
-               vtype[count]="str"
-       } else printf "Unknown data type %s\n", $3;
+       } else printf "Unknown data type %s\n", $2;
        count++;
 }
 
-/^\($/ { print "##{" }
-/^\)$/ { 
+/^\);$/ { 
+       print "\tEXEC SQL END DECLARE SECTION;\n";
        print "\twhile(!feof(f)) {"
        for (i = 0; i < count; i++) {
                if (i != 0) print "\t\tparse_sep(f);"
@@ -62,16 +62,22 @@ $2 ~ /\=/ {
        }
        printf "\t\tparse_nl(f);\n"
 
-       printf "##\t\tappend %s (\n", tablename
+       printf "\t\tEXEC SQL INSERT INTO %s (\n", tablename;
+       for (i = 0; i < count; i++) {
+               if (i != 0) printf ",\n";
+               printf "\t\t\t%s", vname[i];
+       }
+       printf ")\n\t\tVALUES (\n";
        for (i = 0; i < count; i++) {
                if (i != 0) printf ",\n";
-               printf "##\t\t\t%s = t_%s", vname[i], vname[i]
+               printf "\t\t\t:t_%s", vname[i];
        }
-       printf ")\n"
-       printf "\t}\n"
+       printf ");\n\t\tif (sqlca.sqlcode != 0) {\n"
+       printf "\t\t\tcom_err(\"restore\", 0, \"insert failed\");\n"
+       printf "\t\t\texit(2);\n\t\t}\n\t}\n"
        printf "\t(void) fclose(f);\n"
-       printf "##}\n"
+       printf "}\n"
 }
 END { print "/* All done */"
-       print "}" >>"rest1.qc"
+       print "}" >>"rest1.dc";
 }
This page took 0.036516 seconds and 4 git commands to generate.