home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 10 / ioProg_10.iso / soft / optima / samples.z / KeyTable.wxc < prev    next >
Encoding:
Text File  |  1996-04-29  |  6.5 KB  |  235 lines

  1. Save Format v1.3
  2. @begin ClassFile "KeyTable"
  3.  Exported 0;
  4.  
  5.  @begin UserFunction "KeyTable()"
  6.   GencodeSrcLine 15;
  7.   FunctionName "KeyTable::KeyTable()";
  8.  @end;
  9.  
  10.  @begin UserFunction "Prototype for KeyTable()"
  11.   Private 1;
  12.   GencodeSrcLine 28;
  13.   FunctionName "KeyTable::Prototype for KeyTable()";
  14.  @end;
  15.  
  16.  @begin UserFunction "~KeyTable()"
  17.   GencodeSrcLine 19;
  18.   FunctionName "KeyTable::~KeyTable()";
  19.  @end;
  20.  
  21.  @begin UserFunction "Prototype for ~KeyTable()"
  22.   Private 1;
  23.   GencodeSrcLine 30;
  24.   FunctionName "KeyTable::Prototype for ~KeyTable()";
  25.  @end;
  26.  
  27.  @begin UserFunction "Create( WTransaction * transaction )"
  28.   GencodeSrcLine 23;
  29.   FunctionName "KeyTable::Create( WTransaction * transaction )";
  30.  @end;
  31.  
  32.  @begin UserFunction "Prototype for Create( WTransaction * transaction )"
  33.   Private 1;
  34.   GencodeSrcLine 32;
  35.   FunctionName "KeyTable::Prototype for Create( WTransaction * transaction )";
  36.  @end;
  37.  
  38.  @begin UserFunction "NextKey( WTransaction * transaction, const WString & table )"
  39.   GencodeSrcLine 57;
  40.   FunctionName "KeyTable::NextKey( WTransaction * transaction, const WString & table )";
  41.  @end;
  42.  
  43.  @begin UserFunction "Prototype for NextKey( WTransaction * transaction, const WString & table )"
  44.   Private 1;
  45.   GencodeSrcLine 34;
  46.   FunctionName "KeyTable::Prototype for NextKey( WTransaction * transaction, const WString & table )";
  47.  @end;
  48.  
  49.  @begin HPPPrefixBlock
  50. @begin-code HPPPrefix
  51.  
  52. // Declarations added here will be included at the top of the .HPP file
  53.  
  54. #define KT_CUSTOMER     "Customer"
  55. #define KT_PRODUCT      "Product"
  56. #define KT_ORDER        "Sales_order"
  57.  
  58. @end-code;
  59.   GencodeSrcLine 11;
  60.  @end;
  61.  
  62.  @begin CPPPrefixBlock
  63. @begin-code CPPPrefix
  64.  
  65. // Code added here will be included at the top of the .CPP file
  66.  
  67. //  Include definitions for resources.
  68. #include "WRes.h"
  69.  
  70. @end-code;
  71.   GencodeSrcLine 11;
  72.  @end;
  73.  
  74.  @begin ClassContentsBlock
  75. @begin-code ClassContents
  76.  
  77.     public:
  78.         // add your public instance data here
  79.     private:
  80.         // add your private instance data here
  81.     protected:
  82.         // add your protected instance data here
  83.  
  84. @end-code;
  85.   GencodeSrcLine 20;
  86.  @end;
  87.  
  88. @begin-code GeneratedClassContents
  89.  
  90.  
  91. @end-code;
  92.  
  93. @begin-code Code "KeyTable::KeyTable()"
  94.  
  95. KeyTable::KeyTable()
  96. {
  97.     
  98. }
  99.  
  100. @end-code;
  101.  
  102. @begin-code Code "KeyTable::Prototype for KeyTable()"
  103.  
  104.     public:
  105.         KeyTable();
  106.  
  107. @end-code;
  108.  
  109. @begin-code Code "KeyTable::~KeyTable()"
  110.  
  111. KeyTable::~KeyTable()
  112. {
  113.     
  114. }
  115.  
  116. @end-code;
  117.  
  118. @begin-code Code "KeyTable::Prototype for ~KeyTable()"
  119.  
  120.     public:
  121.         ~KeyTable();
  122.  
  123. @end-code;
  124.  
  125. @begin-code Code "KeyTable::Create( WTransaction * transaction )"
  126.  
  127. // Create
  128.  
  129. // Add a table to use in selecting new customer, product, and order numbers.
  130.  
  131. // Normally this table would be part of the database, but as we are using
  132. // the standard Sybase SQL sample database it is not.  A permanent table 
  133. // would provide a way to provide unique numbers to multiple clients
  134. // accessing the database at the same time.  The temporary table allows the code
  135. // to be written as if the table exists, but does not provide unique ids as
  136. // the table can only be seen by the connection that created it.
  137.     
  138. static void KeyTable::Create( WTransaction * transaction )
  139. {
  140.     WQuery      query;
  141.     WString     command;
  142.  
  143.     query.SetTransactionObject( transaction );
  144.     query.SetDisplayErrorDialog( true );
  145.     query.SetDisplayWarningDialog( true );
  146.     
  147.     command = "Declare local temporary table MaxIds"
  148.               " (Name char(80) not null, Id int not null)"
  149.               " on commit preserve rows";
  150.     query.Execute( command );
  151.     
  152.     command = "Insert into MaxIds select '" KT_CUSTOMER "', max(id) from dba.customer";
  153.     query.Execute( command );
  154.     
  155.     command = "Insert into MaxIds select '" KT_PRODUCT "', max(id) from dba.product";
  156.     query.Execute( command );
  157.         
  158.     command = "Insert into MaxIds select '" KT_ORDER "', max(id) from dba.sales_order";
  159.     query.Execute( command );  
  160. }
  161.  
  162. @end-code;
  163.  
  164. @begin-code Code "KeyTable::Prototype for Create( WTransaction * transaction )"
  165.  
  166.     public:
  167.         static void Create( WTransaction * transaction );
  168.  
  169. @end-code;
  170.  
  171. @begin-code Code "KeyTable::NextKey( WTransaction * transaction, const WString & table )"
  172.  
  173. // NextKey
  174.  
  175. // Update and return the next available id for the specified table.
  176.  
  177. // Sybase SQL Anywhere provides row-level write locks at all isolation levels.
  178. // This means that as soon as a connection updates, inserts, or deletes a row
  179. // the row is write-locked until the transaction is committed or rolled back.
  180. // The following code uses this locking to provide a way to get a unique id
  181. // for new customers, products, and orders.
  182.  
  183. // To get a new id, NextKey first issues an Update statement to increment the
  184. // id number for the appropriate table.  In addition to incrementing the value,
  185. // this locks the row and will prevent a similar update by another connection.
  186. // The subsequent select fetches the new id.
  187.  
  188. // Normally this operation would be followed by a commit to remove the write-lock
  189. // and let any blocked connections proceed.  However in this sample all the work is 
  190. // committed or rolled back on exit, to make it easy to try the application without
  191. // changing the sample database.
  192.  
  193. // For more information on transaction processing, see "Using Transactions and Locks"
  194. // in the online Sybase SQL Anywhere manual.
  195.  
  196.  
  197. static WInt KeyTable::NextKey( WTransaction * transaction, const WString & table )
  198. {
  199.     WInt        id;
  200.    
  201.     WQuery      query;
  202.     WString     command;
  203.     WDataValue  value;
  204.  
  205.     query.SetTransactionObject( transaction );
  206.     query.SetDisplayErrorDialog( true );
  207.     query.SetDisplayWarningDialog( true );
  208.     
  209.     // Update the id and write-lock the row
  210.     command.Sprintf( "Update MaxIds set Id = Id + 1 where Name = '%s'", table.GetText() );
  211.     query.Execute( command );
  212.  
  213.     // Fetch the id
  214.     command.Sprintf( "Select Id from MaxIds where Name = '%s'", table.GetText() );
  215.     query.SetSQL( command );
  216.     query.Open();
  217.     query.MoveNext();
  218.     value = query.GetValue( 1 );
  219.     id = value.GetSLONG();
  220.     
  221.     WDBG(( "KeyTable::NextKey '%s' %d", table.GetText(), id ));
  222.     
  223.     return( id );
  224. }
  225.  
  226. @end-code;
  227.  
  228. @begin-code Code "KeyTable::Prototype for NextKey( WTransaction * transaction, const WString & table )"
  229.  
  230.     public:
  231.         static WInt NextKey( WTransaction * transaction, const WString & table );
  232.  
  233. @end-code;
  234. @end;
  235.