home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / bufop.zip / TSTA.CPP < prev    next >
C/C++ Source or Header  |  1993-09-11  |  15KB  |  519 lines

  1. // 
  2. // processed by precompiler  09-11-93  09:32:24
  3. // 
  4. /*****************************************************************/
  5. /* N O T I C E                                                   */
  6. /*  PRECOMPILE THIS PROGRAM ONLY                                 */
  7. /*  IT WILL INCLUDE THE NEEDED HEADER FILE                       */
  8. /*****************************************************************/
  9.  
  10. //
  11. //  Implemenation of the testCls & some
  12. //  misc routines
  13. //
  14.  
  15. #include <iostream.h>
  16.  
  17. //
  18. //  Notice - I include the file here (for the C compiler)
  19. //  and the SQL Include for BUFOP
  20. //  BUFOP will generate the tsta.hpp file from tsta.sqh
  21. //
  22. #include "tsta.hpp"
  23.  
  24. //
  25. //  this will include the host variables found in file tsta.sqh
  26. //  and will product the file tsta.hpp
  27. //
  28. //### EXEC SQL include 'tsta.sqh';
  29.  
  30. //
  31. // include the sqlca which will gen code for prog ID
  32. //  and the boolean variables _found, _error, _warn
  33. //  which can be used instead of the gotos
  34. //
  35. //### EXEC SQL include SQLCA;
  36.  #include <sqlda.h>   // sqlda structure 
  37. #include <sqlca.h>   // sqlca structre 
  38. #include <sqlaprep.h> // api(s) for most calls 
  39. #include <sqlenv.h>   // api(s) for start & stop db 
  40. sqlca sqlca; 
  41. typedef int  Boolean; 
  42. #define false 0
  43. #define true 1 
  44. Boolean _sqlfound = false;
  45. Boolean _sqlerror = false;
  46. Boolean _sqlwarn = false; 
  47. int _RC;
  48. const unsigned char sqla_program_id[40] = {
  49.        0x6f,0x41,0x41,0x42,0x41,0x45,0x43,0x43,0x45,0x44,
  50.        0x20,0x20,0x20,0x20,0x20,0x20,0x74,0x73,0x74,0x61,
  51.        0x2e,0x73,0x71,0x63,0x66,0x41,0x58,0x67,0x4a,0x4c,
  52.        0x4a,0x4a,0x30,0x20,0x20,0x20,0x20,0x20,0x20,0x20};
  53.  
  54.  
  55. // when not found set the _found to false
  56. // else set it to true
  57. //
  58. //### EXEC SQL whenever not found goto _sqlfound;
  59.  
  60. //
  61. //  errors will set the sqlerror boolean
  62. //  Could have used a label but hate thoses things
  63. //  and really hard to implement in a class
  64. //
  65.  
  66. //### EXEC SQL whenever sqlerror goto _sqlerror;
  67.  
  68. //
  69. //  sql warning will set the boolean _sqlwarn
  70. //
  71.  
  72. // EXEC SQL whenever sqlwarning goto _sqlwarn;
  73.  
  74.  
  75.  
  76. // If we get an error - report it through this thing
  77. // dump the error message and return wow! heavy duty stuff
  78. //
  79. void SQLError(char type)
  80. { char * buffer;
  81.  
  82.   buffer = new char(255);
  83.  
  84.    sqlaintp ( buffer,              /* buffer for message text */
  85.               254,                 /* buffer size gota leave a byte free */
  86.               60,                  /* line width */
  87.               &sqlca );            /* SQLCA  will be included in SQL INCLUDE*/
  88.    if (type == 'W') {
  89.       cout << "SQL WARNING " << buffer;
  90.    } else {
  91.       cout << "SQL ERROR " << buffer;
  92.    } /* endif */
  93.    delete buffer;
  94. }
  95.  
  96.  
  97.  
  98.   testCls::testCls() {pn = name;}
  99.   testCls::~testCls() {}
  100.   testCls::create( char * aname, char * aAddr, int aage, int aid)  // init and create
  101.    { 
  102.      memset(name,'\0',sizeof(name));
  103.      memset(addr,'\0',sizeof(addr));
  104.      strcpy(name,aname);
  105.      strcpy(addr, aAddr);
  106.      age = aage;
  107.      recID = aid;
  108.      pn = name;
  109.      insertDBRec();
  110.      }
  111.  
  112. testCls & testCls::operator=(testCls const & a){
  113.    if (this == &a) {
  114.       return *this;
  115.    } else {
  116.       strcpy(name,a.name);
  117.       strcpy(addr,a.addr);
  118.       age = a.age;
  119.       pn = name;
  120.       recID = a.recID;
  121.  
  122.    } /* endif */
  123.    return *this;
  124. }
  125.  
  126.   testCls::changeName(char * aname)
  127.    {strcpy(name,aname);}
  128.  
  129.   testCls::changeID(int id) {recID = id;}
  130.  
  131.   testCls::changeAge(int aage) {age = aage;}
  132.  
  133.  
  134. //
  135. //  read a Database record based on ID
  136. //  notice 'rec' was not declared in the declare section
  137. //  so it could not be used directly
  138. //  I could have declared a variable 'rec' in the header file
  139. //  and used it directly as all variables are global to
  140. //  BUFOP (and to sqlprep)
  141. //
  142.   testCls::read(int rec)
  143.   { recID = rec;
  144. //###   EXEC SQL SELECT name,Addr,Age
  145. //###     INTO :name,:addr :nullInd,:age
  146. //###     FROM test
  147. //###   WHERE id=:recID;
  148.       sqlastrt(&sqla_program_id,NULL,&sqlca); 
  149.      _RC = sqlaaloc(1, 1,1,NULL);// allocate an sqlda 
  150.       sqlasetv(1,0 ,496,4,&recID,NULL,NULL); // set host variable with no null indicator 
  151.      _RC = sqlaaloc(2, 3,1,NULL);// allocate an sqlda 
  152.       sqlasetv(2,0 ,460,40,&name,NULL,NULL); // set host variable with no null indicator 
  153.       sqlasetv(2,1 ,460,60,&addr,&nullInd,NULL); // set host variable with a null indicator 
  154.       sqlasetv(2,2 ,496,4,&age,NULL,NULL); // set host variable with no null indicator 
  155.      sqlacall(24, 1, 1, 2,NULL); 
  156.       if (sqlca.sqlcode < 0) { 
  157.          _sqlerror = true;
  158.        } else { 
  159.           _sqlerror = false;
  160.        } /* endif */ 
  161.       if (sqlca.sqlcode == 100) { 
  162.          _sqlfound = false;
  163.        } else { 
  164.           _sqlfound = true;
  165.        } /* endif */ 
  166.      sqlastop(NULL); 
  167.  
  168.   if (_sqlerror) {
  169.      SQLError('E');
  170.   } else {
  171.      if (_sqlwarn) {
  172.         SQLError('W');
  173.      } /* endif */
  174.   } /* endif */
  175.  
  176.   }
  177.  
  178. //
  179. //  update the record defined in this object
  180. //
  181.  
  182.   testCls::update()
  183.   {
  184. //###  EXEC SQL UPDATE test
  185. //###   SET
  186. //###     name = :name,
  187. //###     addr = :addr:nullInd,
  188. //###     age  = :age
  189. //###   WHERE id = :recID;
  190.       sqlastrt(&sqla_program_id,NULL,&sqlca); 
  191.      _RC = sqlaaloc(3, 4,2,NULL);// allocate an sqlda 
  192.       sqlasetv(3,0 ,460,40,&name,NULL,NULL); // set host variable with no null indicator 
  193.       sqlasetv(3,1 ,460,60,&addr,&nullInd,NULL); // set host variable with a null indicator 
  194.       sqlasetv(3,2 ,496,4,&age,NULL,NULL); // set host variable with no null indicator 
  195.       sqlasetv(3,3 ,496,4,&recID,NULL,NULL); // set host variable with no null indicator 
  196.      sqlacall(24, 2, 3, 0,NULL); 
  197.       if (sqlca.sqlcode < 0) { 
  198.          _sqlerror = true;
  199.        } else { 
  200.           _sqlerror = false;
  201.        } /* endif */ 
  202.       if (sqlca.sqlcode == 100) { 
  203.          _sqlfound = false;
  204.        } else { 
  205.           _sqlfound = true;
  206.        } /* endif */ 
  207.      sqlastop(NULL); 
  208. //###   EXEC SQL COMMIT WORK;
  209.       sqlastrt(&sqla_program_id,NULL,&sqlca); 
  210.      sqlacall(21, 0, 0, 0,NULL); 
  211.       if (sqlca.sqlcode < 0) { 
  212.          _sqlerror = true;
  213.        } else { 
  214.           _sqlerror = false;
  215.        } /* endif */ 
  216.       if (sqlca.sqlcode == 100) { 
  217.          _sqlfound = false;
  218.        } else { 
  219.           _sqlfound = true;
  220.        } /* endif */ 
  221.      sqlastop(NULL); 
  222.   if (_sqlerror) {
  223.      SQLError('E');
  224.   } else {
  225.      if (_sqlwarn) {
  226.         SQLError('W');
  227.      } /* endif */
  228.   } /* endif */
  229.  
  230.   }
  231.  
  232. //
  233. //  create a record on the DB using the data in
  234. //  this object
  235. //
  236.  
  237.   testCls::insertDBRec()
  238. {
  239. //###    EXEC SQL INSERT INTO test (name, addr, age,id)
  240. //###     VALUES (:name,:addr,:age,:recID);
  241.       sqlastrt(&sqla_program_id,NULL,&sqlca); 
  242.      _RC = sqlaaloc(4, 4,3,NULL);// allocate an sqlda 
  243.       sqlasetv(4,0 ,460,40,&name,NULL,NULL); // set host variable with no null indicator 
  244.       sqlasetv(4,1 ,460,60,&addr,NULL,NULL); // set host variable with no null indicator 
  245.       sqlasetv(4,2 ,496,4,&age,NULL,NULL); // set host variable with no null indicator 
  246.       sqlasetv(4,3 ,496,4,&recID,NULL,NULL); // set host variable with no null indicator 
  247.      sqlacall(24, 3, 4, 0,NULL); 
  248.       if (sqlca.sqlcode < 0) { 
  249.          _sqlerror = true;
  250.        } else { 
  251.           _sqlerror = false;
  252.        } /* endif */ 
  253.       if (sqlca.sqlcode == 100) { 
  254.          _sqlfound = false;
  255.        } else { 
  256.           _sqlfound = true;
  257.        } /* endif */ 
  258.      sqlastop(NULL); 
  259. //###    EXEC SQL COMMIT WORK;  
  260.       sqlastrt(&sqla_program_id,NULL,&sqlca); 
  261.      sqlacall(21, 0, 0, 0,NULL); 
  262.       if (sqlca.sqlcode < 0) { 
  263.          _sqlerror = true;
  264.        } else { 
  265.           _sqlerror = false;
  266.        } /* endif */ 
  267.       if (sqlca.sqlcode == 100) { 
  268.          _sqlfound = false;
  269.        } else { 
  270.           _sqlfound = true;
  271.        } /* endif */ 
  272.      sqlastop(NULL); 
  273.   if (_sqlerror) {
  274.      SQLError('E');
  275.   } else {
  276.      if (_sqlwarn) {
  277.         SQLError('W');
  278.      } /* endif */
  279.   } /* endif */
  280. }
  281.  
  282.  
  283. //
  284. //  over ride the << for printing the record
  285. //
  286.  ostream &operator <<(ostream & aStream, testCls const & theRec){
  287. //
  288. //  print the record
  289. //
  290.  
  291.     aStream << "Name:    " << theRec.name << "\n";
  292.     aStream << "Address: " << theRec.addr << "\n";
  293.     aStream << "Age:     " << theRec.age  << "\n";
  294.     return aStream;
  295.  };
  296.  
  297.  
  298. //-----------------------------------------------------------
  299. //  get a collection from the database using dyamic SQL
  300. //  try to be clever and use the << to fill the container
  301. //  NOTE - If you do not have the IBM CSet++ you should remove this
  302. //  class - or do something different with the bag collection
  303. //---------------------------------------------------------------
  304. //
  305. // the basic part of the select statement is used and
  306. // the conditional part is appended
  307. //
  308. const char basicSQL[] = "SELECT NAME, ADDR,AGE,ID FROM TEST  WHERE ";
  309.  
  310. selectTest::  selectTest()
  311. {
  312.    }
  313.  
  314. selectTest::  selectTest(char * const & theCondition)
  315. {
  316.    strcpy(theSelectStr,basicSQL);
  317.    strcat(theSelectStr,theCondition);
  318. }
  319.  
  320. selectTest:: setSelect(char * const & theCondition){
  321.    strcpy(theSelectStr,basicSQL);
  322.    strcat(theSelectStr,theCondition);
  323.  
  324. }
  325.  
  326.  
  327. //
  328. // overload the << to fill the container
  329. //
  330.  nameSet & operator<<(nameSet & aSet, selectTest & s)
  331.  {
  332. //###     EXEC SQL DECLARE C1 CURSOR FOR DSTR;
  333.  
  334. //###     EXEC SQL PREPARE DSTR FROM :s.theSelectStr;
  335.       sqlastrt(&sqla_program_id,NULL,&sqlca); 
  336.      _RC  = sqlasets(strlen( s.theSelectStr),s.theSelectStr,NULL);
  337.      sqlacall(27, 4, 0, 0,NULL); 
  338.       if (sqlca.sqlcode < 0) { 
  339.          _sqlerror = true;
  340.        } else { 
  341.           _sqlerror = false;
  342.        } /* endif */ 
  343.       if (sqlca.sqlcode == 100) { 
  344.          _sqlfound = false;
  345.        } else { 
  346.           _sqlfound = true;
  347.        } /* endif */ 
  348.      sqlastop(NULL); 
  349. //###     EXEC SQL OPEN C1;
  350.       sqlastrt(&sqla_program_id,NULL,&sqlca); 
  351.      sqlacall(26, 4, 0, 0,NULL); 
  352.       if (sqlca.sqlcode < 0) { 
  353.          _sqlerror = true;
  354.        } else { 
  355.           _sqlerror = false;
  356.        } /* endif */ 
  357.       if (sqlca.sqlcode == 100) { 
  358.          _sqlfound = false;
  359.        } else { 
  360.           _sqlfound = true;
  361.        } /* endif */ 
  362.      sqlastop(NULL); 
  363.  
  364.     do {
  365. //###        EXEC SQL FETCH C1 INTO
  366. //###           :s.name,
  367. //###           :s.addr,
  368. //###           :s.age,
  369. //###           :s.recID;
  370.       sqlastrt(&sqla_program_id,NULL,&sqlca); 
  371.      _RC = sqlaaloc(5, 4,4,NULL);// allocate an sqlda 
  372.       sqlasetv(5,0 ,460,40,&s.name,NULL,NULL); // set host variable with no null indicator 
  373.       sqlasetv(5,1 ,460,60,&s.addr,NULL,NULL); // set host variable with no null indicator 
  374.       sqlasetv(5,2 ,496,4,&s.age,NULL,NULL); // set host variable with no null indicator 
  375.       sqlasetv(5,3 ,496,4,&s.recID,NULL,NULL); // set host variable with no null indicator 
  376.      sqlacall(25, 4, 0, 5,NULL); 
  377.       if (sqlca.sqlcode < 0) { 
  378.          _sqlerror = true;
  379.        } else { 
  380.           _sqlerror = false;
  381.        } /* endif */ 
  382.       if (sqlca.sqlcode == 100) { 
  383.          _sqlfound = false;
  384.        } else { 
  385.           _sqlfound = true;
  386.        } /* endif */ 
  387.      sqlastop(NULL); 
  388.        if (_sqlfound) {
  389.           aSet.add(s);
  390.        } /* endif */
  391.          
  392.     } while (_sqlfound || _sqlwarn || _sqlerror ); /* enddo */
  393. //###     EXEC SQL CLOSE C1;
  394.       sqlastrt(&sqla_program_id,NULL,&sqlca); 
  395.      sqlacall(20, 4, 0, 0,NULL); 
  396.       if (sqlca.sqlcode < 0) { 
  397.          _sqlerror = true;
  398.        } else { 
  399.           _sqlerror = false;
  400.        } /* endif */ 
  401.       if (sqlca.sqlcode == 100) { 
  402.          _sqlfound = false;
  403.        } else { 
  404.           _sqlfound = true;
  405.        } /* endif */ 
  406.      sqlastop(NULL); 
  407. //###     EXEC SQL COMMIT WORK;
  408.       sqlastrt(&sqla_program_id,NULL,&sqlca); 
  409.      sqlacall(21, 0, 0, 0,NULL); 
  410.       if (sqlca.sqlcode < 0) { 
  411.          _sqlerror = true;
  412.        } else { 
  413.           _sqlerror = false;
  414.        } /* endif */ 
  415.       if (sqlca.sqlcode == 100) { 
  416.          _sqlfound = false;
  417.        } else { 
  418.           _sqlfound = true;
  419.        } /* endif */ 
  420.      sqlastop(NULL); 
  421.  
  422.    return aSet;
  423. }
  424.  
  425.  
  426.  
  427.  
  428. //==================================================
  429. //  read/write into/out of structures
  430. //====================================================
  431.  
  432.  
  433.  
  434. void readDB(stest * a)   // read indirect using pointer
  435. {
  436. //###  EXEC SQL SELECT name,Addr,Age                 
  437. //###    INTO :a->sname,:a->saddr,:a->sage
  438. //###    FROM test                                   
  439. //###  WHERE id=101;
  440.       sqlastrt(&sqla_program_id,NULL,&sqlca); 
  441.      _RC = sqlaaloc(6, 3,5,NULL);// allocate an sqlda 
  442.       sqlasetv(6,0 ,460,40,&a->sname,NULL,NULL); // set host variable with no null indicator 
  443.       sqlasetv(6,1 ,460,80,&a->saddr,NULL,NULL); // set host variable with no null indicator 
  444.       sqlasetv(6,2 ,496,4,&a->sage,NULL,NULL); // set host variable with no null indicator 
  445.      sqlacall(24, 5, 0, 6,NULL); 
  446.       if (sqlca.sqlcode < 0) { 
  447.          _sqlerror = true;
  448.        } else { 
  449.           _sqlerror = false;
  450.        } /* endif */ 
  451.       if (sqlca.sqlcode == 100) { 
  452.          _sqlfound = false;
  453.        } else { 
  454.           _sqlfound = true;
  455.        } /* endif */ 
  456.      sqlastop(NULL); 
  457.                                                
  458. }
  459.  
  460.  
  461.                                                                                         
  462.  
  463.  
  464.  
  465.  
  466. //=========================================================
  467. //  support functions put here (rather than the main program)
  468. //  because this module goes through the precompiler
  469. //========================================================
  470.  
  471. void startit()
  472.  {
  473. //###    EXEC SQL connect to test in share mode;
  474.  
  475.   unsigned char dbname[]= "test";
  476.      _RC = sqlestrd(dbname, 'S',&sqlca);
  477.       if (sqlca.sqlcode < 0) { 
  478.          _sqlerror = true;
  479.        } else { 
  480.           _sqlerror = false;
  481.        } /* endif */ 
  482.       if (sqlca.sqlcode == 100) { 
  483.          _sqlfound = false;
  484.        } else { 
  485.           _sqlfound = true;
  486.        } /* endif */ 
  487.  }
  488.  
  489. void stopit()
  490. {
  491. //###    EXEC SQL connect reset;
  492.      _RC = sqlestpd(&sqlca);  
  493.       if (sqlca.sqlcode < 0) { 
  494.          _sqlerror = true;
  495.        } else { 
  496.           _sqlerror = false;
  497.        } /* endif */ 
  498.       if (sqlca.sqlcode == 100) { 
  499.          _sqlfound = false;
  500.        } else { 
  501.           _sqlfound = true;
  502.        } /* endif */ 
  503. }
  504.  
  505.  
  506.  
  507.  
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516.  
  517.  
  518.  
  519.