~ubuntu-branches/ubuntu/breezy/ncbi-tools6/breezy

« back to all changes in this revision

Viewing changes to network/medarch/server/sybintfc.c

  • Committer: Bazaar Package Importer
  • Author(s): Aaron M. Ucko
  • Date: 2002-04-04 22:13:09 UTC
  • Revision ID: james.westby@ubuntu.com-20020404221309-vfze028rfnlrldct
Tags: upstream-6.1.20011220a
ImportĀ upstreamĀ versionĀ 6.1.20011220a

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
        sybintfc.c              Execute a Sybase command using a
 
3
                                printf-style format for command building
 
4
 
 
5
        MODULE: sybintf
 
6
        FILES:  sybintf.c (this one) and sybintf.h.
 
7
 
 
8
        This module contains functions which permit a printf-style format
 
9
        to be used for building and executing Sybase queries.  This
 
10
        avoids having the application have to piece the command together
 
11
        from a bunch of little pieces using multiple calls to dbcmd.
 
12
 
 
13
        Note:   This package was adapted for the Medline project from
 
14
                the same module in the GenInfo Backbone database project.
 
15
                Some minor changes had to be made to conform to Medline
 
16
                style conventions.
 
17
 
 
18
        Dependencies:
 
19
 
 
20
            voutf.c     A package supporting printf-style formats with
 
21
                        output directed via function calls instead of
 
22
                        to a file or memory buffer.
 
23
 
 
24
        Edit history:
 
25
 
 
26
            25 July 1991 - Rand S. Huntzinger
 
27
                Moved to Medline project area and adapted the code to
 
28
                meet Medline project requirements.
 
29
 
 
30
            5 April 1991 - Added a print query debugging option.
 
31
 
 
32
        Original Implementation: 7 Dec 89 - Rand S. Huntzinger.
 
33
*
 
34
*
 
35
* RCS Modification History:
 
36
* $Log: sybintfc.c,v $
 
37
* Revision 6.0  1997/08/25 18:37:05  madden
 
38
* Revision changed to 6.0
 
39
*
 
40
* Revision 1.2  1995/05/17 17:56:08  epstein
 
41
* add RCS log revision history
 
42
*
 
43
*/
 
44
 
 
45
#include <stdio.h>
 
46
#include <ctype.h>
 
47
#include "sybintfc.h"
 
48
#include "voutf.h"
 
49
 
 
50
 
 
51
/* These global variables are used for debugging.  You can set them to cause
 
52
   queries to be written to a file.  */
 
53
 
 
54
FILE    *SybaseQueryPrintFile = stdout;         /* Default = standard output */
 
55
int      SybaseQueryPrintCount = 0;             /* Default, don't print! */
 
56
 
 
57
 
 
58
/* These variables are used internally by the voutf processing procedure */
 
59
 
 
60
static  DBPROCESS       *dbproc;
 
61
static  RETCODE          status;
 
62
 
 
63
/*
 
64
        build_sybase_command            Voutf string collection procedure.
 
65
 
 
66
        This procedure collects the little strings generated by the
 
67
        voutf function in interpreting it's format and directs them
 
68
        into the dbcmd() procedure for insertion into the Sybase
 
69
        command.
 
70
 
 
71
        Assumptions:
 
72
 
 
73
          * The calling program has set the static variable "dbproc"
 
74
            to reference the DBPROCESS to be used for building the
 
75
            command.
 
76
 
 
77
          * The static variable "status" will return the status of the
 
78
            last call to this procedure when voutf exits.  If voutf
 
79
            exits with an error, this will contain the error status
 
80
            returned by dbcmd().
 
81
 
 
82
        Parameters: (defined by voutf protocol)
 
83
 
 
84
            s           The string coming in from voutf().
 
85
 
 
86
        Returns: (defined by voutf protocol)
 
87
 
 
88
            TRUE (non-zero) if the command failed,
 
89
            FALSE (zero) if the command was successful.
 
90
*/
 
91
 
 
92
/*
 
93
*       intbuild_sybase_command(chars)
 
94
*/
 
95
static int build_sybase_command( char *s )
 
96
{
 
97
    /* Add the string to the command */
 
98
 
 
99
    return( (status = dbcmd( dbproc, s )) != SUCCEED );
 
100
}
 
101
 
 
102
 
 
103
/*
 
104
*               DumpSybaseCommand(DBPROCESSdbproc,FILEfd)
 
105
*/
 
106
void    DumpSybaseCommand( DBPROCESS *dbproc, FILE *fd )
 
107
{
 
108
        char     buffer[61];
 
109
        int      len = dbstrlen( dbproc );
 
110
        char    *stub = "Query:";
 
111
        int      index = 0;
 
112
        int      copy_count;
 
113
        register char *p;
 
114
 
 
115
        /* Dump the query out to SybaseQueryPrintFile */
 
116
 
 
117
        while( index < len )
 
118
        {
 
119
            copy_count = (( len - index ) > (sizeof(buffer) - 1))
 
120
                ? (sizeof(buffer) - 1) : (len - index);
 
121
            dbstrcpy( dbproc, index, copy_count , buffer );
 
122
            for(p=buffer; *p; p++) if(isspace(*p)) *p = ' ';
 
123
            fprintf(fd, "%-12.12s || %s ||\n", stub, buffer);
 
124
            stub = "";
 
125
            index += copy_count;
 
126
        }
 
127
        fprintf(fd, "\n" );
 
128
}
 
129
 
 
130
 
 
131
/*
 
132
        VExtendSybaseCommand    Add to a Sybase command from a vector
 
133
 
 
134
        This procedure extends the Sybase command buffer using a format and
 
135
        a argument list pointer (usually passed by a variable argument
 
136
        list calling procedure).
 
137
 
 
138
        Parameters:
 
139
 
 
140
            db          The Sybase database process to be used.
 
141
 
 
142
            fmt         A printf-style format for generating this
 
143
                        piece of the command.
 
144
 
 
145
            args        An argument vector containing the arguments
 
146
                        which are to be interpreted according to the
 
147
                        format to generate this piece of the command
 
148
                        string.
 
149
 
 
150
        Returns:
 
151
 
 
152
            Zero if successful, non-zero on an error.
 
153
*/
 
154
 
 
155
/*
 
156
*               VExtendSybaseCommand(DBPROCESSdb,charfmt,voidargs)
 
157
*/
 
158
RETCODE VExtendSybaseCommand( DBPROCESS *db, char *fmt, void *args )
 
159
{
 
160
    /* Load the command into the command buffer */
 
161
 
 
162
    dbproc = db;
 
163
    (void) voutf( build_sybase_command, fmt, args );
 
164
 
 
165
    /* Done */
 
166
 
 
167
    return( status );
 
168
}
 
169
 
 
170
/*
 
171
        VLoadSybaseCommand      Load a Sybase command from a vector
 
172
 
 
173
        This procedure loads the Sybase command buffer using a format and
 
174
        a argument list pointer (usually passed by a variable argument
 
175
        list calling procedure).  This is identical to the
 
176
        VExtendSybaseCommand procedure except any existing Sybase command
 
177
        is cleared out before loading the new text.
 
178
 
 
179
        Parameters:
 
180
 
 
181
            db          The Sybase database process to be used.
 
182
 
 
183
            fmt         A printf-style format for generating this
 
184
                        piece of the command.
 
185
 
 
186
            args        An argument vector containing the arguments
 
187
                        which are to be interpreted according to the
 
188
                        format to generate this piece of the command
 
189
                        string.
 
190
 
 
191
        Returns:
 
192
 
 
193
            Zero if successful, non-zero on an error.
 
194
*/
 
195
 
 
196
/*
 
197
*               VLoadSybaseCommand(DBPROCESSdb,charfmt,voidargs)
 
198
*/
 
199
RETCODE VLoadSybaseCommand( DBPROCESS *db, char *fmt, void *args )
 
200
{
 
201
   RETCODE      status;
 
202
 
 
203
    /* Reset the database channel */
 
204
 
 
205
    dbproc = db;
 
206
    if( (status = dbcancel( dbproc )) != SUCCEED )
 
207
        return( status );
 
208
 
 
209
    /* Load the command into the command buffer */
 
210
 
 
211
    (void) voutf( build_sybase_command, fmt, args );
 
212
 
 
213
    /* Done */
 
214
 
 
215
    return( status );
 
216
}
 
