⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ctable.cpp

📁 用测试OLE DB提供者的一个程序。能够测试出OEL DB提供者到底实现了哪些接口?很灵的。
💻 CPP
📖 第 1 页 / 共 5 页
字号:
		return NULL;
	
	WCHAR* pwszColName = pColInfo->pwszName;
	
	if(pwszColName == NULL)
	{
		pwszColName = ((iOrdinal==0) ? pwszBookmarkName : pwszNullName);
	}
	else if(pwszColName[0] == wEOL)
	{
		pwszColName = ((iOrdinal==0) ? pwszBookmarkName : pwszEmptyName);
	}

	ASSERT(pwszColName);
	return pwszColName;
}


/////////////////////////////////////////////////////////////////
// DBCOLUMNINFO* CRowset::GetColInfo
//
/////////////////////////////////////////////////////////////////
DBCOLUMNINFO* CRowset::GetColInfo(ULONG iOrdinal)
{
	//Due to the bookmark column be present or absent causes the ColInfo
	//to not be lined exactly (1:1) to the Ordinal position...

	//no-ops
	if(m_cColumns==0 || m_rgColumnInfo == NULL || (iOrdinal==0 && m_rgColumnInfo[0].iOrdinal!=0))
		return NULL;
	
	if(m_rgColumnInfo[0].iOrdinal==0)
	{
		//[0,1,2,3,4,5...]
		ASSERT(iOrdinal < m_cColumns);
		ASSERT(m_rgColumnInfo[iOrdinal].iOrdinal == iOrdinal);
		return &m_rgColumnInfo[iOrdinal];
	}
	else
	{
		//[1,2,3,4,5...]
		ASSERT(iOrdinal && iOrdinal <= m_cColumns);
		ASSERT(m_rgColumnInfo[iOrdinal-1].iOrdinal == iOrdinal);
		return &m_rgColumnInfo[iOrdinal-1];
	}
	
	return NULL;
}


/////////////////////////////////////////////////////////////////
// DBBINDING* CRowset::GetBinding
//
/////////////////////////////////////////////////////////////////
DBBINDING* CRowset::GetBinding(ULONG iOrdinal)
{
	//Due to the bookmark column be present or absent causes the Binding
	//to not be lined exactly (1:1) to the Ordinal position...

	//no-ops
	if(m_cBindings==0 || m_rgBindings == NULL || (iOrdinal==0 && m_rgBindings[0].iOrdinal!=0))
		return NULL;
	
	//Bookmark
	if(m_rgBindings[0].iOrdinal == 0)
	{
		//[0,1,2,3,4,5...]
		ASSERT(iOrdinal < m_cColumns);
		ASSERT(m_rgBindings[iOrdinal].iOrdinal == iOrdinal);
		return &m_rgBindings[iOrdinal];
	}
	else
	{
		//[1,2,3,4,5...]
		ASSERT(iOrdinal && iOrdinal <= m_cColumns);
		ASSERT(m_rgBindings[iOrdinal-1].iOrdinal == iOrdinal);
		return &m_rgBindings[iOrdinal-1];
	}
	
	return NULL;
}


/////////////////////////////////////////////////////////////////
// HRESULT CRowset::ValidateRow
//
/////////////////////////////////////////////////////////////////
HRESULT CRowset::ValidateRow(HROW hRow, ULONG ulRefCount)
{
	//Warn user about NULL Rowhandles...
	if(hRow == DB_NULL_HROW || ulRefCount == 0)
	{
		if(IDNO == wMessageBox
			(
				NULL, 
				MB_TASKMODAL | MB_ICONQUESTION | MB_YESNO | MB_DEFBUTTON1, 
				wsz_ERROR, 
				L"hRow == 0x%08x\n"
				L"%s\n\n"
				L"This may crash your Provider...\n"
				L"Do you wish to continue anyway?", 
				hRow,
				hRow == DB_NULL_HROW ? L"Invalid Row Handle" : L"Released Row Handle"
				)
			)
			return E_FAIL;
	}

	return S_OK;
}

