home *** CD-ROM | disk | FTP | other *** search
- //-----------------------------------------------------------------------------
- // Microsoft OLE DB QLAPDEMO Sample
- // Copyright (C) 1995-1998 Microsoft Corporation
- //
- // File: OLAPApp.cpp
- //
- // This file implements the class OLAPApp
- //
- //-----------------------------------------------------------------------------
-
- #include "OLAP.h"
-
- /////////////////////////////////////////////////////////////////////////////////////
- OLAPApp::OLAPApp()
-
- // Constructor for the OLAP application
- /////////////////////////////////////////////////////////////////////////////////////
- {
- m_dwState = 0x00000000;
-
- m_pIDBSource = NULL;
- m_pIDBSession = NULL;
-
- m_pICommand = NULL;
- m_pIMDDataset = NULL;
- }
-
- /////////////////////////////////////////////////////////////////////////////////////
- OLAPApp::~OLAPApp()
-
- // Destructor for the OLAP application
- /////////////////////////////////////////////////////////////////////////////////////
- {
- Reset();
- }
-
- /////////////////////////////////////////////////////////////////////////////////////
- HRESULT OLAPApp::Init()
-
- // Initialization for the OLAP application
- /////////////////////////////////////////////////////////////////////////////////////
- {
- // Initialize COM
- HRESULT hr = CoInitialize(NULL);
-
- if ( FAILED(hr) )
- return hr;
-
- // Mark that we are initialized
- StateOn(OLAP_APP_INITIALIZED);
-
- return hr;
- }
-
- /////////////////////////////////////////////////////////////////////////////////////
- HRESULT OLAPApp::Reset()
-
- // Reset for the OLAP application
- /////////////////////////////////////////////////////////////////////////////////////
- {
- // Disconnect
- Disconnect();
-
- // Uninitialize COM
- CoUninitialize();
-
- // Mark that we are uninitialized
- StateOff(OLAP_APP_INITIALIZED);
-
- return S_OK;
- }
-
-
- /////////////////////////////////////////////////////////////////////////////////////
- HRESULT OLAPApp::Connect( LPTSTR pServer )
-
- // Is the specified string an argument ?
- /////////////////////////////////////////////////////////////////////////////////////
- {
- // App must be initialized.
- if ( !IsState(OLAP_APP_INITIALIZED) )
- return E_FAIL;
-
- // Disconnect from previous connection
- if ( IsState(OLAP_APP_CONNECTED) )
- Disconnect();
-
- DBPROPSET rgPropertySet[1];
- DBPROP rgProperties[1];
-
- IDBInitialize* pIDBInitialize = NULL;
- IDBProperties* pIDBProperties = NULL;
-
- // Initialize property values
- VariantInit(&rgProperties[0].vValue);
-
- // Create IDInitialize object
- // This call uses GUID CLSID_MSOLAP which refers to Microsoft's
- // implementation of OLE DB for OLAP. It can be substituted with
- // GUID of any other OLE DB for OLAP provider. The rest of the code
- // should work with any provider correctly
- HRESULT hr = CoCreateInstance( CLSID_MSOLAP,
- NULL,
- CLSCTX_INPROC_SERVER,
- IID_IDBInitialize,
- (LPVOID*) &pIDBInitialize );
- if ( FAILED(hr) || !pIDBInitialize )
- goto CLEANUP;
-
- // Ask for interface to set properties
- hr = pIDBInitialize->QueryInterface( IID_IDBProperties,
- (void**) &pIDBProperties );
-
- // Identify Property Set
- rgPropertySet[0].rgProperties = rgProperties;
- rgPropertySet[0].cProperties = 1;
- rgPropertySet[0].guidPropertySet = DBPROPSET_DBINIT;
-
- // Fill in Data Source
- rgProperties[0].dwPropertyID = DBPROP_INIT_DATASOURCE;
- rgProperties[0].dwOptions = DBPROPOPTIONS_REQUIRED;
- rgProperties[0].colid = DB_NULLID;
- rgProperties[0].vValue.vt = VT_BSTR;
- rgProperties[0].vValue.bstrVal = SysAllocString( ( pServer && *pServer ) ? pServer : TEXT("localhost") );
- assert(rgProperties[0].vValue.bstrVal);
-
- // Set connection properties
- hr = pIDBProperties->SetProperties(1, rgPropertySet );
- if ( FAILED(hr) )
- goto CLEANUP;
-
- // Initialize the Session.
- hr = pIDBInitialize->Initialize();
- if ( FAILED(hr) )
- goto CLEANUP;
-
- // Ask for interface to create session
- hr = pIDBInitialize->QueryInterface( IID_IDBCreateSession,
- (void**) &m_pIDBSource );
- if ( FAILED(hr) || !m_pIDBSource )
- goto CLEANUP;
-
- // Create session. We use IDBCreateCommand interface to represent
- // session
- hr = m_pIDBSource->CreateSession( NULL,
- IID_IDBCreateCommand,
- (IUnknown**) &m_pIDBSession );
- if ( FAILED(hr) || !m_pIDBSession )
- goto CLEANUP;
-
- // Create text command. This command is reused for every query, so it
- // is kind of optimization to create it here and use for every query
- hr = m_pIDBSession->CreateCommand( NULL, IID_ICommandText, (IUnknown**) &m_pICommand );
- if ( FAILED(hr) || !m_pICommand )
- goto CLEANUP;
-
- // Mark that we are connected
- StateOn(OLAP_APP_CONNECTED);
- StateOn(OLAP_APP_DB_OPEN);
-
- CLEANUP:
-
- VariantClear(&rgProperties[0].vValue);
- if ( pIDBInitialize ) pIDBInitialize->Release();
- if ( pIDBProperties ) pIDBProperties->Release();
-
- return hr;
- }
-
-
- /////////////////////////////////////////////////////////////////////////////////////
- HRESULT OLAPApp::Disconnect()
-
- // Disconnect from OLE DB for OLAP provider
- /////////////////////////////////////////////////////////////////////////////////////
- {
- if ( m_pIMDDataset ) { m_pIMDDataset->Release(); m_pIMDDataset = NULL; }
- if ( m_pICommand ) { m_pICommand->Release(); m_pICommand = NULL; }
- if ( m_pIDBSession ) { m_pIDBSession->Release(); m_pIDBSession = NULL; }
- if ( m_pIDBSource ) { m_pIDBSource->Release(); m_pIDBSource = NULL; }
-
- StateOff(OLAP_APP_CONNECTED);
-
- return S_OK;
- }
-
-
- /////////////////////////////////////////////////////////////////////////////////////
- HRESULT OLAPApp::GetProperty( GUID propSet, DBPROPID propId, VARIANT* pData )
-
- // Get the requested property
- /////////////////////////////////////////////////////////////////////////////////////
- {
- // Must be connected.
- if ( !IsState(OLAP_APP_CONNECTED) || !pData )
- return E_FAIL;
-
- DBPROPIDSET rgPropertyIDSets[1];
- ULONG cPropertySets = 0;
- DBPROPSET* rgPropertySets = NULL;
-
- IDBProperties* pIDBProperties = NULL;
-
- // Ask for interface to get properties
- HRESULT hr = m_pIDBSource->QueryInterface( IID_IDBProperties,
- (void**) &pIDBProperties );
- if ( FAILED(hr) || !pIDBProperties )
- goto CLEANUP;
-
- // We want only one property
- rgPropertyIDSets[0].rgPropertyIDs = &propId;
- rgPropertyIDSets[0].cPropertyIDs = 1;
- rgPropertyIDSets[0].guidPropertySet = propSet;
-
- // Get property value.
- hr = pIDBProperties->GetProperties ( 1,
- rgPropertyIDSets,
- &cPropertySets,
- &rgPropertySets );
-
- if ( FAILED(hr) || !cPropertySets || !rgPropertySets[0].cProperties )
- goto CLEANUP;
-
- // Copy the value of the property
- *pData = rgPropertySets[0].rgProperties[0].vValue;
-
- CLEANUP:
- if ( rgPropertySets ) {
- if ( rgPropertySets[0].rgProperties ) CoTaskMemFree(rgPropertySets[0].rgProperties);
- CoTaskMemFree(rgPropertySets);
- }
-
- if ( pIDBProperties ) pIDBProperties->Release();
- return S_OK;
- }
-
-
- /////////////////////////////////////////////////////////////////////////////////////
- HRESULT OLAPApp::OpenDataBase( LPTSTR tstrDataBase )
-
- // Open the specified OLAP database
- /////////////////////////////////////////////////////////////////////////////////////
- {
- // Must be connected.
- if ( !IsState(OLAP_APP_CONNECTED) || !tstrDataBase || !*tstrDataBase )
- return E_FAIL;
-
- DBPROPSET rgPropertySet[1];
- DBPROP rgProperties[1];
- HRESULT hr;
-
- IDBProperties* pIDBProperties = NULL;
-
- // Initialize property values
- VariantInit(&rgProperties[0].vValue);
-
- // Ask for interface to set properties
- hr = m_pIDBSource->QueryInterface( IID_IDBProperties,
- (void**) &pIDBProperties );
- if ( FAILED(hr) || !pIDBProperties )
- goto CLEANUP;
-
- // Identify Property Set
- rgPropertySet[0].rgProperties = rgProperties;
- rgPropertySet[0].cProperties = 1;
- rgPropertySet[0].guidPropertySet = DBPROPSET_DATASOURCE;
-
- // Fill in Data Source
- rgProperties[0].dwPropertyID = DBPROP_CURRENTCATALOG;
- rgProperties[0].dwOptions = DBPROPOPTIONS_REQUIRED;
- rgProperties[0].colid = DB_NULLID;
- rgProperties[0].vValue.vt = VT_BSTR;
- rgProperties[0].vValue.bstrVal = SysAllocString( tstrDataBase );
- assert(rgProperties[0].vValue.bstrVal);
-
-
- hr = pIDBProperties->SetProperties(1, rgPropertySet);
- if ( FAILED(hr) )
- goto CLEANUP;
-
- // Mark the state that we have database open
- StateOn(OLAP_APP_DB_OPEN);
-
- CLEANUP:
-
- VariantClear(&rgProperties[0].vValue);
- if ( pIDBProperties ) pIDBProperties->Release();
-
- return hr;
- }
-
- /////////////////////////////////////////////////////////////////////////////////////
- HRESULT OLAPApp::ExecQuery( LPTSTR tstrQuery )
-
- // Execute MDX query against OLE DB for OLAP provider
- /////////////////////////////////////////////////////////////////////////////////////
- {
- HRESULT hr;
-
- // Database should be open, query should be non-empty
- if ( !IsState(OLAP_APP_DB_OPEN) || !tstrQuery )
- return E_FAIL;
-
- // If we kept cellset for the previous query, release it, since
- // OLAPApp know how to work with only one cellset at time.
- if ( IsState(OLAP_APP_QUERY_EXECUTED) ) {
- if ( m_pIMDDataset ) { m_pIMDDataset->Release(); m_pIMDDataset = NULL; }
-
- StateOff(OLAP_APP_QUERY_EXECUTED);
- }
-
- // Put the text of the query to the command. The command was created during Init.
- hr = m_pICommand->SetCommandText( MDGUID_MDX, tstrQuery );
- if ( FAILED(hr) )
- goto CLEANUP;
-
- // Execute the query
- hr = m_pICommand->Execute( NULL,
- IID_IMDDataset,
- NULL, // pParams
- NULL, // pcRowsAffected
- (IUnknown**) &m_pIMDDataset );
-
- if ( FAILED(hr) || !m_pIMDDataset )
- goto CLEANUP;
-
- // Mark the state, that query has been executed and cellset allocated
- StateOn(OLAP_APP_QUERY_EXECUTED);
-
- CLEANUP:
- return hr;
- }
-
-
-
-
-