217
 
 
218
/*
 
219
        LoadSybaseCommand       Load a Sybase command via a format.
 
220
 
 
221
        This procedure loads the Sybase command buffer using a variable
 
222
        argument list containing a format which is used to generate the
 
223
        command using printf-style conversions.
 
224
 
 
225
        Parameters:
 
226
 
 
227
            db          The Sybase database process to be used.
 
228
 
 
229
            fmt         A printf-style format for generating this
 
230
                        piece of the command.
 
231
 
 
232
            ...         The remaining arguments are interpreted according
 
233
                        to the format (fmt) using printf conventions to
 
234
                        generate the string to be loaded into the Sybase
 
235
                        command buffer.
 
236
 
 
237
        Returns:
 
238
 
 
239
            Zero if successful, non-zero on an error.
 
240
*/
 
241
 
 
242
/*
 
243
*               LoadSybaseCommand(DBPROCESSdbproc,charfmt,...)
 
244
*/
 
245
RETCODE LoadSybaseCommand( DBPROCESS *dbproc, char *fmt,  ... )
 
246
{
 
247
    va_list     args;
 
248
 
 
249
    /* Extract the fixed parameters */
 
250
 
 
251
    va_start( args, char *fmt );
 
252
 
 
253
    /* Put the command in the buffer */
 
254
 
 
255
    (void) VLoadSybaseCommand( dbproc, fmt, args );
 
256
    va_end( args );
 
257
 
 
258
    /* Done */
 
259
 
 
260
    return( status );
 
261
}
 
262
 
 
263
 
 
264
/*
 
265
        ExtendSybaseCommand     Add to a Sybase command via a format.
 
266
 
 
267
        This procedure extends the Sybase command buffer using a variable
 
268
        argument list containing a format which is used to generate the
 
269
        command using printf-style conversions.  This procedure is
 
270
        identical to LaodSybaseCommand execept that the string generated
 
271
        here is appended to the existing Sybase command buffer instead of
 
272
        replacing it.
 
273
 
 
274
        Parameters:
 
275
 
 
276
            db          The Sybase database process to be used.
 
277
 
 
278
            fmt         A printf-style format for generating this
 
279
                        piece of the command.
 
280
 
 
281
            ...         The remaining arguments are interpreted according
 
282
                        to the format (fmt) using printf conventions to
 
283
                        generate the string to be loaded into the Sybase
 
284
                        command buffer.
 
285
 
 
286
        Returns:
 
287
 
 
288
            Zero if successful, non-zero on an error.
 
289
*/
 
290
 
 
291
/*
 
292
*               ExtendSybaseCommand(DBPROCESSdbproc,charfmt,...)
 
293
*/
 
294
RETCODE ExtendSybaseCommand( DBPROCESS *dbproc, char * fmt, ... )
 
295
{
 
296
    va_list      args;
 
297
 
 
298
    /* Extract the fixed parameters */
 
299
 
 
300
    va_start( args, fmt );
 
301
    dbproc = va_arg( args, DBPROCESS * );
 
302
    fmt = va_arg( args, char * );
 
303
 
 
304
    /* Put the command in the buffer */
 
305
 
 
306
    (void) VExtendSybaseCommand( dbproc, fmt, args );
 
307
    va_end( args );
 
308
 
 
309
    /* Done */
 
310
 
 
311
    return( status );
 
312
}
 
313
 
 
314
 
 
315
/*
 
316
        ExecSybaseCommand       Execute an existing Sybase command
 
317
 
 
318
        This procedure executes the command in the Sybase command buffer
 
319
        and returns the results of the execution.
 
320
 
 
321
        Parameters:
 
322
 
 
323
            dbproc              The Sybase database process pointer.
 
324
 
 
325
        Returns:
 
326
 
 
327
            The return code of dbsqlexec() if it fails; otherwise,
 
328
            the return code of dbresults().  Basically, this should
 
329
            amount to SUCCEED if successful, or FAIL on an error.
 
330
*/
 
331
 
 
332
/*
 
333
*               ExecSybaseCommand(dbproc)
 
334
*/
 
335
RETCODE ExecSybaseCommand( dbproc )
 
336
        DBPROCESS       *dbproc;
 
