2
sybintfc.c Execute a Sybase command using a
3
printf-style format for command building
6
FILES: sybintf.c (this one) and sybintf.h.
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.
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
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.
26
25 July 1991 - Rand S. Huntzinger
27
Moved to Medline project area and adapted the code to
28
meet Medline project requirements.
30
5 April 1991 - Added a print query debugging option.
32
Original Implementation: 7 Dec 89 - Rand S. Huntzinger.
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
40
* Revision 1.2 1995/05/17 17:56:08 epstein
41
* add RCS log revision history
51
/* These global variables are used for debugging. You can set them to cause
52
queries to be written to a file. */
54
FILE *SybaseQueryPrintFile = stdout; /* Default = standard output */
55
int SybaseQueryPrintCount = 0; /* Default, don't print! */
58
/* These variables are used internally by the voutf processing procedure */
60
static DBPROCESS *dbproc;
61
static RETCODE status;
64
build_sybase_command Voutf string collection procedure.
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
73
* The calling program has set the static variable "dbproc"
74
to reference the DBPROCESS to be used for building the
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
82
Parameters: (defined by voutf protocol)
84
s The string coming in from voutf().
86
Returns: (defined by voutf protocol)
88
TRUE (non-zero) if the command failed,
89
FALSE (zero) if the command was successful.
93
* intbuild_sybase_command(chars)
95
static int build_sybase_command( char *s )
97
/* Add the string to the command */
99
return( (status = dbcmd( dbproc, s )) != SUCCEED );
104
* DumpSybaseCommand(DBPROCESSdbproc,FILEfd)
106
void DumpSybaseCommand( DBPROCESS *dbproc, FILE *fd )
109
int len = dbstrlen( dbproc );
110
char *stub = "Query:";
115
/* Dump the query out to SybaseQueryPrintFile */
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);
132
VExtendSybaseCommand Add to a Sybase command from a vector
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).
140
db The Sybase database process to be used.
142
fmt A printf-style format for generating this
143
piece of the command.
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
152
Zero if successful, non-zero on an error.
156
* VExtendSybaseCommand(DBPROCESSdb,charfmt,voidargs)
158
RETCODE VExtendSybaseCommand( DBPROCESS *db, char *fmt, void *args )
160
/* Load the command into the command buffer */
163
(void) voutf( build_sybase_command, fmt, args );
171
VLoadSybaseCommand Load a Sybase command from a vector
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.
181
db The Sybase database process to be used.
183
fmt A printf-style format for generating this
184
piece of the command.
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
193
Zero if successful, non-zero on an error.
197
* VLoadSybaseCommand(DBPROCESSdb,charfmt,voidargs)
199
RETCODE VLoadSybaseCommand( DBPROCESS *db, char *fmt, void *args )
203
/* Reset the database channel */
206
if( (status = dbcancel( dbproc )) != SUCCEED )
209
/* Load the command into the command buffer */
211
(void) voutf( build_sybase_command, fmt, args );
219
LoadSybaseCommand Load a Sybase command via a format.
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.
227
db The Sybase database process to be used.
229
fmt A printf-style format for generating this
230
piece of the command.
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
239
Zero if successful, non-zero on an error.
243
* LoadSybaseCommand(DBPROCESSdbproc,charfmt,...)
245
RETCODE LoadSybaseCommand( DBPROCESS *dbproc, char *fmt, ... )
249
/* Extract the fixed parameters */
251
va_start( args, char *fmt );
253
/* Put the command in the buffer */
255
(void) VLoadSybaseCommand( dbproc, fmt, args );
265
ExtendSybaseCommand Add to a Sybase command via a format.
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
276
db The Sybase database process to be used.
278
fmt A printf-style format for generating this
279
piece of the command.
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
288
Zero if successful, non-zero on an error.
292
* ExtendSybaseCommand(DBPROCESSdbproc,charfmt,...)
294
RETCODE ExtendSybaseCommand( DBPROCESS *dbproc, char * fmt, ... )
298
/* Extract the fixed parameters */
300
va_start( args, fmt );
301
dbproc = va_arg( args, DBPROCESS * );
302
fmt = va_arg( args, char * );
304
/* Put the command in the buffer */
306
(void) VExtendSybaseCommand( dbproc, fmt, args );
316
ExecSybaseCommand Execute an existing Sybase command
318
This procedure executes the command in the Sybase command buffer
319
and returns the results of the execution.
323
dbproc The Sybase database process pointer.
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.
333
* ExecSybaseCommand(dbproc)
335
RETCODE ExecSybaseCommand( dbproc )
338
/* Execute the query */
340
if((status = dbsqlexec( dbproc )) != SUCCEED)
343
/* If the query succeeded, see if we want to print it out */
345
if( SybaseQueryPrintCount != 0 ) {
346
DumpSybaseCommand( dbproc, SybaseQueryPrintFile );
347
if( SybaseQueryPrintCount > 0 ) SybaseQueryPrintCount--;
350
/* Load the results of the command */
352
return( dbresults( dbproc ) );
357
RunSybase Execute a Sybase command specified using a
358
printf-style format and variable argument list.
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().
366
Parameters: (variable argument list)
368
dbproc A pointer to an open Sybase database process
369
record. (ie. a channel to the database)
371
fmt The format string to be interpreted.
373
... The remaining arguments are interpreted
374
according to the format given in the "fmt"
375
argument using printf conventions.
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.
385
* RunSybase(DBPROCESSdbproc,charfmt,...)
387
RETCODE RunSybase( DBPROCESS *dbproc, char *fmt, ... )
391
/* Extract the fixed parameters */
393
va_start( args, fmt );
395
/* Load the command */
397
(void) VLoadSybaseCommand( dbproc, fmt, args );
399
if( status != SUCCEED ) return( status );
401
/* Execute the Sybase command */
403
return( ExecSybaseCommand( dbproc ) );
408
RunSybaseCheck Count records returned by a Sybase command
410
Executes a Sybase command and returns the number of records
411
retrieved by the command. The data is not available.
414
Parameters: (variable argument list)
416
dbproc A pointer to an open Sybase database process
417
record. (ie. a channel to the database)
419
fmt The format string to be interpreted.
421
... The remaining arguments are interpreted
422
according to the format given in the "fmt"
423
argument using printf conventions.
427
The number of rows retrieved or -1 on an error.
432
* RunSybaseCheck(DBPROCESSdbproc,charfmt,...)
434
int RunSybaseCheck( DBPROCESS *dbproc, char *fmt, ... )
439
/* Extract the fixed parameters */
441
va_start( args, fmt );
442
dbproc = va_arg( args, DBPROCESS * );
443
fmt = va_arg( args, char * );
445
/* Load the command */
447
(void) VLoadSybaseCommand( dbproc, fmt, args );
449
if( status != SUCCEED ) return( -1 );
451
/* Execute the Sybase command */
453
if( ExecSybaseCommand( dbproc ) != SUCCEED )
456
count = DBROWS( dbproc );
457
(void) dbcancel( dbproc );