/////////////////////////////////////////////////////////////////
// HRESULT CRowset::ValidateAccessor
//
/////////////////////////////////////////////////////////////////
HRESULT CRowset::ValidateAccessor(HACCESSOR hAccessor, ULONG ulRefCount)
{
	//Warn user about NULL Accessor handle...
	if(hAccessor == DB_NULL_HACCESSOR || ulRefCount == 0)
	{
		if(IDNO == wMessageBox
			(
				NULL, 
				MB_TASKMODAL | MB_ICONQUESTION | MB_YESNO | MB_DEFBUTTON1, 
				wsz_ERROR, 
				L"hAccessor == 0x%08x\n"
				L"%s\n\n"
				L"This may crash your Provider...\n"
				L"Do you wish to continue anyway?", 
				hAccessor,
				hAccessor == DB_NULL_HROW ? L"Invalid Accessor Handle" : L"Released Accessor Handle"
			)
		  )
			return E_FAIL;
	}

	return S_OK;
}

/////////////////////////////////////////////////////////////////
// HRESULT CRowset::GetData
//
/////////////////////////////////////////////////////////////////
HRESULT CRowset::GetData(HROW hRow, HACCESSOR hAccessor, void* pData)
{
	//Since GetData is the only OLEDB method that is allowed to crash
	//if given invalid argumenets (for effiecientcy - provider speicific
	//weither it validates arguments), we should check the params and at the 
	//very least warn the user...
	ASSERT(m_pIRowset);
	HRESULT hr = E_FAIL;
	HWND hWnd = m_pCMDIChild->m_hWnd;
	CListBox* pCListBox = m_pCMDIChild->m_pCListBox;

	//Defaults
	if(hAccessor == NULL)
		hAccessor = m_hAccessor;
	if(pData == NULL)
		pData = m_pData;

	//Check Arguments...
	TESTC(hr = ValidateRow(hRow));
	TESTC(hr = ValidateAccessor(hAccessor));

	//GetData
 	if (m_eRowsetSource == ROWSET_FROMCOMMANDDATASET)
 	{
		hr = m_pCDataset->GetCellData(hRow, hAccessor, pData);
 	}
 	else 
 	{
 		TESTC(pCListBox->OutputPreMethod("IRowset:GetData(0x%08x, 0x%08x, 0x%08x)", hRow, hAccessor, pData));
		XTEST(hWnd, hr = m_pIRowset->GetData(hRow, hAccessor, pData));
		TESTC(pCListBox->OutputPostMethod(hr, "IRowset:GetData(0x%08x, 0x%08x, 0x%08x)", hRow, hAccessor, pData));
	}

CLEANUP:
	return hr;
}	
	

/////////////////////////////////////////////////////////////////
// HRESULT CRowset::GetOriginalData
//
/////////////////////////////////////////////////////////////////
HRESULT CRowset::GetOriginalData(HROW hRow, HACCESSOR hAccessor, void* pData)
{
	//Since GetData is the only OLEDB method that is allowed to crash
	//if given invalid argumenets (for effiecientcy - provider speicific
	//weither it validates arguments), we should check the params and at the 
	//very least warn the user...
	ASSERT(m_pIRowsetUpdate);
	HRESULT hr = S_OK;
	HWND hWnd = m_pCMDIChild->m_hWnd;
	CListBox* pCListBox = m_pCMDIChild->m_pCListBox;

	//Defaults
	if(hAccessor == NULL)
		hAccessor = m_hAccessor;
	if(pData == NULL)
		pData = m_pData;

	//Check Arguments...
	TESTC(hr = ValidateRow(hRow));
	TESTC(hr = ValidateAccessor(hAccessor));

	//GetOriginalData
	TESTC(pCListBox->OutputPreMethod("IRowsetUpdate:GetOriginalData(0x%08x, 0x%08x, 0x%08x)", hRow, hAccessor, pData));
	XTEST(hWnd, hr = m_pIRowsetUpdate->GetOriginalData(hRow, hAccessor, pData));
	TESTC(pCListBox->OutputPostMethod(hr, "IRowsetUpdate:GetOriginalData(0x%08x, 0x%08x, 0x%08x)", hRow, hAccessor, pData));

CLEANUP:
	return hr;
}	


