fetchscript.cpp
来自「本人买的<<VC++项目开发实例>>源代码配套光盘.」· C++ 代码 · 共 180 行
CPP
180 行
// FetchScript.cpp: implementation of the CFetchScript class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "FetchScript.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
static const char * GetAppPath()
{
static char ExeFilePath[MAX_PATH] = {0};
VERIFY( 0 != GetModuleFileName(NULL, ExeFilePath, sizeof(ExeFilePath)));
TRACE("DLL(fetchscript): static const char * GetAppPath() = %s\n", ExeFilePath);
static char drive[_MAX_DRIVE];
static char dir[_MAX_DIR];
static char fname[_MAX_FNAME];
static char ext[_MAX_EXT];
_splitpath(ExeFilePath, drive, dir, fname, ext);
wsprintf(ExeFilePath, "%s%s", drive, dir);
return ExeFilePath;
}
CFetchScript::CFetchScript() :
m_ConnPtr(NULL),
m_strFmtSQL(""),
m_strObjectName(""),
m_strScriptBuffer(""),
m_strServerType("")
{
CString strSQLIniFileName(CString(GetAppPath()) + "sql.ini");
TRACE("strSQLIniFileName = %s\n", strSQLIniFileName);
m_IniFile.SetPath(strSQLIniFileName);
VERIFY(m_IniFile.ReadFile());
}
CFetchScript::~CFetchScript()
{
Reset();
}
CFetchScript::CFetchScript(_ConnectionPtr &pConn):
m_ConnPtr(pConn),
m_strFmtSQL(""),
m_strObjectName(""),
m_strScriptBuffer(""),
m_strServerType("")
{
CString strSQLIniFileName(CString(GetAppPath()) + "sql.ini");
TRACE("strSQLIniFileName = %s\n", strSQLIniFileName);
m_IniFile.SetPath(strSQLIniFileName);
VERIFY(m_IniFile.ReadFile());
}
void CFetchScript::SetConnection(_ConnectionPtr &pConn)
{
m_ConnPtr = pConn;
}
void CFetchScript::SetObjectName(CString &strName)
{
m_strObjectName = strName;
}
void CFetchScript::BuildSQL()
{
m_strFmtSQL = m_IniFile.GetValue(m_strServerType, "HelpText");
/*if(m_strServerType == _T("MSSQLServer"))
{
m_strFmtSQL = "";
}
else if(m_strServerType == _T("Oracle"))
{
m_strFmtSQL = "SELECT text AS DEFINITION "
"FROM all_source "
"WHERE name=%s AND type='PROCEDURE' "
"ORDER BY line";
}
else if(m_strServerType == _T(""))
{
m_strFmtSQL = "exec sp_helptext %s";
}
else if(m_strServerType == _T(""))
{
m_strFmtSQL = "exec sp_helptext %s";
}
else if(m_strServerType == _T(""))
{
m_strFmtSQL = "exec sp_helptext %s";
}*/
}
LPCSTR CFetchScript::GetScript()
{
try
{
BuildSQL(); //取得能得到数据库对象的SQL的格式化字符串
m_strScriptBuffer = _T("");
CString strSQL;
strSQL.Format(m_strFmtSQL, m_strObjectName);
TRACE("String of SQL is \n %s.\n", strSQL);
//因为每个存储过程都会有一个后缀,即一个 ;1, 这样一来,对于一个
//名字为vicProc和存储过程,就会产生一个vicProc;1这样一个名字,
//所以,在下面的程序中要去除这个后缀!
RemoveSuffix(strSQL);
_RecordsetPtr pRst;
if(adStateClosed == m_ConnPtr->State)
m_ConnPtr->Open(L"", L"", L"", 0);
pRst = m_ConnPtr->Execute(strSQL.AllocSysString(), NULL, adCmdText);
_bstr_t bstrScript(L"");
_variant_t varIndex(0L);
while(!pRst->adoEOF)
{
bstrScript += _bstr_t(pRst->Fields->GetItem(varIndex)->Value);
pRst->MoveNext();
}
m_strScriptBuffer = (const char *)bstrScript;
pRst->Close();
m_ConnPtr->Close();
return (LPCSTR)m_strScriptBuffer;
}
catch(_com_error &e)
{
AfxMessageBox((char *)e.Description());
return NULL; //如果出现了COM的错误就显示一下,并且返回一个NULL的
//结果,没有进行下一步的处理.
}
catch(...)
{
throw;
}
}
void CFetchScript::Reset()
{
m_ConnPtr = NULL;
m_strFmtSQL = "";
m_strObjectName = "";
m_strScriptBuffer = "";
}
void CFetchScript::RemoveSuffix(CString &strSQL)
{
//因为每个存储过程都会有一个后缀,即一个 ;1, 这样一来,对于一个
//名字为vicProc和存储过程,就会产生一个vicProc;1这样一个名字,
//所以,在下面的程序中要去除这个后缀!
int nPos;
nPos = strSQL.Find(";");
if(nPos != -1) //found it
{
strSQL = strSQL.Left(nPos);
}
return;
}
void CFetchScript::SetServerType(const char *szServerType)
{
m_strServerType = szServerType;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?