]> andersk Git - moira.git/blame - backup/db2rest.awk
finished & optimized script
[moira.git] / backup / db2rest.awk
CommitLineData
dec49b94 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
9BEGIN { print "/* This file automatically generated */";
10 print "/* Do not edit */";
11 print "#include <stdio.h>";
12 print "void parse_nl(), parse_str(), parse_sep();"
13 print "/* This file automatically generated */" > "rest1.qc";
14 print "/* Do not edit */"
15 print "#include <stdio.h>" >> "rest1.qc"
16 print "FILE *open_file();" >> "rest1.qc"
17 print "do_restores(prefix)\n\tchar *prefix;\n{" >>"rest1.qc"
18}
19
20/^create/ { printf "restore_%s(f)\nFILE *f;\n", $2;
21 printf "\trestore_%s(open_file(prefix, \"%s\"));\n", $2, $2 >> "rest1.qc"
22 tablename = $2;
23 rangename = substr(tablename, 1, 1);
24 count = 0; }
25
26$2 ~ /\=/ {
27 vname[count] = $1;
28 printf "/* %s */\n", $0
364a4f5d 29 if ($3 ~ /i[124]/) {
dec49b94 30 printf "## int t_%s;\n", vname[count]
31 vtype[count]="int"
32 } else if ($3 ~ /text\([0-9]*\)/) {
33 t = split($3, temp, "(")
34 if (t != 2) printf "Can't parse %s\n", $3;
35 t = split(temp[2], temp2, ")")
36 if (t != 2) printf "Can't parse %s\n", temp[2];
37 printf "## char t_%s[%d];\n", vname[count], temp2[1]+1;
38 vsize[count] = temp2[1]+1;
39 vtype[count]="str"
40 } else if ($3 ~ /date/) {
41 printf "## char t_%s[26];\n", vname[count];
42 vtype[count]="str";
43 vsize[count]=26;
44 } else if ($3 ~ /c[0-9]*/) {
45 t = split($3, temp, ",")
46 vsize[count] = substr(temp[1], 2) + 1
47 printf "## char t_%s[%d];\n", vname[count],vsize[count]
48 vtype[count]="str"
49 } else printf "Unknown data type %s\n", $3;
50 count++;
51}
52
53/^\($/ { print "##{" }
54/^\)$/ {
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\tappend %s (\n", tablename
66 for (i = 0; i < count; i++) {
67 if (i != 0) printf ",\n";
68 printf "##\t\t\t%s = t_%s", vname[i], vname[i]
69 }
70 printf ")\n"
71 printf "\t}\n"
72 printf "\t(void) fclose(f);\n"
73 printf "##}\n"
74}
75END { print "/* All done */"
76 print "}" >>"rest1.qc"
77}
This page took 1.457264 seconds and 5 git commands to generate.