home *** CD-ROM | disk | FTP | other *** search
- Save Format v1.3
- @begin ClassFile "KeyTable"
- Exported 0;
-
- @begin UserFunction "KeyTable()"
- GencodeSrcLine 15;
- FunctionName "KeyTable::KeyTable()";
- @end;
-
- @begin UserFunction "Prototype for KeyTable()"
- Private 1;
- GencodeSrcLine 28;
- FunctionName "KeyTable::Prototype for KeyTable()";
- @end;
-
- @begin UserFunction "~KeyTable()"
- GencodeSrcLine 19;
- FunctionName "KeyTable::~KeyTable()";
- @end;
-
- @begin UserFunction "Prototype for ~KeyTable()"
- Private 1;
- GencodeSrcLine 30;
- FunctionName "KeyTable::Prototype for ~KeyTable()";
- @end;
-
- @begin UserFunction "Create( WTransaction * transaction )"
- GencodeSrcLine 23;
- FunctionName "KeyTable::Create( WTransaction * transaction )";
- @end;
-
- @begin UserFunction "Prototype for Create( WTransaction * transaction )"
- Private 1;
- GencodeSrcLine 32;
- FunctionName "KeyTable::Prototype for Create( WTransaction * transaction )";
- @end;
-
- @begin UserFunction "NextKey( WTransaction * transaction, const WString & table )"
- GencodeSrcLine 57;
- FunctionName "KeyTable::NextKey( WTransaction * transaction, const WString & table )";
- @end;
-
- @begin UserFunction "Prototype for NextKey( WTransaction * transaction, const WString & table )"
- Private 1;
- GencodeSrcLine 34;
- FunctionName "KeyTable::Prototype for NextKey( WTransaction * transaction, const WString & table )";
- @end;
-
- @begin HPPPrefixBlock
- @begin-code HPPPrefix
-
- // Declarations added here will be included at the top of the .HPP file
-
- #define KT_CUSTOMER "Customer"
- #define KT_PRODUCT "Product"
- #define KT_ORDER "Sales_order"
-
- @end-code;
- GencodeSrcLine 11;
- @end;
-
- @begin CPPPrefixBlock
- @begin-code CPPPrefix
-
- // Code added here will be included at the top of the .CPP file
-
- // Include definitions for resources.
- #include "WRes.h"
-
- @end-code;
- GencodeSrcLine 11;
- @end;
-
- @begin ClassContentsBlock
- @begin-code ClassContents
-
- public:
- // add your public instance data here
- private:
- // add your private instance data here
- protected:
- // add your protected instance data here
-
- @end-code;
- GencodeSrcLine 20;
- @end;
-
- @begin-code GeneratedClassContents
-
-
- @end-code;
-
- @begin-code Code "KeyTable::KeyTable()"
-
- KeyTable::KeyTable()
- {
-
- }
-
- @end-code;
-
- @begin-code Code "KeyTable::Prototype for KeyTable()"
-
- public:
- KeyTable();
-
- @end-code;
-
- @begin-code Code "KeyTable::~KeyTable()"
-
- KeyTable::~KeyTable()
- {
-
- }
-
- @end-code;
-
- @begin-code Code "KeyTable::Prototype for ~KeyTable()"
-
- public:
- ~KeyTable();
-
- @end-code;
-
- @begin-code Code "KeyTable::Create( WTransaction * transaction )"
-
- // Create
-
- // Add a table to use in selecting new customer, product, and order numbers.
-
- // Normally this table would be part of the database, but as we are using
- // the standard Sybase SQL sample database it is not. A permanent table
- // would provide a way to provide unique numbers to multiple clients
- // accessing the database at the same time. The temporary table allows the code
- // to be written as if the table exists, but does not provide unique ids as
- // the table can only be seen by the connection that created it.
-
- static void KeyTable::Create( WTransaction * transaction )
- {
- WQuery query;
- WString command;
-
- query.SetTransactionObject( transaction );
- query.SetDisplayErrorDialog( true );
- query.SetDisplayWarningDialog( true );
-
- command = "Declare local temporary table MaxIds"
- " (Name char(80) not null, Id int not null)"
- " on commit preserve rows";
- query.Execute( command );
-
- command = "Insert into MaxIds select '" KT_CUSTOMER "', max(id) from dba.customer";
- query.Execute( command );
-
- command = "Insert into MaxIds select '" KT_PRODUCT "', max(id) from dba.product";
- query.Execute( command );
-
- command = "Insert into MaxIds select '" KT_ORDER "', max(id) from dba.sales_order";
- query.Execute( command );
- }
-
- @end-code;
-
- @begin-code Code "KeyTable::Prototype for Create( WTransaction * transaction )"
-
- public:
- static void Create( WTransaction * transaction );
-
- @end-code;
-
- @begin-code Code "KeyTable::NextKey( WTransaction * transaction, const WString & table )"
-
- // NextKey
-
- // Update and return the next available id for the specified table.
-
- // Sybase SQL Anywhere provides row-level write locks at all isolation levels.
- // This means that as soon as a connection updates, inserts, or deletes a row
- // the row is write-locked until the transaction is committed or rolled back.
- // The following code uses this locking to provide a way to get a unique id
- // for new customers, products, and orders.
-
- // To get a new id, NextKey first issues an Update statement to increment the
- // id number for the appropriate table. In addition to incrementing the value,
- // this locks the row and will prevent a similar update by another connection.
- // The subsequent select fetches the new id.
-
- // Normally this operation would be followed by a commit to remove the write-lock
- // and let any blocked connections proceed. However in this sample all the work is
- // committed or rolled back on exit, to make it easy to try the application without
- // changing the sample database.
-
- // For more information on transaction processing, see "Using Transactions and Locks"
- // in the online Sybase SQL Anywhere manual.
-
-
- static WInt KeyTable::NextKey( WTransaction * transaction, const WString & table )
- {
- WInt id;
-
- WQuery query;
- WString command;
- WDataValue value;
-
- query.SetTransactionObject( transaction );
- query.SetDisplayErrorDialog( true );
- query.SetDisplayWarningDialog( true );
-
- // Update the id and write-lock the row
- command.Sprintf( "Update MaxIds set Id = Id + 1 where Name = '%s'", table.GetText() );
- query.Execute( command );
-
- // Fetch the id
- command.Sprintf( "Select Id from MaxIds where Name = '%s'", table.GetText() );
- query.SetSQL( command );
- query.Open();
- query.MoveNext();
- value = query.GetValue( 1 );
- id = value.GetSLONG();
-
- WDBG(( "KeyTable::NextKey '%s' %d", table.GetText(), id ));
-
- return( id );
- }
-
- @end-code;
-
- @begin-code Code "KeyTable::Prototype for NextKey( WTransaction * transaction, const WString & table )"
-
- public:
- static WInt NextKey( WTransaction * transaction, const WString & table );
-
- @end-code;
- @end;
-