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

📄 storeprocess.cpp

📁 扩展mfc的ODBC类
💻 CPP
字号:
// SoreProcess.cpp : implementation file
//

#include "stdafx.h"
#include "StoreProcess.h"
#include ".\storeprocess.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


CSpParam::CSpParam()
{
	ParamType = CFieldExchange::noFieldType;
	Datatype = DT_onType;
	ParamName = _T("");
	Length = 0;
	pExtValue = NULL;
}

CSpParam::CSpParam(CFieldExchange::FieldType ftype, SP_Datatype dtype, CString pname, void* pValue, int length)
{
	Set(ftype, dtype, pname, pValue, length);
}

CSpParam::~CSpParam()
{
}

void CSpParam::Set(CFieldExchange::FieldType ftype, SP_Datatype dtype, CString pname, void* pValue, int length)
{
	ParamType = ftype;
	Datatype = dtype;
	ParamName = pname;
	Length = length;
	pExtValue = NULL;

	if(NULL==pValue)
	{
/*
		switch(Datatype)
		{
		case DT_onType:
			pExtValue = NULL;
			break;
		case DT_Text:
			pExtValue = &interValue.AsText;
			break;
		case DT_Bool:
			pExtValue = &interValue.AsBool;
			break;
		case DT_Long:
			pExtValue = &interValue.AsLong;
			break;
		case DT_Int:
			pExtValue = &interValue.AsInt;
			break;
		case DT_Single:
			pExtValue = &interValue.AsSingle;
			break;
		case DT_Double:
			pExtValue = &interValue.AsDouble;
			break;
		case DT_Date:
			pExtValue = &interValue.AsDate;
			break;
		case DT_Byte:
			pExtValue = &interValue.AsByte;
			break;
		case DT_Binary:
			pExtValue = &interValue.AsBinary;
			break;
		case DT_LongBinary:
			pExtValue = &interValue.AsLongBinary;
			break;
		}
		*/
	}
	else
		pExtValue = pValue;
}

/////////////////////////////////////////////////////////////////////////////
// CStoreProcess

IMPLEMENT_DYNAMIC(CStoreProcess, CRecordset)

CStoreProcess::CStoreProcess(CDatabase* pdb)
	: CRecordset(pdb)
{
	//{{AFX_FIELD_INIT(CStoreProcess)
//	m_ParamId = 0;
//	m_ParamName = _T("");
//	m_ParamValue = _T("");
//	m_nFields = 3;
	//}}AFX_FIELD_INIT

	m_nFields = 0;
	m_nParams = 1;
	m_nDefaultType = snapshot;
	m_Sql = _T("");
	m_retSp = -1;
}

CStoreProcess::~CStoreProcess()
{
	ClearParams();
}

CString CStoreProcess::GetDefaultConnect()
{
	return _T("ODBC;DSN=dbsms");
}

CString CStoreProcess::GetDefaultSQL()
{
	return _T(""); //[dbo].[Sys_Param]
}


void CStoreProcess::DoFieldExchange(CFieldExchange* pFX)
{
	//{{AFX_FIELD_MAP(CStoreProcess)
//	pFX->SetFieldType(CFieldExchange::outputColumn);
//	RFX_Long(pFX, _T("[ParamId]"), m_ParamId);
//	RFX_Text(pFX, _T("[ParamName]"), m_ParamName);
//	RFX_Text(pFX, _T("[ParamValue]"), m_ParamValue);
	//}}AFX_FIELD_MAP

	// store process's return value
	pFX->SetFieldType(CFieldExchange::outputParam);
	RFX_Long(pFX, _T("[return_value]"), m_retSp);

	int nCount = m_Params.GetSize();
	for(int i=0;i<nCount;i++)
	{
		CSpParam* Param = m_Params[i];
		pFX->SetFieldType(Param->ParamType);
		switch(Param->Datatype)
		{
		case DT_onType:
			break;
		case DT_Text:
			if(Param->Length>0)
				RFX_Text(pFX, LPCTSTR(Param->ParamName), *((CString*)(Param->pExtValue)), Param->Length); //>Value.AsText
			else
				RFX_Text(pFX, Param->ParamName, *((CString*)(Param->pExtValue)));
			break;
		case DT_Bool:
			RFX_Bool(pFX, Param->ParamName, *((BOOL*)(Param->pExtValue)));//Param->Value.AsBool
			break;
		case DT_Long:
			RFX_Long(pFX, Param->ParamName, *((long *)(Param->pExtValue)));//Param->Value.AsLong
			break;
		case DT_Int:
			RFX_Int(pFX, Param->ParamName, *((int *)(Param->pExtValue)));//Param->Value.AsInt
			break;
		case DT_Single:
			RFX_Single(pFX, Param->ParamName, *((float *)(Param->pExtValue)));//Param->Value.AsSingle
			break;
		case DT_Double:
			RFX_Double(pFX, Param->ParamName, *((double *)(Param->pExtValue)));//Param->Value.AsDouble
			break;
		case DT_Date:
			RFX_Date(pFX, Param->ParamName, *((CTime *)(Param->pExtValue)));//Param->Value.AsDate
			break;
		case DT_Byte:
			RFX_Byte(pFX, Param->ParamName, *((BYTE *)(Param->pExtValue)));//Param->Value.AsByte
			break;
		case DT_Binary:
			if(Param->Length>0)
				RFX_Binary(pFX, Param->ParamName, *((CByteArray *)(Param->pExtValue)), Param->Length);//Param->Value.AsBinary
			else
				RFX_Binary(pFX, Param->ParamName, *((CByteArray *)(Param->pExtValue)));
			break;
		case DT_LongBinary:
			RFX_LongBinary(pFX, Param->ParamName, *((CLongBinary *)(Param->pExtValue)));//Param->Value.AsLongBinary
			break;
		}
	}
}

