home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 February / CHIP_2_98.iso / software / pelne / optionp / mts4.cab / Account.VC_UpdateReceipt.cpp < prev    next >
C/C++ Source or Header  |  1997-11-14  |  5KB  |  169 lines

  1. // Filename: UpdateReceipt.cpp
  2. //
  3. // Description: Implementation of CUpdateReceipt
  4. //
  5. // This file is provided as part of the Microsoft Transaction Server Samples
  6. //
  7. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT 
  8. // WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, 
  9. // INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
  10. // OF MERCHANTABILITY AND/OR FITNESS FOR A  PARTICULAR 
  11. // PURPOSE.
  12. //
  13. // Copyright (C) 1997 Microsoft Corporation, All rights reserved
  14.  
  15. #include "stdafx.h"
  16. #include "Account.h"
  17. #include "UpdateReceipt.h"
  18.  
  19. #include <mtx.h>
  20. #include <adoid.h>
  21. #include <adoint.h>
  22.  
  23. /////////////////////////////////////////////////////////////////////////////
  24. //
  25.  
  26. STDMETHODIMP CUpdateReceipt::InterfaceSupportsErrorInfo(REFIID riid)
  27. {
  28.     static const IID* arr[] = 
  29.     {
  30.         &IID_IUpdateReceipt,
  31.     };
  32.     
  33.     for (int i=0;i<sizeof(arr)/sizeof(arr[0]);i++)
  34.     {
  35.         if (InlineIsEqualGUID(*arr[i],riid))
  36.             return S_OK;
  37.     }
  38.     return S_FALSE;
  39. }
  40.  
  41. //    Update: update the database next receipt number when 100 receipts have been issued by GetReceipt
  42. //
  43. //    returns: S_OK or E_FAIL
  44.  
  45. STDMETHODIMP CUpdateReceipt::Update (OUT long* plReceiptNo) {
  46.     
  47.     HRESULT hr = S_OK;
  48.  
  49.     IObjectContext* pObjectContext = NULL;
  50.     
  51.     ADOConnection* adoCoConnection = NULL;
  52.     ADORecordset* adoRsReceipt = NULL;
  53.     ADOFields* pFields = NULL;
  54.     ADOField* pField = NULL;
  55.  
  56.     long lErrFlag = 0;
  57.     TCHAR* pErrMsg = NULL;
  58.  
  59.     *plReceiptNo = 0;
  60.  
  61.     try {
  62.  
  63.         // First of all, get the object context
  64.         THROW_ERR ( GetObjectContext(&pObjectContext), "GetObjectContext" );
  65.  
  66.         // Create ADOConnection object and initialize the connection
  67.         THROW_ERR ( CoCreateInstance (CLSID_CADOConnection, NULL, CLSCTX_INPROC_SERVER,
  68.             IID_IADOConnection, (LPVOID *) &adoCoConnection), "CoCreateInstance(CLSID_CADOConnection)" );
  69.  
  70.         BSTR bstrDSN = ::SysAllocString (L"FILEDSN=MTSSamples");
  71.         hr = adoCoConnection->Open(bstrDSN, NULL, NULL, adCmdUnspecified);
  72.         ::SysFreeString (bstrDSN);
  73.         RETHROW_ERR ( hr );
  74.  
  75.         // Update the next receipt record
  76.         CComVariant vAdoNull;
  77.         TCHAR szBuffer [512];
  78.         wsprintf (szBuffer, _T("Update Receipt set NextReceipt = NextReceipt + 100"));
  79.         BSTR bstrSQL = TCHAR2BSTR (szBuffer);
  80.         hr = adoCoConnection->Execute(bstrSQL, &vAdoNull, -1, NULL);
  81.         ::SysFreeString (bstrSQL);
  82.         RETHROW_ERR ( hr );
  83.  
  84.         // Obtain the desired recordset with an SQL query
  85.         bstrSQL = ::SysAllocString (L"SELECT NextReceipt FROM Receipt");
  86.         hr = adoCoConnection->Execute (bstrSQL, &vAdoNull, -1, &adoRsReceipt);
  87.         ::SysFreeString (bstrSQL);
  88.         RETHROW_ERR ( hr );
  89.  
  90.         // Get the appropriate fields
  91.         RETHROW_ERR( adoRsReceipt->get_Fields(&pFields) );
  92.  
  93.         // Get the appropriate field
  94.         CComVariant vField = L"NextReceipt";
  95.         RETHROW_ERR ( pFields->get_Item (vField, &pField) );
  96.         CComVariant vNextReceipt;
  97.         RETHROW_ERR ( pField->get_Value (&vNextReceipt) );
  98.  
  99.  
  100.         // Cleanup resources
  101.         if (pField) pField->Release();
  102.         if (pFields) pFields->Release();
  103.         if (adoRsReceipt) adoRsReceipt->Release();
  104.         if (adoCoConnection) adoCoConnection->Release();
  105.     
  106.         // We are finished and happy
  107.         pObjectContext->SetComplete();
  108.  
  109.         // Prepare return value
  110.         *plReceiptNo = vNextReceipt.lVal;
  111.         hr = S_OK;
  112.  
  113.     } catch (HRESULT hr) {
  114.             
  115.         //
  116.         //    ErrorInfo is saved here because the following ADO cleanup code 
  117.         //    may clear it.
  118.         //
  119.         IErrorInfo * pErrorInfo = NULL;
  120.         GetErrorInfo(NULL, &pErrorInfo);
  121.  
  122.         if (pField)    pField->Release();
  123.         if (pFields) pFields->Release();
  124.         if (adoRsReceipt) adoRsReceipt->Release();
  125.         if (adoCoConnection) adoCoConnection->Release();
  126.                 
  127.         // Fill in error information
  128.         switch (lErrFlag) {
  129.         
  130.         // Unknown error occurred in this object
  131.         case (0):
  132.             TCHAR szErr [512];
  133.             wsprintf (szErr, _T("Error 0x%x from CUpdateReceipt calling %s."), hr, pErrMsg);
  134.             pErrMsg = szErr;
  135.             // Fall through
  136.         
  137.         // An application error occurred in this object
  138.         case (1):
  139.             //
  140.             //    we are going to put our own error in TLS, so if there is one there, clear it
  141.             //
  142.             if (pErrorInfo)
  143.                 pErrorInfo -> Release();
  144.             
  145.             AtlReportError( CLSID_CUpdateReceipt, pErrMsg, IID_IUpdateReceipt, hr);
  146.             break;
  147.  
  148.         case (2): // An error occurred in a called object
  149.             //
  150.             //    put the error back in TLS
  151.             //
  152.             SetErrorInfo(NULL, pErrorInfo);
  153.             break;
  154.         
  155.         // Will never reach here
  156.         default:
  157.             break;
  158.         }
  159.  
  160.         // Indicate our unhappiness
  161.         if (pObjectContext)
  162.             pObjectContext->SetAbort();
  163.     }
  164.     
  165.     // Resource cleanup
  166.     if (pObjectContext)    pObjectContext->Release();
  167.     
  168.     return hr;
  169. }