337
{
 
338
    /* Execute the query */
 
339
 
 
340
    if((status = dbsqlexec( dbproc )) != SUCCEED)
 
341
        return( status );
 
342
 
 
343
    /* If the query succeeded, see if we want to print it out */
 
344
 
 
345
    if( SybaseQueryPrintCount != 0 ) {
 
346
        DumpSybaseCommand( dbproc, SybaseQueryPrintFile );
 
347
        if( SybaseQueryPrintCount > 0 ) SybaseQueryPrintCount--;
 
348
    }
 
349
 
 
350
    /* Load the results of the command */
 
351
 
 
352
    return( dbresults( dbproc ) );
 
353
}
 
354
 
 
355
 
 
356
/*
 
357
        RunSybase       Execute a Sybase command specified using a
 
358
                        printf-style format and variable argument list.
 
359
 
 
360
        This procedure permits a printf-style format and variable argument
 
361
        list to be used to build & execute a sybase command.  It performs
 
362
        the first three steps of the Sybase command execution process,
 
363
        1) command construction using dbcmd(), 2) execution using dbseqlexec
 
364
        and 3) the retrieval of results using dbresults().
 
365
 
 
366
        Parameters: (variable argument list)
 
367
 
 
368
            dbproc      A pointer to an open Sybase database process
 
369
                        record.  (ie. a channel to the database)
 
370
 
 
371
            fmt         The format string to be interpreted.
 
372
 
 
373
            ...         The remaining arguments are interpreted
 
374
                        according to the format given in the "fmt"
 
375
                        argument using printf conventions.
 
376
 
 
377
        Returns:
 
378
 
 
379
            The last return code returned from Sybase.  If successful,
 
380
            this should be SUCCEED; otherwise, it will be the status
 
381
            of the first step which failed.
 
382
*/
 
383
 
 
384
/*
 
385
*               RunSybase(DBPROCESSdbproc,charfmt,...)
 
386
*/
 
387
RETCODE RunSybase( DBPROCESS *dbproc, char *fmt, ... )
 
388
{
 
389
    va_list      args;
 
390
 
 
391
    /* Extract the fixed parameters */
 
392
 
 
393
    va_start( args, fmt );
 
394
 
 
395
    /* Load the command */
 
396
 
 
397
    (void) VLoadSybaseCommand( dbproc, fmt, args );
 
398
    va_end( args );
 
399
    if( status != SUCCEED ) return( status );
 
400
 
 
401
    /* Execute the Sybase command */
 
402
 
 
403
    return( ExecSybaseCommand( dbproc ) );
 
404
}
 
405
 
 
406
 
 
407
/*
 
408
        RunSybaseCheck  Count records returned by a Sybase command
 
409
 
 
410
        Executes a Sybase command and returns the number of records
 
411
        retrieved by the command.  The data is not available.
 
412
 
 
413
 
 
414
        Parameters: (variable argument list)
 
415
 
 
416
            dbproc      A pointer to an open Sybase database process
 
417
                        record.  (ie. a channel to the database)
 
418
 
 
419
            fmt         The format string to be interpreted.
 
420
 
 
421
            ...         The remaining arguments are interpreted
 
422
                        according to the format given in the "fmt"
 
423
                        argument using printf conventions.
 
424
 
 
425
        Returns:
 
426
 
 
427
            The number of rows retrieved or -1 on an error.
 
428
        
 
429
*/
 
430
 
 
431
/*
 
432
*               RunSybaseCheck(DBPROCESSdbproc,charfmt,...)
 
433
*/
 
434
int     RunSybaseCheck( DBPROCESS *dbproc, char *fmt, ... )
 
435
{
 
436
    va_list      args;
 
437
    int          count;
 
438
 
 
439
    /* Extract the fixed parameters */
 
440
 
 
441
    va_start( args, fmt );
 
442
    dbproc = va_arg( args, DBPROCESS * );
 
443
    fmt = va_arg( args, char * );
 
444
 
 
445
    /* Load the command */
 
446
 
 
447
    (void) VLoadSybaseCommand( dbproc, fmt, args );
 
448
    va_end( args );
 
449
    if( status != SUCCEED ) return( -1 );
 
450
 
 
451
    /* Execute the Sybase command */
 
452
 
 
453
    if( ExecSybaseCommand( dbproc ) != SUCCEED )
 
454
        return( -1 );
 
455
 
 
456
    count = DBROWS( dbproc );
 
457
    (void) dbcancel( dbproc );
 
458
 
 
459
    return( count );
 
460
}
 
461