/////////////////////////////////////////////////////////////////////////////
// CStoreProcess diagnostics

#ifdef _DEBUG
void CStoreProcess::AssertValid() const
{
	CRecordset::AssertValid();
}

void CStoreProcess::Dump(CDumpContext& dc) const
{
	CRecordset::Dump(dc);
}
#endif //_DEBUG

BOOL CStoreProcess::Open(UINT nOpenType, LPCTSTR lpszSQL, DWORD dwOptions) 
{
	// TODO: Add your specialized code here and/or call the base class
	ASSERT(!IsOpen());
	ASSERT_VALID(this);
	ASSERT(lpszSQL == NULL || AfxIsValidString(lpszSQL));
	ASSERT(nOpenType == AFX_DB_USE_DEFAULT_TYPE ||
		nOpenType == dynaset || nOpenType == snapshot ||
		nOpenType == forwardOnly || nOpenType == dynamic);
	ASSERT(!(dwOptions & readOnly && dwOptions & appendOnly));

	// Can only use optimizeBulkAdd with appendOnly recordsets
	ASSERT((dwOptions & optimizeBulkAdd && dwOptions & appendOnly) ||
		!(dwOptions & optimizeBulkAdd));

	// forwardOnly recordsets have limited functionality
	ASSERT(!(nOpenType == forwardOnly && dwOptions & skipDeletedRecords));

	// Cache state info and allocate hstmt
	SetState(nOpenType, lpszSQL, dwOptions);
	if(!AllocHstmt())
		return FALSE;

	// Check if bookmarks upported (CanBookmark depends on open DB)
	ASSERT(dwOptions & useBookmarks ? CanBookmark() : TRUE);

	TRY
	{
		OnSetOptions(m_hstmt);

		// Allocate the field/param status arrays, if necessary
		BOOL bUnbound = FALSE;
		if (m_nFields > 0 || m_nParams > 0)
			AllocStatusArrays();
		else
			bUnbound = TRUE;

		// Build SQL and prep/execute or just execute direct
		BuildSQL(lpszSQL);
		PrepareAndExecute();

		// Cache some field info and prepare the rowset
		AllocAndCacheFieldInfo();
		AllocRowset();

		// If late binding, still need to allocate status arrays
		if (bUnbound && (m_nFields > 0 || m_nParams > 0))
			AllocStatusArrays();

		// Give derived classes a call before binding
		PreBindFields();

		// Fetch the first row of data
		if(m_nFields>0)
			MoveNext();

		// If EOF, then result set empty, so set BOF as well
		m_bBOF = m_bEOF;
	}
	CATCH_ALL(e)
	{
		Close();
		THROW_LAST();
	}
	END_CATCH_ALL

	return TRUE;
}

// Add paramter
int CStoreProcess::AddParam(CSpParam* Param)
{
	CSpParam *pnew = new CSpParam;
	*pnew = *Param;
	m_Params.Add(pnew);
	if(Param->ParamType>CFieldExchange::outputColumn)
		m_nParams++;
	else if(Param->ParamType==CFieldExchange::outputColumn)
		m_nFields++;

	return 0;
}

int CStoreProcess::Reset(void)
{
	ClearParams();
	m_nParams = 0;
	m_nFields = 0;

	return 0;
}


void CStoreProcess::SetSql(CString sql)
{
	Reset();
	m_Sql = sql;
}

int CStoreProcess::Execute(void)
{
	int count = m_nParams-1;
	if(count>0)
	{
		m_Sql += "(?";
		for(int i=0;i<count-1;i++)
			m_Sql += ",?";
		m_Sql += ")";
	}
	m_Sql += "}";

	if(Open(CRecordset::snapshot, m_Sql))
		return m_retSp;
	else
		return -111;
}

// set store process's name, only name
int CStoreProcess::SetStoreProc(CString procname)
{
//	m_Sql = procname;
	m_Sql.Format("{?=CALL %s",procname);

	return 0;
}

int CStoreProcess::SetDatabase(CDatabase * pDb)
{
	m_pDatabase = pDb;
	return 0;
}

int CStoreProcess::ClearParams(void)
{
	int count = m_Params.GetSize();
	for(int i=0;i<count;i++)
	{
		CSpParam *param = m_Params[i];
		delete param;
	}
	m_Params.RemoveAll();

	return 0;
}

⌨️ 快捷键说明

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