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