?? sqlquery.cpp
字號:
/////////////////////////////////////////////////////////////////
// //
// SQLQuery.cpp //
// implementation of the CSQLQuery class //
//-------------------------------------------------------------//
// By Eugene Khodakovsky //
// June, 2000 //
// Eugene@cpplab.com //
// Last Update: April, 2002 //
/////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "SQLQuery.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#define pACCESSOR ((CDynamicAccessor*)m_pCommand)
//////////////////////////////////////////////////////////////////////
// CQueryObject
IMPLEMENT_DYNAMIC(CQueryObject,CObject);
CQueryObject::CQueryObject()
{
m_value.vt = VT_EMPTY;
}
CQueryObject::CQueryObject(const COleVariant& value)
{
m_value = value;
}
CQueryObject::~CQueryObject()
{
HRESULT hr = ::VariantClear(&m_value);
if (FAILED(hr))
{
ASSERT(0);
}
}
void CQueryObject::RemoveAndDestroy()
{
HRESULT hr = ::VariantClear(&m_value);
if (FAILED(hr))
{
ASSERT(0);
}
delete this;
}
CQueryObject& CQueryObject::operator>>(BYTE &value)
{
if( m_value.vt == VT_EMPTY || m_value.vt == VT_NULL)
{
value = NULL;
}
else
{
ASSERT(m_value.vt == VT_I1 || m_value.vt == VT_UI1);
value = m_value.intVal;
}
return *this;
}
CQueryObject& CQueryObject::operator>>(short &value)
{
if( m_value.vt == VT_EMPTY || m_value.vt == VT_NULL)
{
value = NULL;
}
else
{
ASSERT(m_value.vt == VT_I2);
value = m_value.iVal;
}
return *this;
}
CQueryObject& CQueryObject::operator>>(COleCurrency &value)
{
if( m_value.vt == VT_EMPTY || m_value.vt == VT_NULL)
{
value.SetStatus(COleCurrency::CurrencyStatus::invalid);
}
else
{
ASSERT(m_value.vt == VT_CY);
value = m_value;
}
return *this;
}
CQueryObject& CQueryObject::operator>>(CY &value)
{
if( m_value.vt == VT_EMPTY || m_value.vt == VT_NULL)
{
value.Lo = NULL;
value.Hi = NULL;
}
else
{
ASSERT(m_value.vt == VT_CY);
value = m_value.cyVal;
}
return *this;
}
CQueryObject& CQueryObject::operator>>(int &value)
{
if( m_value.vt == VT_EMPTY || m_value.vt == VT_NULL)
{
value = NULL;
}
else
{
ASSERT(m_value.vt == VT_I4);
value = m_value.iVal;
}
return *this;
}
CQueryObject& CQueryObject::operator>>(long &value)
{
if( m_value.vt == VT_EMPTY || m_value.vt == VT_NULL)
{
value = NULL;
}
else
{
ASSERT(m_value.vt == VT_I4);
value = m_value.lVal;
}
return *this;
}
CQueryObject& CQueryObject::operator>>(float &value)
{
if( m_value.vt == VT_EMPTY || m_value.vt == VT_NULL)
{
value = NULL;
}
else
{
ASSERT(m_value.vt == VT_R4);
value = m_value.fltVal;
}
return *this;
}
CQueryObject& CQueryObject::operator>>(double &value)
{
if( m_value.vt == VT_EMPTY || m_value.vt == VT_NULL)
{
value = NULL;
}
else
{
ASSERT(m_value.vt == VT_R8);
value = m_value.dblVal;
}
return *this;
}
CQueryObject& CQueryObject::operator>>(COleDateTime &value)
{
if( m_value.vt == VT_EMPTY || m_value.vt == VT_NULL)
{
//value = NULL;
}
else
{
ASSERT(m_value.vt == VT_DATE);
value = m_value.date;
}
return *this;
}
CQueryObject& CQueryObject::operator>>(CTime &value)
{
COleDateTime date;
*this >> date;
CTime time( date.GetYear(),date.GetMonth(),date.GetDay(),
date.GetHour(),date.GetMinute(),date.GetSecond());
value = time;
return *this;
}
CQueryObject& CQueryObject::operator>>(CString &value)
{
if( m_value.vt == VT_EMPTY || m_value.vt == VT_NULL)
{
value.Empty();
}
else
{
ASSERT(m_value.vt == VT_BSTR);
value = m_value.bstrVal;
}
return *this;
}
CQueryObject& CQueryObject::operator>(CString &value)
{
COleDateTime date;
switch(m_value.vt)
{
default:
ASSERT(0);
break;
case VT_CY:
{
COleCurrency cur(m_value.cyVal);
value = cur.Format();
}
break;
case VT_BOOL:
value.Format("%d",m_value.boolVal);
break;
case VT_DATE:
date = m_value;
value = date.Format("%d %b.%Y");
break;
case VT_EMPTY:
case VT_NULL:
value.Empty();
break;
case VT_I2:
case VT_I4:
value.Format("%d",m_value.iVal);
break;
case VT_R4:
value.Format("%.2f",m_value.fltVal);
break;
case VT_R8:
value.Format("%.2f",m_value.dblVal);
break;
case VT_BSTR:
*this >> value;
break;
}
return *this;
}
//////////////////////////////////////////////////////////////////////
// CQueryObjectList
CQueryObjectList::CQueryObjectList():
m_pos(NULL)
{
}
CQueryObjectList::~CQueryObjectList()
{
RemoveAndDestroy();
}
void CQueryObjectList::RemoveAndDestroy()
{
POSITION pos = GetHeadPosition();
while(pos != NULL)
{
GetNext(pos)->RemoveAndDestroy();
}
RemoveAll();
}
void CQueryObjectList::InitPosition()
{
m_pos = GetHeadPosition();
}
CQueryObjectList& CQueryObjectList::operator>>(BYTE &value)
{
if(m_pos)
*GetNext(m_pos)>> value;
return *this;
}
CQueryObjectList& CQueryObjectList::operator>>(short &value)
{
if(m_pos)
*GetNext(m_pos)>> value;
return *this;
}
CQueryObjectList& CQueryObjectList::operator>>(CY &value)
{
if(m_pos)
*GetNext(m_pos)>> value;
return *this;
}
CQueryObjectList& CQueryObjectList::operator>>(COleCurrency &value)
{
if(m_pos)
*GetNext(m_pos)>> value;
return *this;
}
CQueryObjectList& CQueryObjectList::operator>>(int &value)
{
if(m_pos)
*GetNext(m_pos)>> value;
return *this;
}
CQueryObjectList& CQueryObjectList::operator>>(long &value)
{
if(m_pos)
*GetNext(m_pos)>> value;
return *this;
}
CQueryObjectList& CQueryObjectList::operator>>(float &value)
{
if(m_pos)
*GetNext(m_pos)>> value;
return *this;
}
CQueryObjectList& CQueryObjectList::operator>>(double &value)
{
if(m_pos)
*GetNext(m_pos)>> value;
return *this;
}
CQueryObjectList& CQueryObjectList::operator>>(CTime &value)
{
if(m_pos)
*GetNext(m_pos)>> value;
return *this;
}
CQueryObjectList& CQueryObjectList::operator>>(COleDateTime &value)
{
if(m_pos)
*GetNext(m_pos)>> value;
return *this;
}
CQueryObjectList& CQueryObjectList::operator>>(CString &value)
{
if(m_pos)
*GetNext(m_pos)>> value;
return *this;
}
CQueryObjectList& CQueryObjectList::operator>(CString &value)
{
if(m_pos)
*GetNext(m_pos)> value;
return *this;
}
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
IMPLEMENT_DYNAMIC(CSQLQuery,CObject);
CSQLQuery::CSQLQuery(CDataSource* pDataSource,bool bShowError):
m_pDataSource(pDataSource),
m_bShowError(bShowError),
m_pSession(NULL),
m_bMySession(false),
m_pCommand(NULL)
{
if(pDataSource)
{
InitSession();
CComVariant var;
if(SUCCEEDED(pDataSource->
GetProperty(DBPROPSET_DATASOURCE,DBPROP_CURRENTCATALOG, &var) &&
var.vt != VT_EMPTY && var.vt != VT_NULL))
{
USES_CONVERSION;
m_strDatabase = OLE2T(var.bstrVal);
}
else
{
ASSERT(0);
}
}
}
CSQLQuery::CSQLQuery(CDataSource* pDataSource,CSession* pSession,bool bShowError):
m_pDataSource(pDataSource),
m_bShowError(bShowError),
m_pSession(pSession),
m_bMySession(false),
m_pCommand(NULL)
{
if(pDataSource)
{
CComVariant var;
if(SUCCEEDED(pDataSource->
GetProperty(DBPROPSET_DATASOURCE,DBPROP_CURRENTCATALOG, &var) &&
var.vt != VT_EMPTY && var.vt != VT_NULL))
{
USES_CONVERSION;
m_strDatabase = OLE2T(var.bstrVal);
}
else
{
ASSERT(0);
}
}
}
CSQLQuery::CSQLQuery(CSession* pSession,bool bShowError):
m_pDataSource(NULL),
m_bShowError(bShowError),
m_pSession(pSession),
m_bMySession(false),
m_pCommand(NULL)
{
}
CSQLQuery::~CSQLQuery()
{
m_object_list.RemoveAndDestroy();
if(m_bMySession && m_pSession)
delete m_pSession;
m_pSession = NULL;
DeleteCommand();
}
void CSQLQuery::ShowError(LPCSTR error)
{
m_strError = error;
if(m_bShowError)
MessageBox(NULL,error,"CSQLQuery",MB_ICONERROR | MB_OK);
}
void CSQLQuery::ShowDBError(IUnknown* pUnk, const IID& iid )
{
CString strError;
CDBErrorInfo errInfo;
ULONG ulRecords = 0;
HRESULT hr = errInfo.GetErrorRecords(pUnk,iid, &ulRecords);
if (FAILED(hr) || hr == S_FALSE || ulRecords == 0)
{
ShowError(_T("NOSPECIFIED_SOURCE"));
return ;
}
else
{
LCID lcid = GetUserDefaultLCID();
for (ULONG l=0; l<ulRecords; l++)
{
// Get the error information from the source
struct MYERRORINFO* pInfo = new MYERRORINFO;
hr = errInfo.GetAllErrorInfo(l, lcid, &pInfo->bstrDescription,
&pInfo->bstrSource, &pInfo->guid, &pInfo->dwHelpContext,
&pInfo->bstrHelpFile);
if (FAILED(hr))
{
delete pInfo;
continue;
}
strError += CString(pInfo->bstrDescription) + "\n";
delete pInfo;
}
}
ShowError(_T(strError));
}
bool CSQLQuery::CheckDBError(IUnknown* pUnk, const IID& iid )
{
CString strError;
CDBErrorInfo errInfo;
ULONG ulRecords = 0;
HRESULT hr = errInfo.GetErrorRecords(pUnk,iid, &ulRecords);
if (FAILED(hr) || hr == S_FALSE || ulRecords == 0)
{
// ShowError(_T("NOSPECIFIED_SOURCE"));
return false;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -