示例#1
0
void foo()
{
	abc(  1,   2,   3);
	abc( 10,  20,  30);
	abc(100, 200, 300);
	cab(3, 2, 1, 0);
	brat(     "foo", 2000, 3000);
	brat("question",    2,  -42);
	brat(       "a",  -22,    1);
	while (1)
	{
		brat(     "foo", 2000, 3000);
		brat("question",    2,  -42);
		brat(       "a",  -22,    1);
	}
	brat("foo", 2000, 3000);
	brat(  "a",  -22,    1);
}
// CMSOmniProvRowset::Execute
// 1. Parse the SQL query,
// 2. Execute the query, and 
// 3. Build the initial rowset
HRESULT CMSOmniProvRowset::Execute(DBPARAMS * pParams, DBROWCOUNT* pcRowsAffected)
{
	USES_CONVERSION;
	CMSOmniProvRowset* pT = (CMSOmniProvRowset*) this;
	CMSOmniProvRowset::ObjectLock cab((CMSOmniProvRowset*) this);
	HRESULT hr;

	_bstr_t m_bstrFileName; 
	if ( FAILED(hr=pT->GetDataSource(m_bstrFileName)) )
		return hr;

	// Check the property value whether read/ updatabiliy property is set or not...
	_variant_t varUpd;
	GetPropValue(&DBPROPSET_ROWSET,DBPROP_UPDATABILITY, &varUpd);
	if ( 0 != varUpd.iVal )
	{
		// 1. a) Build the file's Schema m_prgColInfo from the '.sxt' file,
		//     b) Open the file '.txt', and 
		//     c) Fill the m_DBFile.m_Rows structure
		// Open in exclusive mode
		if (!m_DBFile.Open((LPCTSTR) m_bstrFileName,true))
			return DB_E_NOTABLE;
		if (!m_DBFile.FillRowArray()) 
				return E_FAIL;
	}
	else // Open in non-exclusive mode
	{
		if (!m_DBFile.Open((LPCTSTR) m_bstrFileName,false))
			return DB_E_NOTABLE;
		if (!m_DBFile.FillRowArray()) 
				return E_FAIL;
	}

	// Validate Command 
    // 2. PARSE the SQL Query here  (Only SELECT * FROM <Table_Name> is supported)
	TCHAR sep[] = " ";
	_bstr_t bstrSQL(pT->m_strCommandText);
	LPTSTR  pchNextToken = NULL;
	TCHAR * token = _tcstok_s((TCHAR*) bstrSQL, (TCHAR*) sep, &pchNextToken);

	if (!CheckTable((TCHAR*) token)) 
	{
		// The Rowset was created using the ICommand::Execute( )...
		// Only "SELECT * FROM Table_Name"  Queries are supported
		if(_tcsicmp(token,TEXT("select")) != 0)
		{
			ATLTRACE2(atlTraceDBProvider,0,(const TCHAR*) (_bstr_t("Query: '")+ bstrSQL + _bstr_t("' is not a valid Query\n")));
			return DB_E_ERRORSINCOMMAND;
		}
		ATLTRACE2(atlTraceDBProvider,0,(const TCHAR*) (_bstr_t("\tIt is a valid '")+_bstr_t(token) + _bstr_t("' Query\n")));
		
		TCHAR szTblNm[MAX_TABLE_NAME_SIZE];
		while (token != NULL)
		{
			_tcscpy_s(szTblNm, _countof(szTblNm), token);
			token= _tcstok_s(NULL,(TCHAR*) sep, &pchNextToken);
		}
		if (!CheckTable((TCHAR*) szTblNm)) 
			return DB_E_NOTABLE;
	}

	// Allocate proxy buffers based on the schema information 
	// Each CRow contains proxy buffer that the data is trasnferred to in the native 
	// format.  This information then needs to be copied out to the file in character format
	// on SetData() calls.

	CreateColInfo();

	AllocateProxyBuffers();

	if (pcRowsAffected != NULL)
		*pcRowsAffected = m_DBFile.m_Rows.GetCount();

	return S_OK;  
}
//CMSOmniProvRowset :: GetDataSource
// Retrieves the file name including the path to the schema '.sxt' file...
HRESULT CMSOmniProvRowset::GetDataSource(_bstr_t &m_bstrLoc)
{
	CMSOmniProvRowset* pT = (CMSOmniProvRowset*) this;
	CMSOmniProvRowset::ObjectLock cab((CMSOmniProvRowset*) this);
	CComPtr<IDBCreateCommand> spSession = NULL;
	CComPtr<IRowset> spRowset = NULL;

	HRESULT hr = pT->GetSite(IID_IDBCreateCommand, (void**) &spSession);
	if (SUCCEEDED(hr))  // The Rowset was created from an IOpenRowset::OpenRowset( )...
	{
			// Get to DBPROP_INIT_DATASOURCE property
			CComPtr<IDBCreateSession> spInit;
			CComPtr<IObjectWithSite> spCreator2 = NULL;
	
			if (FAILED(hr = spSession->QueryInterface(IID_IObjectWithSite,(void**) &spCreator2)))
			{
					ATLTRACE2(atlTraceDBProvider,0,"FATAL ERROR: Cannot get to the IObjectWithSite from ICommand...\n");
					return E_FAIL;
			}

			if (FAILED(hr = spCreator2->GetSite(IID_IDBCreateSession,(void**) &spInit)))
			{
					ATLTRACE2(atlTraceDBProvider,0,"FATAL ERROR: Cannot get to the IDBCreateSession from ICommand...\n");
					return E_FAIL;
			}
			// Initialize the property variables 
			ULONG               cPropertyIDSets =1;
			DBPROPIDSET   rgPropertyIDSets[1];
			ULONG              cPropertySets;
			DBPROPSET *        prgPropertySets;
			DBPROPID	rgPropId[1];

			rgPropId[0] = DBPROP_INIT_DATASOURCE;
			rgPropertyIDSets[0].rgPropertyIDs = rgPropId;
			rgPropertyIDSets[0].cPropertyIDs = 1;
			rgPropertyIDSets[0].guidPropertySet = DBPROPSET_DBINIT;
			CComPtr<IDBProperties> spProperties = NULL;

			hr = spInit->QueryInterface(IID_IDBProperties,(void**) &spProperties);
			if(FAILED(hr))
			{
				ATLTRACE2(atlTraceDBProvider,0,"FATAL ERROR: Cannot get to the IDBCreateSession'ss IDBProperties...\n");
				return hr;
			}

			spProperties->GetProperties(cPropertyIDSets, rgPropertyIDSets,&cPropertySets, &prgPropertySets) ;
			m_bstrLoc = _bstr_t(prgPropertySets->rgProperties[0].vValue);
	}
	else // The Rowset was created from ICommand::Execute( )
	{
		CComPtr<ICommand> spCommand=NULL;
		hr = pT->GetSite(IID_ICommand,(void**) &spCommand);
		if(FAILED(hr))
		{
			ATLTRACE2(atlTraceDBProvider,0,"FATAL ERROR: Cannot get to the ICommand of the Rowset...\n");
			return E_FAIL;
		}	

		CComPtr<IObjectWithSite> spCreator = NULL;
		if (FAILED(hr = spCommand->QueryInterface(IID_IObjectWithSite,(void**) &spCreator)))
		{
				ATLTRACE2(atlTraceDBProvider,0,"FATAL ERROR: Cannot get to the IObjectWithSite from ICommand...\n");
				return E_FAIL;
		}
		
		if (FAILED(hr = spCreator->GetSite(IID_IDBCreateCommand,(void**) &spSession)))
		{
				ATLTRACE2(atlTraceDBProvider,0,"FATAL ERROR: Cannot get to the IDBCreateSession from ICommand...\n");
				return E_FAIL;
		}

		CComPtr<IDBCreateSession> spInit;
		CComPtr<IObjectWithSite> spCreator2 = NULL;
		if (FAILED(hr = spSession->QueryInterface(IID_IObjectWithSite,(void**) &spCreator2)))
		{
				ATLTRACE2(atlTraceDBProvider,0,"FATAL ERROR: Cannot get to the IObjectWithSite from ICommand...\n");
				return E_FAIL;
		}
		if (FAILED(hr = spCreator2->GetSite(IID_IDBCreateSession,(void**) &spInit)))
		{
				ATLTRACE2(atlTraceDBProvider,0,"FATAL ERROR: Cannot get to the IDBCreateSession from ICommand...\n");
				return E_FAIL;
		}

		// Get to DBPROP_INIT_DATASOURCE
		ULONG			cPropertyIDSets =1;
		DBPROPIDSET		rgPropertyIDSets[1];
		ULONG			cPropertySets;
		DBPROPSET *		prgPropertySets;
		DBPROPID		rgPropId[1];

		rgPropId[0] = DBPROP_INIT_DATASOURCE;
		rgPropertyIDSets[0].rgPropertyIDs = rgPropId;
		rgPropertyIDSets[0].cPropertyIDs = 1;
		rgPropertyIDSets[0].guidPropertySet = DBPROPSET_DBINIT;
		
		CComPtr<IDBProperties> spProperties = NULL;
		hr = spInit->QueryInterface(IID_IDBProperties,(void**) &spProperties);
		if(FAILED(hr))
		{
			ATLTRACE2(atlTraceDBProvider,0,"FATAL ERROR: Cannot get to the IDBCreateSession'ss IDBProperties...\n");
			return hr;
		}

		spProperties->GetProperties(cPropertyIDSets, rgPropertyIDSets,&cPropertySets, &prgPropertySets) ;
		m_bstrLoc = _bstr_t(prgPropertySets->rgProperties[0].vValue);
	}
	return hr;
}