]> andersk Git - moira.git/blob - backup/db2bkup.awk
6efbd9e5df13c5214543305da88141c52a7eb2a3
[moira.git] / backup / db2bkup.awk
1 #       $Source$
2 #       $Header$
3 #
4 #       This converts the file used to originally create the database
5 #       into a program to back it up.
6 #       This is not guaranteed to work for all data types; it may
7 #       need to be extended.
8
9 BEGIN { 
10         print "/* This file automatically generated */";
11         print "/* Do not edit */\n";
12         print "#include <stdio.h>";
13         print "EXEC SQL INCLUDE sqlca;";
14         print "#include \"dump_db.h\"";
15         print "#define dump_bin dump_str\n";
16
17         print "/* This file automatically generated */" > "bkup1.pc";
18         print "/* Do not edit */\n" >> "bkup1.pc";
19         print "#include <stdio.h>" >> "bkup1.pc";
20         print "FILE *open_file();" >> "bkup1.pc";
21         print "do_backups(prefix)\n\tchar *prefix;\n{" >> "bkup1.pc";
22 }
23
24 $1=="#" { next; }
25
26 /^create/ { 
27         printf "dump_%s(f)\nFILE *f;\n{\n\tEXEC SQL BEGIN DECLARE SECTION;\n", $3;
28         printf "\tdump_%s(open_file(prefix, \"%s\"));\n", $3, $3 >> "bkup1.pc";
29
30         tablename = $3;
31         rangename = substr(tablename, 1, 1);
32         count = 0;
33         next;
34 }
35
36 NF>=2 {
37         vname[count] = $1; 
38         printf "/* %s */\n", $0;
39         if ($2 ~ /INTEGER/ || $2 ~ /SMALLINT/ || $2 ~ /INTEGER1/) {
40                 printf "\tint\tt_%s;\n", vname[count];
41                 vtype[count]="int";
42         } else if ($2 ~ /CHAR\([0-9]*\)/) {
43                 t = split($2, temp, "(");
44                 if (t != 2) printf "Can't parse %s\n", $2;
45                 t = split(temp[2], temp2, ")");
46                 if (t != 2) printf "Can't parse %s\n", temp[2];
47                 printf "\tchar\tt_%s[%d];\n", vname[count], temp2[1]+1;
48                 if ($1 == "signature") {
49                         vtype[count]="bin";
50                         printf "\tEXEC SQL VAR t_signature IS STRING(%d);\n", temp2[1]+1;
51                 } else {
52                         vtype[count]="str";
53                 }
54                 vsize[count] = temp2[1]+1;
55         } else if ($2 ~ /DATE/) {
56                 printf "\tchar\tt_%s[26];\n", vname[count];
57                 vtype[count]="date";
58         } else printf "Unknown data type %s\n", $2;
59         count++;
60 }
61
62 /^\);$/ { 
63         printf "\tEXEC SQL END DECLARE SECTION;\n";
64         printf "\tEXEC SQL DECLARE c_%s CURSOR FOR\n", tablename;
65         printf "\t\tSELECT\n";
66         for (i = 0; i < count; i++) {
67                 if (i != 0) {
68                         print ",";
69                 }
70                 if(vtype[i] ~ /date/) {
71                         printf "\t\t\tTO_CHAR(%s, 'DD-mon-YYYY HH24:MI:SS')", vname[i];
72                 } else printf "\t\t\t%s", vname[i];
73         }
74         printf " FROM %s;\n", tablename;
75
76         printf "\tEXEC SQL OPEN c_%s;\n", tablename;
77         printf "\twhile(1) {\n\t\tEXEC SQL FETCH c_%s INTO\n", tablename;
78         for (i = 0; i < count; i++) {
79                 if (i != 0) printf ",\n";
80                 printf "\t\t\t:t_%s", vname[i];
81         }
82         printf ";\n";
83         printf "\t\tif(sqlca.sqlcode != 0) break;\n";
84         for (i = 0; i < count; i++) {
85                 if (i != 0) print "\t\tdump_sep(f);";
86                 if (vtype[i] ~ /str/ || vtype[i] ~ /date/) {
87                         printf "\t\tdump_str(f, strtrim(t_%s));\n", vname[i];
88                 } else {
89                         printf "\t\tdump_%s(f, t_%s);\n", vtype[i], vname[i];
90                 }
91         }
92         printf "\t\tdump_nl(f);\n";
93         printf "\t}\n";
94         printf "\tEXEC SQL CLOSE c_%s;\n", tablename;
95         printf "\tsafe_close(f);\n";
96         printf "}\n\n";
97 }
98
99 END {
100         print "/* All done */";
101         print "}" >> "bkup1.pc";
102 }
This page took 0.428853 seconds and 3 git commands to generate.