home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / bufop.zip / TSTA.SQC < prev    next >
Text File  |  1993-09-07  |  7KB  |  304 lines

  1. /*****************************************************************/
  2. /* N O T I C E                                                   */
  3. /*  PRECOMPILE THIS PROGRAM ONLY                                 */
  4. /*  IT WILL INCLUDE THE NEEDED HEADER FILE                       */
  5. /*****************************************************************/
  6.  
  7. //
  8. //  Implemenation of the testCls & some
  9. //  misc routines
  10. //
  11.  
  12. #include <iostream.h>
  13.  
  14. //
  15. //  Notice - I include the file here (for the C compiler)
  16. //  and the SQL Include for BUFOP
  17. //  BUFOP will generate the tsta.hpp file from tsta.sqh
  18. //
  19. #include "tsta.hpp"
  20.  
  21. //
  22. //  this will include the host variables found in file tsta.sqh
  23. //  and will product the file tsta.hpp
  24. //
  25. EXEC SQL include 'tsta.sqh';
  26.  
  27. //
  28. // include the sqlca which will gen code for prog ID
  29. //  and the boolean variables _found, _error, _warn
  30. //  which can be used instead of the gotos
  31. //
  32. EXEC SQL include SQLCA;
  33.  
  34. // when not found set the _found to false
  35. // else set it to true
  36. //
  37. EXEC SQL whenever not found goto _sqlfound;
  38.  
  39. //
  40. //  errors will set the sqlerror boolean
  41. //  Could have used a label but hate thoses things
  42. //  and really hard to implement in a class
  43. //
  44.  
  45. EXEC SQL whenever sqlerror goto _sqlerror;
  46.  
  47. //
  48. //  sql warning will set the boolean _sqlwarn
  49. //
  50.  
  51. // EXEC SQL whenever sqlwarning goto _sqlwarn;
  52.  
  53.  
  54.  
  55. // If we get an error - report it through this thing
  56. // dump the error message and return wow! heavy duty stuff
  57. //
  58. void SQLError(char type)
  59. { char * buffer;
  60.  
  61.   buffer = new char(255);
  62.  
  63.    sqlaintp ( buffer,              /* buffer for message text */
  64.               254,                 /* buffer size gota leave a byte free */
  65.               60,                  /* line width */
  66.               &sqlca );            /* SQLCA  will be included in SQL INCLUDE*/
  67.    if (type == 'W') {
  68.       cout << "SQL WARNING " << buffer;
  69.    } else {
  70.       cout << "SQL ERROR " << buffer;
  71.    } /* endif */
  72.    delete buffer;
  73. }
  74.  
  75.  
  76.  
  77.   testCls::testCls() {pn = name;}
  78.   testCls::~testCls() {}
  79.   testCls::create( char * aname, char * aAddr, int aage, int aid)  // init and create
  80.    { 
  81.      memset(name,'\0',sizeof(name));
  82.      memset(addr,'\0',sizeof(addr));
  83.      strcpy(name,aname);
  84.      strcpy(addr, aAddr);
  85.      age = aage;
  86.      recID = aid;
  87.      pn = name;
  88.      insertDBRec();
  89.      }
  90.  
  91. testCls & testCls::operator=(testCls const & a){
  92.    if (this == &a) {
  93.       return *this;
  94.    } else {
  95.       strcpy(name,a.name);
  96.       strcpy(addr,a.addr);
  97.       age = a.age;
  98.       pn = name;
  99.       recID = a.recID;
  100.  
  101.    } /* endif */
  102.    return *this;
  103. }
  104.  
  105.   testCls::changeName(char * aname)
  106.    {strcpy(name,aname);}
  107.  
  108.   testCls::changeID(int id) {recID = id;}
  109.  
  110.   testCls::changeAge(int aage) {age = aage;}
  111.  
  112.  
  113. //
  114. //  read a Database record based on ID
  115. //  notice 'rec' was not declared in the declare section
  116. //  so it could not be used directly
  117. //  I could have declared a variable 'rec' in the header file
  118. //  and used it directly as all variables are global to
  119. //  BUFOP (and to sqlprep)
  120. //
  121.   testCls::read(int rec)
  122.   { recID = rec;
  123.   EXEC SQL SELECT name,Addr,Age
  124.     INTO :name,:addr :nullInd,:age
  125.     FROM test
  126.   WHERE id=:recID;
  127.  
  128.   if (_sqlerror) {
  129.      SQLError('E');
  130.   } else {
  131.      if (_sqlwarn) {
  132.         SQLError('W');
  133.      } /* endif */
  134.   } /* endif */
  135.  
  136.   }
  137.  
  138. //
  139. //  update the record defined in this object
  140. //
  141.  
  142.   testCls::update()
  143.   {
  144.  EXEC SQL UPDATE test
  145.   SET
  146.     name = :name,
  147.     addr = :addr:nullInd,
  148.     age  = :age
  149.   WHERE id = :recID;
  150.   EXEC SQL COMMIT WORK;
  151.   if (_sqlerror) {
  152.      SQLError('E');
  153.   } else {
  154.      if (_sqlwarn) {
  155.         SQLError('W');
  156.      } /* endif */
  157.   } /* endif */
  158.  
  159.   }
  160.  
  161. //
  162. //  create a record on the DB using the data in
  163. //  this object
  164. //
  165.  
  166.   testCls::insertDBRec()
  167. {
  168.    EXEC SQL INSERT INTO test (name, addr, age,id)
  169.     VALUES (:name,:addr,:age,:recID);
  170.    EXEC SQL COMMIT WORK;  
  171.   if (_sqlerror) {
  172.      SQLError('E');
  173.   } else {
  174.      if (_sqlwarn) {
  175.         SQLError('W');
  176.      } /* endif */
  177.   } /* endif */
  178. }
  179.  
  180.  
  181. //
  182. //  over ride the << for printing the record
  183. //
  184.  ostream &operator <<(ostream & aStream, testCls const & theRec){
  185. //
  186. //  print the record
  187. //
  188.  
  189.     aStream << "Name:    " << theRec.name << "\n";
  190.     aStream << "Address: " << theRec.addr << "\n";
  191.     aStream << "Age:     " << theRec.age  << "\n";
  192.     return aStream;
  193.  };
  194.  
  195.  
  196. //-----------------------------------------------------------
  197. //  get a collection from the database using dyamic SQL
  198. //  try to be clever and use the << to fill the container
  199. //  NOTE - If you do not have the IBM CSet++ you should remove this
  200. //  class - or do something different with the bag collection
  201. //---------------------------------------------------------------
  202. //
  203. // the basic part of the select statement is used and
  204. // the conditional part is appended
  205. //
  206. const char basicSQL[] = "SELECT NAME, ADDR,AGE,ID FROM TEST  WHERE ";
  207.  
  208. selectTest::  selectTest()
  209. {
  210.    }
  211.  
  212. selectTest::  selectTest(char * const & theCondition)
  213. {
  214.    strcpy(theSelectStr,basicSQL);
  215.    strcat(theSelectStr,theCondition);
  216. }
  217.  
  218. selectTest:: setSelect(char * const & theCondition){
  219.    strcpy(theSelectStr,basicSQL);
  220.    strcat(theSelectStr,theCondition);
  221.  
  222. }
  223.  
  224.  
  225. //
  226. // overload the << to fill the container
  227. //
  228.  nameSet & operator<<(nameSet & aSet, selectTest & s)
  229.  {
  230.     EXEC SQL DECLARE C1 CURSOR FOR DSTR;
  231.  
  232.     EXEC SQL PREPARE DSTR FROM :s.theSelectStr;
  233.     EXEC SQL OPEN C1;
  234.  
  235.     do {
  236.        EXEC SQL FETCH C1 INTO
  237.           :s.name,
  238.           :s.addr,
  239.           :s.age,
  240.           :s.recID;
  241.        if (_sqlfound) {
  242.           aSet.add(s);
  243.        } /* endif */
  244.          
  245.     } while (_sqlfound || _sqlwarn || _sqlerror ); /* enddo */
  246.     EXEC SQL CLOSE C1;
  247.     EXEC SQL COMMIT WORK;
  248.  
  249.    return aSet;
  250. }
  251.  
  252.  
  253.  
  254.  
  255. //==================================================
  256. //  read/write into/out of structures
  257. //====================================================
  258.  
  259.  
  260.  
  261. void readDB(stest * a)   // read indirect using pointer
  262. {
  263.  EXEC SQL SELECT name,Addr,Age                 
  264.    INTO :a->sname,:a->saddr,:a->sage
  265.    FROM test                                   
  266.  WHERE id=101;
  267.                                                
  268. }
  269.  
  270.  
  271.                                                                                         
  272.  
  273.  
  274.  
  275.  
  276. //=========================================================
  277. //  support functions put here (rather than the main program)
  278. //  because this module goes through the precompiler
  279. //========================================================
  280.  
  281. void startit()
  282.  {
  283.    EXEC SQL connect to test in share mode;
  284.  }
  285.  
  286. void stopit()
  287. {
  288.    EXEC SQL connect reset;
  289. }
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.