#include "sftraceback.h"

/************************************************************************/
/*						SFGetDataSourceProperties()						*/
/*	Get Data Source Prop from a ICommand pointer						*/
/*	Inteface passed in is released.										*/
/************************************************************************/
IDBProperties *SFGetDataSourceProperties(ICommand *pICommand)
{
	HRESULT	hr;
	IGetDataSource *pIGetDataSource; // Session Interface

	if (!pICommand)
		return NULL;

	// IGetDataSource is a mandatory interface on session
	hr = pICommand->GetDBSession(IID_IGetDataSource,(IUnknown **) &pIGetDataSource);
	pICommand->Release();

	if (SUCCEEDED(hr))
	{
		return SFGetDataSourceProperties(pIGetDataSource);	
	}

	return NULL;
}


/************************************************************************/
/*						SFGetDataSourceProperties()						*/
/*	Get Data Source Prop from a IRowsetInfo pointer						*/
/*	Inteface passed in is released.										*/
/************************************************************************/
IDBProperties *SFGetDataSourceProperties(IRowsetInfo* pIRInfo)
{
	HRESULT hr;

	if (!pIRInfo)
		return NULL;

	// The parent of the RInfo can be either a command or session.

	// Try the command first.
	ICommand *pICommand;
	hr = pIRInfo->GetSpecification(IID_ICommand, (IUnknown **) &pICommand);

	if (SUCCEEDED(hr))
	{
		pIRInfo->Release();
		return SFGetDataSourceProperties(pICommand);
	}

	// Try the session now.
	IGetDataSource *pIGetDataSource;
	hr = pIRInfo->GetSpecification(IID_IGetDataSource,(IUnknown **) &pIGetDataSource);
	pIRInfo->Release();

	if (SUCCEEDED(hr))
	{
		return SFGetDataSourceProperties(pIGetDataSource);
	}

	return NULL;
}

/************************************************************************/
/*						SFGetDataSourceProperties()						*/
/*	Get Data Source proper from a Session pointer (IGetDataSource)		*/
/*	Interface passed in is released.									*/
/************************************************************************/
IDBProperties *SFGetDataSourceProperties(IGetDataSource *pIGetDataSource)
{
	if (!pIGetDataSource)
		return NULL;

	IDBProperties *pIDBProp;

	pIGetDataSource->GetDataSource(IID_IDBProperties, (IUnknown **) &pIDBProp);
	pIGetDataSource->Release();

	return pIDBProp;
}

/************************************************************************/
/*						SFGetDataSourceProperties()						*/
/*	Get Data Source proper from an IUnknown								*/
/*	but must be a rowset/command or session								*/
/*	Interface passed in is released.									*/
/************************************************************************/
IDBProperties *SFGetDataSourceProperties(IUnknown *pIUnknown)
{
	HRESULT	hr;

	// Rowset
	IRowsetInfo *pRInfo;

	hr = pIUnknown->QueryInterface(IID_IRowsetInfo, (void **) &pRInfo);
	if (SUCCEEDED(hr))
	{
		pIUnknown->Release();
		return SFGetDataSourceProperties(pRInfo);
	}

	// Command
	ICommand	*pICommand;
	
	hr = pIUnknown->QueryInterface(IID_ICommand, (void **) &pICommand);
	if (SUCCEEDED(hr))
	{
		pIUnknown->Release();
		return SFGetDataSourceProperties(pICommand);
	}

	// Session
	IGetDataSource *pIGetDataSource;

	hr = pIUnknown->QueryInterface(IID_IGetDataSource, (void **) &pIGetDataSource);
	if (SUCCEEDED(hr))
	{
		pIUnknown->Release();
		return SFGetDataSourceProperties(pIGetDataSource);
	}

	// Data Source itself
	IDBProperties *pIDBProperties;
	pIUnknown->QueryInterface(IID_IDBProperties, (void **) &pIDBProperties);
	pIUnknown->Release();

	return pIDBProperties;
}