//
// MODULE: Ado2.cpp
//
// AUTHOR: Carlos Antollini <cantollini@hotmail.com>
//
// Copyright (c) 2001-2004. All Rights Reserved.
//
// Date: August 01, 2005
//
// Version 2.20
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed unmodified by any means PROVIDING it is
// not sold for profit without the authors written consent, and
// providing that this notice and the authors name and all copyright
// notices remains intact.
//
// An email letting me know how you are using it would be nice as well.
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability for any damage/loss of business that
// this product may cause.
//
//
//////////////////////////////////////////////////////////////////////
#include "ado2.h"
#define ChunkSize 100
///////////////////////////////////////////////////////
//
// CJetEngine Class
//
BOOL CJetEngine::CompactDatabase(CString strDatabaseSource, CString strDatabaseDestination)
{
try
{
::CoInitialize(NULL);
IJetEnginePtr jet(__uuidof(JetEngine));
HRESULT hr = jet->CompactDatabase(_bstr_t(strDatabaseSource.GetBuffer(0)), _bstr_t(strDatabaseDestination.GetBuffer(0)));
jet.Release();
::CoUninitialize();
return hr == S_OK;
}
catch(_com_error)
{
::CoUninitialize();
return FALSE;
}
}
BOOL CJetEngine::RefreshCache(ADODB::_Connection *pconn)
{
//Added by doodle@email.it
try
{
::CoInitialize(NULL);
IJetEnginePtr jet(__uuidof(JetEngine));
HRESULT hr = jet->RefreshCache(pconn);
::CoUninitialize();
return hr == S_OK;
}
catch(_com_error)
{
::CoUninitialize();
return FALSE;
}
return FALSE;
}
///////////////////////////////////////////////////////
//
// 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
throw CADOException(e.Error(), e.Description());
}
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 && m_pRecordset->GetState()!= adStateClosed;
}
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 /*= _T("")*/)
{
try
{
_variant_t vtSchemaID = vtMissing;
if(strlen(SchemaID) != 0)
{
vtSchemaID = SchemaID;
nSchema = adSchemaProviderSpecific;
}
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_CY: //Added by John Andy Johnson!!!!
vtFld.ChangeType(VT_R8);
val = vtFld.dblVal;
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.