cdbexpdatabase.cpp

来自「本人买的<<VC++项目开发实例>>源代码配套光盘.」· C++ 代码 · 共 250 行

CPP
250
字号
// Copyright (C) 1991 - 1999 Rational Software Corporation

#include "stdafx.h"
#include "CDBExpDatabase.h"
#include "cdbexptable.h"
#include "cdbexpview.h"
#include "CDBExpStoreProc.h"

IMPLEMENT_DYNAMIC(CDBExpDatabase, CDBTreeNode)
//##ModelId=3C535EC10250
void CDBExpDatabase::EnumObjects(ADODB::SchemaEnum type, const CString& strSubType)
{
//	ASSERT(NULL != m_ptrConnection);
	int nListIndex = type; // nListIndex 是在m_obStruct中的 SchemaEnum所对应的
						//对象链表的索引, 它和OBS_XXX的变量是一样的。
	CString szObjectLabel; //存放OpenSchema返回的行集的列头的名称
	CString strSubTypeLabel; //存放OpenSchema返回的行集的子类型的列头的名称
	if(ADODB::adSchemaTables == type && strSubType == "TABLE")
	{
		nListIndex = 0;
		szObjectLabel = _T("TABLE_NAME");
		strSubTypeLabel = _T("TABLE_TYPE");
	}
	else if(ADODB::adSchemaTables == type && strSubType == "VIEW")
	{
		szObjectLabel = _T("TABLE_NAME");
		strSubTypeLabel = _T("TABLE_TYPE");
		nListIndex = 1;
	}
	else if(ADODB::adSchemaProcedures == type)
	{
		szObjectLabel = _T("PROCEDURE_NAME");
		strSubTypeLabel = _T("");
		nListIndex = 2;
	}
	
		
	//查看nListIndex是不是正确!!
	if(nListIndex >= _count_of(m_ObStruct))
		throw _T("使用了不能支持的Schema的类型!");

	//下面的语句是用来防止每次都要重新调用一次查找的
	//如果,上次已经找完了,此次就直接打开数据对象。
	if(true == this->m_ObStruct[nListIndex].bHasExpanded)
		return;

	HRESULT hr = S_OK;
	ADODB::_RecordsetPtr  pRstSchema;
	if(NULL == m_ptrConnection)
		hr = m_ptrConnection.CreateInstance(__uuidof(ADODB::Connection));
	try
	{
		if(FAILED(hr))
			_com_issue_error(hr);
		_bstr_t strCnn = m_strCnn;
		m_ptrConnection->Open(strCnn, "", "", NULL);
		//打开由 type 决定的 Schema
		pRstSchema = m_ptrConnection->OpenSchema(type);//ADODB::adSchemaTables);
		_variant_t    vIndex = (short)0;

        while(!(pRstSchema->adoEOF))
        {
            _bstr_t obName;
			obName = pRstSchema->Fields->
                GetItem((const char *)szObjectLabel)->Value;
//           TRACE("Name: %s\n\n",(LPCSTR) obName);
			_bstr_t table_type = "";
			if(!strSubTypeLabel.IsEmpty())
			{
				table_type = pRstSchema->Fields->
					GetItem((const char *)strSubTypeLabel)->Value;
 //           TRACE("type: %s\n\n",(LPCSTR) table_type);
			}
            pRstSchema->MoveNext();

			if(!(strSubType == (const char *)table_type || strSubType.IsEmpty()))
				continue;

			CDBTreeNode *pNode;
			switch(nListIndex) //nListIndex 如果是0,那么就是TABLE,如果是1,就是VIEW,如果是2,就是StoredProcedure
			{
			case 0:
				pNode = new CDBExpTable((char *)obName);
				break;
			case 1:
				pNode = new CDBExpView((char *)obName);
				break;
			case 2:
				pNode = new CDBExpStoreProc((char *)obName);
				break;
			}

			ASSERT(pNode);

			this->m_ObStruct[nListIndex].m_List.AddTail(pNode);
			this->m_ObStruct[nListIndex].bHasExpanded = true;
        }
        // Clean up objects before exit.
        pRstSchema->Close();
        m_ptrConnection->Close();
           
		
	}
	catch(_com_error &e)
	{
		AfxMessageBox((const char *)e.Description());
	}
	catch(...)
	{
		throw;
	}

}



