]> andersk Git - moira.git/blob - dbck/fix.qc
Initial revision
[moira.git] / dbck / fix.qc
1 /* $Header$
2  *
3  * User interface routines for dbck (SMS database consistency checker)
4  *
5  *  (c) Copyright 1988 by the Massachusetts Institute of Technology.
6  *  For copying and distribution information, please see the file
7  *  <mit-copyright.h>.
8  */
9
10 #include <mit-copyright.h>
11 #include <sms.h>
12 #include <stdio.h>
13 #include "dbck.h"
14
15
16 ##char *_table;
17 ##char *_idfield;
18
19 ##generic_ffunc(id)
20 ##int id;
21 ##{
22 ##  int rowcount;
23
24 ##  delete _table where _table._idfield = id
25 ##  inquire_equel(rowcount = "rowcount")
26     if (rowcount > 0)
27       printf("%d entr%s deleted\n", rowcount, rowcount==1?"y":"ies");
28     else
29       printf("Not deleted\n");
30 ##}
31
32
33 generic_delete(sq, pfunc, table, idfield, preen)
34 struct save_queue *sq;
35 void (*pfunc)();
36 char *table, *idfield;
37 int preen;
38 {
39     _table = table;
40     _idfield = idfield;
41     generic_fix(sq, pfunc, "Delete", generic_ffunc, preen);
42 }
43
44
45 single_delete(table, idfield, id)
46 char *table, *idfield;
47 int id;
48 {
49     _table = table;
50     _idfield = idfield;
51     generic_ffunc(id);
52 }
53
54
55 ##zero_fix(table, zrfield, idfield, id)
56 ##char *table, *zrfield, *idfield;
57 ##int id;
58 ##{
59 ##  int rowcount;
60
61 ##  replace table (zrfield = 0) where table.idfield = id
62 ##  inquire_equel(rowcount = "rowcount")
63     if (rowcount > 0)
64       printf("%d entr%s fixed\n", rowcount, rowcount==1?"y":"ies");
65     else
66       printf("Not fixed\n");
67 ##}
68
69
70 generic_fix(sq, pfunc, msg, ffunc, preen)
71 struct save_queue *sq;
72 char *msg;
73 int (*pfunc)(), (*ffunc)();
74 int preen;
75 {
76     int id;
77
78     while (sq_get_data(sq, &id)) {
79         if ((*pfunc)(id) == 0 && singe_fix(msg, preen))
80           (*ffunc)(id);
81     }
82     sq_destroy(sq);
83 }
84
85
86 int single_fix(msg, preen)
87 char *msg;
88 int preen;
89 {
90     int lmode;
91
92     lmode = mode;
93     if (mode == MODE_PREEN) {
94         if (preen)
95           lmode = MODE_YES;
96         else
97           lmode = MODE_NO;
98     }
99
100     switch (lmode) {
101     case MODE_ASK:
102         if (!prompt(msg))
103           break;
104     case MODE_YES:
105         return(1);
106         break;
107     case MODE_NO:
108         ;
109     }
110     return(0);
111 }
112
113
114 int prompt(msg)
115 char *msg;
116 {
117     char buf[BUFSIZ];
118 ##  extern int dcmenable;
119
120     while (1) {
121         printf("%s (Y/N/Q)? ", msg);
122         fflush(stdout);
123         gets(buf);
124         if (buf[0] == 'Y' || buf[0] == 'y')
125           return(1);
126         if (buf[0] == 'N' || buf[0] == 'n')
127           return(0);
128         if (buf[0] == 'Q' || buf[0] == 'q') {
129             if (prompt("Are you sure you want to quit")) {
130                 if (prompt("Save database changes")) {
131 ##                  end transaction
132 ##                  replace values (value = dcmenable)
133 ##                      where values.name = "dcm_enable"
134 ##                  exit
135                     exit(0);
136                 } else {
137                     cleanup();
138                 }
139             }
140         }
141     }
142 }
143
144
145 /**
146  ** set_next_object_id - set next object id in values table
147  **
148  ** Inputs: object - object name in values table and in objects
149  **         table - name of table objects are found in
150  **
151  ** - called before an APPEND operation to set the next object id to
152  **   be used for the new record to the next free value
153  **
154  **/
155
156 int set_next_object_id(object, table)
157     char *object;
158     char *table;
159 ##{
160 ##  char *name, *tbl;
161 ##  int rowcount, exists, value;
162
163     name = object;
164     tbl = table;
165 ##  range of v is values
166 ##  repeat retrieve (value = v.#value) where v.#name = @name
167 ##  inquire_equel(rowcount = "rowcount")
168     if (rowcount != 1)
169         return(SMS_NO_ID);
170
171 ##  retrieve (exists = any(tbl.name where tbl.name = value))
172 ##  inquire_equel(rowcount = "rowcount")
173     if (rowcount != 1)
174         return(SMS_NO_ID);
175     while (exists) {
176         value++;
177         if (value > MAX_ID_VALUE)
178             value = MIN_ID_VALUE;
179 ##      retrieve (exists = any(tbl.name where tbl.name = value))
180     }
181
182     printf("setting ID %s to %d", name, value);
183 ##  repeat replace v (#value = @value) where v.#name = @name
184     return(SMS_SUCCESS);
185 ##}
186
187
188 ##generic_fix_id(table, idfield, txtfield, oldid, name)
189 ##char *table;
190 ##char *idfield;
191 ##char *txtfield;
192 ##int oldid;
193 ##char *name;
194 ##{
195 ##  int rowcount, id;
196
197     set_next_object_id(table, idfield);
198 ##  retrieve (id = values.value) where values.#name = idfield
199 ##  replace table (idfield = values.value) where values.#name = idfield and
200 ##      table.idfield = oldid and table.txtfield = name
201 ##  inquire_equel(rowcount = "rowcount")
202     if (rowcount == 1)
203       printf("Fixed\n");
204     else
205       printf("Not fixed, rowcount = %d\n", rowcount);
206     return(id);
207 ##}
This page took 0.156256 seconds and 5 git commands to generate.