]> andersk Git - moira.git/blob - backup/db2rest.awk
Bomb out with an error message if something goes wrong. Duh!
[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 {
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 "void parse_nl(), parse_str(FILE *, char *, int), parse_sep();\n";
16
17         print "/* This file automatically generated */" > "rest1.pc";
18         print "/* Do not edit */\n" >> "rest1.pc";
19         print "#include <stdio.h>" >> "rest1.pc";
20         print "FILE *open_file();" >> "rest1.pc";
21         print "do_restores(prefix)\n\tchar *prefix;\n{" >> "rest1.pc";
22 }
23
24 $1=="#" { next; }
25
26 /^create/ {
27         printf "restore_%s(f)\nFILE *f;\n", $3;
28         print "{\n\tEXEC SQL BEGIN DECLARE SECTION;";
29         printf "\trestore_%s(open_file(prefix, \"%s\"));\n", $3, $3 >> "rest1.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 vtype[count]="str";
53                 vsize[count] = temp2[1]+1;
54         } else if ($2 ~ /DATE/) {
55                 printf "\tchar\tt_%s[26];\n", vname[count];
56                 vtype[count]="date";
57         } else printf "Unknown data type %s\n", $2;
58         count++;
59 }
60
61 /^\);$/ { 
62         printf "\tEXEC SQL END DECLARE SECTION;\n\tint count=0;\n";
63
64         print "\twhile(!feof(f)) {";
65         print "\t\tif(!(++count%100)) {\n\t\t\tEXEC SQL COMMIT;\n\t\t}\n";
66
67         for (i = 0; i < count; i++) {
68                 if (i != 0) print "\t\tparse_sep(f);";
69                 if (vtype[i] ~ /int/) {
70                         printf("\t\tt_%s = parse_int(f);\n", vname[i]);
71                 } else if (vtype[i] ~ /date/) {
72                         printf "\t\tparse_str(f, t_%s, 26);\n", vname[i];
73                 } else {
74                         printf "\t\tparse_str(f, t_%s, %d);\n", vname[i], vsize[i];
75                 }
76                 if (i == 0) print "\t\tif (feof(f)) break;";
77         }
78         printf "\t\tparse_nl(f);\n"
79
80         printf "\t\tEXEC SQL INSERT INTO %s (\n", tablename;
81         for (i = 0; i < count; i++) {
82                 if (i != 0) printf ",\n";
83                 printf "\t\t\t%s", vname[i];
84         }
85         printf ")\n\t\tVALUES (\n";
86         for (i = 0; i < count; i++) {
87                 if (i != 0) printf ",\n";
88                 if (vtype[i] ~ /date/) {
89                         printf "\t\t\tTO_DATE(NVL(:t_%s,TO_CHAR(SYSDATE, 'DD_mon-YYYY HH24:MI:SS')), 'DD-mon-YYYY HH24:MI:SS')", vname[i];
90                 } else if(vtype[i] ~ /int/) {
91                         printf "\t\t\t:t_%s", vname[i];
92                 } else {
93                         printf "\t\t\tNVL(:t_%s,CHR(0))", vname[i];
94                 }
95         }
96         printf ");\n\t\tif (sqlca.sqlcode != 0) {\n";
97         printf "\t\t\tsqlca.sqlerrm.sqlerrmc[sqlca.sqlerrm.sqlerrml]=0;\n";
98         printf "\t\t\tprintf(\"%%s\\n\", sqlca.sqlerrm.sqlerrmc);\n";
99         printf "\t\t\tcom_err(\"restore\", 0, \"insert failed\");\n";
100         printf "\t\t\texit(2);\n\t\t}\n\t}\n";
101         printf "\t(void) fclose(f);\n";
102         printf "\tEXEC SQL COMMIT;\n";
103         printf "}\n\n";
104 }
105
106
107 END {
108         print "/* All done */";
109         print "}" >> "rest1.pc";
110 }
This page took 0.052166 seconds and 5 git commands to generate.