]>
Commit | Line | Data |
---|---|---|
dec49b94 | 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 { print "/* This file automatically generated */"; | |
10 | print "/* Do not edit */"; | |
11 | print "#include <stdio.h>"; | |
3388d7f8 | 12 | print "EXEC SQL INCLUDE sqlca;"; |
dec49b94 | 13 | print "void parse_nl(), parse_str(), parse_sep();" |
3388d7f8 | 14 | print "/* This file automatically generated */" > "rest1.dc"; |
15 | print "/* Do not edit */" >> "rest1.dc"; | |
16 | print "#include <stdio.h>" >> "rest1.dc"; | |
17 | print "FILE *open_file();" >> "rest1.dc"; | |
18 | print "do_restores(prefix)\n\tchar *prefix;\n{" >>"rest1.dc"; | |
dec49b94 | 19 | } |
20 | ||
3388d7f8 | 21 | $1=="#" { next; } |
22 | ||
23 | /^create/ { printf "restore_%s(f)\nFILE *f;\n", $3; | |
24 | print "{\n\tEXEC SQL BEGIN DECLARE SECTION;"; | |
25 | printf "\trestore_%s(open_file(prefix, \"%s\"));\n", $3, $3 >> "rest1.dc"; | |
26 | tablename = $3; | |
dec49b94 | 27 | rangename = substr(tablename, 1, 1); |
3388d7f8 | 28 | count = 0; |
29 | next; } | |
dec49b94 | 30 | |
3388d7f8 | 31 | NF >= 2 { |
dec49b94 | 32 | vname[count] = $1; |
33 | printf "/* %s */\n", $0 | |
3388d7f8 | 34 | if ($2 ~ /INTEGER/ || $2 ~ /SMALLINT/ || $2 ~ /INTEGER1/) { |
35 | printf "\tint\tt_%s;\n", vname[count] | |
dec49b94 | 36 | vtype[count]="int" |
3388d7f8 | 37 | } else if ($2 ~ /CHAR\([0-9]*\)/) { |
38 | t = split($2, temp, "(") | |
39 | if (t != 2) printf "Can't parse %s\n", $2; | |
dec49b94 | 40 | t = split(temp[2], temp2, ")") |
41 | if (t != 2) printf "Can't parse %s\n", temp[2]; | |
3388d7f8 | 42 | printf "\tchar\tt_%s[%d];\n", vname[count], temp2[1]+1; |
dec49b94 | 43 | vsize[count] = temp2[1]+1; |
44 | vtype[count]="str" | |
3388d7f8 | 45 | } else if ($2 ~ /DATE/) { |
46 | printf "\tchar\tt_%s[26];\n", vname[count]; | |
dec49b94 | 47 | vtype[count]="str"; |
48 | vsize[count]=26; | |
3388d7f8 | 49 | } else printf "Unknown data type %s\n", $2; |
dec49b94 | 50 | count++; |
51 | } | |
52 | ||
3388d7f8 | 53 | /^\);$/ { |
54 | print "\tEXEC SQL END DECLARE SECTION;\n"; | |
dec49b94 | 55 | print "\twhile(!feof(f)) {" |
56 | for (i = 0; i < count; i++) { | |
57 | if (i != 0) print "\t\tparse_sep(f);" | |
58 | if (vtype[i] ~ /int/) { | |
59 | printf("\t\tt_%s = parse_int(f);\n", vname[i]); | |
60 | } else printf "\t\tparse_%s(f, t_%s, %d);\n", vtype[i], vname[i], vsize[i] | |
61 | if (i == 0) print "\t\tif (feof(f)) break;" | |
62 | } | |
63 | printf "\t\tparse_nl(f);\n" | |
64 | ||
3388d7f8 | 65 | printf "\t\tEXEC SQL INSERT INTO %s (\n", tablename; |
66 | for (i = 0; i < count; i++) { | |
67 | if (i != 0) printf ",\n"; | |
68 | printf "\t\t\t%s", vname[i]; | |
69 | } | |
70 | printf ")\n\t\tVALUES (\n"; | |
dec49b94 | 71 | for (i = 0; i < count; i++) { |
72 | if (i != 0) printf ",\n"; | |
3388d7f8 | 73 | printf "\t\t\t:t_%s", vname[i]; |
dec49b94 | 74 | } |
3388d7f8 | 75 | printf ");\n\t\tif (sqlca.sqlcode != 0) {\n" |
76 | printf "\t\t\tcom_err(\"restore\", 0, \"insert failed\");\n" | |
77 | printf "\t\t\texit(2);\n\t\t}\n\t}\n" | |
dec49b94 | 78 | printf "\t(void) fclose(f);\n" |
3388d7f8 | 79 | printf "}\n" |
dec49b94 | 80 | } |
81 | END { print "/* All done */" | |
3388d7f8 | 82 | print "}" >>"rest1.dc"; |
dec49b94 | 83 | } |