home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dbutil.zip / BUFOP.ZIP / OPTIONS.CPP < prev    next >
Text File  |  1993-11-17  |  8KB  |  240 lines

  1. #include <sqlaprep.h>
  2. #include <fstream.h>
  3. #include <stdlib.h>
  4. #include "compsql.hpp"
  5. #include <Idate.hpp>
  6. #include <itime.hpp>
  7. #include <istring.hpp>
  8. #include <sqlaprep.h>
  9. #include <sql.h>
  10.  
  11.  
  12. ofstream monitor;     // monitor output file name from compsql.h
  13. extern sqlca mysqlca;
  14.  
  15.  
  16. sqla_program_id progID;  // used eternal by gensql
  17.           
  18. /*****************************************************************
  19. * FUNCTION Options
  20. * Initialize the database and set up the options for precompiler
  21. * this function goes through the input parameters and gets the
  22. * user specifed options.
  23. * then initialzies the precompiler services
  24. * and generates a program ID into the new source file
  25. ******************************************************************/
  26.  
  27. sqla_options * optionArray;
  28.  
  29.  static ITime startTime;
  30.  
  31. void startBanner(IString const & progname)            // paint a banner
  32.  {IDate compileDate;
  33.    startTime.now();
  34.    compileDate.today();
  35.    monitor << "C set++ precompiler for IBM DB2 /2 \n";
  36.    monitor << "  compiling: ";
  37.    monitor << progname << " ";
  38.    monitor << compileDate;
  39.    monitor << " ";
  40.    monitor << startTime;
  41.  }
  42.  
  43. void endBanner(void)            // paint ending banner
  44.  { ITime endTime,elapsedTime;
  45.    unsigned short termOption = SQLA_SAVE;
  46.    sqlafini(&termOption,NULL,&mysqlca);
  47.  
  48.    endTime.now();
  49.    elapsedTime = endTime - startTime;
  50.    monitor << "\n\n Elapsed time: " << elapsedTime << "\n";
  51.    if (mysqlca.sqlwarn[6] == 1) {
  52.       monitor << "access program created \n";
  53.    } else {
  54.       monitor << "WARNING - ACCESS PROGRAM NOT CREATED \n";
  55.    } /* endif */
  56.    if (mysqlca.sqlwarn[7] == 1) {
  57.       monitor << "bind program created\n";
  58.    } else {
  59.       monitor << "WARNING - BIND PROGRAM NOT CREATED\n";
  60.    }
  61.  
  62.    monitor << "precompiler ending " << endTime <<"\n";
  63.    monitor.close();
  64.  
  65. }
  66.  
  67. void setAnOption(int OptionType, int OptionValue)
  68. // set an option in the option array
  69. {int used;
  70.    used = optionArray->header.used;   // next unused option
  71.    optionArray->header.used++;
  72.  
  73.    optionArray->option[used].type = OptionType;
  74.    optionArray->option[used].val = OptionValue;
  75.  
  76. }
  77.  
  78. Boolean setOptions(IString & PrgName, int argCnt, char * argV[])  // return prm name
  79.  
  80.  
  81. { IString dbName;
  82.   IString anOption;
  83.   IString PrgBase;
  84.   IString bindName = "";
  85.   IString packageName = "";
  86.   IString monitorFileName;
  87.   IString word;
  88.   int blocking;
  89.   int isolation;
  90.  
  91. //
  92. //   the precompiler requires at least three options on initialization
  93. //   these are set to the default values here but are over ridden in the
  94. //   event the user specifies another value
  95. //
  96.   int format = SQLA_POA_DEF;       // required
  97.   int package= SQLA_CREATE_PLAN;        // required
  98.   int createBindFile = SQLA_CREATE_BIND_FILE; // required
  99.  
  100.  
  101. //   optionArray = calloc(SQLA_NUM_OPTIONS(10));  // allocate options
  102.  
  103.     optionArray = (sqla_options * ) calloc((sizeof(sqla_options::sqla_options_header) +
  104.                                            (10)*sizeof(sqla_options::sqla_option)),1);
  105.     optionArray->header.allocated = 10;
  106.                                                 
  107.  
  108.  
  109.  
  110.    char optimize;
  111.    PrgName = argV[1];                // this should be input prog name
  112.    PrgBase = PrgName;
  113.    if (PrgBase.includes('.')) {
  114.       PrgBase = PrgBase.remove(PrgBase.indexOf('.'),4);
  115.    } /* endif */
  116.    monitorFileName = PrgBase + ".pre";  // default name of monitor output
  117.    dbName = argV[2];
  118.  
  119.                 // for each argument passed - decode type type
  120.                 // first two options are prog name and database name
  121.  
  122.    for (int J=3;J<argCnt ;J++ ) {
  123.       anOption = argV[J];           // get the option and decode
  124.       anOption = anOption.upperCase();
  125.       anOption = anOption.strip('-');  // allow either - or /
  126.       anOption = anOption.strip('/');
  127.       anOption = anOption.change('=',' '); // make two words
  128.                                         // option name & filename
  129.  
  130.       switch (anOption[0]) {
  131.       case 'B': word = anOption.word(2);
  132.                 if (word.length() == 0) {  // use default
  133.                    bindName = PrgBase;    // of program name
  134.                 } else { 
  135.                    bindName = word;       // use the one provided
  136.                 } /* endif */
  137.          break;
  138.       case 'F': if (anOption.includes("USA")) {
  139.                    format = SQLA_POA_USA;
  140.                  } else {
  141.                     if (anOption.includes("EUR")) {
  142.                        format = SQLA_POA_EUR;
  143.                     } else {
  144.                     if (anOption.includes("ISO")) {
  145.                        format = SQLA_POA_ISO;
  146.                     } else {
  147.                     if (anOption.includes("JIS")) {
  148.                        format = SQLA_POA_JIS;
  149.                     } else {
  150.                     if (anOption.includes("LOC")) {
  151.                        format = SQLA_POA_LOC;
  152.                     } /* endif */
  153.                     } /* endif */
  154.                     } /* endif */
  155.                     } /* endif */
  156.                 } /* endif */
  157.  
  158.          break;     // end of format
  159.       case 'I': if (anOption.includes("RR")){
  160.                     isolation =SQLA_REPEATABLE_READ;
  161.                  } else {
  162.                  if (anOption.includes("CS")) {
  163.                     isolation =SQL_CURSOR_STAB;
  164.                  } else { isolation =SQL_UNCOM_READ;
  165.  
  166.                  } /* endif */
  167.                  } /* endif */
  168.                  setAnOption(SQLA_ISOLATION,blocking);
  169.          break; // end of isolation
  170.  
  171.       case 'K': if (anOption.includes("ALL")) {     //record blocking
  172.                     blocking =SQL_BL_ALL;
  173.                  } else {
  174.                  if (anOption.includes("UNAMBIG")) {
  175.                     blocking =SQL_BL_UNAMBIG;
  176.                  } else {
  177.                     blocking = SQL_NO_BL;
  178.                  } /* endif */
  179.                  } /* endif */
  180.                  setAnOption(SQLA_BLOCK,blocking);
  181.  
  182.          break;
  183.       case 'C':  optimize = true;
  184.          break;
  185.       case 'M': if (anOption.includes("M=")) {
  186.                   monitorFileName = anOption.remove("M=",1);  // change default name
  187.                   } /* endif */
  188.          break;
  189.       } /* endswitch */
  190.    } /* endfor */
  191.  
  192. //  all input options done - set the three required options
  193. //  some strange things here - the documentation has no
  194. //  options to create or not create a plan
  195. //  put the precompiler MUST specify - so I default to create the plan
  196.  
  197.     setAnOption(SQLA_FORMAT,format);
  198.     setAnOption(SQLA_ACCESS_PLAN,package);
  199.     setAnOption(SQLA_BIND_FILE,createBindFile);
  200.  
  201. //
  202. //  init the precompiler servies
  203. //
  204.     monitor.open(monitorFileName);      // open monitor before any errors
  205.     if (!monitor) {
  206.        cout << "could not open monitor file " << monitorFileName;
  207.     } else {
  208.     } /* endif */
  209.     startBanner(PrgName);            
  210.                // paint banner to monitor
  211.     monitor.close();                    // debug - make sure buffer written
  212.     monitor.open(monitorFileName,ios::app);  // open and append to file
  213.  
  214.  
  215.     unsigned short PrgLen,dbLen,bindLen;
  216.     PrgLen  = PrgBase.length();
  217.     dbLen = dbName.length();
  218.     bindLen = bindName.length();
  219.     sqlainit(&PrgLen,PrgBase,
  220.              &dbLen,dbName,
  221.              NULL,NULL,                       // spare - in case of flat?
  222.              &bindLen,bindName,
  223.              optionArray,
  224.              &progID,
  225.              NULL,                           // another spare - wow!
  226.              &mysqlca);
  227.  
  228.     free(optionArray);
  229.     if (mysqlca.sqlcode < 0) {
  230.        SQLError(0,monitor,false);
  231.        endBanner();
  232.        return false;
  233.  
  234.     } /* endif */
  235.     return true;
  236. }
  237.  
  238.  
  239.       
  240.