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

📄 entityset.h

📁 摘要:本文是笔者根据数据库编程经验
💻 H
字号:
// EntitySet.h: interface for the CEntitySet class.
// 动态实体集类
//////////////////////////////////////////////////////////////////////

#pragma once

#include "Entity.h"

template <class T>
class CEntitySet
{
public:
	CEntitySet(const CString& strName);
	virtual ~CEntitySet(void);

private:
	CString m_strEntityName;

	CAdoConnection* m_pConn;

	vector<T*> m_vecEnt;
	CString    m_strSQL;

public:
	void SetConn(CAdoConnection* pConn);
	void SetSQLString(const CString& strSQL);

	BOOL Execute(void);
	BOOL Clear(void);

	T* operator[](int index) const;
	int GetSize(void) const;
};

template <class T>
CEntitySet<T>::CEntitySet(const CString& strName) : m_strEntityName(strName), m_pConn(NULL)
{
	m_strSQL = TEXT("Select * from ") + m_strEntityName;
}

template <class T>
CEntitySet<T>::~CEntitySet(void)
{
	if(m_vecEnt.size() > 0)
		Clear();
}

template <class T>
void CEntitySet<T>::SetConn(CAdoConnection* pConn)
{
	m_pConn = pConn;
}

template <class T>
void CEntitySet<T>::SetSQLString(const CString& strSQL)
{
	m_strSQL += TEXT(" where ") + strSQL; 
}

template <class T>
BOOL CEntitySet<T>::Execute(void)
{
	if(m_vecEnt.size() > 0)
		if(!Clear())
			return FALSE;
	
	if(m_pConn == NULL) 
		return FALSE;
	
	CAdoRecordSet rs(m_pConn);
	if(rs.Open(m_strSQL) == -1) 
		return FALSE;

	while(!rs.IsEOF())
	{
		CEntity* pEntity = new T(m_strEntityName);
		ASSERT(pEntity );
		if(pEntity  == NULL)
		{
			rs.Close();
			return FALSE;
		}

		if(!pEntity ->Read(&rs))
			break;
		m_vecEnt.push_back(static_cast<T*>(pEntity));
		rs.MoveNext();
	}
	rs.Close();

	return TRUE;
}

template <class T>
BOOL CEntitySet<T>::Clear(void)
{
	std::vector<T*>::iterator theIt;
	for(theIt = m_vecEnt.begin(); theIt != m_vecEnt.end(); ++theIt)
	{
		T* p = *theIt;	
		m_vecEnt.erase(theIt);
		theIt--;
		delete p;		
	}

	return m_vecEnt.size() == 0;
}

template <class T>
T* CEntitySet<T>::operator[](int index) const
{
	if(index < 0 || index > m_vecEnt.size())
		return NULL;

	if(m_vecEnt.size() == 0)
		return NULL;

	return m_vecEnt[index];
}

template <class T>
int CEntitySet<T>::GetSize(void) const
{
	return m_vecEnt.size();
}

⌨️ 快捷键说明

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