]> andersk Git - moira.git/blob - server/qrtn.pc
.dc -> .pc
[moira.git] / server / qrtn.pc
1 /*
2  *      $Source$
3  *      $Author$
4  *      $Header$
5  *
6  *      Copyright (C) 1987, 1988 by the Massachusetts Institute of Technology
7  *      For copying and distribution information, please see the file
8  *      <mit-copyright.h>.
9  * 
10  */
11
12 #ifndef lint
13 static char *rcsid_qrtn_dc = "$Header$";
14 #endif lint
15
16 #include <mit-copyright.h>
17 #include <string.h>
18 #include "mr_server.h"
19 #include "query.h"
20 EXEC SQL INCLUDE sqlca;  /* SQL Communications Area */
21 EXEC SQL INCLUDE sqlda;  /* SQL Descriptor Area */
22 #include "qrtn.h"
23
24 SQLDA *mr_sqlda;
25 EXEC SQL BEGIN DECLARE SECTION; 
26 int mr_sig_length;
27 int idummy;                            
28 char cdummy[MR_CDUMMY_LEN];            
29 char stmt_buf[MR_STMTBUF_LEN];
30 EXEC SQL END DECLARE SECTION; 
31
32 char *Argv[QMAXARGS];
33 extern char *table_name[];
34 extern char *sqlbuffer[QMAXARGS];
35
36 int dbms_errno = 0;
37 int mr_errcode = 0;
38 EXEC SQL BEGIN DECLARE SECTION; 
39 int query_timeout = 30;
40 char *database = "moira";
41 EXEC SQL END DECLARE SECTION;
42 extern char *whoami;
43 extern FILE *journal;
44
45 int mr_verify_query(client *cl, struct query *q, int argc, char *argv_ro[]);
46 int do_retrieve(struct query *q, char *pqual, char *psort,
47                 int (*action)(), char *actarg);
48 int do_update(struct query *q, char *argv[], char *qual,
49               int (*action)(), char *actarg);
50 int do_append(struct query *q, char *argv[], char *pqual,
51               int (*action)(), char *actarg);
52 int do_delete(struct query *q, char *qual,
53               int (*action)(), char *actarg);
54 void build_sql_stmt(char *result_buf, char *cmd, char *targetlist,
55                     char *argv[], char *qual);
56 char *build_sort(struct validate *v, char *sort);
57
58 /* from qvalidate.dc */
59 int validate_fields(struct query *q, char *argv[], struct valobj *vo, int n);
60 int validate_row(struct query *q, char *argv[], struct validate *v);
61
62
63 /*
64  * dbmserr: Called when the DBMS indicates an error.
65  */
66
67 void dbmserr(void)
68 {
69     EXEC SQL BEGIN DECLARE SECTION; 
70     char err_msg[256];
71     EXEC SQL END DECLARE SECTION;
72     int bufsize=256, msglength=0;
73
74     dbms_errno = -sqlca.sqlcode;
75     mr_errcode = MR_DBMS_ERR;
76     com_err(whoami, MR_DBMS_ERR, " code %d\n", dbms_errno);
77     sqlglm(err_msg, &bufsize, &msglength);
78     err_msg[msglength]=0;
79     com_err(whoami, 0, "SQL error text = %s", err_msg);
80     critical_alert("MOIRA", "Moira server encountered DBMS ERROR %d\n%s",
81                        dbms_errno, err_msg);
82 }
83
84 /* This is declarative, not executed.  Applies from here on, in this file. */
85 EXEC SQL WHENEVER SQLERROR DO dbmserr();
86
87 int mr_open_database(void)
88 {
89     register int i;
90     SQLDA *mr_alloc_sqlda();
91     static first_open = 1;
92
93     if (first_open) {
94         first_open = 0;
95
96         /* initialize local argv */
97         for (i = 0; i < 16; i++)
98           Argv[i] = malloc(ARGLEN);
99
100         mr_sqlda = mr_alloc_sqlda();       
101
102         incremental_init();
103         flush_cache();
104     }
105         
106     dbms_errno = 0;
107     mr_errcode = 0;
108
109     /* open the database */
110     EXEC SQL CONNECT :database IDENTIFIED BY :database;
111
112     if(dbms_errno) 
113         return(mr_errcode);
114
115     EXEC SQL SELECT data_length INTO :mr_sig_length FROM user_tab_columns WHERE table_name='USERS' and column_name='SIGNATURE';
116     EXEC SQL COMMIT WORK;
117     if(dbms_errno)
118         return(mr_errcode);
119
120     return(MR_SUCCESS);
121 }
122
123 void mr_close_database(void)
124 {
125     flush_cache();
126     EXEC SQL COMMIT RELEASE;
127 }
128
129 int mr_check_access(cl, name, argc, argv_ro)
130      client *cl;
131      char *name, *argv_ro[];
132      int argc;
133 {
134     struct query *q;
135
136     dbms_errno = 0;
137     mr_errcode = 0;
138
139     q = get_query_by_name(name, cl->args->mr_version_no);
140     if (q == (struct query *)0)
141         return(MR_NO_HANDLE);
142
143     return(mr_verify_query(cl, q, argc, argv_ro));    
144 }
145
146 int mr_process_query(cl, name, argc, argv_ro, action, actarg)
147      client *cl;
148      char *name, *argv_ro[], *actarg;
149      int argc, (*action)();
150 {
151     register struct query *q;
152     register int status;
153     register struct validate *v;
154     char qual[256];
155     char sort[32];
156     char *pqual;
157     char *psort;
158     EXEC SQL BEGIN DECLARE SECTION; 
159     char *table;
160     EXEC SQL END DECLARE SECTION; 
161     struct save_queue *sq;
162
163     dbms_errno = 0;
164     mr_errcode = 0;
165
166     /* list queries command */
167     if (!strcmp(name, "_list_queries")) {
168         list_queries(cl->args->mr_version_no, action, actarg);
169         return(MR_SUCCESS);
170     }
171
172     /* help query command */
173     if (!strcmp(name, "_help")) {
174         if (argc < 1)
175             return(MR_ARGS);
176         q = get_query_by_name(argv_ro[0], cl->args->mr_version_no);
177         if (q == (struct query *)0) return(MR_NO_HANDLE);
178         help_query(q, action, actarg);
179         return(MR_SUCCESS);
180     }
181
182     /* get query structure, return error if named query does not exist */
183     q = get_query_by_name(name, cl->args->mr_version_no);
184     if (q == (struct query *)0) return(MR_NO_HANDLE);
185     v = q->validate;
186
187     /* setup argument vector, verify access and arguments */
188     if ((status = mr_verify_query(cl, q, argc, argv_ro)) != MR_SUCCESS)
189         goto out;
190
191     /* perform any special query pre-processing */
192     if (v && v->pre_rtn) {
193         status = (*v->pre_rtn)(q, Argv, cl, 0);
194         if (status != MR_SUCCESS)
195             goto out;
196     }
197
198     switch (q->type) {
199     case RETRIEVE:
200         /* for queries that do not permit wildcarding, check if row
201            uniquely exists */
202         if (v && v->field) {
203             status = validate_row(q, Argv, v); 
204             if (status != MR_EXISTS) break;
205         }
206
207         /* build "where" clause if needed */
208         if (q->qual) {
209             build_qual(q->qual, q->argc, Argv, qual); 
210             pqual = qual;
211         } else {
212             pqual = 0;
213         }
214
215         /* build "sort" clause if needed */
216         if (v && v->valobj) {
217             psort = build_sort(v, sort);
218         } else {
219             psort = 0;
220         }
221
222         /* if there is a followup routine, then we must save the results */
223         /* of the first query for use by the followup routine */
224         /* if q->rvar = NULL, perform post_rtn only */
225         if (q->rvar) {
226             if (v && v->post_rtn) {
227                 sq = sq_create();
228                 status = do_retrieve(q, pqual, psort, sq_save_args,
229                                      (char *)sq);
230                 if (status != MR_SUCCESS) {
231                     sq_destroy(sq);
232                     break;
233                 }
234                 status = (*v->post_rtn)(q, sq, v, action, actarg, cl);
235             } else {
236                 /* normal retrieve */
237                 status = do_retrieve(q, pqual, psort, action, actarg);
238             }
239             if (status != MR_SUCCESS) break;
240         } else {
241             status = (*v->post_rtn)(q, Argv, cl, action, actarg);
242         }
243
244         break;
245
246     case UPDATE:
247         /* see if row already exists */
248         if (v->field) {
249             status = validate_row(q, Argv, v);
250             if (status != MR_EXISTS) break;
251         }
252
253         /* build "where" clause and perform update */
254         /* if q->rvar = NULL, perform post_rtn only */
255         if (q->rvar) {
256             build_qual(q->qual, q->argc, Argv, qual);
257             incremental_before(q->rtable, qual, argv_ro);
258             status = do_update(q, &Argv[q->argc], qual, action, actarg);
259             incremental_after(q->rtable, qual, argv_ro);
260             if (status != MR_SUCCESS) break;
261             flush_name(argv_ro[0], q->rtable);
262             table = table_name[q->rtable];
263             if (strcmp(q->shortname, "sshi") && strcmp(q->shortname, "ssif")) {
264                 EXEC SQL UPDATE tblstats
265                   SET updates = updates + 1, modtime = SYSDATE
266                   WHERE table_name = :table;
267             }
268         }
269
270         /* execute followup routine (if any) */
271         if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
272
273         break;
274
275     case APPEND:
276         /* see if row already exists */
277         if (v->field) {
278             status = validate_row(q, Argv, v);
279             if (status != MR_NO_MATCH) break;
280         }
281
282         /* build "where" clause if needed */
283         if (q->qual) {
284             build_qual(q->qual, q->argc, Argv, qual);
285             pqual = qual;
286         } else {
287             pqual = 0;
288         }
289
290         /* perform the append */
291         /* if q->rvar = NULL, perform post_rtn only */
292         if (q->rvar) {
293             incremental_clear_before();
294             status = do_append(q, &Argv[q->argc], pqual, action, actarg);
295             if (status != MR_SUCCESS) break;
296             if (v && v->object_id) {
297                 sprintf(qual, "%s.%s = %s",q->rvar, v->object_id, 
298                         Argv[q->argc+q->vcnt]);
299                 incremental_after(q->rtable, qual, argv_ro);
300             } else
301               incremental_after(q->rtable, pqual, argv_ro);
302
303             table = table_name[q->rtable];
304             EXEC SQL UPDATE tblstats
305               SET appends = appends + 1, modtime = SYSDATE
306               WHERE table_name = :table;
307         }
308         
309         /* execute followup routine */
310         if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
311         break;
312
313     case DELETE:
314         /* see if row already exists */
315         if (v->field) {
316             status = validate_row(q, Argv, v);
317             if (status != MR_EXISTS) break;
318         }
319
320         /* build "where" clause and perform delete */
321         /* if q->rvar = NULL, perform post_rtn only */
322         if (q->rvar) {
323             build_qual(q->qual, q->argc, Argv, qual);
324             table = table_name[q->rtable];
325             incremental_before(q->rtable, qual, argv_ro);
326             status = do_delete(q, qual, action, actarg);
327             incremental_clear_after();
328             if (status != MR_SUCCESS) break;
329             flush_name(argv_ro[0], q->rtable);
330             EXEC SQL UPDATE tblstats
331               SET deletes = deletes + 1, modtime = SYSDATE
332               WHERE table_name = :table;
333         }
334
335         /* execute followup routine */
336         if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
337         break;
338
339     }
340
341 out:
342     if (status == MR_SUCCESS && dbms_errno != 0) {
343         com_err(whoami, MR_INTERNAL, "Server didn't notice DBMS ERROR %d",
344                        dbms_errno);
345         status = mr_errcode;
346     }
347
348     if (q->type == RETRIEVE) {
349         EXEC SQL COMMIT WORK;
350     } else {
351         if (status == MR_SUCCESS) {
352         EXEC SQL COMMIT WORK;
353             if (journal) {
354                 char buf[1024];
355                 int i;
356                 extern time_t now;
357
358                 fprintf(journal, "%% %s %s %s",
359                         cl->clname, cl->entity, ctime(&now));
360                 fprintf(journal, "%s[%d] ", q->name, cl->args->mr_version_no);
361                 for (i = 0; i < argc; i++) {
362                     if (i != 0) {
363                         putc(' ', journal);
364                     }
365                     requote(buf, argv_ro[i], sizeof(buf));
366                     fputs(buf, journal);
367                 }
368                 putc('\n', journal);
369                 fflush(journal);
370             }
371             incremental_update();
372         } else {
373             cache_abort();
374             EXEC SQL ROLLBACK WORK;
375             incremental_flush();
376         }
377     }
378     cache_commit(); /* commit following abort is safe */
379
380     if (status != MR_SUCCESS && log_flags & LOG_RES)
381         com_err(whoami, status, " (Query failed)");
382     return(status);
383 }
384
385 int build_qual(fmt_buf, argc, argv, qual)
386      char *fmt_buf, *argv[], *qual;
387      int argc;
388 {
389     char *res, *fmt;
390
391     for(res=qual, fmt=fmt_buf; *fmt; fmt++) {
392         if(*fmt=='%') {
393             if(*++fmt) {
394                 switch(*fmt) {                       
395                   case '%':                              /* %% -> % */
396                     *res++ = *fmt;           
397                     break;
398                   case 's':
399                     if(*argv[0]) {
400                         char *p=*argv;
401                         while(*p) {
402                             if(*p=='\'') *res++='\'';   /* double the ' */
403                             *res++=*p++;
404                         }
405                     }
406                     argv++;
407                     break;
408                   case 'd':
409                     res+=sprintf(res,"%d",*(int *)*argv++);
410                     break;
411                   default:                               /* Swallow other %? pairs */
412                     break;
413                 }
414             } else break;
415         } else *res++ = *fmt;                            /* text -> result buffer */
416     }
417     *res='\0';
418 }
419
420 char *build_sort(v, sort)
421      struct validate *v;
422      char *sort;
423 {
424     register struct valobj *vo;
425     register int n;
426     char elem[16];
427
428     n = v->objcnt;
429     vo = v->valobj;
430     *sort = 0;
431
432     while (--n >= 0) {
433         if (vo->type == V_SORT) {
434             sprintf(elem, "%d", vo->index + 1);    /* Result column number */
435             if (*sort) strcat(sort, ", ");
436             strcat(sort, elem);
437         }
438         vo++;
439     }
440
441     return ((*sort) ? sort : 0);
442 }
443
444
445 /* Build arguement vector, verify query and arguments */
446
447 int privileged;
448
449 int mr_verify_query(cl, q, argc, argv_ro)
450      client *cl;
451      struct query *q;
452      int argc;
453      char *argv_ro[];
454 {
455     register int argreq;
456     register int status;
457     register struct validate *v = q->validate;
458     register int i;
459     register char *to,*fr,*stop;
460
461     privileged = 0;
462
463     /* copy the arguments into a local argv that we can modify */
464     if (argc >= QMAXARGS)
465       return(MR_ARGS);
466     for (i = 0; i < argc; i++) {
467         for (to=Argv[i], fr=argv_ro[i], stop=to+ARGLEN; (*fr) && (to<stop);)
468           *to++ = *fr++;
469
470         if (*fr) 
471           return(MR_ARG_TOO_LONG);
472         *to='\0';
473
474         if (*--to == '\\')
475           return(MR_BAD_CHAR);
476     }
477
478     /* check initial query access */
479     status = check_query_access(q, Argv, cl);
480     if (status != MR_SUCCESS && status != MR_PERM)
481         return(status);
482     if (status == MR_SUCCESS)
483         privileged++;
484
485     /* check argument count */
486     argreq = q->argc;
487     if (q->type == UPDATE || q->type == APPEND) argreq += q->vcnt;
488     if (argc != argreq) return(MR_ARGS);
489
490     /* validate arguments */
491     if (v && v->valobj) {
492         status = validate_fields(q, Argv, v->valobj, v->objcnt);
493         if (status != MR_SUCCESS) return(status);
494     }
495
496     /* perform special query access check */
497     if (!privileged && v && v->acs_rtn) {
498         status = (*v->acs_rtn)(q, Argv, cl);
499         if (status != MR_SUCCESS && status != MR_PERM)
500             return(status);
501         if (status == MR_SUCCESS)
502             return(MR_SUCCESS);
503     }
504
505     return(privileged ? MR_SUCCESS : MR_PERM);
506 }
507
508
509 /* This routine caches info from the database.  Each query acl is stored
510  * in the query structure, and whether that acl contains everybody.
511  */
512
513 int check_query_access(q, argv, cl)
514      struct query *q;
515      char *argv[];
516      client *cl;
517 {
518     EXEC SQL BEGIN DECLARE SECTION; 
519     char *name;
520     int acl_id;
521     static int def_uid;
522     EXEC SQL END DECLARE SECTION; 
523     int client_id;
524     char *client_type;
525
526     /* initialize default uid */
527     if (def_uid == 0) {
528         EXEC SQL SELECT users_id INTO :def_uid FROM users WHERE login='default';
529     }
530
531     /* get query access control list */
532     if (q->acl != 0)
533       acl_id = q->acl;
534     else {
535         name = q->shortname;
536         EXEC SQL SELECT list_id INTO :acl_id FROM capacls WHERE tag = :name;
537         if (sqlca.sqlcode < 0) return(MR_DBMS_ERR);
538         if (sqlca.sqlcode == SQL_NO_MATCH) return(MR_PERM);
539         q->acl = acl_id;
540
541         /* check for default access */
542         EXEC SQL SELECT member_id INTO :acl_id FROM imembers
543           WHERE list_id = :acl_id AND member_type = 'USER' 
544             AND member_id = :def_uid;
545         if (sqlca.sqlerrd[2] == 0)
546           q->everybody = 0;
547         else
548           q->everybody = 1;
549     }
550
551     if (q->everybody)
552       return(MR_SUCCESS);
553
554     if (get_client(cl, &client_type, &client_id) != MR_SUCCESS)
555       return(MR_PERM);
556     if (find_member("LIST", acl_id, client_type, client_id))
557       return(MR_SUCCESS);
558     else
559       return(MR_PERM);
560 }
561
562
563 int get_client(cl, client_type, client_id)
564      client *cl;
565      char **client_type;
566      int *client_id;
567 {
568     if (cl->users_id > 0) {
569         *client_id = cl->users_id;
570         *client_type = "USER";
571         return(MR_SUCCESS);
572     }
573
574     if (cl->client_id < 0) {
575         *client_id = -cl->users_id;
576         *client_type = "KERBEROS";
577         return(MR_SUCCESS);
578     }
579
580     return(MR_PERM);
581 }
582
583 int find_member(list_type, list_id, member_type, member_id)
584      char *list_type, *member_type;
585      int list_id, member_id;
586 {
587     EXEC SQL BEGIN DECLARE SECTION; 
588     int flag;
589     EXEC SQL END DECLARE SECTION; 
590
591     if (!strcmp(strtrim(list_type), strtrim(member_type)) &&
592         list_id == member_id)
593         return(1);
594
595     /* see if client is a direct member of list */
596     flag = 0;
597     EXEC SQL SELECT member_id INTO :flag FROM imembers
598       WHERE list_id = :list_id AND member_type = :member_type 
599         AND member_id = :member_id;
600     if(flag!=0) flag=1;             /** Not strictly necessary */
601     if (sqlca.sqlcode == 0)
602       return(flag);            
603     return(0);
604 }
605
606
607 int do_retrieve(q, pqual, psort, action, actarg)
608      struct query *q;
609      char *pqual, *psort, *actarg;
610      int (*action)();
611 {
612     build_sql_stmt(stmt_buf,"SELECT",q->tlist,NULL,pqual);
613     if(psort) {
614       strcat(stmt_buf," ORDER BY ");
615       strcat(stmt_buf,psort);
616     }
617
618     return do_for_all_rows(stmt_buf, q->vcnt, action, (int)actarg);
619 }
620
621 char *sqlstrstr(str, pat)
622      char *str, *pat;
623 {
624     register char *p=pat;
625     
626     do {
627         if(*str=='\'') {    /* Skip over single-quote delimited substrings */ 
628             while(*++str && (*str!='\'')) 
629                 ; 
630             continue;
631         }
632         if(*str==*p) {
633             register char *s;
634             s=str; 
635             while(*++p && (*++s==*p))
636                 ; 
637             if(*p) p=pat;  /* failed */
638         }
639     } while(*p && *++str);
640
641     if(!*str) str=NULL;
642     return(str);
643 }
644
645 void optimize_sql_stmt(buf)
646      char *buf;
647 {
648     char *point=buf, *pat, *eopat, *esc1, *esc2, *csr;
649
650     for(point=buf; point=sqlstrstr(point,"LIKE"); point++)  {
651         /* Now pointing to string "LIKE" */
652
653         /* Look at next word */
654         for(pat=point+4; *pat==' '; pat++) ;
655
656         /* Is it a single-quote delimited string? */
657         if(*pat!='\'') continue;
658
659         /* look for "escape" clause - save escape character */
660         /* 1. Find end of pattern */
661         for(eopat=pat+1; 1; eopat++) {
662             if(*eopat=='\'') {
663                 if(eopat[1]=='\'')  /* single-quote is self-escaping */
664                     eopat++;
665                 else
666                     break;
667             }
668         }
669
670         /* 2. Look at next word */
671         for(esc1=eopat; *++esc1==' ';) ;
672
673         /* 3. esc1=0 if not "ESCAPE '?'", where the ? may be any character. */
674         if(strncmp(esc1,"ESCAPE",6)) esc1=NULL;
675
676         if(esc1) {
677             for(esc2=esc1+6; *esc2==' '; esc2++) ;
678             
679             if(*esc2++!='\'') continue; /* Bad SQL syntax. Skip. */
680             /* esc2 now points at the escape character itself */
681             if(esc2[1]!='\'') continue; /* Weird escape string. Skip. */
682         } else {
683             esc2="\\";
684         }
685
686         /* Is pattern free from special characters? */
687         for(csr=pat; csr<eopat; csr++) 
688             if((*csr=='%') || (*csr=='_') || (*csr==*esc2)) break;
689         if(csr!=eopat) continue; /* Uses pattern matching. Skip. */
690
691         /* Optimize the query statement */
692         /* 1. Change "LIKE" to " =  " */
693         memcpy(point," =  ",4);
694         
695         /* 2. Change "ESCAPE" to "      " */
696         if(esc1) {
697             memset(esc1,' ',6);
698             /* 3. Change  "'*'" to "   " */
699             /*    (Changes '''' to "    ") */
700             if(esc2) memset(esc2-1,' ',(*esc2=='\'')?4:3); 
701         }
702     }
703 }
704
705 void build_sql_stmt(result_buf, cmd, targetlist, argv, qual)
706      char *result_buf, *cmd, *targetlist, *argv[], *qual;
707 {
708     char fmt_buf[MR_STMTBUF_LEN];
709     register char *res, *fmt;
710
711     if(qual)
712       sprintf(fmt_buf,"%s %s WHERE %s",cmd,targetlist,qual);
713     else
714       sprintf(fmt_buf,"%s %s",cmd,targetlist);
715
716     for(res=result_buf, fmt=fmt_buf; *fmt; fmt++) {
717         if(*fmt=='%') {
718             if(*++fmt) {
719                 switch(*fmt) {                       
720                   case '%':                              /* %% -> % */
721                     *res++ = *fmt;           
722                     break;
723                   case 's':
724                     if(*argv[0]) {
725                         char *p=*argv;
726                         while(*p) {
727                             if(*p=='\'') *res++='\'';   /* double the ' */
728                             *res++=*p++;
729                         }
730                     }
731                     argv++;
732                     break;
733                   case 'd':
734                     res+=sprintf(res,"%d",*(int *)*argv++);
735                     break;
736                   default:                               /* Swallow other %? pairs */
737                     break;
738                 }
739             } else break;
740         } else *res++ = *fmt;                            /* text -> result buffer */
741     }
742     *res='\0';
743
744     optimize_sql_stmt(result_buf);
745 }
746
747 int do_update(q, argv, qual, action, actarg)
748      struct query *q;
749      char *argv[], *qual, *actarg;
750      int (*action)();
751 {
752     build_sql_stmt(stmt_buf,"UPDATE",q->tlist,argv,qual);
753     EXEC SQL EXECUTE IMMEDIATE :stmt_buf;
754     if (mr_errcode) return(mr_errcode);
755     return(MR_SUCCESS);
756 }
757
758 int do_append(q, argv, pqual, action, actarg)
759      struct query *q;
760      char *argv[], *pqual, *actarg;
761      int (*action)();
762 {
763     build_sql_stmt(stmt_buf,"INSERT",q->tlist,argv,pqual);
764     EXEC SQL EXECUTE IMMEDIATE :stmt_buf;
765     if (mr_errcode) return(mr_errcode);
766     return(MR_SUCCESS);
767 }
768
769 int do_delete(q, qual, action, actarg)
770      struct query *q;
771      char *qual, *actarg;
772      int (*action)();
773 {
774     sprintf(stmt_buf,"DELETE FROM %s WHERE %s",table_name[q->rtable],qual);
775     EXEC SQL EXECUTE IMMEDIATE :stmt_buf;
776     if (mr_errcode) return(mr_errcode);
777     return(MR_SUCCESS);
778 }
779
780
781 /**
782  ** set_next_object_id - set next object id in values table
783  **
784  ** Inputs: object - object name in values table and in objects
785  **         table - name of table objects are found in
786  **         limit - should the ID be range limited
787  **
788  ** - called before an APPEND operation to set the next object id to
789  **   be used for the new record to the next free value
790  **
791  **/
792
793 int set_next_object_id(object, table, limit)
794 EXEC SQL BEGIN DECLARE SECTION;
795      char *object;
796 EXEC SQL END DECLARE SECTION;
797      enum tables table;
798      int limit;
799 {
800     EXEC SQL BEGIN DECLARE SECTION; 
801     int value;
802     EXEC SQL END DECLARE SECTION;
803     int starting_value;
804     
805     EXEC SQL SELECT value INTO :value FROM numvalues WHERE name = :object;
806     if (sqlca.sqlerrd[2] != 1)
807         return(MR_NO_ID);
808
809     starting_value=value;
810     while (1) { 
811         if (limit && value > MAX_ID_VALUE) 
812             value = MIN_ID_VALUE;
813
814         sprintf(stmt_buf,"SELECT %s FROM %s WHERE %s=%d",
815                 object,table_name[table],object,value);
816         dosql(sqlbuffer);
817         if (sqlca.sqlcode < 0) return(mr_errcode);
818         if (sqlca.sqlcode == SQL_NO_MATCH) break;
819
820         value++;
821         if (limit && value == starting_value) {   
822             com_err(whoami,0,"All id values have been used");
823             return(MR_NO_ID);
824         }
825     }
826
827     if (LOG_RES)
828         com_err(whoami, 0, "setting ID %s to %d", object, value);
829     EXEC SQL UPDATE numvalues SET value = :value WHERE name = :object;
830     return(MR_SUCCESS);
831 }
832
833
834 /* Turn a kerberos name into the user's ID of the account that principal
835  * owns.  Sets the kerberos ID and user ID.
836  */
837
838 int set_krb_mapping(name, login, ok, kid, uid)
839      char *name, *login;
840      int ok, *kid, *uid;
841 {
842     EXEC SQL BEGIN DECLARE SECTION; 
843     int u_id, k_id;
844     char *krbname;
845     EXEC SQL END DECLARE SECTION; 
846
847     krbname = name;
848     *kid = 0;
849     *uid = 0;
850
851     EXEC SQL SELECT km.users_id, km.string_id INTO :u_id, :k_id
852       FROM krbmap km, strings str
853       WHERE km.string_id = str.string_id AND str.string = :krbname;
854     EXEC SQL COMMIT WORK;
855
856     if (dbms_errno) return(mr_errcode);
857     
858     if (sqlca.sqlerrd[2] == 1) {    /* rowcount */
859         *kid = -k_id;
860         *uid = u_id;
861         return(MR_SUCCESS);
862     }
863
864     if (name_to_id(name, STRINGS_TABLE, &k_id) == MR_SUCCESS)
865       *kid = -k_id;
866
867     if (!ok) {             
868         *uid = *kid;
869         return(MR_SUCCESS);
870     }
871
872     if (name_to_id(login, USERS_TABLE, uid) != MR_SUCCESS)
873       *uid = 0;
874
875     if (*kid == 0)
876       *kid = *uid;
877     if (dbms_errno) return(mr_errcode);
878     return(MR_SUCCESS);
879 }
880
881
882 /* For now this just checks the argc's.  It should also see that there
883  * are no duplicate names.
884  */
885
886 void sanity_check_queries(void)
887 {
888     register int i;
889     int maxv = 0, maxa = 0;
890     extern int QueryCount2;
891     extern struct query Queries2[];
892
893 #define MAX(x,y) ((x) > (y) ? (x) : (y))
894
895     for (i = 0; i < QueryCount2; i++) {
896         maxv = MAX(maxv, Queries2[i].vcnt);
897         maxa = MAX(maxa, Queries2[i].argc);
898     }
899     if (MAX(maxv, maxa) > QMAXARGS) {
900         com_err(whoami, 0, "A query has more args than QMAXARGS");
901         exit(1);
902     }
903 }
904
905
906 /* Generically do a SELECT, storing the results in the provided buffers */
907
908 void dosql(buffers)
909      char *buffers[];
910 {
911   int i, errcode=0, errlen;
912
913   EXEC SQL PREPARE inc_stmt FROM :stmt_buf;
914   if(sqlca.sqlcode) return;
915   EXEC SQL DECLARE inc_crs CURSOR FOR inc_stmt;
916   EXEC SQL OPEN inc_crs;
917   mr_sqlda->N = QMAXARGS;
918   EXEC SQL DESCRIBE SELECT LIST FOR inc_stmt INTO mr_sqlda;
919   mr_sqlda->N = mr_sqlda->F;
920   for(i=0; i<mr_sqlda->N; i++) {
921     mr_sqlda->V[i]=buffers[i];
922     mr_sqlda->T[i]=97;
923     mr_sqlda->L[i]=ARGLEN;
924   }
925   EXEC SQL FETCH inc_crs USING DESCRIPTOR mr_sqlda;
926
927   /* if we got an error from the FETCH, we have to preserve it or the
928      close will reset it and the caller will think nothing happened */
929   if(sqlca.sqlcode) {
930     errcode=sqlca.sqlcode;
931     errlen=sqlca.sqlerrm.sqlerrml;
932   }
933
934   EXEC SQL CLOSE inc_crs;
935   if(errcode) {
936     sqlca.sqlcode=errcode;
937     sqlca.sqlerrm.sqlerrml=errlen;
938   }
939 }
940
941 int do_for_all_rows(query, count, action, actarg)
942 EXEC SQL BEGIN DECLARE SECTION;
943      char *query;
944 EXEC SQL END DECLARE SECTION;
945      int count, (*action)(), actarg;
946 {
947   int i, rowcount=0;
948
949   EXEC SQL PREPARE stmt FROM :query;
950   if(sqlca.sqlcode) return;
951   EXEC SQL DECLARE curs CURSOR FOR stmt;
952   EXEC SQL OPEN curs;
953   mr_sqlda->N = count;
954   EXEC SQL DESCRIBE SELECT LIST FOR stmt INTO mr_sqlda;
955   mr_sqlda->N = mr_sqlda->F;
956   for(i=0; i<mr_sqlda->N; i++) {
957     mr_sqlda->V[i]=sqlbuffer[i];
958     mr_sqlda->T[i]=97;
959     mr_sqlda->L[i]=ARGLEN;
960   }
961
962   while(1) {
963     EXEC SQL FETCH curs USING DESCRIPTOR mr_sqlda;
964     if(sqlca.sqlcode != 0) break;
965     (*action)(count, sqlbuffer, actarg);
966     rowcount++;
967   }
968   EXEC SQL CLOSE curs;
969   
970   if (mr_errcode) return(mr_errcode);
971   return ((rowcount == 0) ? MR_NO_MATCH : MR_SUCCESS);
972 }
973
974
975 /* eof:qrtn.dc */
This page took 1.806029 seconds and 5 git commands to generate.