[24319] | 1 | # $Id: db2bkup.awk 3956 2010-01-05 20:56:56Z zacheiss $ |
---|
[23095] | 2 | # |
---|
| 3 | # This converts the file used to originally create the database |
---|
| 4 | # into a program to back it up. |
---|
| 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 \"dump_db.h\""; |
---|
| 14 | print "#include \"bkup.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 \"dump_db.h\"" >> "bkup1.pc"; |
---|
| 20 | print "#include \"bkup.h\"" >> "bkup1.pc"; |
---|
| 21 | print "void do_backups(char *prefix)\n{" >> "bkup1.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 dump_%s(FILE *f)\n{\n EXEC SQL BEGIN DECLARE SECTION;\n", $3; |
---|
| 31 | printf " dump_%s(open_file(prefix, \"%s\"));\n", $3, $3 >> "bkup1.pc"; |
---|
| 32 | printf "void dump_%s(FILE *f);\n", $3 >> "bkup.h"; |
---|
| 33 | |
---|
| 34 | tablename = $3; |
---|
| 35 | rangename = substr(tablename, 1, 1); |
---|
| 36 | count = 0; |
---|
| 37 | next; |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | NF>=2 { |
---|
| 41 | vname[count] = $1; |
---|
| 42 | printf " /* %s */\n", $0; |
---|
| 43 | if ($2 ~ /INTEGER/ || $2 ~ /SMALLINT/ || $2 ~ /INTEGER1/) { |
---|
| 44 | printf " char\tt_%s[39];\n", vname[count]; |
---|
| 45 | vtype[count]="int"; |
---|
| 46 | } else if ($2 ~ /CHAR\([0-9]*\)/) { |
---|
| 47 | t = split($2, temp, "("); |
---|
| 48 | if (t != 2) printf "Can't parse %s\n", $2; |
---|
| 49 | t = split(temp[2], temp2, ")"); |
---|
| 50 | if (t != 2) printf "Can't parse %s\n", temp[2]; |
---|
| 51 | printf " char\tt_%s[%d];\n", vname[count], temp2[1]+1; |
---|
| 52 | if ($1 == "signature" || $1 == "sid") { |
---|
| 53 | vtype[count]="bin"; |
---|
| 54 | printf " EXEC SQL VAR t_%s IS STRING(%d);\n", vname[count], temp2[1]+1; |
---|
| 55 | } else { |
---|
| 56 | vtype[count]="str"; |
---|
| 57 | } |
---|
| 58 | vsize[count] = temp2[1]+1; |
---|
| 59 | } else if ($2 ~ /DATE/) { |
---|
| 60 | printf " char\tt_%s[26];\n", vname[count]; |
---|
| 61 | vtype[count]="date"; |
---|
| 62 | } else printf "Unknown data type %s\n", $2; |
---|
| 63 | count++; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | /^\);$/ { |
---|
| 67 | printf " EXEC SQL END DECLARE SECTION;\n\n"; |
---|
| 68 | printf " EXEC SQL DECLARE c_%s CURSOR FOR SELECT\n", tablename; |
---|
| 69 | for (i = 0; i < count; i++) { |
---|
| 70 | if (i != 0) { |
---|
| 71 | print ","; |
---|
| 72 | } |
---|
| 73 | if(vtype[i] ~ /date/) { |
---|
| 74 | printf " TO_CHAR(%s, 'DD-mon-YYYY HH24:MI:SS')", vname[i]; |
---|
| 75 | } else if(vtype[i] ~ /int/) { |
---|
| 76 | printf " TO_CHAR(%s)", vname[i]; |
---|
| 77 | } else printf " %s", vname[i]; |
---|
| 78 | } |
---|
| 79 | printf " FROM %s;\n", tablename; |
---|
| 80 | |
---|
| 81 | printf " EXEC SQL OPEN c_%s;\n", tablename; |
---|
| 82 | printf " while (1)\n {\n EXEC SQL FETCH c_%s INTO\n", tablename; |
---|
| 83 | for (i = 0; i < count; i++) { |
---|
| 84 | if (i != 0) printf ",\n"; |
---|
| 85 | printf " :t_%s", vname[i]; |
---|
| 86 | } |
---|
| 87 | printf ";\n"; |
---|
| 88 | printf " if (sqlca.sqlcode != 0)\n break;\n"; |
---|
| 89 | for (i = 0; i < count; i++) { |
---|
| 90 | if (i != 0) print " dump_sep(f);"; |
---|
| 91 | if (vtype[i] ~ /str/ || vtype[i] ~ /date/ || vtype[i] ~ /int/) { |
---|
| 92 | printf " dump_str(f, endtrim(t_%s));\n", vname[i]; |
---|
| 93 | } else { |
---|
| 94 | printf " dump_%s(f, t_%s);\n", vtype[i], vname[i]; |
---|
| 95 | } |
---|
| 96 | } |
---|
| 97 | printf " dump_nl(f);\n"; |
---|
| 98 | printf " }\n"; |
---|
| 99 | printf " EXEC SQL CLOSE c_%s;\n", tablename; |
---|
| 100 | printf " safe_close(f);\n"; |
---|
| 101 | printf "}\n\n"; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | END { |
---|
| 105 | print "/* All done */"; |
---|
| 106 | print "}" >> "bkup1.pc"; |
---|
| 107 | } |
---|