]> andersk Git - moira.git/blame - server/qrtn.pc
Remove unused elements from an SQL query
[moira.git] / server / qrtn.pc
CommitLineData
1a41acb7 1/*
2 * $Source$
3 * $Author$
4 * $Header$
b070f8a1 5 *
1a41acb7 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 *
b070f8a1 10 */
11
12#ifndef lint
048b1b94 13static char *rcsid_qrtn_dc = "$Header$";
b070f8a1 14#endif lint
15
16#include <mit-copyright.h>
cc0088db 17#include <string.h>
b070f8a1 18#include "mr_server.h"
03c05291 19#include "query.h"
048b1b94 20EXEC SQL INCLUDE sqlca; /* SQL Communications Area */
21EXEC SQL INCLUDE sqlda; /* SQL Descriptor Area */
22#include "qrtn.h"
23
03c05291 24SQLDA *mr_sqlda;
048b1b94 25EXEC SQL BEGIN DECLARE SECTION;
1741dca0 26int mr_sig_length;
048b1b94 27int idummy;
28char cdummy[MR_CDUMMY_LEN];
29char stmt_buf[MR_STMTBUF_LEN];
30EXEC SQL END DECLARE SECTION;
b070f8a1 31
30cf9ed3 32char *Argv[QMAXARGS];
03c05291 33extern char *table_name[];
34extern char *sqlbuffer[QMAXARGS];
b070f8a1 35
03c05291 36int dbms_errno = 0;
b070f8a1 37int mr_errcode = 0;
048b1b94 38EXEC SQL BEGIN DECLARE SECTION;
39int query_timeout = 30;
923a939b 40char *database = "moira";
048b1b94 41EXEC SQL END DECLARE SECTION;
b070f8a1 42extern char *whoami;
43extern FILE *journal;
44
03c05291 45int mr_verify_query(client *cl, struct query *q, int argc, char *argv_ro[]);
46int do_retrieve(struct query *q, char *pqual, char *psort,
47 int (*action)(), char *actarg);
48int do_update(struct query *q, char *argv[], char *qual,
49 int (*action)(), char *actarg);
50int do_append(struct query *q, char *argv[], char *pqual,
51 int (*action)(), char *actarg);
52int do_delete(struct query *q, char *qual,
53 int (*action)(), char *actarg);
54void build_sql_stmt(char *result_buf, char *cmd, char *targetlist,
55 char *argv[], char *qual);
56char *build_sort(struct validate *v, char *sort);
57
58/* from qvalidate.dc */
59int validate_fields(struct query *q, char *argv[], struct valobj *vo, int n);
60int validate_row(struct query *q, char *argv[], struct validate *v);
61
45bf7573 62
048b1b94 63/*
03c05291 64 * dbmserr: Called when the DBMS indicates an error.
b070f8a1 65 */
66
03c05291 67void dbmserr(void)
b070f8a1 68{
45bf7573 69 EXEC SQL BEGIN DECLARE SECTION;
70 char err_msg[256];
71 EXEC SQL END DECLARE SECTION;
03c05291 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);
b070f8a1 82}
83
45bf7573 84/* This is declarative, not executed. Applies from here on, in this file. */
03c05291 85EXEC SQL WHENEVER SQLERROR DO dbmserr();
048b1b94 86
03c05291 87int mr_open_database(void)
b070f8a1 88{
89 register int i;
03c05291 90 SQLDA *mr_alloc_sqlda();
b070f8a1 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
03c05291 100 mr_sqlda = mr_alloc_sqlda();
c9a214e4 101
b070f8a1 102 incremental_init();
103 flush_cache();
104 }
105
03c05291 106 dbms_errno = 0;
b070f8a1 107 mr_errcode = 0;
108
109 /* open the database */
03c05291 110 EXEC SQL CONNECT :database IDENTIFIED BY :database;
111
112 if(dbms_errno)
1741dca0 113 return(mr_errcode);
114
03c05291 115 EXEC SQL SELECT data_length INTO :mr_sig_length FROM user_tab_columns WHERE table_name='USERS' and column_name='SIGNATURE';
3e6ff9e4 116 EXEC SQL COMMIT WORK;
03c05291 117 if(dbms_errno)
1741dca0 118 return(mr_errcode);
119
120 return(MR_SUCCESS);
b070f8a1 121}
122
03c05291 123void mr_close_database(void)
b070f8a1 124{
125 flush_cache();
03c05291 126 EXEC SQL COMMIT RELEASE;
b070f8a1 127}
128
03c05291 129int mr_check_access(cl, name, argc, argv_ro)
130 client *cl;
131 char *name, *argv_ro[];
132 int argc;
b070f8a1 133{
134 struct query *q;
b070f8a1 135
03c05291 136 dbms_errno = 0;
b070f8a1 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
048b1b94 143 return(mr_verify_query(cl, q, argc, argv_ro));
b070f8a1 144}
145
03c05291 146int mr_process_query(cl, name, argc, argv_ro, action, actarg)
147 client *cl;
148 char *name, *argv_ro[], *actarg;
149 int argc, (*action)();
b070f8a1 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;
048b1b94 158 EXEC SQL BEGIN DECLARE SECTION;
03c05291 159 char *table;
048b1b94 160 EXEC SQL END DECLARE SECTION;
b070f8a1 161 struct save_queue *sq;
b070f8a1 162
03c05291 163 dbms_errno = 0;
b070f8a1 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) {
048b1b94 203 status = validate_row(q, Argv, v);
b070f8a1 204 if (status != MR_EXISTS) break;
205 }
206
207 /* build "where" clause if needed */
208 if (q->qual) {
048b1b94 209 build_qual(q->qual, q->argc, Argv, qual);
b070f8a1 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();
960b073b 228 status = do_retrieve(q, pqual, psort, sq_save_args,
229 (char *)sq);
b070f8a1 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);
03c05291 262 table = table_name[q->rtable];
b070f8a1 263 if (strcmp(q->shortname, "sshi") && strcmp(q->shortname, "ssif")) {
048b1b94 264 EXEC SQL UPDATE tblstats
03c05291 265 SET updates = updates + 1, modtime = SYSDATE
266 WHERE table_name = :table;
b070f8a1 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
b070f8a1 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) {
1b247a36 297 sprintf(qual, "%s.%s = %s",q->rvar, v->object_id,
45bf7573 298 Argv[q->argc+q->vcnt]);
b070f8a1 299 incremental_after(q->rtable, qual, argv_ro);
300 } else
301 incremental_after(q->rtable, pqual, argv_ro);
302
03c05291 303 table = table_name[q->rtable];
048b1b94 304 EXEC SQL UPDATE tblstats
03c05291 305 SET appends = appends + 1, modtime = SYSDATE
306 WHERE table_name = :table;
b070f8a1 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);
03c05291 324 table = table_name[q->rtable];
048b1b94 325 incremental_before(q->rtable, qual, argv_ro);
b070f8a1 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);
048b1b94 330 EXEC SQL UPDATE tblstats
03c05291 331 SET deletes = deletes + 1, modtime = SYSDATE
332 WHERE table_name = :table;
b070f8a1 333 }
334
335 /* execute followup routine */
336 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
337 break;
338
339 }
340
341out:
03c05291 342 if (status == MR_SUCCESS && dbms_errno != 0) {
343 com_err(whoami, MR_INTERNAL, "Server didn't notice DBMS ERROR %d",
344 dbms_errno);
b070f8a1 345 status = mr_errcode;
346 }
347
048b1b94 348 if (q->type == RETRIEVE) {
048b1b94 349 EXEC SQL COMMIT WORK;
048b1b94 350 } else {
b070f8a1 351 if (status == MR_SUCCESS) {
048b1b94 352 EXEC SQL COMMIT WORK;
b070f8a1 353 if (journal) {
03c05291 354 char buf[1024];
b070f8a1 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 {
1a41acb7 373 cache_abort();
03c05291 374 EXEC SQL ROLLBACK WORK;
b070f8a1 375 incremental_flush();
376 }
377 }
1a41acb7 378 cache_commit(); /* commit following abort is safe */
b070f8a1 379
380 if (status != MR_SUCCESS && log_flags & LOG_RES)
381 com_err(whoami, status, " (Query failed)");
382 return(status);
383}
384
03c05291 385int build_qual(fmt_buf, argc, argv, qual)
386 char *fmt_buf, *argv[], *qual;
387 int argc;
b070f8a1 388{
03c05291 389 char *res, *fmt;
b070f8a1 390
03c05291 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 */
b070f8a1 416 }
03c05291 417 *res='\0';
b070f8a1 418}
419
03c05291 420char *build_sort(v, sort)
421 struct validate *v;
422 char *sort;
b070f8a1 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) {
048b1b94 434 sprintf(elem, "%d", vo->index + 1); /* Result column number */
b070f8a1 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
f1bc925a 447int privileged;
448
03c05291 449int mr_verify_query(cl, q, argc, argv_ro)
450 client *cl;
451 struct query *q;
452 int argc;
453 char *argv_ro[];
b070f8a1 454{
455 register int argreq;
456 register int status;
457 register struct validate *v = q->validate;
458 register int i;
99e09b48 459 register char *to,*fr,*stop;
b070f8a1 460
f1bc925a 461 privileged = 0;
462
b4530d60 463 /* check argument count */
464 argreq = q->argc;
465 if (q->type == UPDATE || q->type == APPEND) argreq += q->vcnt;
466 if (argc != argreq) return(MR_ARGS);
467
b070f8a1 468 /* copy the arguments into a local argv that we can modify */
b070f8a1 469 for (i = 0; i < argc; i++) {
03c05291 470 for (to=Argv[i], fr=argv_ro[i], stop=to+ARGLEN; (*fr) && (to<stop);)
471 *to++ = *fr++;
472
99e09b48 473 if (*fr)
474 return(MR_ARG_TOO_LONG);
475 *to='\0';
476
477 if (*--to == '\\')
b070f8a1 478 return(MR_BAD_CHAR);
479 }
480
481 /* check initial query access */
482 status = check_query_access(q, Argv, cl);
483 if (status != MR_SUCCESS && status != MR_PERM)
484 return(status);
485 if (status == MR_SUCCESS)
486 privileged++;
487
b070f8a1 488 /* validate arguments */
489 if (v && v->valobj) {
490 status = validate_fields(q, Argv, v->valobj, v->objcnt);
491 if (status != MR_SUCCESS) return(status);
492 }
493
494 /* perform special query access check */
495 if (!privileged && v && v->acs_rtn) {
496 status = (*v->acs_rtn)(q, Argv, cl);
497 if (status != MR_SUCCESS && status != MR_PERM)
498 return(status);
499 if (status == MR_SUCCESS)
f1bc925a 500 return(MR_SUCCESS);
b070f8a1 501 }
502
503 return(privileged ? MR_SUCCESS : MR_PERM);
504}
505
506
507/* This routine caches info from the database. Each query acl is stored
508 * in the query structure, and whether that acl contains everybody.
509 */
510
03c05291 511int check_query_access(q, argv, cl)
512 struct query *q;
513 char *argv[];
514 client *cl;
048b1b94 515{
516 EXEC SQL BEGIN DECLARE SECTION;
517 char *name;
518 int acl_id;
048b1b94 519 static int def_uid;
520 EXEC SQL END DECLARE SECTION;
b070f8a1 521
522 /* initialize default uid */
523 if (def_uid == 0) {
048b1b94 524 EXEC SQL SELECT users_id INTO :def_uid FROM users WHERE login='default';
b070f8a1 525 }
526
527 /* get query access control list */
528 if (q->acl != 0)
529 acl_id = q->acl;
530 else {
531 name = q->shortname;
048b1b94 532 EXEC SQL SELECT list_id INTO :acl_id FROM capacls WHERE tag = :name;
03c05291 533 if (sqlca.sqlcode < 0) return(MR_DBMS_ERR);
534 if (sqlca.sqlcode == SQL_NO_MATCH) return(MR_PERM);
b070f8a1 535 q->acl = acl_id;
536
537 /* check for default access */
048b1b94 538 EXEC SQL SELECT member_id INTO :acl_id FROM imembers
539 WHERE list_id = :acl_id AND member_type = 'USER'
540 AND member_id = :def_uid;
541 if (sqlca.sqlerrd[2] == 0)
542 q->everybody = 0;
543 else
544 q->everybody = 1;
b070f8a1 545 }
546
547 if (q->everybody)
548 return(MR_SUCCESS);
549
f3c08a60 550 if (find_member("LIST", acl_id, cl))
b070f8a1 551 return(MR_SUCCESS);
552 else
553 return(MR_PERM);
048b1b94 554}
b070f8a1 555
556
f3c08a60 557int find_member(list_type, list_id, cl)
558 char *list_type;
559 int list_id;
03c05291 560 client *cl;
048b1b94 561{
562 EXEC SQL BEGIN DECLARE SECTION;
f3c08a60 563 int flag, users_id, client_id;
048b1b94 564 EXEC SQL END DECLARE SECTION;
b070f8a1 565
f3c08a60 566 if (!strcmp(strtrim(list_type), "USER") &&
567 list_id == cl->users_id)
568 return(1);
569
570 if (!strcmp(strtrim(list_type), "KERBEROS") &&
571 list_id == -cl->client_id)
b070f8a1 572 return(1);
573
f3c08a60 574 /* see if client is a member of list */
048b1b94 575 flag = 0;
f3c08a60 576 users_id = cl->users_id;
577 client_id = -cl->client_id;
2a467737 578 EXEC SQL SELECT COUNT(member_id) INTO :flag FROM imembers
579 WHERE list_id = :list_id
f3c08a60 580 AND ( ( member_type = 'USER' AND member_id = :users_id )
581 OR (member_type = 'KERBEROS' AND member_id = :client_id ) );
048b1b94 582 if (sqlca.sqlcode == 0)
583 return(flag);
584 return(0);
585}
b070f8a1 586
587
03c05291 588int do_retrieve(q, pqual, psort, action, actarg)
589 struct query *q;
590 char *pqual, *psort, *actarg;
591 int (*action)();
048b1b94 592{
03c05291 593 build_sql_stmt(stmt_buf,"SELECT",q->tlist,NULL,pqual);
594 if(psort) {
595 strcat(stmt_buf," ORDER BY ");
596 strcat(stmt_buf,psort);
b070f8a1 597 }
598
960b073b 599 return do_for_all_rows(stmt_buf, q->vcnt, action, (int)actarg);
048b1b94 600}
601
03c05291 602char *sqlstrstr(str, pat)
603 char *str, *pat;
cc0088db 604{
605 register char *p=pat;
606
607 do {
608 if(*str=='\'') { /* Skip over single-quote delimited substrings */
609 while(*++str && (*str!='\''))
610 ;
611 continue;
612 }
613 if(*str==*p) {
614 register char *s;
615 s=str;
616 while(*++p && (*++s==*p))
617 ;
618 if(*p) p=pat; /* failed */
619 }
620 } while(*p && *++str);
621
622 if(!*str) str=NULL;
623 return(str);
624}
625
626void optimize_sql_stmt(buf)
03c05291 627 char *buf;
cc0088db 628{
629 char *point=buf, *pat, *eopat, *esc1, *esc2, *csr;
630
631 for(point=buf; point=sqlstrstr(point,"LIKE"); point++) {
632 /* Now pointing to string "LIKE" */
633
634 /* Look at next word */
635 for(pat=point+4; *pat==' '; pat++) ;
636
637 /* Is it a single-quote delimited string? */
638 if(*pat!='\'') continue;
639
640 /* look for "escape" clause - save escape character */
641 /* 1. Find end of pattern */
642 for(eopat=pat+1; 1; eopat++) {
643 if(*eopat=='\'') {
644 if(eopat[1]=='\'') /* single-quote is self-escaping */
645 eopat++;
646 else
647 break;
648 }
649 }
650
651 /* 2. Look at next word */
652 for(esc1=eopat; *++esc1==' ';) ;
653
654 /* 3. esc1=0 if not "ESCAPE '?'", where the ? may be any character. */
655 if(strncmp(esc1,"ESCAPE",6)) esc1=NULL;
656
657 if(esc1) {
658 for(esc2=esc1+6; *esc2==' '; esc2++) ;
659
660 if(*esc2++!='\'') continue; /* Bad SQL syntax. Skip. */
661 /* esc2 now points at the escape character itself */
662 if(esc2[1]!='\'') continue; /* Weird escape string. Skip. */
663 } else {
664 esc2="\\";
665 }
666
667 /* Is pattern free from special characters? */
668 for(csr=pat; csr<eopat; csr++)
669 if((*csr=='%') || (*csr=='_') || (*csr==*esc2)) break;
670 if(csr!=eopat) continue; /* Uses pattern matching. Skip. */
671
672 /* Optimize the query statement */
673 /* 1. Change "LIKE" to " = " */
674 memcpy(point," = ",4);
675
676 /* 2. Change "ESCAPE" to " " */
677 if(esc1) {
678 memset(esc1,' ',6);
679 /* 3. Change "'*'" to " " */
680 /* (Changes '''' to " ") */
681 if(esc2) memset(esc2-1,' ',(*esc2=='\'')?4:3);
682 }
683 }
684}
685
03c05291 686void build_sql_stmt(result_buf, cmd, targetlist, argv, qual)
687 char *result_buf, *cmd, *targetlist, *argv[], *qual;
048b1b94 688{
689 char fmt_buf[MR_STMTBUF_LEN];
99e09b48 690 register char *res, *fmt;
691
692 if(qual)
693 sprintf(fmt_buf,"%s %s WHERE %s",cmd,targetlist,qual);
694 else
695 sprintf(fmt_buf,"%s %s",cmd,targetlist);
696
697 for(res=result_buf, fmt=fmt_buf; *fmt; fmt++) {
698 if(*fmt=='%') {
699 if(*++fmt) {
700 switch(*fmt) {
701 case '%': /* %% -> % */
702 *res++ = *fmt;
703 break;
704 case 's':
705 if(*argv[0]) {
03c05291 706 char *p=*argv;
707 while(*p) {
708 if(*p=='\'') *res++='\''; /* double the ' */
709 *res++=*p++;
710 }
99e09b48 711 }
712 argv++;
713 break;
714 case 'd':
03c05291 715 res+=sprintf(res,"%d",*(int *)*argv++);
99e09b48 716 break;
717 default: /* Swallow other %? pairs */
718 break;
719 }
720 } else break;
721 } else *res++ = *fmt; /* text -> result buffer */
048b1b94 722 }
723 *res='\0';
cc0088db 724
725 optimize_sql_stmt(result_buf);
048b1b94 726}
b070f8a1 727
03c05291 728int do_update(q, argv, qual, action, actarg)
729 struct query *q;
730 char *argv[], *qual, *actarg;
731 int (*action)();
048b1b94 732{
733 build_sql_stmt(stmt_buf,"UPDATE",q->tlist,argv,qual);
c9a214e4 734 EXEC SQL EXECUTE IMMEDIATE :stmt_buf;
b070f8a1 735 if (mr_errcode) return(mr_errcode);
736 return(MR_SUCCESS);
048b1b94 737}
b070f8a1 738
03c05291 739int do_append(q, argv, pqual, action, actarg)
740 struct query *q;
741 char *argv[], *pqual, *actarg;
742 int (*action)();
048b1b94 743{
744 build_sql_stmt(stmt_buf,"INSERT",q->tlist,argv,pqual);
c9a214e4 745 EXEC SQL EXECUTE IMMEDIATE :stmt_buf;
b070f8a1 746 if (mr_errcode) return(mr_errcode);
747 return(MR_SUCCESS);
048b1b94 748}
b070f8a1 749
03c05291 750int do_delete(q, qual, action, actarg)
751 struct query *q;
752 char *qual, *actarg;
753 int (*action)();
048b1b94 754{
03c05291 755 sprintf(stmt_buf,"DELETE FROM %s WHERE %s",table_name[q->rtable],qual);
048b1b94 756 EXEC SQL EXECUTE IMMEDIATE :stmt_buf;
b070f8a1 757 if (mr_errcode) return(mr_errcode);
758 return(MR_SUCCESS);
048b1b94 759}
b070f8a1 760
761
762/**
763 ** set_next_object_id - set next object id in values table
764 **
765 ** Inputs: object - object name in values table and in objects
766 ** table - name of table objects are found in
1a41acb7 767 ** limit - should the ID be range limited
b070f8a1 768 **
769 ** - called before an APPEND operation to set the next object id to
770 ** be used for the new record to the next free value
771 **
772 **/
773
03c05291 774int set_next_object_id(object, table, limit)
775EXEC SQL BEGIN DECLARE SECTION;
776 char *object;
777EXEC SQL END DECLARE SECTION;
778 enum tables table;
779 int limit;
048b1b94 780{
781 EXEC SQL BEGIN DECLARE SECTION;
c9a214e4 782 int value;
048b1b94 783 EXEC SQL END DECLARE SECTION;
45bf7573 784 int starting_value;
785
048b1b94 786 EXEC SQL SELECT value INTO :value FROM numvalues WHERE name = :object;
787 if (sqlca.sqlerrd[2] != 1)
1a41acb7 788 return(MR_NO_ID);
789
45bf7573 790 starting_value=value;
c9a214e4 791 while (1) {
45bf7573 792 if (limit && value > MAX_ID_VALUE)
793 value = MIN_ID_VALUE;
794
03c05291 795 sprintf(stmt_buf,"SELECT %s FROM %s WHERE %s=%d",
796 object,table_name[table],object,value);
797 dosql(sqlbuffer);
45bf7573 798 if (sqlca.sqlcode < 0) return(mr_errcode);
03c05291 799 if (sqlca.sqlcode == SQL_NO_MATCH) break;
45bf7573 800
45bf7573 801 value++;
802 if (limit && value == starting_value) {
803 com_err(whoami,0,"All id values have been used");
804 return(MR_NO_ID);
805 }
b070f8a1 806 }
807
808 if (LOG_RES)
c9a214e4 809 com_err(whoami, 0, "setting ID %s to %d", object, value);
048b1b94 810 EXEC SQL UPDATE numvalues SET value = :value WHERE name = :object;
b070f8a1 811 return(MR_SUCCESS);
048b1b94 812}
b070f8a1 813
814
815/* Turn a kerberos name into the user's ID of the account that principal
816 * owns. Sets the kerberos ID and user ID.
817 */
818
819int set_krb_mapping(name, login, ok, kid, uid)
03c05291 820 char *name, *login;
821 int ok, *kid, *uid;
048b1b94 822{
823 EXEC SQL BEGIN DECLARE SECTION;
824 int u_id, k_id;
825 char *krbname;
826 EXEC SQL END DECLARE SECTION;
b070f8a1 827
828 krbname = name;
829 *kid = 0;
830 *uid = 0;
831
fbeb0263 832 EXEC SQL SELECT km.users_id, km.string_id INTO :u_id, :k_id
833 FROM krbmap km, strings str
834 WHERE km.string_id = str.string_id AND str.string = :krbname;
3e6ff9e4 835 EXEC SQL COMMIT WORK;
048b1b94 836
03c05291 837 if (dbms_errno) return(mr_errcode);
b070f8a1 838
048b1b94 839 if (sqlca.sqlerrd[2] == 1) { /* rowcount */
b070f8a1 840 *kid = -k_id;
841 *uid = u_id;
842 return(MR_SUCCESS);
843 }
844
03c05291 845 if (name_to_id(name, STRINGS_TABLE, &k_id) == MR_SUCCESS)
b070f8a1 846 *kid = -k_id;
847
048b1b94 848 if (!ok) {
b070f8a1 849 *uid = *kid;
850 return(MR_SUCCESS);
851 }
852
03c05291 853 if (name_to_id(login, USERS_TABLE, uid) != MR_SUCCESS)
b070f8a1 854 *uid = 0;
855
856 if (*kid == 0)
857 *kid = *uid;
03c05291 858 if (dbms_errno) return(mr_errcode);
b070f8a1 859 return(MR_SUCCESS);
048b1b94 860}
b070f8a1 861
862
863/* For now this just checks the argc's. It should also see that there
864 * are no duplicate names.
865 */
866
03c05291 867void sanity_check_queries(void)
b070f8a1 868{
869 register int i;
870 int maxv = 0, maxa = 0;
b070f8a1 871 extern int QueryCount2;
872 extern struct query Queries2[];
b070f8a1 873
874#define MAX(x,y) ((x) > (y) ? (x) : (y))
875
b070f8a1 876 for (i = 0; i < QueryCount2; i++) {
877 maxv = MAX(maxv, Queries2[i].vcnt);
878 maxa = MAX(maxa, Queries2[i].argc);
879 }
880 if (MAX(maxv, maxa) > QMAXARGS) {
881 com_err(whoami, 0, "A query has more args than QMAXARGS");
882 exit(1);
883 }
884}
048b1b94 885
03c05291 886
887/* Generically do a SELECT, storing the results in the provided buffers */
888
889void dosql(buffers)
890 char *buffers[];
891{
892 int i, errcode=0, errlen;
893
894 EXEC SQL PREPARE inc_stmt FROM :stmt_buf;
895 if(sqlca.sqlcode) return;
896 EXEC SQL DECLARE inc_crs CURSOR FOR inc_stmt;
897 EXEC SQL OPEN inc_crs;
898 mr_sqlda->N = QMAXARGS;
899 EXEC SQL DESCRIBE SELECT LIST FOR inc_stmt INTO mr_sqlda;
900 mr_sqlda->N = mr_sqlda->F;
901 for(i=0; i<mr_sqlda->N; i++) {
902 mr_sqlda->V[i]=buffers[i];
903 mr_sqlda->T[i]=97;
904 mr_sqlda->L[i]=ARGLEN;
905 }
906 EXEC SQL FETCH inc_crs USING DESCRIPTOR mr_sqlda;
907
908 /* if we got an error from the FETCH, we have to preserve it or the
909 close will reset it and the caller will think nothing happened */
910 if(sqlca.sqlcode) {
911 errcode=sqlca.sqlcode;
912 errlen=sqlca.sqlerrm.sqlerrml;
913 }
914
915 EXEC SQL CLOSE inc_crs;
916 if(errcode) {
917 sqlca.sqlcode=errcode;
918 sqlca.sqlerrm.sqlerrml=errlen;
919 }
920}
921
922int do_for_all_rows(query, count, action, actarg)
923EXEC SQL BEGIN DECLARE SECTION;
924 char *query;
925EXEC SQL END DECLARE SECTION;
926 int count, (*action)(), actarg;
927{
928 int i, rowcount=0;
929
930 EXEC SQL PREPARE stmt FROM :query;
931 if(sqlca.sqlcode) return;
932 EXEC SQL DECLARE curs CURSOR FOR stmt;
933 EXEC SQL OPEN curs;
934 mr_sqlda->N = count;
935 EXEC SQL DESCRIBE SELECT LIST FOR stmt INTO mr_sqlda;
936 mr_sqlda->N = mr_sqlda->F;
937 for(i=0; i<mr_sqlda->N; i++) {
938 mr_sqlda->V[i]=sqlbuffer[i];
939 mr_sqlda->T[i]=97;
940 mr_sqlda->L[i]=ARGLEN;
941 }
942
943 while(1) {
944 EXEC SQL FETCH curs USING DESCRIPTOR mr_sqlda;
945 if(sqlca.sqlcode != 0) break;
946 (*action)(count, sqlbuffer, actarg);
947 rowcount++;
948 }
949 EXEC SQL CLOSE curs;
950
951 if (mr_errcode) return(mr_errcode);
952 return ((rowcount == 0) ? MR_NO_MATCH : MR_SUCCESS);
953}
954
955
048b1b94 956/* eof:qrtn.dc */
This page took 0.212052 seconds and 5 git commands to generate.