DBSESS.CPP

//-------------------------------------------------------------------- 
// Microsoft OLE DB Sample Provider
// (C) Copyright 1994 - 1996 Microsoft Corporation. All Rights Reserved.
//
// @doc
//
// @module DBSESS.CPP | CDBSession object implementation
//
//

// Includes ------------------------------------------------------------------

#include "headers.h"


// Code ----------------------------------------------------------------------

// CDBSession::CDBSession --------------------------------------------------
//
// @mfunc Constructor for this class
//
// @rdesc NONE
//
CDBSession::CDBSession
(
LPUNKNOWN pUnkOuter //@parm IN | Outer Unkown Pointer
)
{
// Initialize simple member vars
m_cRef = 0L;
m_pUnkOuter = pUnkOuter;
*m_szPath = '\0';
m_fRowsetCreated = FALSE;

// Initially, NULL all contained interfaces
m_pIGetDataSource= NULL;
m_pIOpenRowset= NULL;

// Pointer to parent object
m_pCDataSource= NULL;

// Increment global object count.
OBJECT_CONSTRUCTED();

return;
}


// CDBSession::~CDBSession -------------------------------------------------
//
// @mfunc Destructor for this class
//
// @rdesc NONE
//
CDBSession:: ~CDBSession
(
void
)
{
// Free contained interfaces
delete m_pIGetDataSource;
delete m_pIOpenRowset;

m_pCDataSource->Release();
m_pCDataSource->RemoveSession();

// Decrement global object count.
OBJECT_DESTRUCTED();

return;
}


// CDBSession::FInit --------------------------------------------------------
//
// @mfunc Initialize the command Object
//
// @rdesc Did the Initialization Succeed
// @flag TRUE | Initialization succeeded
// @flag FALSE | Initialization failed
//
BOOL CDBSession::FInit
(
char*szPath,
CDataSource*pCDataSource
)
{
LPUNKNOWN pIUnknown = (LPUNKNOWN) this;

if (m_pUnkOuter)
pIUnknown = m_pUnkOuter;

// just make a copy for ourselves.
if (!lstrcpyn( m_szPath, szPath, MAX_PATH ))
return FALSE;

// Establish parent object pointer
assert( pCDataSource );
m_pCDataSource= pCDataSource;

m_pCDataSource->AddRef();

// Allocate contained interface objects
m_pIGetDataSource= new CImpIGetDataSource( this, pIUnknown );
m_pIOpenRowset= new CImpIOpenRowset( this, pIUnknown );
m_pISessionProperties= new CImpISessionProperties( this, pIUnknown );

return (BOOL) (m_pIGetDataSource && m_pIOpenRowset);
}



// CDBSession::QueryInterface -----------------------------------------------
//
// @mfunc Returns a pointer to a specified interface. Callers use
// QueryInterface to determine which interfaces the called object
// supports.
//
// @rdesc HRESULT indicating the status of the method
// @flag S_OK | Interface is supported and ppvObject is set.
// @flag E_NOINTERFACE | Interface is not supported by the object
// @flag E_INVALIDARG | One or more arguments are invalid.
//
STDMETHODIMP CDBSession::QueryInterface
(
REFIID riid, //@parm IN | Interface ID of the interface being queried for.
LPVOID * ppv //@parm OUT | Pointer to interface that was instantiated
)
{
// Is the pointer bad?
if (ppv == NULL)
return ResultFromScode( E_INVALIDARG );

// Place NULL in *ppv in case of failure
*ppv = NULL;

// This is the non-delegating IUnknown implementation
if (riid == IID_IUnknown)
*ppv = (LPVOID) this;
else if (!m_pCDataSource->m_fDSOInitialized)
return ResultFromScode(E_UNEXPECTED);
else if (riid == IID_IGetDataSource)
*ppv = (LPVOID) m_pIGetDataSource;
else if (riid == IID_IOpenRowset)
*ppv = (LPVOID) m_pIOpenRowset;
else if (riid == IID_ISessionProperties)
*ppv = (LPVOID) m_pISessionProperties;

// If we're going to return an interface, AddRef it first
if (*ppv)
{
((LPUNKNOWN) *ppv)->AddRef();
return ResultFromScode( S_OK );
}
else
return ResultFromScode( E_NOINTERFACE );
}


// CDBSession::AddRef -------------------------------------------------------
//
// @mfunc Increments a persistence count for the object
//
// @rdesc Current reference count
//
STDMETHODIMP_( ULONG ) CDBSession::AddRef
(
void
)
{
return ++m_cRef;
}


// CDBSession::Release ------------------------------------------------------
//
// @mfunc Decrements a persistence count for the object and if
// persistence count is 0, the object destroys itself.
//
// @rdesc Current reference count
//
STDMETHODIMP_( ULONG ) CDBSession::Release
(
void
)
{
if (!--m_cRef)
{
delete this;
return 0;
}

return m_cRef;
}

//-----------------------------------------------------------------------------
// CImpIGetDataSource::GetDataSource
//
// @mfunc Retrieve an interface pointer on the session object
//
// @rdesc
//@flag S_OK | Session Object Interface returned
//@flag E_INVALIDARG | ppDataSource was NULL
//@flag E_NOINTERFACE | IID not supported
//
STDMETHODIMP CImpIGetDataSource::GetDataSource
(
REFIIDriid,// @parm IN | IID desired
IUnknown**ppDataSource// @parm OUT | ptr to interface
)
{
// Check Function Arguments
if( ppDataSource == NULL )
return ResultFromScode(E_INVALIDARG);

assert( m_pObj->m_pCDataSource );
return ((m_pObj->m_pCDataSource)->QueryInterface(riid, (LPVOID*)ppDataSource));
}

// ISessionProperties::GetProperties ----------------------------------------------------
//
// @mfunc Returns current settings of all properties in the DBPROPFLAGS_SESSION property
//group
// @rdesc HRESULT
// @flag S_OK | The method succeeded
// @flag E_INVALIDARG | pcProperties or prgPropertyInfo was NULL
// @flag E_OUTOFMEMORY | Out of memory
//
STDMETHODIMP CImpISessionProperties::GetProperties
(
ULONGcPropertySets,//@parm IN | count of restiction guids
const DBPROPIDSETrgPropertySets[],//@parm IN | restriction guids
ULONG* pcProperties,//@parm OUT | count of properties returned
DBPROPSET**prgProperties//@parm OUT | property information returned
)
{
assert( m_pObj );
assert( m_pObj->m_pUtilProp );

// just pass this call on to the utility object that manages our properties
return m_pObj->m_pUtilProp->GetProperties(
cPropertySets,
rgPropertySets,
pcProperties,
prgProperties );
}


// CImpISessionProperties::SetProperties --------------------------------------------
//
// @mfunc Set properties in the DBPROPFLAGS_SESSION property group
//
// @rdesc HRESULT
// @flag E_INVALIDARG | cProperties was not equal to 0 and rgProperties was NULL
// @flag E_NOTIMPL| this method is not implemented
//
STDMETHODIMP CImpISessionProperties::SetProperties
(
ULONGcProperties,
DBPROPSETrgProperties[]
)
{
assert( m_pObj );
assert( m_pObj->m_pUtilProp );

// just pass this call on to the utility object that manages our properties
return m_pObj->m_pUtilProp->SetProperties(
cProperties,
rgProperties);
}