]> andersk Git - moira.git/blob - lib/mr_query.c
MR_INGRES_ERR -> MR_DBMS_ERR
[moira.git] / lib / mr_query.c
1 /*
2  *      $Source$
3  *      $Author$
4  *      $Header$
5  *
6  *      Copyright (C) 1987 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_sms_query_c = "$Header$";
14 #endif lint
15
16 #include <mit-copyright.h>
17 #include "mr_private.h"
18 #include <string.h>
19
20 /*
21  * This routine is the primary external interface to the mr library.
22  *
23  * It builds a new argument vector with the query handle prepended,
24  * and calls mr_query_internal.
25  */
26 int level = 0;
27
28 int mr_query(name, argc, argv, callproc, callarg)
29     char *name;         /* Query name */
30     int argc;           /* Arg count */
31     char **argv;                /* Args */
32     int (*callproc)();  /* Callback procedure */
33     char *callarg;              /* Callback argument */
34 {
35     register char **nargv = (char **)malloc(sizeof(char *) * (argc+1));
36     register int status = 0;
37     nargv[0] = name;
38     memcpy((char *)(nargv+1), (char *)argv, sizeof(char *) * argc);
39     status = mr_query_internal(argc+1, nargv, callproc, callarg);
40     free(nargv);
41     return status;
42 }
43 /*
44  * This routine makes an MR query.
45  *
46  * argv[0] is the query name.
47  * argv[1..argc-1] are the query arguments.
48  *
49  * callproc is called once for each returned value, with arguments
50  * argc, argv, and callarg.
51  * If it returns a non-zero value, further calls to it are not done, and
52  * all future data from the server is ignored (there should be some
53  * way to send it a quench..)
54  */
55
56 int mr_query_internal(argc, argv, callproc, callarg)
57     int argc;           /* Arg count */
58     char **argv;                /* Args */
59     int (*callproc)();  /* Callback procedure */
60     char *callarg;              /* Callback argument */
61 {
62     int status;
63     mr_params params_st;
64     register mr_params *params = NULL;
65     mr_params *reply = NULL;
66     int stopcallbacks = 0;
67
68     if (level) return MR_QUERY_NOT_REENTRANT;
69     
70     CHECK_CONNECTED;
71     level++;
72
73     params = &params_st; 
74     params->mr_version_no = sending_version_no;
75     params->mr_procno = MR_QUERY;
76     params->mr_argc = argc;
77     params->mr_argl = NULL;
78     params->mr_argv = argv;
79         
80     if ((status = mr_do_call(params, &reply)))
81         goto punt;
82
83     while ((status = reply->mr_status) == MR_MORE_DATA) {
84         if (!stopcallbacks) 
85             stopcallbacks =
86                 (*callproc)(reply->mr_argc, reply->mr_argv, callarg);
87         mr_destroy_reply(reply);
88         reply = NULL;
89
90         initialize_operation(_mr_recv_op, mr_start_recv, &reply,
91                              (int (*)())NULL);
92         queue_operation(_mr_conn, CON_INPUT, _mr_recv_op);
93
94         mr_complete_operation(_mr_recv_op);
95         if (OP_STATUS(_mr_recv_op) != OP_COMPLETE) {
96             mr_disconnect();
97             status = MR_ABORTED;
98             goto punt_1;
99         }
100     }   
101 punt:
102     mr_destroy_reply(reply);
103 punt_1:
104     level--;
105     return status;
106 }
This page took 0.042357 seconds and 5 git commands to generate.