1 | # $Id: db2rest.awk 3956 2010-01-05 20:56:56Z zacheiss $ |
---|
2 | # |
---|
3 | # This converts the file used to originally create the database |
---|
4 | # into a program to restore it from a backup. |
---|
5 | # This is not guaranteed to work for all data types; it may |
---|
6 | # need to be extended. |
---|
7 | |
---|
8 | BEGIN { |
---|
9 | print "/* This file automatically generated */"; |
---|
10 | print "/* Do not edit */\n"; |
---|
11 | print "EXEC SQL INCLUDE sqlca;"; |
---|
12 | print "EXEC SQL WHENEVER SQLERROR DO dbmserr();"; |
---|
13 | print "#include <com_err.h>"; |
---|
14 | print "#include \"dump_db.h\""; |
---|
15 | print "#include \"rest.h\""; |
---|
16 | |
---|
17 | print "/* This file automatically generated */" > "rest1.pc"; |
---|
18 | print "/* Do not edit */\n" >> "rest1.pc"; |
---|
19 | print "#include \"dump_db.h\"" >> "rest1.pc"; |
---|
20 | print "#include \"rest.h\"" >> "rest1.pc"; |
---|
21 | print "void do_restores(char *prefix)\n{" >> "rest1.pc"; |
---|
22 | |
---|
23 | print "/* This file automatically generated */" > "bkup.h"; |
---|
24 | print "/* Do not edit */\n" >> "bkup.h"; |
---|
25 | } |
---|
26 | |
---|
27 | $1=="#" { next; } |
---|
28 | |
---|
29 | /^create/ { |
---|
30 | printf "void restore_%s(FILE *f)\n", $3; |
---|
31 | print "{\n EXEC SQL BEGIN DECLARE SECTION;"; |
---|
32 | printf " restore_%s(open_file(prefix, \"%s\"));\n", $3, $3 >> "rest1.pc"; |
---|
33 | printf "void restore_%s(FILE *f);\n", $3 >> "rest.h"; |
---|
34 | |
---|
35 | tablename = $3; |
---|
36 | rangename = substr(tablename, 1, 1); |
---|
37 | count = 0; |
---|
38 | next; |
---|
39 | } |
---|
40 | |
---|
41 | NF >= 2 { |
---|
42 | vname[count] = $1; |
---|
43 | printf " /* %s */\n", $0; |
---|
44 | if ($2 ~ /INTEGER/ || $2 ~ /SMALLINT/ || $2 ~ /INTEGER1/) { |
---|
45 | printf " char\tt_%s[39];\n", vname[count]; |
---|
46 | vtype[count]="int"; |
---|
47 | } else if ($2 ~ /CHAR\([0-9]*\)/) { |
---|
48 | t = split($2, temp, "("); |
---|
49 | if (t != 2) printf "Can't parse %s\n", $2; |
---|
50 | t = split(temp[2], temp2, ")"); |
---|
51 | if (t != 2) printf "Can't parse %s\n", temp[2]; |
---|
52 | printf " char\tt_%s[%d];\n", vname[count], temp2[1]+1; |
---|
53 | if ($1 == "signature" || $1 == "sid") { |
---|
54 | vtype[count]="bin"; |
---|
55 | printf " EXEC SQL VAR t_%s IS STRING(%d);\n", vname[count], temp2[1]+1; |
---|
56 | } else vtype[count]="str"; |
---|
57 | vsize[count] = temp2[1]+1; |
---|
58 | } else if ($2 ~ /DATE/) { |
---|
59 | printf " char\tt_%s[26];\n", vname[count]; |
---|
60 | vtype[count]="date"; |
---|
61 | } else printf "Unknown data type %s\n", $2; |
---|
62 | count++; |
---|
63 | } |
---|
64 | |
---|
65 | /^\);$/ { |
---|
66 | printf " EXEC SQL END DECLARE SECTION;\n\n int count = 0;\n"; |
---|
67 | |
---|
68 | print " while (!feof(f))\n {"; |
---|
69 | print " if (!(++count % 100))\n EXEC SQL COMMIT;\n"; |
---|
70 | |
---|
71 | for (i = 0; i < count; i++) { |
---|
72 | if (i != 0) print " parse_sep(f);"; |
---|
73 | if (vtype[i] ~ /int/) { |
---|
74 | printf(" parse_str(f, t_%s, 39);\n", vname[i]); |
---|
75 | } else if (vtype[i] ~ /date/) { |
---|
76 | printf " parse_str(f, t_%s, 26);\n", vname[i]; |
---|
77 | } else { |
---|
78 | printf " parse_str(f, t_%s, %d);\n", vname[i], vsize[i]; |
---|
79 | } |
---|
80 | if (i == 0) print " if (feof(f))\n break;"; |
---|
81 | } |
---|
82 | printf " parse_nl(f);\n" |
---|
83 | |
---|
84 | printf " EXEC SQL INSERT INTO %s (\n", tablename; |
---|
85 | for (i = 0; i < count; i++) { |
---|
86 | if (i != 0) printf ",\n"; |
---|
87 | printf " %s", vname[i]; |
---|
88 | } |
---|
89 | printf ")\n VALUES (\n"; |
---|
90 | for (i = 0; i < count; i++) { |
---|
91 | if (i != 0) printf ",\n"; |
---|
92 | if (vtype[i] ~ /date/) { |
---|
93 | printf " TO_DATE(NVL(:t_%s,TO_CHAR(SYSDATE, 'DD_mon-YYYY HH24:MI:SS')), 'DD-mon-YYYY HH24:MI:SS')", vname[i]; |
---|
94 | } else if(vtype[i] ~ /int/) { |
---|
95 | printf " TO_NUMBER(:t_%s)", vname[i]; |
---|
96 | } else { |
---|
97 | printf " NVL(:t_%s,CHR(0))", vname[i]; |
---|
98 | } |
---|
99 | } |
---|
100 | printf ");\n if (sqlca.sqlcode != 0)\n {\n"; |
---|
101 | printf " sqlca.sqlerrm.sqlerrmc[sqlca.sqlerrm.sqlerrml] = 0;\n"; |
---|
102 | printf " printf(\"%%s\\n\", sqlca.sqlerrm.sqlerrmc);\n"; |
---|
103 | printf " com_err(\"restore\", 0, \"insert failed\");\n"; |
---|
104 | printf " exit(2);\n }\n }\n"; |
---|
105 | printf " fclose(f);\n"; |
---|
106 | printf " EXEC SQL COMMIT;\n"; |
---|
107 | printf "}\n\n"; |
---|
108 | } |
---|
109 | |
---|
110 | |
---|
111 | END { |
---|
112 | print "/* All done */"; |
---|
113 | print "}" >> "rest1.pc"; |
---|
114 | } |
---|