3 * User interface routines for dbck (Moira database consistency checker)
5 * (c) Copyright 1988 by the Massachusetts Institute of Technology.
6 * For copying and distribution information, please see the file
10 #include <mit-copyright.h>
15 EXEC SQL INCLUDE sqlca; /* SQL Communications Area */
16 EXEC SQL INCLUDE sqlda; /* SQL Descriptor Area */
18 static char fix_qc_rcsid[] = "$Header$";
20 EXEC SQL BEGIN DECLARE SECTION;
24 EXEC SQL END DECLARE SECTION;
26 extern SQLDA *mr_sqlda;
28 int generic_ffunc(int id)
32 sprintf(stmt_buf, "DELETE FROM %s WHERE %s.%s = %d",
33 _table, _table, _idfield, id);
34 EXEC SQL EXECUTE IMMEDIATE :stmt_buf;
35 rowcount = sqlca.sqlerrd[2];
37 printf("%d entr%s deleted\n", rowcount, rowcount == 1 ? "y" : "ies");
39 printf("Not deleted\n");
44 int generic_delete(struct save_queue *sq, void (*pfunc)(), char *table,
45 char *idfield, int preen)
49 generic_fix(sq, pfunc, "Delete", generic_ffunc, preen);
53 int single_delete(char *table, char *idfield, int id)
61 int zero_fix(char *tbl, char *zrfield, char *idfield, int id)
65 sprintf(stmt_buf, "UPDATE %s SET %s = 0 WHERE %s.%s = %d",
66 tbl, zrfield, tbl, idfield, id);
67 EXEC SQL EXECUTE IMMEDIATE :stmt_buf;
68 rowcount = sqlca.sqlerrd[2];
70 printf("%d entr%s fixed\n", rowcount, rowcount == 1 ? "y" : "ies");
72 printf("Not fixed\n");
77 int single_fix(char *msg, int preen)
79 if (mode == MODE_PREEN)
97 int generic_fix(struct save_queue *sq, int (*pfunc)(), char *msg,
98 int (*ffunc)(), int preen)
102 while (sq_get_data(sq, &id))
104 if ((*pfunc)(id) == 0 && single_fix(msg, preen))
111 int prompt(char *msg)
114 EXEC SQL BEGIN DECLARE SECTION;
115 extern int dcmenable;
116 EXEC SQL END DECLARE SECTION;
120 printf("%s (Y/N/Q)? ", msg);
123 if (buf[0] == 'Y' || buf[0] == 'y')
125 if (buf[0] == 'N' || buf[0] == 'n')
127 if (buf[0] == 'Q' || buf[0] == 'q')
129 if (prompt("Are you sure you want to quit"))
131 if (prompt("Save database changes"))
133 EXEC SQL COMMIT WORK;
139 EXEC SQL ROLLBACK WORK;
140 EXEC SQL UPDATE numvalues SET value = :dcmenable
141 WHERE name = 'dcm_enable';
151 ** set_next_object_id - set next object id in values table
153 ** Inputs: object - object name in values table and in objects
154 ** table - name of table objects are found in
156 ** - called before an APPEND operation to set the next object id to
157 ** be used for the new record to the next free value
161 int set_next_object_id(char *object, char *tablename)
163 EXEC SQL BEGIN DECLARE SECTION;
165 char stmt_buf[256], out_buf[256];
166 EXEC SQL END DECLARE SECTION;
167 int starting_value, errcode = 0;
169 EXEC SQL SELECT value INTO :value FROM numvalues WHERE name = :object;
170 if (sqlca.sqlerrd[2] != 1)
173 starting_value = value;
176 if (value > MAX_ID_VALUE)
177 value = MIN_ID_VALUE;
179 sprintf(stmt_buf, "SELECT %s FROM %s WHERE %s = %d",
180 object, tablename, object, value);
181 EXEC SQL PREPARE inc_stmt FROM :stmt_buf;
182 EXEC SQL DECLARE inc_crs CURSOR FOR inc_stmt;
183 EXEC SQL OPEN inc_crs;
185 EXEC SQL DESCRIBE SELECT LIST FOR inc_stmt INTO mr_sqlda;
186 mr_sqlda->N = mr_sqlda->F;
187 mr_sqlda->V[0] = out_buf;
189 mr_sqlda->L[0] = 255;
190 EXEC SQL FETCH inc_crs USING DESCRIPTOR mr_sqlda;
192 /* if we got an error from the FETCH, we have to preserve it or the
193 close will reset it and the caller with think nothing happened */
195 errcode = sqlca.sqlcode;
197 EXEC SQL CLOSE inc_crs;
204 if (value == starting_value)
208 printf("setting ID %s to %d\n", object, value);
209 EXEC SQL UPDATE numvalues SET value = :value WHERE name = :object;
215 int generic_fix_id(char *tbl, char *idfield, char *txtfield,
216 int oldid, char *name)
218 EXEC SQL BEGIN DECLARE SECTION;
220 EXEC SQL END DECLARE SECTION;
222 set_next_object_id(tbl, idfield);
223 EXEC SQL SELECT value INTO :id FROM numvalues WHERE name = :idfield;
224 sprintf(stmt_buf, "UPDATE %s SET %s = %d WHERE %s = %d AND %s = '%s'",
225 tbl, idfield, id, idfield, oldid, txtfield, name);
226 EXEC SQL EXECUTE IMMEDIATE :stmt_buf;
227 rowcount = sqlca.sqlerrd[2];
231 printf("Not fixed, rowcount = %d\n", rowcount);