]> andersk Git - moira.git/blob - backup/db2rest.awk
In the past year, added: /mit/info/public-mailing-lists maintained
[moira.git] / backup / db2rest.awk
1 #       $Source$
2 #       $Header$
3 #
4 #       This converts the file used to originally create the database
5 #       into a program to restore it from a backup.
6 #       This is not guaranteed to work for all data types; it may
7 #       need to be extended.
8
9 BEGIN { print "/* This file automatically generated */";
10         print "/* Do not edit */";
11         print "#include <stdio.h>";
12         print "EXEC SQL INCLUDE sqlca;";
13         print "void parse_nl(), parse_str(), parse_sep();"
14         print "/* This file automatically generated */" > "rest1.dc";
15         print "/* Do not edit */" >> "rest1.dc";
16         print "#include <stdio.h>" >> "rest1.dc";
17         print "FILE *open_file();" >> "rest1.dc";
18         print "do_restores(prefix)\n\tchar *prefix;\n{" >>"rest1.dc";
19 }
20
21 $1=="#" { next; }
22
23 /^create/ { printf "restore_%s(f)\nFILE *f;\n", $3;
24         print "{\n\tEXEC SQL BEGIN DECLARE SECTION;";
25         printf "\trestore_%s(open_file(prefix, \"%s\"));\n", $3, $3 >> "rest1.dc";
26         tablename = $3;
27         rangename = substr(tablename, 1, 1);
28         count = 0;
29         next; }
30
31 NF >= 2 {
32         vname[count] = $1; 
33         printf "/* %s */\n", $0
34         if ($2 ~ /INTEGER/ || $2 ~ /SMALLINT/ || $2 ~ /INTEGER1/) {
35                 printf "\tint\tt_%s;\n", vname[count]
36                 vtype[count]="int"
37         } else if ($2 ~ /CHAR\([0-9]*\)/) {
38                 t = split($2, temp, "(")
39                 if (t != 2) printf "Can't parse %s\n", $2;
40                 t = split(temp[2], temp2, ")")
41                 if (t != 2) printf "Can't parse %s\n", temp[2];
42                 printf "\tchar\tt_%s[%d];\n", vname[count], temp2[1]+1;
43                 vsize[count] = temp2[1]+1;
44                 vtype[count]="str"
45         } else if ($2 ~ /DATE/) {
46                 printf "\tchar\tt_%s[26];\n", vname[count];
47                 vtype[count]="str";
48                 vsize[count]=26;
49         } else printf "Unknown data type %s\n", $2;
50         count++;
51 }
52
53 /^\);$/ { 
54         print "\tEXEC SQL END DECLARE SECTION;\n";
55         print "\twhile(!feof(f)) {"
56         for (i = 0; i < count; i++) {
57                 if (i != 0) print "\t\tparse_sep(f);"
58                 if (vtype[i] ~ /int/) {
59                         printf("\t\tt_%s = parse_int(f);\n", vname[i]);
60                 } else printf "\t\tparse_%s(f, t_%s, %d);\n", vtype[i], vname[i], vsize[i]
61                 if (i == 0) print "\t\tif (feof(f)) break;"
62         }
63         printf "\t\tparse_nl(f);\n"
64
65         printf "\t\tEXEC SQL INSERT INTO %s (\n", tablename;
66         for (i = 0; i < count; i++) {
67                 if (i != 0) printf ",\n";
68                 printf "\t\t\t%s", vname[i];
69         }
70         printf ")\n\t\tVALUES (\n";
71         for (i = 0; i < count; i++) {
72                 if (i != 0) printf ",\n";
73                 printf "\t\t\t:t_%s", vname[i];
74         }
75         printf ");\n\t\tif (sqlca.sqlcode != 0) {\n"
76         printf "\t\t\tcom_err(\"restore\", 0, \"insert failed\");\n"
77         printf "\t\t\texit(2);\n\t\t}\n\t}\n"
78         printf "\t(void) fclose(f);\n"
79         printf "}\n"
80 }
81 END { print "/* All done */"
82         print "}" >>"rest1.dc";
83 }
This page took 0.301563 seconds and 5 git commands to generate.