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