#include "ConnectionEvents.h"
ADODBConnectionEvents::ADODBConnectionEvents(HWND m_hwnd, _Connection* pConnection) {
hwnd = m_hwnd;
refCount = 0;
dwCookie = 0;
pCnnPt = NULL;
IConnectionPointContainer* pCnnPtContainer = NULL;
HRESULT hr = pConnection->QueryInterface(IID_IConnectionPointContainer, reinterpret_cast<void**>(&pCnnPtContainer));
if (FAILED(hr)) {
return;
}
hr = pCnnPtContainer->FindConnectionPoint(__uuidof(ADODB::ConnectionEvents), &pCnnPt);
pCnnPtContainer->Release();
pCnnPtContainer = NULL;
if (FAILED(hr)) return;
refCount = 1;
}
HRESULT ADODBConnectionEvents::InfoMessage(Error* pError, EventStatusEnum* adStatus, _Connection* pConnection)
{
return E_NOTIMPL;
}
HRESULT ADODBConnectionEvents::BeginTransComplete(long TransactionLevel, Error* pError, EventStatusEnum* adStatus, _Connection* pConnection)
{
return E_NOTIMPL;
}
HRESULT ADODBConnectionEvents::CommitTransComplete(Error* pError, EventStatusEnum* adStatus, _Connection* pConnection)
{
return E_NOTIMPL;
}
HRESULT ADODBConnectionEvents::RollbackTransComplete(Error* pError, EventStatusEnum* adStatus, _Connection* pConnection)
{
return E_NOTIMPL;
}
HRESULT ADODBConnectionEvents::WillExecute(BSTR* Source, CursorTypeEnum* CursorType, LockTypeEnum* LockType, long* Options, EventStatusEnum* adStatus, _Command* pCommand, _Recordset* pRecordset, _Connection* pConnection)
{
return E_NOTIMPL;
}
HRESULT ADODBConnectionEvents::ExecuteComplete(long RecordsAffected, Error* pError, EventStatusEnum* adStatus, _Command* pCommand, _Recordset* pRecordset, _Connection* pConnection)
{
return E_NOTIMPL;
}
HRESULT ADODBConnectionEvents::WillConnect(BSTR* bstrConnectionString, BSTR* UserID, BSTR* Password, long* Options, EventStatusEnum* adStatus, _Connection* pConnection)
{
return E_NOTIMPL;
}
HRESULT ADODBConnectionEvents::ConnectComplete(Error* pError, EventStatusEnum* adStatus, _Connection* pConnection)
{
MessageBox(hwnd, L"ConnectComplete event", L"Event", MB_OK | MB_ICONINFORMATION);
*adStatus = adStatusUnwantedEvent;
return S_OK;
}
HRESULT ADODBConnectionEvents::Disconnect(EventStatusEnum* adStatus, _Connection* pConnection)
{
MessageBox(hwnd, L"Disconnect", L"Event", MB_OK | MB_ICONINFORMATION);
*adStatus = adStatusUnwantedEvent;
return S_OK;
}
ULONG ADODBConnectionEvents::AddRef()
{
InterlockedIncrement(&refCount);
return refCount;
}
ULONG ADODBConnectionEvents::Release() {
InterlockedDecrement(&refCount);
if (!refCount) {
delete this;
return 0;
}
return refCount;
}
HRESULT ADODBConnectionEvents::QueryInterface(REFIID riid, void** ppvObject) {
if (!ppvObject) return E_POINTER;
if (IsEqualIID(riid, IID_IUnknown) || IsEqualIID(riid, _uuidof(ConnectionEventsVt))) {
AddRef();
*ppvObject = this;
return S_OK;
}
return E_NOINTERFACE;
}
ADODBConnectionEvents::~ADODBConnectionEvents() {
if (pCnnPt) {
pCnnPt->Unadvise(dwCookie);
pCnnPt->Release();
}
}
BOOL ADODBConnectionEvents::SetConnectionPoint(IConnectionPoint* lpCnnPt) {
if (!lpCnnPt) return FALSE;
pCnnPt = lpCnnPt;
return TRUE;
}
BOOL ADODBConnectionEvents::Advise() {
if (!pCnnPt) return FALSE;
return SUCCEEDED(pCnnPt->Advise((IUnknown*)this, &dwCookie));
}
BOOL ADODBConnectionEvents::Unadvise() {
if (!pCnnPt) return FALSE;
return SUCCEEDED(pCnnPt->Unadvise(dwCookie));
}