//-----------------------------------------------------------------------------
// Microsoft OLE DB TABLECOPY Sample
// Copyright (C) 1996 By Microsoft Corporation.
//
// @doc
//
// @module DATASOURCE.CPP
//
//-----------------------------------------------------------------------------
/////////////////////////////////////////////////////////////////
// Includes
//
/////////////////////////////////////////////////////////////////
#include "WinMain.h"
#include "Common.h"
#include "DataSource.h"
#include "msdasql.h"//CLSID_MSDASQL provider guid
/////////////////////////////////////////////////////////////////
// CDataSource::CDataSource
//
/////////////////////////////////////////////////////////////////
CDataSource::CDataSource()
{
m_pIDBInitialize= NULL;
m_pIDBCreateCommand= NULL;
m_pICommandText= NULL;
m_pwszCatalog= NULL;
m_pwszDataSource= NULL;
m_pwszDBMS= NULL;
m_pwszDBMSVer= NULL;
m_pwszUserName= NULL;
m_pwszPassword= NULL;
m_pwszProvider= NULL;
m_pwszProviderVer= NULL;
m_pwszProviderOLEDBVer= NULL;
m_fReadOnly= FALSE;
m_fPrimaryKeysSupported = FALSE;
m_fMultipleParamSets = FALSE;
m_fConnected = FALSE;
}
/////////////////////////////////////////////////////////////////
// CDataSource::~CDataSource
//
/////////////////////////////////////////////////////////////////
CDataSource::~CDataSource()
{
Disconnect();
}
/////////////////////////////////////////////////////////////////
// BOOL CDataSource::IsConnected
//
/////////////////////////////////////////////////////////////////
BOOL CDataSource::IsConnected()
{
return m_fConnected;
}
/////////////////////////////////////////////////////////////////
// BOOL CDataSource::IsSimilar
//
/////////////////////////////////////////////////////////////////
BOOL CDataSource::IsSimilar(CDataSource* pCDataSource)
{
ASSERT(pCDataSource);
//Must be Connected to compare
if(!IsConnected() || !pCDataSource->IsConnected())
return FALSE;
ASSERT(m_pwszDBMS && pCDataSource->m_pwszDBMS);
ASSERT(m_pwszDBMSVer && pCDataSource->m_pwszDBMSVer);
//Similiar: Must be the same DBMS and Version
if(wcscmp(m_pwszDBMS, pCDataSource->m_pwszDBMS)==0 &&
wcscmp(m_pwszDBMSVer, pCDataSource->m_pwszDBMSVer)==0)
return TRUE;
return FALSE;
}
/////////////////////////////////////////////////////////////////
// BOOL CDataSource::IsEqual
//
/////////////////////////////////////////////////////////////////
BOOL CDataSource::IsEqual(CDataSource* pCDataSource)
{
ASSERT(pCDataSource);
//Must be Connected to compare
if(!IsConnected() || !pCDataSource->IsConnected())
return FALSE;
ASSERT(m_pwszCatalog && pCDataSource->m_pwszCatalog);
ASSERT(m_pwszDBMS && pCDataSource->m_pwszDBMS);
ASSERT(m_pwszDBMSVer && pCDataSource->m_pwszDBMSVer);
//Equal:
//Must have same Catalog, DBMS, Version
//Note, Don't have to have the same DataSource Name (an access
//database can have multiple names for the same file)
if(wcscmp(m_pwszCatalog, pCDataSource->m_pwszCatalog)==0 &&
wcscmp(m_pwszDBMS, pCDataSource->m_pwszDBMS)==0 &&
wcscmp(m_pwszDBMSVer, pCDataSource->m_pwszDBMSVer)==0)
return TRUE;
return FALSE;
}
/////////////////////////////////////////////////////////////////
// BOOL CDataSource::Disconnect
//
/////////////////////////////////////////////////////////////////
BOOL CDataSource::Disconnect()
{
m_fConnected = FALSE;
SAFE_FREE(m_pwszCatalog);
SAFE_FREE(m_pwszDataSource);
SAFE_FREE(m_pwszDBMS);
SAFE_FREE(m_pwszDBMSVer);
SAFE_FREE(m_pwszUserName);
SAFE_FREE(m_pwszPassword);
SAFE_FREE(m_pwszProvider);
SAFE_FREE(m_pwszProviderVer);
SAFE_FREE(m_pwszProviderOLEDBVer);
SAFE_RELEASE(m_pIDBInitialize);
SAFE_RELEASE(m_pIDBCreateCommand);
SAFE_RELEASE(m_pICommandText);
return TRUE;
}
/////////////////////////////////////////////////////////////////
// BOOL CDataSource::Connect
//
/////////////////////////////////////////////////////////////////
BOOL CDataSource::Connect(HWND hDlg)
{
ASSERT(hDlg);
HRESULThr;
ULONG cPropSets = 0;
DBPROPSET*rgPropSets = NULL;
IDBProperties*pIDBProperties = NULL;
IDBCreateSession* pIDBCreateSession = NULL;
//If there are current connections, close them
Disconnect();
//Get the IDBInitalize interface
hr = CoCreateInstance(CLSID_MSDASQL, NULL, CLSCTX_INPROC_SERVER, IID_IDBInitialize, (void**)&m_pIDBInitialize);
CHECK_ERRORS(hr, IID_IDBInitialize, L"CoCreateInstance Failed for CLSID_MSDASQL!");
//Create the Init Properties structs
//Set DBPROP_INIT_HWND / DBPROP_INIT_PROMPT
TESTC(hr = SetupInitProps(hDlg, &cPropSets, &rgPropSets));
XTESTC(hr = m_pIDBInitialize->QueryInterface(IID_IDBProperties, (void **)&pIDBProperties));
//Set the DataSource Properties
XTESTC(hr = pIDBProperties->SetProperties(cPropSets, rgPropSets));
//Initailize
//The user might have canceled the connect, don't display extended error info
TESTC(hr = m_pIDBInitialize->Initialize());
//Get the Session Object
XTESTC(hr = m_pIDBInitialize->QueryInterface(IID_IDBCreateSession, (void **)&pIDBCreateSession));
//Create the SessionObject
XTESTC(hr = pIDBCreateSession->CreateSession(NULL, IID_IDBCreateCommand, (IUnknown**)&m_pIDBCreateCommand));
//Get the CommandText object
XTESTC(hr = m_pIDBCreateCommand->CreateCommand(NULL, IID_ICommandText, (IUnknown**)&m_pICommandText));
//Now get the connection properties
TESTC(hr = GetConnectionProps());
//We are now successfully connected
m_fConnected = TRUE;
CLEANUP:
FreeProperties(cPropSets, rgPropSets);
//Release Interfaces
SAFE_RELEASE(pIDBProperties);
SAFE_RELEASE(pIDBCreateSession);
return (hr==S_OK);
}
/////////////////////////////////////////////////////////////////////////////
// HRESULT CDataSource::SetupInitProps
//
// Set up the initialization properties to make a connection to DSO
/////////////////////////////////////////////////////////////////////////////
HRESULT CDataSource::SetupInitProps(HWND hWnd, ULONG* pcPropSets, DBPROPSET** prgPropSets)
{
ASSERT(pcPropSets && prgPropSets);
HRESULT hr = S_OK;
//Allocate outer PropSet struct
ULONG cPropSets = 1;
DBPROPSET* rgPropSets = NULL;
SAFE_ALLOC(rgPropSets, DBPROPSET, cPropSets);
//Allocate inner properties
rgPropSets[0].guidPropertySet = DBPROPSET_DBINIT;
rgPropSets[0].cProperties = 2;
SAFE_ALLOC(rgPropSets[0].rgProperties, DBPROP, rgPropSets[0].cProperties);
// window handle to display dialog boxes to get the required input
rgPropSets[0].rgProperties[0].dwPropertyID= DBPROP_INIT_HWND;
rgPropSets[0].rgProperties[0].dwOptions= DBPROPOPTIONS_REQUIRED;
rgPropSets[0].rgProperties[0].dwStatus= DBPROPSTATUS_OK;
rgPropSets[0].rgProperties[0].colid= DB_NULLID;
rgPropSets[0].rgProperties[0].vValue.vt = VT_I4;
rgPropSets[0].rgProperties[0].vValue.lVal = (LONG)hWnd;
// prompt level
rgPropSets[0].rgProperties[1].dwPropertyID= DBPROP_INIT_PROMPT;
rgPropSets[0].rgProperties[1].dwOptions= DBPROPOPTIONS_REQUIRED;
rgPropSets[0].rgProperties[1].dwStatus= DBPROPSTATUS_OK;
rgPropSets[0].rgProperties[1].colid= DB_NULLID;
rgPropSets[0].rgProperties[1].vValue.vt = VT_I2;
rgPropSets[0].rgProperties[1].vValue.iVal = DBPROMPT_COMPLETE;
//Assign pointers
*pcPropSets = cPropSets;
*prgPropSets = rgPropSets;
CLEANUP:
return hr;
}
/////////////////////////////////////////////////////////////////////////////
// HRESULT CDataSource::GetConnectionProps
//
/////////////////////////////////////////////////////////////////////////////
HRESULT CDataSource::GetConnectionProps()
{
HRESULT hr;
DWORD dwSqlSupport = 0;
//Should have disconnected first
ASSERT(m_pwszDataSource == NULL);
ASSERT(m_pwszDBMS == NULL);
ASSERT(m_pwszDBMSVer == NULL);
ASSERT(m_pwszUserName == NULL);
ASSERT(m_pwszPassword == NULL);
//DBPROP_CURRENTCATALOG
TESTC(hr = GetProperty(m_pIDBInitialize, DBPROP_CURRENTCATALOG,
DBPROPSET_DATASOURCE, &m_pwszCatalog));
//DBPROP_DATASOURCENAME
TESTC(hr = GetProperty(m_pIDBInitialize, DBPROP_DATASOURCENAME,
DBPROPSET_DATASOURCEINFO, &m_pwszDataSource));
//DBPROP_DBMSNAME
TESTC(hr = GetProperty(m_pIDBInitialize, DBPROP_DBMSNAME,
DBPROPSET_DATASOURCEINFO, &m_pwszDBMS));
//DBPROP_DBMSVER
TESTC(hr = GetProperty(m_pIDBInitialize, DBPROP_DBMSVER,
DBPROPSET_DATASOURCEINFO, &m_pwszDBMSVer));
//DBPROP_USERNAME
TESTC(hr = GetProperty(m_pIDBInitialize, DBPROP_USERNAME,
DBPROPSET_DATASOURCEINFO, &m_pwszUserName));
//DBPROP_DATASOURCEREADONLY
TESTC(hr = GetProperty(m_pIDBInitialize, DBPROP_DATASOURCEREADONLY,
DBPROPSET_DATASOURCEINFO, &m_fReadOnly));
//DBPROP_PROVIDERNAME
TESTC(hr = GetProperty(m_pIDBInitialize, DBPROP_PROVIDERNAME,
DBPROPSET_DATASOURCEINFO, &m_pwszProvider));
//DBPROP_PROVIDEROLEDBVER
TESTC(hr = GetProperty(m_pIDBInitialize, DBPROP_PROVIDEROLEDBVER,
DBPROPSET_DATASOURCEINFO, &m_pwszProviderVer));
//DBPROP_PROVIDERVER
TESTC(hr = GetProperty(m_pIDBInitialize, DBPROP_PROVIDERVER,
DBPROPSET_DATASOURCEINFO, &m_pwszProviderOLEDBVer));
//DBPROP_MULTIPLEPARAMSETS
TESTC(hr = GetProperty(m_pIDBInitialize, DBPROP_MULTIPLEPARAMSETS,
DBPROPSET_DATASOURCEINFO, &m_fMultipleParamSets));
//DBPROP_SQLSUPPORT
TESTC(hr = GetProperty(m_pIDBInitialize, DBPROP_SQLSUPPORT,
DBPROPSET_DATASOURCEINFO, &dwSqlSupport));
//Are PrimaryKeys supported
m_fPrimaryKeysSupported = dwSqlSupport & DBPROPVAL_SQL_ANSI89_IEF;
CLEANUP:
return hr;
}