4 # This converts the file used to originally create the database
5 # into a program to back it up.
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 "#include \"dump_db.h\"";
16 print "#define dump_bin dump_str\n";
18 print "/* This file automatically generated */" > "bkup1.pc";
19 print "/* Do not edit */\n" >> "bkup1.pc";
20 print "#include <stdio.h>" >> "bkup1.pc";
21 print "FILE *open_file(char *prefix, char *suffix);\n" >> "bkup1.pc";
22 print "int do_backups(char *prefix)\n{" >> "bkup1.pc";
28 printf "int dump_%s(FILE *f)\n{\n EXEC SQL BEGIN DECLARE SECTION;\n", $3;
29 printf " dump_%s(open_file(prefix, \"%s\"));\n", $3, $3 >> "bkup1.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;
55 vsize[count] = temp2[1]+1;
56 } else if ($2 ~ /DATE/) {
57 printf " char\tt_%s[26];\n", vname[count];
59 } else printf "Unknown data type %s\n", $2;
64 printf " EXEC SQL END DECLARE SECTION;\n\n";
65 printf " EXEC SQL DECLARE c_%s CURSOR FOR SELECT\n", tablename;
66 for (i = 0; i < count; i++) {
70 if(vtype[i] ~ /date/) {
71 printf " TO_CHAR(%s, 'DD-mon-YYYY HH24:MI:SS')", vname[i];
72 } else printf " %s", vname[i];
74 printf " FROM %s;\n", tablename;
76 printf " EXEC SQL OPEN c_%s;\n", tablename;
77 printf " while (1)\n {\n EXEC SQL FETCH c_%s INTO\n", tablename;
78 for (i = 0; i < count; i++) {
79 if (i != 0) printf ",\n";
80 printf " :t_%s", vname[i];
83 printf " if (sqlca.sqlcode != 0)\n break;\n";
84 for (i = 0; i < count; i++) {
85 if (i != 0) print " dump_sep(f);";
86 if (vtype[i] ~ /str/ || vtype[i] ~ /date/) {
87 printf " dump_str(f, strtrim(t_%s));\n", vname[i];
89 printf " dump_%s(f, t_%s);\n", vtype[i], vname[i];
92 printf " dump_nl(f);\n";
94 printf " EXEC SQL CLOSE c_%s;\n", tablename;
95 printf " safe_close(f);\n";
100 print "/* All done */";
101 print "}" >> "bkup1.pc";