home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
dbutil.zip
/
BUFOP.ZIP
/
OPTIONS.CPP
< prev
next >
Wrap
Text File
|
1993-11-17
|
8KB
|
240 lines
#include <sqlaprep.h>
#include <fstream.h>
#include <stdlib.h>
#include "compsql.hpp"
#include <Idate.hpp>
#include <itime.hpp>
#include <istring.hpp>
#include <sqlaprep.h>
#include <sql.h>
ofstream monitor; // monitor output file name from compsql.h
extern sqlca mysqlca;
sqla_program_id progID; // used eternal by gensql
/*****************************************************************
* FUNCTION Options
* Initialize the database and set up the options for precompiler
* this function goes through the input parameters and gets the
* user specifed options.
* then initialzies the precompiler services
* and generates a program ID into the new source file
******************************************************************/
sqla_options * optionArray;
static ITime startTime;
void startBanner(IString const & progname) // paint a banner
{IDate compileDate;
startTime.now();
compileDate.today();
monitor << "C set++ precompiler for IBM DB2 /2 \n";
monitor << " compiling: ";
monitor << progname << " ";
monitor << compileDate;
monitor << " ";
monitor << startTime;
}
void endBanner(void) // paint ending banner
{ ITime endTime,elapsedTime;
unsigned short termOption = SQLA_SAVE;
sqlafini(&termOption,NULL,&mysqlca);
endTime.now();
elapsedTime = endTime - startTime;
monitor << "\n\n Elapsed time: " << elapsedTime << "\n";
if (mysqlca.sqlwarn[6] == 1) {
monitor << "access program created \n";
} else {
monitor << "WARNING - ACCESS PROGRAM NOT CREATED \n";
} /* endif */
if (mysqlca.sqlwarn[7] == 1) {
monitor << "bind program created\n";
} else {
monitor << "WARNING - BIND PROGRAM NOT CREATED\n";
}
monitor << "precompiler ending " << endTime <<"\n";
monitor.close();
}
void setAnOption(int OptionType, int OptionValue)
// set an option in the option array
{int used;
used = optionArray->header.used; // next unused option
optionArray->header.used++;
optionArray->option[used].type = OptionType;
optionArray->option[used].val = OptionValue;
}
Boolean setOptions(IString & PrgName, int argCnt, char * argV[]) // return prm name
{ IString dbName;
IString anOption;
IString PrgBase;
IString bindName = "";
IString packageName = "";
IString monitorFileName;
IString word;
int blocking;
int isolation;
//
// the precompiler requires at least three options on initialization
// these are set to the default values here but are over ridden in the
// event the user specifies another value
//
int format = SQLA_POA_DEF; // required
int package= SQLA_CREATE_PLAN; // required
int createBindFile = SQLA_CREATE_BIND_FILE; // required
// optionArray = calloc(SQLA_NUM_OPTIONS(10)); // allocate options
optionArray = (sqla_options * ) calloc((sizeof(sqla_options::sqla_options_header) +
(10)*sizeof(sqla_options::sqla_option)),1);
optionArray->header.allocated = 10;
char optimize;
PrgName = argV[1]; // this should be input prog name
PrgBase = PrgName;
if (PrgBase.includes('.')) {
PrgBase = PrgBase.remove(PrgBase.indexOf('.'),4);
} /* endif */
monitorFileName = PrgBase + ".pre"; // default name of monitor output
dbName = argV[2];
// for each argument passed - decode type type
// first two options are prog name and database name
for (int J=3;J<argCnt ;J++ ) {
anOption = argV[J]; // get the option and decode
anOption = anOption.upperCase();
anOption = anOption.strip('-'); // allow either - or /
anOption = anOption.strip('/');
anOption = anOption.change('=',' '); // make two words
// option name & filename
switch (anOption[0]) {
case 'B': word = anOption.word(2);
if (word.length() == 0) { // use default
bindName = PrgBase; // of program name
} else {
bindName = word; // use the one provided
} /* endif */
break;
case 'F': if (anOption.includes("USA")) {
format = SQLA_POA_USA;
} else {
if (anOption.includes("EUR")) {
format = SQLA_POA_EUR;
} else {
if (anOption.includes("ISO")) {
format = SQLA_POA_ISO;
} else {
if (anOption.includes("JIS")) {
format = SQLA_POA_JIS;
} else {
if (anOption.includes("LOC")) {
format = SQLA_POA_LOC;
} /* endif */
} /* endif */
} /* endif */
} /* endif */
} /* endif */
break; // end of format
case 'I': if (anOption.includes("RR")){
isolation =SQLA_REPEATABLE_READ;
} else {
if (anOption.includes("CS")) {
isolation =SQL_CURSOR_STAB;
} else { isolation =SQL_UNCOM_READ;
} /* endif */
} /* endif */
setAnOption(SQLA_ISOLATION,blocking);
break; // end of isolation
case 'K': if (anOption.includes("ALL")) { //record blocking
blocking =SQL_BL_ALL;
} else {
if (anOption.includes("UNAMBIG")) {
blocking =SQL_BL_UNAMBIG;
} else {
blocking = SQL_NO_BL;
} /* endif */
} /* endif */
setAnOption(SQLA_BLOCK,blocking);
break;
case 'C': optimize = true;
break;
case 'M': if (anOption.includes("M=")) {
monitorFileName = anOption.remove("M=",1); // change default name
} /* endif */
break;
} /* endswitch */
} /* endfor */
// all input options done - set the three required options
// some strange things here - the documentation has no
// options to create or not create a plan
// put the precompiler MUST specify - so I default to create the plan
setAnOption(SQLA_FORMAT,format);
setAnOption(SQLA_ACCESS_PLAN,package);
setAnOption(SQLA_BIND_FILE,createBindFile);
//
// init the precompiler servies
//
monitor.open(monitorFileName); // open monitor before any errors
if (!monitor) {
cout << "could not open monitor file " << monitorFileName;
} else {
} /* endif */
startBanner(PrgName);
// paint banner to monitor
monitor.close(); // debug - make sure buffer written
monitor.open(monitorFileName,ios::app); // open and append to file
unsigned short PrgLen,dbLen,bindLen;
PrgLen = PrgBase.length();
dbLen = dbName.length();
bindLen = bindName.length();
sqlainit(&PrgLen,PrgBase,
&dbLen,dbName,
NULL,NULL, // spare - in case of flat?
&bindLen,bindName,
optionArray,
&progID,
NULL, // another spare - wow!
&mysqlca);
free(optionArray);
if (mysqlca.sqlcode < 0) {
SQLError(0,monitor,false);
endBanner();
return false;
} /* endif */
return true;
}