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