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