/////////////////////////////////////////////////////////////////
// HRESULT CRowset::GetVisibleData
//
/////////////////////////////////////////////////////////////////
HRESULT CRowset::GetVisibleData(HROW hRow, HACCESSOR hAccessor, void* pData)
{
	//Since GetData is the only OLEDB method that is allowed to crash
	//if given invalid argumenets (for effiecientcy - provider speicific
	//weither it validates arguments), we should check the params and at the 
	//very least warn the user...
	ASSERT(m_pIRowsetResynch);
	HRESULT hr = S_OK;
	HWND hWnd = m_pCMDIChild->m_hWnd;
	CListBox* pCListBox = m_pCMDIChild->m_pCListBox;

	//Defaults
	if(hAccessor == NULL)
		hAccessor = m_hAccessor;
	if(pData == NULL)
		pData = m_pData;

	//Check Arguments...
	TESTC(hr = ValidateRow(hRow));
	TESTC(hr = ValidateAccessor(hAccessor));

	//GetVisibleData
	TESTC(pCListBox->OutputPreMethod("IRowsetResynch:GetVisibleData(0x%08x, 0x%08x, 0x%08x)", hRow, hAccessor, pData));
	XTEST(hWnd, hr = m_pIRowsetResynch->GetVisibleData(hRow, hAccessor, pData));
	TESTC(pCListBox->OutputPostMethod(hr, "IRowsetResynch:GetVisibleData(0x%08x, 0x%08x, 0x%08x)", hRow, hAccessor, pData));

CLEANUP:
	return hr;
}	


/////////////////////////////////////////////////////////////////
// HRESULT CRowset::GetBookmark
//
/////////////////////////////////////////////////////////////////
HRESULT CRowset::GetBookmark(HROW hRow, ULONG* pcbBookmark, BYTE** ppBookmark, void** ppData)
{
	ASSERT(pcbBookmark);
	ASSERT(ppBookmark);
	HRESULT hr = S_OK;
	*pcbBookmark = 0;
	*ppBookmark = NULL;
	
	DBSTATUS dwStatus = 0;
	DWORD dwLength = 0;
	
	if(m_cBmkBindings==0 || m_rgBmkBindings==NULL)
		return E_FAIL;

	//Allocate enough space for the bookmark
	//And Status Length of course!
	SAFE_ALLOC(*ppData, BYTE, m_rgBmkBindings[0].cbMaxLen + sizeof(DBSTATUS) + sizeof(ULONG) + 10);

	//GetData to obtain Bookmark value (delegate)
	TESTC(hr = GetData(hRow, m_hBmkAccessor, *ppData));
	
	//Output Data
	*pcbBookmark = BINDING_LENGTH(m_rgBmkBindings[0], *ppData);
	*ppBookmark  = (BYTE*)&BINDING_VALUE(m_rgBmkBindings[0], *ppData);

CLEANUP:
	return hr;
}

/////////////////////////////////////////////////////////////////
// HRESULT CRowset::GetNextRows
//
/////////////////////////////////////////////////////////////////
HRESULT CRowset::GetNextRows(LONG lOffset, LONG cRows, ULONG* pcRowsObtained, HROW** prghRows)
{
	ASSERT(m_pIRowset);
	HRESULT hr = S_OK;
	HWND hWnd = m_pCMDIChild->m_hWnd;
	CListBox* pCListBox = m_pCMDIChild->m_pCListBox;

	ULONG cRowsObtained = 0;
	HROW* rghRows = NULL;
	if(prghRows)
		rghRows = *prghRows;

	//GetNextRows
	TESTC(pCListBox->OutputPreMethod("IRowset::GetNextRows(NULL, %d, %d, &%d, &0x%08x)", lOffset, cRows, cRowsObtained, rghRows));
	XTEST(hWnd, hr = m_pIRowset->GetNextRows(
		NULL,						// hChapter
		lOffset,					// lOffset
		cRows,						// cRows
		&cRowsObtained,				// cRowsObtained
		&rghRows));					// rghRows
	TESTC(pCListBox->OutputPostMethod(hr, "IRowset::GetNextRows(NULL, %d, %d, &%d, &0x%08x)", lOffset, cRows, cRowsObtained, rghRows));

CLEANUP:
	if(pcRowsObtained)
		*pcRowsObtained = cRowsObtained;
	
	if(prghRows)
		*prghRows = rghRows;
	else
		SAFE_FREE(rghRows);
	return hr;
}	


