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
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(FILE *), parse_str(FILE *, char *, int), parse_sep(FILE *);\n";
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(char *prefix, char *suffix);\n" >> "rest1.pc";
21 print "int do_restores(char *prefix)\n{" >> "rest1.pc";
27 printf "int restore_%s(FILE *f)\n", $3;
28 print "{\n EXEC SQL BEGIN DECLARE SECTION;";
29 printf " restore_%s(open_file(prefix, \"%s\"));\n", $3, $3 >> "rest1.pc";
32 rangename = substr(tablename, 1, 1);
39 printf " /* %s */\n", $0;
40 if ($2 ~ /INTEGER/ || $2 ~ /SMALLINT/ || $2 ~ /INTEGER1/) {
41 printf " int\tt_%s;\n", vname[count];
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 " char\tt_%s[%d];\n", vname[count], temp2[1]+1;
49 if ($1 == "signature") {
51 printf " EXEC 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 " char\tt_%s[26];\n", vname[count];
57 } else printf "Unknown data type %s\n", $2;
62 printf " EXEC SQL END DECLARE SECTION;\n\n int count = 0;\n";
64 print " while (!feof(f))\n {";
65 print " if (!(++count % 100))\n EXEC SQL COMMIT;\n";
67 for (i = 0; i < count; i++) {
68 if (i != 0) print " parse_sep(f);";
69 if (vtype[i] ~ /int/) {
70 printf(" t_%s = parse_int(f);\n", vname[i]);
71 } else if (vtype[i] ~ /date/) {
72 printf " parse_str(f, t_%s, 26);\n", vname[i];
74 printf " parse_str(f, t_%s, %d);\n", vname[i], vsize[i];
76 if (i == 0) print " if (feof(f))\n break;";
78 printf " parse_nl(f);\n"
80 printf " EXEC SQL INSERT INTO %s (\n", tablename;
81 for (i = 0; i < count; i++) {
82 if (i != 0) printf ",\n";
83 printf " %s", vname[i];
85 printf ")\n VALUES (\n";
86 for (i = 0; i < count; i++) {
87 if (i != 0) printf ",\n";
88 if (vtype[i] ~ /date/) {
89 printf " TO_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_%s", vname[i];
93 printf " NVL(:t_%s,CHR(0))", vname[i];
96 printf ");\n if (sqlca.sqlcode != 0)\n {\n";
97 printf " sqlca.sqlerrm.sqlerrmc[sqlca.sqlerrm.sqlerrml] = 0;\n";
98 printf " printf(\"%%s\\n\", sqlca.sqlerrm.sqlerrmc);\n";
99 printf " com_err(\"restore\", 0, \"insert failed\");\n";
100 printf " exit(2);\n }\n }\n";
101 printf " fclose(f);\n";
102 printf " EXEC SQL COMMIT;\n";
108 print "/* All done */";
109 print "}" >> "rest1.pc";