//
// MODULE: Ado.cpp
//
// AUTHOR: Carlos Antollini
//
// mailto: cantollini@hotmail.com
//
// Date: 11/19/2002
//
// Version 2.09
//
#include "ado.h"
#define ChunkSize 100
///////////////////////////////////////////////////////
//
// CADODatabase Class
//
DWORD CADODatabase::GetRecordCount(_RecordsetPtr m_pRs)
{
DWORD numRows = 0;
numRows = m_pRs->GetRecordCount();
if(numRows == -1)
{
if(m_pRs->EndOfFile != VARIANT_TRUE)
m_pRs->MoveFirst();
while(m_pRs->EndOfFile != VARIANT_TRUE)
{
numRows++;
m_pRs->MoveNext();
}
if(numRows > 0)
m_pRs->MoveFirst();
}
return numRows;
}
BOOL CADODatabase::Open(LPCTSTR lpstrConnection, LPCTSTR lpstrUserID, LPCTSTR lpstrPassword)
{
HRESULT hr = S_OK;
if(IsOpen())
Close();
if(strcmp(lpstrConnection, _T("")) != 0)
m_strConnection = lpstrConnection;
ASSERT(!m_strConnection.IsEmpty());
try
{
if(m_nConnectionTimeout != 0)
m_pConnection->PutConnectionTimeout(m_nConnectionTimeout);
hr = m_pConnection->Open(_bstr_t(m_strConnection), _bstr_t(lpstrUserID), _bstr_t(lpstrPassword), NULL);
return hr == S_OK;
}
catch(_com_error &e)
{
dump_com_error(e);
return FALSE;
}
}
void CADODatabase::dump_com_error(_com_error &e)
{
CString ErrorStr;
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
ErrorStr.Format( "CADODataBase Error\n\tCode = %08lx\n\tCode meaning = %s\n\tSource = %s\n\tDescription = %s\n",
e.Error(), e.ErrorMessage(), (LPCSTR)bstrSource, (LPCSTR)bstrDescription);
m_strErrorDescription = (LPCSTR)bstrDescription ;
m_strLastError = _T("Connection String = " + GetConnectionString() + '\n' + ErrorStr);
m_dwLastError = e.Error();
#ifdef _DEBUG
AfxMessageBox(ErrorStr, MB_OK | MB_ICONERROR );
#endif
}
BOOL CADODatabase::IsOpen()
{
if(m_pConnection )
return m_pConnection->GetState() != adStateClosed;
return FALSE;
}
void CADODatabase::Close()
{
if(IsOpen())
m_pConnection->Close();
}
///////////////////////////////////////////////////////
//
// CADORecordset Class
//
CADORecordset::CADORecordset()
{
m_pRecordset = NULL;
m_pCmd = NULL;
m_strQuery = _T("");
m_strLastError = _T("");
m_dwLastError = 0;
m_pRecBinding = NULL;
m_pRecordset.CreateInstance(__uuidof(Recordset));
m_pCmd.CreateInstance(__uuidof(Command));
m_nEditStatus = CADORecordset::dbEditNone;
m_nSearchDirection = CADORecordset::searchForward;
}
CADORecordset::CADORecordset(CADODatabase* pAdoDatabase)
{
m_pRecordset = NULL;
m_pCmd = NULL;
m_strQuery = _T("");
m_strLastError = _T("");
m_dwLastError = 0;
m_pRecBinding = NULL;
m_pRecordset.CreateInstance(__uuidof(Recordset));
m_pCmd.CreateInstance(__uuidof(Command));
m_nEditStatus = CADORecordset::dbEditNone;
m_nSearchDirection = CADORecordset::searchForward;
m_pConnection = pAdoDatabase->GetActiveConnection();
}
BOOL CADORecordset::Open(_ConnectionPtr mpdb, LPCTSTR lpstrExec, int nOption)
{
Close();
if(strcmp(lpstrExec, _T("")) != 0)
m_strQuery = lpstrExec;
ASSERT(!m_strQuery.IsEmpty());
if(m_pConnection == NULL)
m_pConnection = mpdb;
m_strQuery.TrimLeft();
BOOL bIsSelect = m_strQuery.Mid(0, strlen("Select ")).CompareNoCase("select ") == 0 && nOption == openUnknown;
try
{
m_pRecordset->CursorType = adOpenStatic;
m_pRecordset->CursorLocation = adUseClient;
if(bIsSelect || nOption == openQuery || nOption == openUnknown)
m_pRecordset->Open((LPCSTR)m_strQuery, _variant_t((IDispatch*)mpdb, TRUE),
adOpenStatic, adLockOptimistic, adCmdUnknown);
else if(nOption == openTable)
m_pRecordset->Open((LPCSTR)m_strQuery, _variant_t((IDispatch*)mpdb, TRUE),
adOpenKeyset, adLockOptimistic, adCmdTable);
else if(nOption == openStoredProc)
{
m_pCmd->ActiveConnection = mpdb;
m_pCmd->CommandText = _bstr_t(m_strQuery);
m_pCmd->CommandType = adCmdStoredProc;
m_pConnection->CursorLocation = adUseClient;
m_pRecordset = m_pCmd->Execute(NULL, NULL, adCmdText);
}
else
{
TRACE( "Unknown parameter. %d", nOption);
return FALSE;
}
}
catch(_com_error &e)
{
dump_com_error(e);
return FALSE;
}
return m_pRecordset != NULL;
}
BOOL CADORecordset::Open(LPCTSTR lpstrExec, int nOption)
{
ASSERT(m_pConnection != NULL);
ASSERT(m_pConnection->GetState() != adStateClosed);
return Open(m_pConnection, lpstrExec, nOption);
}
BOOL CADORecordset::OpenSchema(int nSchema, LPCTSTR SchemaID)
{
try
{
_variant_t vtSchemaID = vtMissing;
if(strlen(SchemaID) != 0)
vtSchemaID = SchemaID;
m_pRecordset = m_pConnection->OpenSchema((enum SchemaEnum)nSchema, vtMissing, vtSchemaID);
return TRUE;
}
catch(_com_error &e)
{
dump_com_error(e);
return FALSE;
}
}
BOOL CADORecordset::Requery()
{
if(IsOpen())
{
try
{
m_pRecordset->Requery(adExecuteRecord);
}
catch(_com_error &e)
{
dump_com_error(e);
return FALSE;
}
}
return TRUE;
}
BOOL CADORecordset::GetFieldValue(LPCTSTR lpFieldName, double& dbValue)
{
double val = (double)NULL;
_variant_t vtFld;
try
{
vtFld = m_pRecordset->Fields->GetItem(lpFieldName)->Value;
switch(vtFld.vt)
{
case VT_R4:
val = vtFld.fltVal;
break;
case VT_R8:
val = vtFld.dblVal;
break;
case VT_DECIMAL:
//Corrected by José Carlos Martínez Galán
val = vtFld.decVal.Lo32;
val *= (vtFld.decVal.sign == 128)? -1 : 1;
val /= pow(10, vtFld.decVal.scale);
break;
case VT_UI1:
val = vtFld.iVal;
break;
case VT_I2:
case VT_I4:
val = vtFld.lVal;
break;
case VT_INT:
val = vtFld.intVal;
break;
case VT_NULL:
case VT_EMPTY:
val = 0;
break;
default:
val = vtFld.dblVal;
}
dbValue = val;
return TRUE;
}
catch(_com_error &e)
{
dump_com_error(e);
return FALSE;
}
}
BOOL CADORecordset::GetFieldValue(int nIndex, double& dbValue)
{
double val = (double)NULL;
_variant_t vtFld;
_variant_t vtIndex;
vtIndex.vt = VT_I2;
vtIndex.iVal = nIndex;
try
{
vtFld = m_pRecordset->Fields->GetItem(vtIndex)->Value;
switch(vtFld.vt)
{
case VT_R4:
val = vtFld.fltVal;
break;
case VT_R8:
val = vtFld.dblVal;
break;
case VT_DECIMAL:
//Corrected by José Carlos Martínez Galán
val = vtFld.decVal.Lo32;
val *= (vtFld.decVal.sign == 128)? -1 : 1;
val /= pow(10, vtFld.decVal.scale);
break;
case VT_UI1:
val = vtFld.iVal;
break;
case VT_I2:
case VT_I4:
val = vtFld.lVal;
break;
case VT_INT:
val = vtFld.intVal;
break;
case VT_NULL:
case VT_EMPTY:
val = 0;
break;
default:
val = 0;
}
dbValue = val;
return TRUE;
}
catch(_com_error &e)
{
dump_com_error(e);
return FALSE;
}
}
BOOL CADORecordset::GetFieldValue(LPCTSTR lpFieldName, long& lValue)
{
long val = (long)NULL;
_variant_t vtFld;
try
{
vtFld = m_pRecordset->Fields->GetItem(lpFieldName)->Value;
if(vtFld.vt != VT_NULL && vtFld.vt != VT_EMPTY)
val = vtFld.lVal;
lValue = val;
return TRUE;
}
catch(_com_error &e)
{
dump_com_error(e);
return FALSE;
}
}
BOOL CADORecordset::GetFieldValue(int nIndex, long& lValue)
{
long val = (long)NULL;
_variant_t vtFld;
_variant_t vtIndex;
vtIndex.vt = VT_I2;
vtIndex.iVal = nIndex;
try
{
vtFld = m_pRecordset->Fields->GetItem(vtIndex)->Value;
if(vtFld.vt != VT_NULL && vtFld.vt != VT_EMPTY)
val = vtFld.lVal;
lValue = val;
return TRUE;
}
catch(_com_error &e)
{
dump_com_error(e);
return FALSE;
}
}
BOOL CADORecordset::GetFieldValue(LPCTSTR lpFieldName, unsigned long& ulValue)
{
long val = (long)NULL;
_variant_t vtFld;
try
{
vtFld = m_pRecordset->Fields->GetItem(lpFieldName)->Value;
if(vtFld.vt != VT_NULL && vtFld.vt != VT_