//##ModelId=3C5366420126
CDBExpDatabase::~CDBExpDatabase()
{
	// ToDo: Add your specialized code here and/or call the base class
	for(int i = 0; i < _count_of(m_ObStruct); i++)
	{	
		//删除各个子对象
		if(m_ObStruct[i].bHasExpanded)
		{

			POSITION pos = m_ObStruct[i].m_List.GetHeadPosition();
			while(NULL != pos)
				delete m_ObStruct[i].m_List.GetNext(pos);
		}
		//删除掉固定的PackageNode的对象
		delete m_ObStruct[i].m_pPackageNode;
	}

}

//##ModelId=3C536652015B
CDBExpDatabase::CDBExpDatabase(const CString& strCnn)
{
	// ToDo: Add your specialized code here and/or call the base class
	for(int i = 0; i < _count_of(m_ObStruct); i++)
	{
		m_ObStruct[i].bHasExpanded		= false;
		m_ObStruct[i].m_pPackageNode	= NULL;
	}
	m_strCnn = strCnn;
}


//##ModelId=3C5B9BA00013
CString CDBExpDatabase::CreateValidConnStr()
{
	CWaitCursor wait;
	ADODB::_ConnectionPtr ptrTempConnection;
	CString strCnn;
	CString sMsg;
	CWnd* pFrame = (CWnd *)AfxGetMainWnd();
	ASSERT(pFrame != NULL);
	try
	{
		HRESULT hr = S_OK;
		MSDASC::IDataSourceLocatorPtr ptrDataSourceWnd = NULL;
		hr = ptrDataSourceWnd.CreateInstance(__uuidof(MSDASC::DataLinks));
		if(FAILED(hr))
			_com_issue_error(hr);
        
		hr = ptrDataSourceWnd->put_hWnd((long)pFrame->m_hWnd);
		if(FAILED(hr))
			_com_issue_error(hr);
		
		IDispatchPtr ptrDispatch = NULL;
		hr = ptrDataSourceWnd->PromptNew(&ptrDispatch);
		if(FAILED(hr))
			_com_issue_error(hr);
		if(ptrDispatch != NULL)
		{
			ptrTempConnection = ptrDispatch;
			BSTR bstrConnectString = ptrTempConnection->ConnectionString;
			USES_CONVERSION;
			strCnn = W2CT(bstrConnectString);
			TRACE(_T("Connect String: %s\n"), (LPCTSTR)strCnn);
		}
		
		
		if(ptrDataSourceWnd != NULL)
		{
			ptrDataSourceWnd.Release();
			ptrDataSourceWnd = NULL;
		}
		
	}
	catch(...)
	{
		throw;
	}
	return strCnn;
}


//##ModelId=3C5CCDD300A5
BOOL CDBExpDatabase::OpenRelatedView(LPARAM lParam)
{
	// ToDo: Add your specialized code here
	ASSERT(FALSE);
	return static_cast<BOOL>(0);
}


//##ModelId=3CDCDA5801C8
char * CDBExpDatabase::GetDatabaseType()
{
	static char szDatabaseType[31];
	
	if(m_strCnn.Find("Provider=SQLOLEDB") >= 0)
	{
		//MSSQLServer
		strcpy(szDatabaseType, "MSSQLServer");
	}
	else if(m_strCnn.Find("Provider=//paradox") >= 0)
	{
		//paradox
	}
	else if(m_strCnn.Find("Provider=//foxbase") >= 0)
	{
		//foxbase
	}
	else if(m_strCnn.Find("Provider=Microsoft.Jet") >= 0)
	{
		//access
		strcpy(szDatabaseType, "Access");
	}
	else if(m_strCnn.Find("Provider=//sybase") >= 0)
	{
		//sybase
	}
	else if(m_strCnn.Find("Provider=//oracle") >= 0)
	{
		//oracle
	}
	else if(m_strCnn.Find("Provider=//informix") >= 0)
	{
		//informix
	}
	else if(m_strCnn.Find("Provider=//other") >= 0)
	{
		//other
	}
	return szDatabaseType;
}

⌨️ 快捷键说明

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