home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 32 / IOPROG_32.ISO / SOFT / SqlEval7 / MSOLAP / samples / Samples.exe / CppOlapDemo / OLAPApp.cpp next >
Encoding:
C/C++ Source or Header  |  1998-10-30  |  10.4 KB  |  337 lines

  1. //-----------------------------------------------------------------------------
  2. // Microsoft OLE DB QLAPDEMO Sample
  3. // Copyright (C) 1995-1998 Microsoft Corporation
  4. //
  5. // File: OLAPApp.cpp
  6. //
  7. // This file implements the class OLAPApp
  8. //
  9. //-----------------------------------------------------------------------------
  10.  
  11. #include "OLAP.h"
  12.  
  13. /////////////////////////////////////////////////////////////////////////////////////
  14. OLAPApp::OLAPApp()
  15.  
  16. // Constructor for the OLAP application
  17. /////////////////////////////////////////////////////////////////////////////////////
  18. {
  19.     m_dwState = 0x00000000;    
  20.  
  21.     m_pIDBSource  = NULL; 
  22.     m_pIDBSession = NULL;
  23.  
  24.     m_pICommand   = NULL;
  25.     m_pIMDDataset = NULL;
  26. }
  27.  
  28. /////////////////////////////////////////////////////////////////////////////////////
  29. OLAPApp::~OLAPApp()
  30.  
  31. // Destructor for the OLAP application
  32. /////////////////////////////////////////////////////////////////////////////////////
  33. {
  34.     Reset();
  35. }
  36.  
  37. /////////////////////////////////////////////////////////////////////////////////////
  38. HRESULT     OLAPApp::Init()
  39.  
  40. // Initialization for the OLAP application
  41. /////////////////////////////////////////////////////////////////////////////////////
  42. {
  43.     // Initialize COM
  44.     HRESULT  hr = CoInitialize(NULL);
  45.  
  46.     if ( FAILED(hr) ) 
  47.         return hr;
  48.  
  49.     // Mark that we are initialized
  50.     StateOn(OLAP_APP_INITIALIZED);
  51.  
  52.     return hr;
  53. }
  54.  
  55. /////////////////////////////////////////////////////////////////////////////////////
  56. HRESULT     OLAPApp::Reset()
  57.  
  58. // Reset for the OLAP application
  59. /////////////////////////////////////////////////////////////////////////////////////
  60. {
  61.     // Disconnect 
  62.     Disconnect();
  63.  
  64.     // Uninitialize COM
  65.     CoUninitialize();
  66.  
  67.     // Mark that we are uninitialized
  68.     StateOff(OLAP_APP_INITIALIZED);
  69.  
  70.     return S_OK;
  71. }
  72.  
  73.  
  74. /////////////////////////////////////////////////////////////////////////////////////
  75. HRESULT     OLAPApp::Connect( LPTSTR  pServer )
  76.  
  77. // Is the specified string an argument ?
  78. /////////////////////////////////////////////////////////////////////////////////////
  79. {
  80.     // App must be initialized.
  81.     if ( !IsState(OLAP_APP_INITIALIZED) )
  82.         return E_FAIL;
  83.  
  84.     // Disconnect from previous connection
  85.     if ( IsState(OLAP_APP_CONNECTED) )
  86.         Disconnect();
  87.  
  88.     DBPROPSET    rgPropertySet[1];
  89.     DBPROP        rgProperties[1];
  90.  
  91.     IDBInitialize*      pIDBInitialize = NULL;
  92.     IDBProperties*      pIDBProperties = NULL;
  93.  
  94.     // Initialize property values
  95.     VariantInit(&rgProperties[0].vValue);
  96.  
  97.     // Create IDInitialize object
  98.     // This call uses GUID CLSID_MSOLAP which refers to Microsoft's
  99.     // implementation of OLE DB for OLAP. It can be substituted with
  100.     // GUID of any other OLE DB for OLAP provider. The rest of the code
  101.     // should work with any provider correctly
  102.     HRESULT  hr = CoCreateInstance( CLSID_MSOLAP,
  103.                                     NULL, 
  104.                                     CLSCTX_INPROC_SERVER,
  105.                                     IID_IDBInitialize,
  106.                                     (LPVOID*) &pIDBInitialize );
  107.     if ( FAILED(hr) || !pIDBInitialize )
  108.         goto CLEANUP;
  109.  
  110.     // Ask for interface to set properties
  111.     hr = pIDBInitialize->QueryInterface( IID_IDBProperties,
  112.                                          (void**) &pIDBProperties );
  113.  
  114.     // Identify Property Set
  115.     rgPropertySet[0].rgProperties         =   rgProperties;
  116.     rgPropertySet[0].cProperties        =    1;
  117.     rgPropertySet[0].guidPropertySet    =    DBPROPSET_DBINIT;
  118.  
  119.     // Fill in Data Source
  120.     rgProperties[0].dwPropertyID        =    DBPROP_INIT_DATASOURCE;
  121.     rgProperties[0].dwOptions            =    DBPROPOPTIONS_REQUIRED;
  122.     rgProperties[0].colid                =    DB_NULLID;
  123.     rgProperties[0].vValue.vt            =    VT_BSTR;
  124.     rgProperties[0].vValue.bstrVal        =    SysAllocString( ( pServer && *pServer ) ? pServer : TEXT("localhost") );
  125.     assert(rgProperties[0].vValue.bstrVal);
  126.  
  127.     // Set connection properties
  128.     hr = pIDBProperties->SetProperties(1, rgPropertySet );
  129.     if ( FAILED(hr) )
  130.         goto CLEANUP;
  131.  
  132.     // Initialize the Session.
  133.     hr =  pIDBInitialize->Initialize();
  134.     if ( FAILED(hr) )
  135.         goto CLEANUP;
  136.  
  137.     // Ask for interface to create session
  138.     hr = pIDBInitialize->QueryInterface( IID_IDBCreateSession, 
  139.                                          (void**) &m_pIDBSource );
  140.     if ( FAILED(hr) || !m_pIDBSource )
  141.         goto CLEANUP;
  142.  
  143.     // Create session. We use IDBCreateCommand interface to represent
  144.     // session
  145.     hr = m_pIDBSource->CreateSession( NULL,
  146.                                       IID_IDBCreateCommand,
  147.                                       (IUnknown**) &m_pIDBSession );
  148.     if ( FAILED(hr) || !m_pIDBSession )
  149.         goto CLEANUP;
  150.  
  151.     // Create text command. This command is reused for every query, so it
  152.     // is kind of optimization to create it here and use for every query
  153.     hr = m_pIDBSession->CreateCommand( NULL, IID_ICommandText, (IUnknown**) &m_pICommand );
  154.     if ( FAILED(hr) || !m_pICommand )
  155.         goto CLEANUP;
  156.  
  157.     // Mark that we are connected
  158.     StateOn(OLAP_APP_CONNECTED);
  159.     StateOn(OLAP_APP_DB_OPEN);
  160.  
  161. CLEANUP:
  162.  
  163.     VariantClear(&rgProperties[0].vValue);
  164.     if ( pIDBInitialize )    pIDBInitialize->Release();
  165.     if ( pIDBProperties )    pIDBProperties->Release();
  166.  
  167.     return hr;
  168. }
  169.  
  170.  
  171. /////////////////////////////////////////////////////////////////////////////////////
  172. HRESULT     OLAPApp::Disconnect()
  173.  
  174. // Disconnect from OLE DB for OLAP provider
  175. /////////////////////////////////////////////////////////////////////////////////////
  176. {
  177.     if ( m_pIMDDataset ) { m_pIMDDataset->Release(); m_pIMDDataset = NULL; }
  178.     if ( m_pICommand   ) { m_pICommand->Release();   m_pICommand   = NULL; }
  179.     if ( m_pIDBSession ) { m_pIDBSession->Release(); m_pIDBSession = NULL; }
  180.     if ( m_pIDBSource  ) { m_pIDBSource->Release();  m_pIDBSource  = NULL; }
  181.  
  182.     StateOff(OLAP_APP_CONNECTED);
  183.  
  184.     return S_OK;
  185. }
  186.  
  187.  
  188. /////////////////////////////////////////////////////////////////////////////////////
  189. HRESULT     OLAPApp::GetProperty( GUID  propSet, DBPROPID  propId, VARIANT*  pData )
  190.  
  191. // Get the requested property
  192. /////////////////////////////////////////////////////////////////////////////////////
  193. {
  194.     // Must be connected.
  195.     if ( !IsState(OLAP_APP_CONNECTED) || !pData )
  196.         return E_FAIL;
  197.  
  198.     DBPROPIDSET        rgPropertyIDSets[1];
  199.     ULONG            cPropertySets  = 0;
  200.     DBPROPSET*        rgPropertySets = NULL;
  201.  
  202.     IDBProperties*  pIDBProperties = NULL;
  203.  
  204.     // Ask for interface to get properties
  205.     HRESULT  hr = m_pIDBSource->QueryInterface( IID_IDBProperties,
  206.                                                 (void**) &pIDBProperties );
  207.     if ( FAILED(hr) || !pIDBProperties )
  208.         goto CLEANUP;
  209.  
  210.     // We want only one property
  211.     rgPropertyIDSets[0].rgPropertyIDs   = &propId;
  212.     rgPropertyIDSets[0].cPropertyIDs    = 1;
  213.     rgPropertyIDSets[0].guidPropertySet = propSet;
  214.  
  215.     // Get property value.
  216.     hr = pIDBProperties->GetProperties ( 1, 
  217.                                          rgPropertyIDSets,
  218.                                          &cPropertySets,
  219.                                          &rgPropertySets );
  220.  
  221.     if ( FAILED(hr) || !cPropertySets || !rgPropertySets[0].cProperties ) 
  222.         goto CLEANUP;
  223.  
  224.     // Copy the value of the property 
  225.     *pData = rgPropertySets[0].rgProperties[0].vValue;
  226.  
  227. CLEANUP:
  228.     if ( rgPropertySets ) {
  229.         if ( rgPropertySets[0].rgProperties ) CoTaskMemFree(rgPropertySets[0].rgProperties);
  230.         CoTaskMemFree(rgPropertySets);
  231.     }
  232.  
  233.     if ( pIDBProperties ) pIDBProperties->Release();
  234.     return S_OK;
  235. }
  236.  
  237.  
  238. /////////////////////////////////////////////////////////////////////////////////////
  239. HRESULT     OLAPApp::OpenDataBase( LPTSTR  tstrDataBase )
  240.  
  241. // Open the specified OLAP database
  242. /////////////////////////////////////////////////////////////////////////////////////
  243. {
  244.     // Must be connected.
  245.     if ( !IsState(OLAP_APP_CONNECTED) || !tstrDataBase || !*tstrDataBase )
  246.         return E_FAIL;
  247.  
  248.     DBPROPSET    rgPropertySet[1];
  249.     DBPROP        rgProperties[1];
  250.     HRESULT     hr;
  251.  
  252.     IDBProperties*  pIDBProperties = NULL;
  253.  
  254.     // Initialize property values
  255.     VariantInit(&rgProperties[0].vValue);
  256.  
  257.     // Ask for interface to set properties
  258.     hr = m_pIDBSource->QueryInterface( IID_IDBProperties,
  259.                                                 (void**) &pIDBProperties );
  260.     if ( FAILED(hr) || !pIDBProperties )
  261.         goto CLEANUP;
  262.  
  263.     // Identify Property Set
  264.     rgPropertySet[0].rgProperties      = rgProperties;
  265.     rgPropertySet[0].cProperties     = 1;
  266.     rgPropertySet[0].guidPropertySet = DBPROPSET_DATASOURCE;
  267.  
  268.     // Fill in Data Source
  269.     rgProperties[0].dwPropertyID     = DBPROP_CURRENTCATALOG;
  270.     rgProperties[0].dwOptions         = DBPROPOPTIONS_REQUIRED;
  271.     rgProperties[0].colid             = DB_NULLID;
  272.     rgProperties[0].vValue.vt         = VT_BSTR;
  273.     rgProperties[0].vValue.bstrVal     = SysAllocString( tstrDataBase );
  274.     assert(rgProperties[0].vValue.bstrVal);
  275.  
  276.  
  277.     hr = pIDBProperties->SetProperties(1, rgPropertySet);
  278.     if ( FAILED(hr) )
  279.         goto CLEANUP;
  280.  
  281.     // Mark the state that we have database open
  282.     StateOn(OLAP_APP_DB_OPEN);
  283.  
  284. CLEANUP:
  285.  
  286.     VariantClear(&rgProperties[0].vValue);
  287.     if ( pIDBProperties ) pIDBProperties->Release();
  288.  
  289.     return hr;
  290. }
  291.  
  292. /////////////////////////////////////////////////////////////////////////////////////
  293. HRESULT  OLAPApp::ExecQuery( LPTSTR tstrQuery )
  294.  
  295. // Execute MDX query against OLE DB for OLAP provider
  296. /////////////////////////////////////////////////////////////////////////////////////
  297. {
  298.     HRESULT     hr;
  299.  
  300.     // Database should be open, query should be non-empty
  301.     if ( !IsState(OLAP_APP_DB_OPEN) || !tstrQuery )
  302.         return E_FAIL;
  303.  
  304.     // If we kept cellset for the previous query, release it, since
  305.     // OLAPApp know how to work with only one cellset at time.
  306.     if ( IsState(OLAP_APP_QUERY_EXECUTED) ) {
  307.         if ( m_pIMDDataset ) { m_pIMDDataset->Release(); m_pIMDDataset = NULL; }
  308.  
  309.         StateOff(OLAP_APP_QUERY_EXECUTED);
  310.     }
  311.  
  312.     // Put the text of the query to the command. The command was created during Init.
  313.     hr = m_pICommand->SetCommandText( MDGUID_MDX, tstrQuery );
  314.     if ( FAILED(hr) )
  315.         goto CLEANUP;
  316.  
  317.     // Execute the query
  318.     hr = m_pICommand->Execute(  NULL,
  319.                                 IID_IMDDataset,
  320.                                 NULL,        // pParams
  321.                                 NULL,        // pcRowsAffected
  322.                                 (IUnknown**) &m_pIMDDataset );
  323.  
  324.     if ( FAILED(hr) || !m_pIMDDataset )
  325.         goto CLEANUP;
  326.  
  327.     // Mark the state, that query has been executed and cellset allocated
  328.     StateOn(OLAP_APP_QUERY_EXECUTED);
  329.  
  330. CLEANUP:
  331.     return hr;
  332. }
  333.  
  334.  
  335.  
  336.  
  337.