6 * Copyright (C) 1987, 1988 by the Massachusetts Institute of Technology
11 static char *rcsid_qrtn_qc = "$Header$";
15 #include "sms_server.h"
19 static int ingres_errno = 0;
23 #define INGRES_BAD_INT 4111
24 #define INGRES_BAD_DATE 4302
25 #define INGRES_DEADLOCK 4700
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.
33 static int ingerr(num)
40 ingres_errno = SMS_INTEGER;
43 ingres_errno = SMS_DATE;
46 ingres_errno = SMS_DEADLOCK;
49 ingres_errno = SMS_INGRES_ERR;
50 com_err(whoami, SMS_INGRES_ERR, " code %d\n", *num);
51 sprintf(buf, "Ingres error %d", *num);
52 send_zgram("ingres_error", buf);
58 int sms_open_database()
63 /* initialize local argv */
64 for (i = 0; i < 16; i++)
65 Argv[i] = malloc(ARGLEN);
71 /* open the database */
76 int sms_close_database()
81 sms_check_access(cl, name, argc, argv_ro)
88 struct query *get_query_by_name();
90 q = get_query_by_name(name, cl->args->sms_version_no);
91 if (q == (struct query *)0)
92 return(SMS_NO_HANDLE);
94 return(sms_verify_query(cl, q, argc, argv_ro));
97 sms_process_query(cl, name, argc, argv_ro, action, actarg)
105 register struct query *q;
107 register struct validate *v;
113 struct save_queue *sq;
114 struct query *get_query_by_name();
116 struct save_queue *sq_create();
119 /* list queries command */
120 if (!strcmp(name, "_list_queries")) {
121 list_queries(cl->args->sms_version_no, action, actarg);
125 /* help query command */
126 if (!strcmp(name, "_help")) {
129 q = get_query_by_name(argv_ro[0], cl->args->sms_version_no);
130 if (q == (struct query *)0) return(SMS_NO_HANDLE);
131 help_query(q, action, actarg);
135 /* get query structure, return error if named query does not exist */
136 q = get_query_by_name(name, cl->args->sms_version_no);
137 if (q == (struct query *)0) return(SMS_NO_HANDLE);
140 if (q->type != RETRIEVE)
143 /* setup argument vector, verify access and arguments */
144 if ((status = sms_verify_query(cl, q, argc, argv_ro)) != SMS_SUCCESS)
147 /* perform any special query pre-processing */
148 if (v && v->pre_rtn) {
149 status = (*v->pre_rtn)(q, Argv, cl, 0);
150 if (status != SMS_SUCCESS)
156 /* for queries that do not permit wildcarding, check if row
159 status = validate_row(q, Argv, v);
160 if (status != SMS_EXISTS) break;
163 /* build "where" clause if needed */
165 build_qual(q->qual, q->argc, Argv, qual);
171 /* build "sort" clause if needed */
172 if (v && v->valobj) {
173 psort = build_sort(v, sort);
178 /* if there is a followup routine, then we must save the results */
179 /* of the first query for use by the followup routine */
180 /* if q->rvar = NULL, perform post_rtn only */
182 if (v && v->post_rtn) {
184 status = do_retrieve(q, pqual, psort, sq_save_args, sq);
185 if (status != SMS_SUCCESS) {
189 status = (*v->post_rtn)(q, sq, v, action, actarg, cl);
191 /* normal retrieve */
192 status = do_retrieve(q, pqual, psort, action, actarg);
194 if (status != SMS_SUCCESS) break;
196 ## repeat replace tblstats (retrieves = tblstats.retrieves + 1)
197 ## where tblstats.#table = @table
199 status = (*v->post_rtn)(q, Argv, cl, action, actarg);
205 /* see if row already exists */
207 status = validate_row(q, Argv, v);
208 if (status != SMS_EXISTS) break;
211 /* build "where" clause and perform update */
212 /* if q->rvar = NULL, perform post_rtn only */
214 build_qual(q->qual, q->argc, Argv, qual);
215 status = do_update(q, &Argv[q->argc], qual, action, actarg);
216 if (status != SMS_SUCCESS) break;
218 ## repeat replace tblstats (updates = tblstats.updates + 1,
220 ## where tblstats.#table = @table
223 /* execute followup routine (if any) */
224 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
229 /* see if row already exists */
231 status = validate_row(q, Argv, v);
232 if (status != SMS_NO_MATCH) break;
235 /* increment id number if necessary */
237 status = set_next_object_id(v->object_id, q->rtable);
238 if (status != SMS_SUCCESS) break;
241 /* build "where" clause if needed */
243 build_qual(q->qual, q->argc, Argv, qual);
249 /* perform the append */
250 /* if q->rvar = NULL, perform post_rtn only */
252 status = do_append(q, &Argv[q->argc], pqual, action, actarg);
253 if (status != SMS_SUCCESS) break;
255 ## repeat replace tblstats (appends = tblstats.appends + 1,
257 ## where tblstats.#table = @table
260 /* execute followup routine */
261 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
265 /* see if row already exists */
267 status = validate_row(q, Argv, v);
268 if (status != SMS_EXISTS) break;
271 /* build "where" clause and perform delete */
272 /* if q->rvar = NULL, perform post_rtn only */
274 build_qual(q->qual, q->argc, Argv, qual);
275 status = do_delete(q, qual, action, actarg);
276 if (status != SMS_SUCCESS) break;
278 ## repeat replace tblstats (deletes = tblstats.deletes + 1,
280 ## where tblstats.#table = @table
283 /* execute followup routine */
284 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
290 if (q->type != RETRIEVE) {
291 if (status == SMS_SUCCESS) {
292 ## end transaction /* commit to this */
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++) {
305 requote(buf, argv_ro[i], sizeof(buf));
312 ## abort /* it never happened */
316 if (status != SMS_SUCCESS && log_flags & LOG_RES)
317 com_err(whoami, status, " (Query failed)");
321 build_qual(fmt, argc, argv, qual)
333 for (i = 0; i < argc; i++) {
335 if (c++ == (char *)0) return(SMS_ARGS);
339 *(int *)&args[i] = *(int *)argv[i]; /* sigh */
341 return(SMS_INGRES_ERR);
350 sprintf(qual, fmt, args[0]);
354 sprintf(qual, fmt, args[0], args[1]);
358 sprintf(qual, fmt, args[0], args[1], args[2]);
362 sprintf(qual, fmt, args[0], args[1], args[2], args[3]);
370 register struct validate *v;
373 register struct valobj *vo;
382 if (vo->type == V_SORT) {
383 sprintf(elem, "RET_VAR%d", vo->index + 1);
384 if (*sort) strcat(sort, ", ");
390 return ((*sort) ? sort : 0);
394 /* Build arguement vector, verify query and arguments */
396 sms_verify_query(cl, q, argc, argv_ro)
404 register struct validate *v = q->validate;
406 register int privileged = 0;
408 /* copy the arguments into a local argv that we can modify */
409 for (i = 0; i < argc; i++) {
410 if (strlen(argv_ro[i]) < ARGLEN)
411 strcpy(Argv[i], argv_ro[i]);
413 return(SMS_ARG_TOO_LONG);
416 /* check initial query access */
417 status = check_query_access(q, Argv, cl);
418 if (status != SMS_SUCCESS && status != SMS_PERM)
420 if (status == SMS_SUCCESS)
423 /* check argument count */
425 if (q->type == UPDATE || q->type == APPEND) argreq += q->vcnt;
426 if (argc != argreq) return(SMS_ARGS);
428 /* validate arguments */
429 if (v && v->valobj) {
430 status = validate_fields(q, Argv, v->valobj, v->objcnt);
431 if (status != SMS_SUCCESS) return(status);
434 /* perform special query access check */
435 if (!privileged && v && v->acs_rtn) {
436 status = (*v->acs_rtn)(q, Argv, cl);
437 if (status != SMS_SUCCESS && status != SMS_PERM)
439 if (status == SMS_SUCCESS)
443 return(privileged ? SMS_SUCCESS : SMS_PERM);
446 check_query_access(q, argv, cl)
456 ## static int def_uid;
461 /* get query access control list */
463 ## repeat retrieve (acl_id = capacls.list_id) where capacls.tag = @name
464 ## inquire_equel (rowcount = "rowcount", errorno = "errorno")
465 if (errorno != 0) return(SMS_INGRES_ERR);
466 if (rowcount == 0) return(SMS_PERM);
468 /* initialize default uid */
470 ## retrieve (def_uid = users.users_id) where users.login = "default"
473 /* check for default access */
474 ## range of m is members
475 ## repeat retrieve (exists = any(m.#member_id where m.list_id = @acl_id and
476 ## m.member_type = "USER" and m.#member_id = def_uid))
477 if (exists) return(SMS_SUCCESS);
479 /* parse client name */
480 status = get_client(cl, &client_type, &client_id);
481 if (status != SMS_SUCCESS) return(status);
483 /* see if client is in the list (or any of its sub-lists) */
484 exists = find_member("LIST", acl_id, client_type, client_id, 0);
485 return ((exists) ? SMS_SUCCESS : SMS_PERM);
488 get_client(cl, client_type, client_id)
498 if (cl->clname == NULL)
501 /* for now ignore instances */
504 /* if client is from local realm, get users_id */
505 if (!strcmp(krb->realm, krb_realm)) {
506 *client_id = cl->users_id;
507 *client_type = "USER";
511 /* otherwise use string_id */
513 ## repeat retrieve (member_id = strings.string_id)
514 ## where strings.string = @name
516 /* make sure we found a users or string id */
517 ## inquire_equel (rowcount = "rowcount")
518 if (rowcount == 0) return(SMS_PERM);
520 *client_type = "STRING";
521 *client_id = member_id;
525 ##find_member(list_type, list_id, member_type, member_id, sq)
528 ## char *member_type;
530 struct save_queue *sq;
535 struct save_queue *sq_create();
537 /* see if client is a direct member of list */
538 ## repeat retrieve (exists = any(m.#member_id where
539 ## m.#list_id = @list_id and
540 ## m.#member_type = @member_type and
541 ## m.#member_id = @member_id))
542 if (exists) return(1);
544 /* are there any sub-lists? */
545 ## repeat retrieve (exists = any(m.#member_id where m.#list_id = @list_id and
546 ## m.#member_type = "LIST"))
547 if (!exists) return(0);
549 /* yes; now recurse through sublists */
551 /* create a save queue */
552 if (sq == (struct save_queue *)0) {
559 /* save all sublist ids */
560 ## range of m is members
561 ## retrieve (sublist = m.#member_id)
562 ## where m.#list_id = list_id and m.#member_type = "LIST"
564 sq_save_unique_data(sq, sublist);
567 if (child) return(0);
569 /* at top-level, check sub-lists for client (breadth-first search) */
570 while (sq_get_data(sq, &sublist)) {
571 exists = find_member(list_type, sublist, member_type, member_id, sq);
582 do_retrieve(q, pqual, psort, action, actarg)
583 register struct query *q;
595 static char **vaddrs = (char **)NULL;
600 if ((vaddrs = (char **)malloc(sizeof(char *) * QMAXARGS)) == NULL) {
601 com_err(whoami, SMS_NO_MEM, "setting up static argv");
604 for (i = 0; i < QMAXARGS; i++) {
605 if ((vaddrs[i] = malloc(QMAXARGSIZE)) == NULL) {
606 com_err(whoami, SMS_NO_MEM, "setting up static argv");
615 ## range of rvar is rtable
622 ## retrieve unique (param (q->tlist, vaddrs)) where cqual
625 (*action)(q->vcnt, vaddrs, actarg);
628 ## retrieve unique (param (q->tlist, vaddrs))
631 (*action)(q->vcnt, vaddrs, actarg);
638 ## retrieve unique (param (q->tlist, vaddrs)) where cqual
640 (*action)(q->vcnt, vaddrs, actarg);
643 ## retrieve unique (param (q->tlist, vaddrs))
645 (*action)(q->vcnt, vaddrs, actarg);
650 ## inquire_equel (rowcount = "rowcount", errorno = "errorno")
651 if (errorno != 0) return(SMS_INGRES_ERR);
652 return ((rowcount == 0) ? SMS_NO_MATCH : SMS_SUCCESS);
655 do_update(q, argv, qual, action, actarg)
656 register struct query *q;
669 ## range of rvar is rtable
672 ## replace rvar (param (q->tlist, argv))
675 ## inquire_equel (errorno = "errorno")
676 if (errorno == INGRES_BAD_INT)
678 else if (errorno != 0)
679 return(SMS_INGRES_ERR);
683 do_append(q, argv, pqual, action, actarg)
684 register struct query *q;
697 ## range of rvar is rtable
701 ## append to rtable (param (q->tlist, argv)) where cqual
703 ## append to rtable (param (q->tlist, argv))
706 ## inquire_equel (errorno = "errorno")
707 if (errorno == INGRES_BAD_INT)
709 else if (errorno != 0)
710 return(SMS_INGRES_ERR);
714 do_delete(q, qual, action, actarg)
715 register struct query *q;
727 ## range of rvar is rtable
730 ## delete rvar where cqual
732 ## inquire_equel (errorno = "errorno")
733 if (errorno != 0) return(SMS_INGRES_ERR);
739 ** set_next_object_id - set next object id in values table
741 ** Inputs: object - object name in values table and in objects
742 ** table - name of table objects are found in
744 ** - called before an APPEND operation to set the next object id to
745 ** be used for the new record to the next free value
749 set_next_object_id(object, table)
754 ## int rowcount, exists, value;
758 ## range of v is values
759 ## repeat retrieve (value = v.#value) where v.#name = @name
760 ## inquire_equel(rowcount = "rowcount")
764 ## retrieve (exists = any(tbl.name where tbl.name = value))
765 ## inquire_equel(rowcount = "rowcount")
770 if (value > MAX_ID_VALUE)
771 value = MIN_ID_VALUE;
772 ## retrieve (exists = any(tbl.name where tbl.name = value))
776 com_err(whoami, 0, "setting ID %s to %d", name, value);
777 ## repeat replace v (#value = @value) where v.#name = @name
782 /* For now this just checks the argc's. It should also see that there
783 * are no duplicate names.
786 sanity_check_queries()
789 int maxv = 0, maxa = 0;
790 extern int QueryCount1, QueryCount2;
791 extern struct query Queries1[], Queries2[];
792 #define MAX(x,y) ((x) > (y) ? (x) : (y))
794 for (i = 0; i < QueryCount1; i++) {
795 maxv = MAX(maxv, Queries1[i].vcnt);
796 maxa = MAX(maxa, Queries1[i].argc);
798 for (i = 0; i < QueryCount2; i++) {
799 maxv = MAX(maxv, Queries2[i].vcnt);
800 maxa = MAX(maxa, Queries2[i].argc);
802 if (MAX(maxv, maxa) > QMAXARGS) {
803 com_err(whoami, 0, "A query has more args than QMAXARGS");
813 * c-continued-statement-offset: 4
815 * c-argdecl-indent: 4