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