]>
Commit | Line | Data |
---|---|---|
d26cae4e | 1 | /* |
2 | * $Source$ | |
3 | * $Author$ | |
4 | * $Header$ | |
5 | * | |
83526631 | 6 | * Copyright (C) 1987, 1988 by the Massachusetts Institute of Technology |
faad6f39 | 7 | * |
d26cae4e | 8 | */ |
9 | ||
10 | #ifndef lint | |
11 | static char *rcsid_qrtn_qc = "$Header$"; | |
12 | #endif lint | |
13 | ||
97479f6f | 14 | #include "query.h" |
97479f6f | 15 | #include "sms_server.h" |
16 | ||
17 | char *Argv[16]; | |
18 | ||
19 | static int ingres_errno = 0; | |
7536027d | 20 | extern char *whoami; |
83526631 | 21 | extern FILE *journal; |
97479f6f | 22 | |
8fcb440e | 23 | #define INGRES_BAD_INT 4111 |
24 | #define INGRES_BAD_DATE 4302 | |
25 | ||
d26cae4e | 26 | /* |
27 | * ingerr: (supposedly) called when Ingres indicates an error. | |
28 | * I have not yet been able to get this to work to intercept a | |
29 | * database open error. | |
30 | */ | |
31 | ||
32 | static int ingerr(num) | |
33 | int *num; | |
97479f6f | 34 | { |
8fcb440e | 35 | char buf[256]; |
36 | ||
37 | switch (*num) { | |
38 | case INGRES_BAD_INT: | |
39 | ingres_errno = SMS_INTEGER; | |
40 | break; | |
41 | case INGRES_BAD_DATE: | |
42 | ingres_errno = SMS_DATE; | |
43 | break; | |
44 | default: | |
45 | ingres_errno = SMS_INGRES_ERR; | |
46 | com_err(whoami, SMS_INGRES_ERR, " code %d\n", *num); | |
47 | sprintf(buf, "Ingres error %d", *num); | |
48 | send_zgram("ingres_error", buf); | |
49 | return (*num); | |
50 | } | |
51 | return (0); | |
97479f6f | 52 | } |
53 | ||
d26cae4e | 54 | int sms_open_database() |
97479f6f | 55 | { |
d26cae4e | 56 | register int i; |
83526631 | 57 | char *malloc(); |
97479f6f | 58 | |
d26cae4e | 59 | /* initialize local argv */ |
60 | for (i = 0; i < 16; i++) | |
83526631 | 61 | Argv[i] = malloc(ARGLEN); |
97479f6f | 62 | |
d26cae4e | 63 | IIseterr(ingerr); |
97479f6f | 64 | |
d26cae4e | 65 | ingres_errno = 0; |
97479f6f | 66 | |
d26cae4e | 67 | /* open the database */ |
68 | ## ingres sms | |
69 | return ingres_errno; | |
97479f6f | 70 | } |
71 | ||
d26cae4e | 72 | int sms_close_database() |
97479f6f | 73 | { |
d26cae4e | 74 | ## exit |
97479f6f | 75 | } |
76 | ||
7536027d | 77 | sms_check_access(cl, name, argc, argv_ro) |
78 | client *cl; | |
79 | char *name; | |
80 | int argc; | |
81 | char *argv_ro[]; | |
82 | { | |
9969e1bc | 83 | struct query *q; |
7536027d | 84 | struct query *get_query_by_name(); |
7536027d | 85 | |
83526631 | 86 | q = get_query_by_name(name, cl->args->sms_version_no); |
9969e1bc | 87 | if (q == (struct query *)0) |
88 | return(SMS_NO_HANDLE); | |
7536027d | 89 | |
9969e1bc | 90 | return(sms_verify_query(cl, q, argc, argv_ro)); |
7536027d | 91 | } |
92 | ||
93 | sms_process_query(cl, name, argc, argv_ro, action, actarg) | |
94 | client *cl; | |
d26cae4e | 95 | char *name; |
96 | int argc; | |
97 | char *argv_ro[]; | |
98 | int (*action)(); | |
99 | char *actarg; | |
97479f6f | 100 | { |
d26cae4e | 101 | register struct query *q; |
7536027d | 102 | register int status; |
7536027d | 103 | register struct validate *v; |
7536027d | 104 | char qual[256]; |
cc1231a1 | 105 | char sort[32]; |
7536027d | 106 | char *pqual; |
cc1231a1 | 107 | char *psort; |
7536027d | 108 | ## char *table; |
109 | struct save_queue *sq; | |
d26cae4e | 110 | struct query *get_query_by_name(); |
7536027d | 111 | int sq_save_args(); |
112 | struct save_queue *sq_create(); | |
cc1231a1 | 113 | char *build_sort(); |
d26cae4e | 114 | |
7536027d | 115 | /* list queries command */ |
116 | if (!strcmp(name, "_list_queries")) { | |
83526631 | 117 | list_queries(cl->args->sms_version_no, action, actarg); |
7536027d | 118 | return(SMS_SUCCESS); |
119 | } | |
120 | ||
121 | /* help query command */ | |
122 | if (!strcmp(name, "_help")) { | |
83526631 | 123 | if (argc < 1) |
124 | return(SMS_ARGS); | |
125 | q = get_query_by_name(argv_ro[0], cl->args->sms_version_no); | |
7536027d | 126 | if (q == (struct query *)0) return(SMS_NO_HANDLE); |
127 | help_query(q, action, actarg); | |
128 | return(SMS_SUCCESS); | |
129 | } | |
130 | ||
131 | /* get query structure, return error if named query does not exist */ | |
83526631 | 132 | q = get_query_by_name(name, cl->args->sms_version_no); |
7536027d | 133 | if (q == (struct query *)0) return(SMS_NO_HANDLE); |
134 | v = q->validate; | |
135 | ||
d353058b | 136 | if (q->type != RETRIEVE) |
137 | ## begin transaction | |
138 | ||
9969e1bc | 139 | /* setup argument vector, verify access and arguments */ |
140 | if ((status = sms_verify_query(cl, q, argc, argv_ro)) != SMS_SUCCESS) | |
d353058b | 141 | goto out; |
7536027d | 142 | |
143 | /* perform any special query pre-processing */ | |
144 | if (v && v->pre_rtn) { | |
145 | status = (*v->pre_rtn)(q, Argv, cl, 0); | |
9969e1bc | 146 | if (status != SMS_SUCCESS) |
d353058b | 147 | goto out; |
7536027d | 148 | } |
149 | ||
d26cae4e | 150 | switch (q->type) { |
faad6f39 | 151 | case RETRIEVE: |
7536027d | 152 | /* for queries that do not permit wildcarding, check if row |
153 | uniquely exists */ | |
154 | if (v && v->field) { | |
155 | status = validate_row(q, Argv, v); | |
156 | if (status != SMS_EXISTS) break; | |
157 | } | |
158 | ||
159 | /* build "where" clause if needed */ | |
d26cae4e | 160 | if (q->qual) { |
161 | build_qual(q->qual, q->argc, Argv, qual); | |
7536027d | 162 | pqual = qual; |
163 | } else { | |
164 | pqual = 0; | |
165 | } | |
166 | ||
cc1231a1 | 167 | /* build "sort" clause if needed */ |
168 | if (v && v->valobj) { | |
169 | psort = build_sort(v, sort); | |
170 | } else { | |
171 | psort = 0; | |
172 | } | |
173 | ||
7536027d | 174 | /* if there is a followup routine, then we must save the results */ |
175 | /* of the first query for use by the followup routine */ | |
cc1231a1 | 176 | /* if q->rvar = NULL, perform post_rtn only */ |
177 | if (q->rvar) { | |
7536027d | 178 | if (v && v->post_rtn) { |
179 | sq = sq_create(); | |
cc1231a1 | 180 | status = do_retrieve(q, pqual, psort, sq_save_args, sq); |
7536027d | 181 | if (status != SMS_SUCCESS) { |
182 | sq_destroy(sq); | |
183 | break; | |
184 | } | |
83526631 | 185 | status = (*v->post_rtn)(q, sq, v, action, actarg, cl); |
7536027d | 186 | } else { |
187 | /* normal retrieve */ | |
cc1231a1 | 188 | status = do_retrieve(q, pqual, psort, action, actarg); |
7536027d | 189 | } |
190 | if (status != SMS_SUCCESS) break; | |
cc1231a1 | 191 | table = q->rtable; |
7536027d | 192 | ## repeat replace tblstats (retrieves = tblstats.retrieves + 1) |
193 | ## where tblstats.#table = @table | |
194 | } else { | |
83526631 | 195 | status = (*v->post_rtn)(q, Argv, cl, action, actarg); |
7536027d | 196 | } |
197 | ||
d26cae4e | 198 | break; |
199 | ||
faad6f39 | 200 | case UPDATE: |
7536027d | 201 | /* see if row already exists */ |
202 | if (v->field) { | |
203 | status = validate_row(q, Argv, v); | |
204 | if (status != SMS_EXISTS) break; | |
205 | } | |
206 | ||
207 | /* build "where" clause and perform update */ | |
cc1231a1 | 208 | /* if q->rvar = NULL, perform post_rtn only */ |
209 | if (q->rvar) { | |
7536027d | 210 | build_qual(q->qual, q->argc, Argv, qual); |
211 | status = do_update(q, &Argv[q->argc], qual, action, actarg); | |
212 | if (status != SMS_SUCCESS) break; | |
cc1231a1 | 213 | table = q->rtable; |
7536027d | 214 | ## repeat replace tblstats (updates = tblstats.updates + 1, |
215 | ## modtime = "now") | |
216 | ## where tblstats.#table = @table | |
d26cae4e | 217 | } |
7536027d | 218 | |
219 | /* execute followup routine (if any) */ | |
83526631 | 220 | if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl); |
7536027d | 221 | |
d26cae4e | 222 | break; |
97479f6f | 223 | |
faad6f39 | 224 | case APPEND: |
7536027d | 225 | /* see if row already exists */ |
226 | if (v->field) { | |
227 | status = validate_row(q, Argv, v); | |
228 | if (status != SMS_NO_MATCH) break; | |
d26cae4e | 229 | } |
7536027d | 230 | |
231 | /* increment id number if necessary */ | |
d353058b | 232 | if (v->object_id) { |
83526631 | 233 | status = set_next_object_id(v->object_id, q->rtable); |
d353058b | 234 | if (status != SMS_SUCCESS) break; |
235 | } | |
7536027d | 236 | |
cc1231a1 | 237 | /* build "where" clause if needed */ |
238 | if (q->qual) { | |
239 | build_qual(q->qual, q->argc, Argv, qual); | |
240 | pqual = qual; | |
241 | } else { | |
242 | pqual = 0; | |
243 | } | |
244 | ||
7536027d | 245 | /* perform the append */ |
cc1231a1 | 246 | /* if q->rvar = NULL, perform post_rtn only */ |
247 | if (q->rvar) { | |
248 | status = do_append(q, &Argv[q->argc], pqual, action, actarg); | |
7536027d | 249 | if (status != SMS_SUCCESS) break; |
cc1231a1 | 250 | table = q->rtable; |
7536027d | 251 | ## repeat replace tblstats (appends = tblstats.appends + 1, |
252 | ## modtime = "now") | |
253 | ## where tblstats.#table = @table | |
254 | } | |
255 | ||
256 | /* execute followup routine */ | |
83526631 | 257 | if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl); |
d26cae4e | 258 | break; |
97479f6f | 259 | |
faad6f39 | 260 | case DELETE: |
7536027d | 261 | /* see if row already exists */ |
262 | if (v->field) { | |
263 | status = validate_row(q, Argv, v); | |
264 | if (status != SMS_EXISTS) break; | |
265 | } | |
266 | ||
267 | /* build "where" clause and perform delete */ | |
cc1231a1 | 268 | /* if q->rvar = NULL, perform post_rtn only */ |
269 | if (q->rvar) { | |
7536027d | 270 | build_qual(q->qual, q->argc, Argv, qual); |
271 | status = do_delete(q, qual, action, actarg); | |
272 | if (status != SMS_SUCCESS) break; | |
cc1231a1 | 273 | table = q->rtable; |
7536027d | 274 | ## repeat replace tblstats (deletes = tblstats.deletes + 1, |
275 | ## modtime = "now") | |
276 | ## where tblstats.#table = @table | |
97479f6f | 277 | } |
7536027d | 278 | |
279 | /* execute followup routine */ | |
83526631 | 280 | if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl); |
d26cae4e | 281 | break; |
7536027d | 282 | |
d26cae4e | 283 | } |
7536027d | 284 | |
d353058b | 285 | out: |
3df9e21e | 286 | if (q->type != RETRIEVE) { |
287 | if (status == SMS_SUCCESS) { | |
d353058b | 288 | ## end transaction /* commit to this */ |
83526631 | 289 | if (journal) { |
290 | char buf[1024], *bp; | |
291 | int i; | |
292 | extern time_t now; | |
293 | ||
294 | fprintf(journal, "%% %s %s %s", | |
295 | cl->clname, cl->entity, ctime(&now)); | |
296 | fprintf(journal, "%s[%d] ", q->name, cl->args->sms_version_no); | |
297 | for (i = 0; i < argc; i++) { | |
298 | if (i != 0) { | |
299 | putc(' ', journal); | |
300 | } | |
301 | requote(buf, argv_ro[i], sizeof(buf)); | |
302 | fputs(buf, journal); | |
303 | } | |
304 | putc('\n', journal); | |
305 | fflush(journal); | |
306 | } | |
3df9e21e | 307 | } else { |
d353058b | 308 | ## abort /* it never happened */ |
3df9e21e | 309 | } |
310 | } | |
7536027d | 311 | |
1bf7f927 | 312 | if (status != SMS_SUCCESS && log_flags & LOG_RES) |
313 | com_err(whoami, status, " (Query failed)"); | |
7536027d | 314 | return(status); |
97479f6f | 315 | } |
316 | ||
317 | build_qual(fmt, argc, argv, qual) | |
318 | char *fmt; | |
319 | int argc; | |
320 | char *argv[]; | |
321 | char *qual; | |
322 | { | |
7536027d | 323 | register char *c; |
324 | register int i; | |
325 | char *args[4]; | |
83526631 | 326 | char *index(); |
7536027d | 327 | |
328 | c = fmt; | |
329 | for (i = 0; i < argc; i++) { | |
83526631 | 330 | c = index(c, '%'); |
7536027d | 331 | if (c++ == (char *)0) return(SMS_ARGS); |
332 | if (*c == 's') | |
333 | args[i] = argv[i]; | |
334 | else if (*c == 'd') | |
335 | *(int *)&args[i] = *(int *)argv[i]; /* sigh */ | |
336 | else | |
337 | return(SMS_INGRES_ERR); | |
338 | } | |
339 | ||
d26cae4e | 340 | switch (argc) { |
341 | case 0: | |
342 | strcpy(qual, fmt); | |
343 | break; | |
344 | ||
345 | case 1: | |
7536027d | 346 | sprintf(qual, fmt, args[0]); |
d26cae4e | 347 | break; |
348 | ||
349 | case 2: | |
7536027d | 350 | sprintf(qual, fmt, args[0], args[1]); |
d26cae4e | 351 | break; |
352 | ||
353 | case 3: | |
7536027d | 354 | sprintf(qual, fmt, args[0], args[1], args[2]); |
d26cae4e | 355 | break; |
356 | ||
357 | case 4: | |
7536027d | 358 | sprintf(qual, fmt, args[0], args[1], args[2], args[3]); |
d26cae4e | 359 | break; |
360 | } | |
83526631 | 361 | return(SMS_SUCCESS); |
97479f6f | 362 | } |
363 | ||
cc1231a1 | 364 | char * |
365 | build_sort(v, sort) | |
366 | register struct validate *v; | |
367 | char *sort; | |
368 | { | |
369 | register struct valobj *vo; | |
370 | register int n; | |
371 | char elem[16]; | |
372 | ||
373 | n = v->objcnt; | |
374 | vo = v->valobj; | |
375 | *sort = 0; | |
376 | ||
377 | while (--n >= 0) { | |
378 | if (vo->type == V_SORT) { | |
379 | sprintf(elem, "RET_VAR%d", vo->index + 1); | |
380 | if (*sort) strcat(sort, ", "); | |
381 | strcat(sort, elem); | |
382 | } | |
383 | vo++; | |
384 | } | |
385 | ||
386 | return ((*sort) ? sort : 0); | |
387 | } | |
388 | ||
9969e1bc | 389 | |
390 | /* Build arguement vector, verify query and arguments */ | |
391 | ||
392 | sms_verify_query(cl, q, argc, argv_ro) | |
393 | client *cl; | |
394 | struct query *q; | |
395 | int argc; | |
396 | char *argv_ro[]; | |
397 | { | |
398 | register int argreq; | |
399 | register int status; | |
400 | register struct validate *v = q->validate; | |
401 | register int i; | |
402 | register int privileged = 0; | |
403 | ||
404 | /* copy the arguments into a local argv that we can modify */ | |
405 | for (i = 0; i < argc; i++) { | |
406 | if (strlen(argv_ro[i]) < ARGLEN) | |
407 | strcpy(Argv[i], argv_ro[i]); | |
408 | else | |
409 | return(SMS_ARG_TOO_LONG); | |
410 | } | |
411 | ||
412 | /* check initial query access */ | |
413 | status = check_query_access(q, Argv, cl); | |
414 | if (status != SMS_SUCCESS && status != SMS_PERM) | |
415 | return(status); | |
416 | if (status == SMS_SUCCESS) | |
417 | privileged++; | |
418 | ||
419 | /* check argument count */ | |
420 | argreq = q->argc; | |
421 | if (q->type == UPDATE || q->type == APPEND) argreq += q->vcnt; | |
422 | if (argc != argreq) return(SMS_ARGS); | |
423 | ||
424 | /* validate arguments */ | |
425 | if (v && v->valobj) { | |
426 | status = validate_fields(q, Argv, v->valobj, v->objcnt); | |
427 | if (status != SMS_SUCCESS) return(status); | |
428 | } | |
429 | ||
430 | /* perform special query access check */ | |
431 | if (!privileged && v && v->acs_rtn) { | |
432 | status = (*v->acs_rtn)(q, Argv, cl); | |
433 | if (status != SMS_SUCCESS && status != SMS_PERM) | |
434 | return(status); | |
435 | if (status == SMS_SUCCESS) | |
436 | privileged++; | |
437 | } | |
438 | ||
439 | return(privileged ? SMS_SUCCESS : SMS_PERM); | |
440 | } | |
441 | ||
7536027d | 442 | check_query_access(q, argv, cl) |
443 | struct query *q; | |
444 | char *argv[]; | |
445 | client *cl; | |
d26cae4e | 446 | ##{ |
7536027d | 447 | ## char *name; |
448 | ## int acl_id; | |
449 | ## int exists; | |
d26cae4e | 450 | ## int rowcount; |
cc1231a1 | 451 | ## int errorno; |
7536027d | 452 | ## static int def_uid; |
453 | int status; | |
454 | int client_id; | |
455 | char *client_type; | |
456 | ||
457 | /* get query access control list */ | |
458 | name = q->shortname; | |
459 | ## repeat retrieve (acl_id = capacls.list_id) where capacls.tag = @name | |
cc1231a1 | 460 | ## inquire_equel (rowcount = "rowcount", errorno = "errorno") |
461 | if (errorno != 0) return(SMS_INGRES_ERR); | |
7536027d | 462 | if (rowcount == 0) return(SMS_PERM); |
d26cae4e | 463 | |
7536027d | 464 | /* initialize default uid */ |
465 | if (def_uid == 0) { | |
466 | ## retrieve (def_uid = users.users_id) where users.login = "default" | |
d26cae4e | 467 | } |
97479f6f | 468 | |
7536027d | 469 | /* check for default access */ |
470 | ## range of m is members | |
471 | ## repeat retrieve (exists = any(m.#member_id where m.list_id = @acl_id and | |
472 | ## m.member_type = "USER" and m.#member_id = def_uid)) | |
473 | if (exists) return(SMS_SUCCESS); | |
474 | ||
475 | /* parse client name */ | |
476 | status = get_client(cl, &client_type, &client_id); | |
477 | if (status != SMS_SUCCESS) return(status); | |
478 | ||
7536027d | 479 | /* see if client is in the list (or any of its sub-lists) */ |
83526631 | 480 | exists = find_member("LIST", acl_id, client_type, client_id, 0); |
7536027d | 481 | return ((exists) ? SMS_SUCCESS : SMS_PERM); |
482 | ##} | |
97479f6f | 483 | |
7536027d | 484 | get_client(cl, client_type, client_id) |
485 | client *cl; | |
486 | char **client_type; | |
487 | int *client_id; | |
488 | ##{ | |
489 | struct krbname *krb; | |
490 | ## int member_id; | |
491 | ## char *name; | |
492 | ## int rowcount; | |
493 | ||
6ec81cbf | 494 | if (cl->clname == NULL) |
495 | return SMS_PERM; | |
496 | ||
19bf23eb | 497 | /* for now ignore instances */ |
7536027d | 498 | krb = &cl->kname; |
7536027d | 499 | |
500 | /* if client is from local realm, get users_id */ | |
501 | if (!strcmp(krb->realm, krb_realm)) { | |
83526631 | 502 | *client_id = cl->users_id; |
7536027d | 503 | *client_type = "USER"; |
83526631 | 504 | return(SMS_SUCCESS); |
7536027d | 505 | } |
83526631 | 506 | |
507 | /* otherwise use string_id */ | |
508 | name = cl->clname; | |
509 | ## repeat retrieve (member_id = strings.string_id) | |
510 | ## where strings.string = @name | |
7536027d | 511 | |
512 | /* make sure we found a users or string id */ | |
d26cae4e | 513 | ## inquire_equel (rowcount = "rowcount") |
7536027d | 514 | if (rowcount == 0) return(SMS_PERM); |
515 | ||
83526631 | 516 | *client_type = "STRING"; |
7536027d | 517 | *client_id = member_id; |
518 | return(SMS_SUCCESS); | |
519 | ##} | |
97479f6f | 520 | |
83526631 | 521 | ##find_member(list_type, list_id, member_type, member_id, sq) |
522 | char *list_type; | |
7536027d | 523 | ## int list_id; |
524 | ## char *member_type; | |
525 | ## int member_id; | |
526 | struct save_queue *sq; | |
527 | ##{ | |
528 | ## int exists; | |
529 | ## int sublist; | |
530 | int child; | |
531 | struct save_queue *sq_create(); | |
532 | ||
533 | /* see if client is a direct member of list */ | |
534 | ## repeat retrieve (exists = any(m.#member_id where | |
535 | ## m.#list_id = @list_id and | |
536 | ## m.#member_type = @member_type and | |
537 | ## m.#member_id = @member_id)) | |
538 | if (exists) return(1); | |
539 | ||
540 | /* are there any sub-lists? */ | |
7536027d | 541 | ## repeat retrieve (exists = any(m.#member_id where m.#list_id = @list_id and |
542 | ## m.#member_type = "LIST")) | |
543 | if (!exists) return(0); | |
544 | ||
545 | /* yes; now recurse through sublists */ | |
546 | ||
547 | /* create a save queue */ | |
548 | if (sq == (struct save_queue *)0) { | |
549 | sq = sq_create(); | |
550 | child = 0; | |
551 | } else { | |
552 | child = 1; | |
d26cae4e | 553 | } |
97479f6f | 554 | |
7536027d | 555 | /* save all sublist ids */ |
556 | ## range of m is members | |
557 | ## retrieve (sublist = m.#member_id) | |
558 | ## where m.#list_id = list_id and m.#member_type = "LIST" | |
559 | ## { | |
560 | sq_save_unique_data(sq, sublist); | |
561 | ## } | |
562 | ||
83526631 | 563 | if (child) return(0); |
7536027d | 564 | |
7536027d | 565 | /* at top-level, check sub-lists for client (breadth-first search) */ |
566 | while (sq_get_data(sq, &sublist)) { | |
83526631 | 567 | exists = find_member(list_type, sublist, member_type, member_id, sq); |
7536027d | 568 | if (exists) { |
569 | sq_destroy(sq); | |
570 | return(1); | |
571 | } | |
572 | } | |
83526631 | 573 | sq_destroy(sq); |
574 | return(0); | |
d26cae4e | 575 | ##} |
97479f6f | 576 | |
83526631 | 577 | |
cc1231a1 | 578 | do_retrieve(q, pqual, psort, action, actarg) |
d26cae4e | 579 | register struct query *q; |
cc1231a1 | 580 | char *pqual; |
581 | char *psort; | |
d26cae4e | 582 | int (*action)(); |
583 | char *actarg; | |
584 | ##{ | |
585 | ## char *rvar; | |
586 | ## char *rtable; | |
587 | ## char *cqual; | |
cc1231a1 | 588 | ## char *csort; |
d26cae4e | 589 | ## int rowcount; |
cc1231a1 | 590 | ## int errorno; |
83526631 | 591 | static char **vaddrs = (char **)NULL; |
592 | ||
593 | if (!vaddrs) { | |
594 | register int i; | |
595 | ||
596 | if ((vaddrs = (char **)malloc(sizeof(char *) * QMAXARGS)) == NULL) { | |
597 | com_err(whoami, SMS_NO_MEM, "setting up static argv"); | |
598 | exit(1); | |
599 | } | |
600 | for (i = 0; i < QMAXARGS; i++) { | |
601 | if ((vaddrs[i] = malloc(QMAXARGSIZE)) == NULL) { | |
602 | com_err(whoami, SMS_NO_MEM, "setting up static argv"); | |
603 | exit(1); | |
604 | } | |
605 | } | |
606 | } | |
7536027d | 607 | |
d26cae4e | 608 | if (q->rvar) { |
609 | rvar = q->rvar; | |
610 | rtable = q->rtable; | |
611 | ## range of rvar is rtable | |
612 | } | |
97479f6f | 613 | |
cc1231a1 | 614 | if (psort) { |
615 | csort = psort; | |
616 | if (pqual) { | |
617 | cqual = pqual; | |
83526631 | 618 | ## retrieve unique (param (q->tlist, vaddrs)) where cqual |
cc1231a1 | 619 | ## sort by csort |
620 | ## { | |
83526631 | 621 | (*action)(q->vcnt, vaddrs, actarg); |
cc1231a1 | 622 | ## } |
623 | } else { | |
83526631 | 624 | ## retrieve unique (param (q->tlist, vaddrs)) |
cc1231a1 | 625 | ## sort by csort |
626 | ## { | |
83526631 | 627 | (*action)(q->vcnt, vaddrs, actarg); |
cc1231a1 | 628 | ## } |
629 | } | |
630 | ||
7536027d | 631 | } else { |
cc1231a1 | 632 | if (pqual) { |
633 | cqual = pqual; | |
83526631 | 634 | ## retrieve unique (param (q->tlist, vaddrs)) where cqual |
cc1231a1 | 635 | ## { |
83526631 | 636 | (*action)(q->vcnt, vaddrs, actarg); |
cc1231a1 | 637 | ## } |
638 | } else { | |
83526631 | 639 | ## retrieve unique (param (q->tlist, vaddrs)) |
cc1231a1 | 640 | ## { |
83526631 | 641 | (*action)(q->vcnt, vaddrs, actarg); |
cc1231a1 | 642 | ## } |
643 | } | |
7536027d | 644 | } |
97479f6f | 645 | |
cc1231a1 | 646 | ## inquire_equel (rowcount = "rowcount", errorno = "errorno") |
647 | if (errorno != 0) return(SMS_INGRES_ERR); | |
7536027d | 648 | return ((rowcount == 0) ? SMS_NO_MATCH : SMS_SUCCESS); |
d26cae4e | 649 | ##} |
97479f6f | 650 | |
e38c941b | 651 | do_update(q, argv, qual, action, actarg) |
d26cae4e | 652 | register struct query *q; |
653 | char *argv[]; | |
654 | char *qual; | |
655 | int (*action)(); | |
656 | char *actarg; | |
657 | ##{ | |
658 | ## char *rvar; | |
659 | ## char *rtable; | |
660 | ## char *cqual; | |
cc1231a1 | 661 | ## int errorno; |
97479f6f | 662 | |
d26cae4e | 663 | rvar = q->rvar; |
664 | rtable = q->rtable; | |
665 | ## range of rvar is rtable | |
97479f6f | 666 | |
d26cae4e | 667 | cqual = qual; |
668 | ## replace rvar (param (q->tlist, argv)) | |
669 | ## where cqual | |
97479f6f | 670 | |
cc1231a1 | 671 | ## inquire_equel (errorno = "errorno") |
8fcb440e | 672 | if (errorno == 4111) |
673 | return(SMS_INTEGER); | |
674 | else if (errorno != 0) | |
675 | return(SMS_INGRES_ERR); | |
7536027d | 676 | return(SMS_SUCCESS); |
d26cae4e | 677 | ##} |
97479f6f | 678 | |
cc1231a1 | 679 | do_append(q, argv, pqual, action, actarg) |
d26cae4e | 680 | register struct query *q; |
681 | char *argv[]; | |
cc1231a1 | 682 | char *pqual; |
d26cae4e | 683 | int (*action)(); |
684 | char *actarg; | |
685 | ##{ | |
686 | ## char *rvar; | |
687 | ## char *rtable; | |
7536027d | 688 | ## char *cqual; |
cc1231a1 | 689 | ## int errorno; |
97479f6f | 690 | |
d26cae4e | 691 | rvar = q->rvar; |
692 | rtable = q->rtable; | |
693 | ## range of rvar is rtable | |
97479f6f | 694 | |
cc1231a1 | 695 | if (pqual) { |
696 | cqual = pqual; | |
7536027d | 697 | ## append to rtable (param (q->tlist, argv)) where cqual |
698 | } else { | |
699 | ## append to rtable (param (q->tlist, argv)) | |
700 | } | |
97479f6f | 701 | |
cc1231a1 | 702 | ## inquire_equel (errorno = "errorno") |
8fcb440e | 703 | if (errorno == INGRES_BAD_INT) |
704 | return(SMS_INTEGER); | |
705 | else if (errorno != 0) | |
706 | return(SMS_INGRES_ERR); | |
7536027d | 707 | return(SMS_SUCCESS); |
d26cae4e | 708 | ##} |
97479f6f | 709 | |
e38c941b | 710 | do_delete(q, qual, action, actarg) |
d26cae4e | 711 | register struct query *q; |
712 | char *qual; | |
713 | int (*action)(); | |
714 | char *actarg; | |
715 | ##{ | |
716 | ## char *rvar; | |
717 | ## char *rtable; | |
718 | ## char *cqual; | |
cc1231a1 | 719 | ## int errorno; |
97479f6f | 720 | |
d26cae4e | 721 | rvar = q->rvar; |
722 | rtable = q->rtable; | |
723 | ## range of rvar is rtable | |
97479f6f | 724 | |
d26cae4e | 725 | cqual = qual; |
726 | ## delete rvar where cqual | |
97479f6f | 727 | |
cc1231a1 | 728 | ## inquire_equel (errorno = "errorno") |
729 | if (errorno != 0) return(SMS_INGRES_ERR); | |
7536027d | 730 | return(SMS_SUCCESS); |
d26cae4e | 731 | ##} |
97479f6f | 732 | |
d26cae4e | 733 | |
83526631 | 734 | /** |
735 | ** set_next_object_id - set next object id in values table | |
736 | ** | |
737 | ** Inputs: object - object name in values table and in objects | |
738 | ** table - name of table objects are found in | |
739 | ** | |
740 | ** - called before an APPEND operation to set the next object id to | |
741 | ** be used for the new record to the next free value | |
742 | ** | |
743 | **/ | |
744 | ||
745 | set_next_object_id(object, table) | |
746 | char *object; | |
747 | char *table; | |
748 | ##{ | |
749 | ## char *name, *tbl; | |
750 | ## int rowcount, exists, value; | |
751 | ||
752 | name = object; | |
753 | tbl = table; | |
754 | ## range of v is values | |
755 | ## repeat retrieve (value = v.#value) where v.#name = @name | |
756 | ## inquire_equel(rowcount = "rowcount") | |
757 | if (rowcount != 1) | |
8fcb440e | 758 | return(SMS_NO_ID); |
83526631 | 759 | |
760 | ## retrieve (exists = any(tbl.name where tbl.name = value)) | |
761 | ## inquire_equel(rowcount = "rowcount") | |
762 | if (rowcount != 1) | |
8fcb440e | 763 | return(SMS_NO_ID); |
83526631 | 764 | while (exists) { |
765 | value++; | |
8fcb440e | 766 | if (value > MAX_ID_VALUE) |
767 | value = MIN_ID_VALUE; | |
83526631 | 768 | ## retrieve (exists = any(tbl.name where tbl.name = value)) |
769 | } | |
770 | ||
771 | if (LOG_RES) | |
772 | com_err(whoami, 0, "setting ID %s to %d", name, value); | |
773 | ## repeat replace v (#value = @value) where v.#name = @name | |
774 | return(SMS_SUCCESS); | |
775 | ##} | |
776 | ||
777 | ||
778 | /* For now this just checks the argc's. It should also see that there | |
779 | * are no duplicate names. | |
780 | */ | |
781 | ||
782 | sanity_check_queries() | |
783 | { | |
784 | register int i; | |
785 | int maxv = 0, maxa = 0; | |
786 | extern int QueryCount1, QueryCount2; | |
787 | extern struct query Queries1[], Queries2[]; | |
788 | #define MAX(x,y) ((x) > (y) ? (x) : (y)) | |
789 | ||
790 | for (i = 0; i < QueryCount1; i++) { | |
791 | maxv = MAX(maxv, Queries1[i].vcnt); | |
792 | maxa = MAX(maxa, Queries1[i].argc); | |
793 | } | |
794 | for (i = 0; i < QueryCount2; i++) { | |
795 | maxv = MAX(maxv, Queries2[i].vcnt); | |
796 | maxa = MAX(maxa, Queries2[i].argc); | |
797 | } | |
798 | if (MAX(maxv, maxa) > QMAXARGS) { | |
799 | com_err(whoami, 0, "A query has more args than QMAXARGS"); | |
800 | exit(1); | |
801 | } | |
802 | } | |
803 | ||
804 | ||
d26cae4e | 805 | /* |
806 | * Local Variables: | |
807 | * mode: c | |
808 | * c-indent-level: 4 | |
809 | * c-continued-statement-offset: 4 | |
810 | * c-brace-offset: -4 | |
811 | * c-argdecl-indent: 4 | |
812 | * c-label-offset: -4 | |
813 | * End: | |
814 | */ | |
815 |