/////////////////////////////////////////////////////////////////
// HRESULT CRowset::AddRefRows
//
/////////////////////////////////////////////////////////////////
HRESULT CRowset::AddRefRows(ULONG cRows, HROW* rghRows, ULONG* rgRefCounts)
{
	ASSERT(m_pIRowset);
	HRESULT hr = S_OK;
	HWND hWnd = m_pCMDIChild->m_hWnd;
	CListBox* pCListBox = m_pCMDIChild->m_pCListBox;

	//Alloc Array for Status
	DBROWSTATUS* rgRowStatus = NULL;
	SAFE_ALLOC(rgRowStatus, DBROWSTATUS, cRows);

	//AddRefRows
	TESTC(hr = pCListBox->OutputPreMethod("IRowset::AddRefRows(%d, 0x%08x, 0x%08x, 0x%08x)", cRows, rghRows, rgRefCounts, rgRowStatus));
	XTEST(hWnd, hr = m_pIRowset->AddRefRows(cRows, rghRows, rgRefCounts, rgRowStatus));
	if(hr==DB_S_ERRORSOCCURRED || hr==DB_E_ERRORSOCCURRED)
		DisplayRowErrors(hWnd, cRows, rghRows, rgRowStatus);
	TESTC(hr = pCListBox->OutputPostMethod(hr, "IRowset::AddRefRows(%d, 0x%08x, 0x%08x, 0x%08x)", cRows, rghRows, rgRefCounts, rgRowStatus));

CLEANUP:
	SAFE_FREE(rgRowStatus);
	return hr;
}	


/////////////////////////////////////////////////////////////////
// HRESULT CRowset::ReleaseRows
//
/////////////////////////////////////////////////////////////////
HRESULT CRowset::ReleaseRows(ULONG cRows, HROW* rghRows, ULONG* rgRefCounts)
{
	ASSERT(m_pIRowset);
	HRESULT hr = S_OK;
	HWND hWnd = m_pCMDIChild->m_hWnd;
	CListBox* pCListBox = m_pCMDIChild->m_pCListBox;

	//Alloc Array for Status
	DBROWSTATUS* rgRowStatus = NULL;
	SAFE_ALLOC(rgRowStatus, DBROWSTATUS, cRows);

	//ReleaseRows
	TESTC(hr = pCListBox->OutputPreMethod("IRowset::ReleaseRows(%d, 0x%08x, NULL, 0x%08x, 0x%08x)", cRows, rghRows, rgRefCounts, rgRowStatus));
	XTEST(hWnd, hr = m_pIRowset->ReleaseRows(cRows, rghRows, NULL, rgRefCounts, rgRowStatus));
	if(hr==DB_S_ERRORSOCCURRED || hr==DB_E_ERRORSOCCURRED)
		DisplayRowErrors(hWnd, cRows, rghRows, rgRowStatus);
	TESTC(hr = pCListBox->OutputPostMethod(hr, "IRowset::ReleaseRows(%d, 0x%08x, NULL, 0x%08x, 0x%08x)", cRows, rghRows, rgRefCounts, rgRowStatus));

CLEANUP:
	SAFE_FREE(rgRowStatus);
	return hr;
}	


////////////////////////////////////////////////////////////////
// CRowset::SetColumnData
//
/////////////////////////////////////////////////////////////////
HRESULT CRowset::SetColumnData(DBBINDING* pBinding, void* pData, DBSTATUS dwStatus, CHAR* pszValue)
{
	ASSERT(pBinding);
	ASSERT(pData);
	ASSERT(pszValue);

	WCHAR wszBuffer[MAX_COL_SIZE+1];
	VARIANT vVariant;
	VariantInit(&vVariant);
	HRESULT hr = S_OK;
	
	//set STATUS
	BINDING_STATUS(*pBinding, pData) = dwStatus;
	BINDING_LENGTH(*pBinding, pData) = 0;
	BINDING_VALUE(*pBinding, pData) = NULL;

	//STATUS
	switch(dwStatus)
	{
		case DBSTATUS_S_ISNULL:
		case DBSTATUS_S_DEFAULT:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -