📄 atldbcli.h
字号:
// Use if you didn't pass the GUID to the constructor.
void SetGUID(const GUID& guid)
{
guidPropertySet = guid;
}
// Add the passed property to the property set
bool AddProperty(DWORD dwPropertyID, const VARIANT& var)
{
HRESULT hr;
if (!Add())
return false;
rgProperties[cProperties].dwPropertyID = dwPropertyID;
hr = ::VariantCopy(&(rgProperties[cProperties].vValue), const_cast<VARIANT*>(&var));
if (FAILED(hr))
return false;
cProperties++;
return true;
}
// Add the passed property to the property set
bool AddProperty(DWORD dwPropertyID, LPCSTR szValue)
{
USES_CONVERSION;
if (!Add())
return false;
rgProperties[cProperties].dwPropertyID = dwPropertyID;
rgProperties[cProperties].vValue.vt = VT_BSTR;
rgProperties[cProperties].vValue.bstrVal = SysAllocString(A2COLE(szValue));
if (rgProperties[cProperties].vValue.bstrVal == NULL)
return false;
cProperties++;
return true;
}
// Add the passed property to the property set
bool AddProperty(DWORD dwPropertyID, LPCWSTR szValue)
{
USES_CONVERSION;
if (!Add())
return false;
rgProperties[cProperties].dwPropertyID = dwPropertyID;
rgProperties[cProperties].vValue.vt = VT_BSTR;
rgProperties[cProperties].vValue.bstrVal = SysAllocString(W2COLE(szValue));
if (rgProperties[cProperties].vValue.bstrVal == NULL)
return false;
cProperties++;
return true;
}
// Add the passed property to the property set
bool AddProperty(DWORD dwPropertyID, bool bValue)
{
if (!Add())
return false;
rgProperties[cProperties].dwPropertyID = dwPropertyID;
rgProperties[cProperties].vValue.vt = VT_BOOL;
#pragma warning(disable: 4310) // cast truncates constant value
rgProperties[cProperties].vValue.boolVal = (bValue) ? VARIANT_TRUE : VARIANT_FALSE;
#pragma warning(default: 4310)
cProperties++;
return true;
}
// Add the passed property to the property set
bool AddProperty(DWORD dwPropertyID, BYTE bValue)
{
if (!Add())
return false;
rgProperties[cProperties].dwPropertyID = dwPropertyID;
rgProperties[cProperties].vValue.vt = VT_UI1;
rgProperties[cProperties].vValue.bVal = bValue;
cProperties++;
return true;
}
// Add the passed property to the property set
bool AddProperty(DWORD dwPropertyID, short nValue)
{
if (!Add())
return false;
rgProperties[cProperties].dwPropertyID = dwPropertyID;
rgProperties[cProperties].vValue.vt = VT_I2;
rgProperties[cProperties].vValue.iVal = nValue;
cProperties++;
return true;
}
// Add the passed property to the property set
bool AddProperty(DWORD dwPropertyID, long nValue)
{
if (!Add())
return false;
rgProperties[cProperties].dwPropertyID = dwPropertyID;
rgProperties[cProperties].vValue.vt = VT_I4;
rgProperties[cProperties].vValue.lVal = nValue;
cProperties++;
return true;
}
// Add the passed property to the property set
bool AddProperty(DWORD dwPropertyID, float fltValue)
{
if (!Add())
return false;
rgProperties[cProperties].dwPropertyID = dwPropertyID;
rgProperties[cProperties].vValue.vt = VT_R4;
rgProperties[cProperties].vValue.fltVal = fltValue;
cProperties++;
return true;
}
// Add the passed property to the property set
bool AddProperty(DWORD dwPropertyID, double dblValue)
{
if (!Add())
return false;
rgProperties[cProperties].dwPropertyID = dwPropertyID;
rgProperties[cProperties].vValue.vt = VT_R8;
rgProperties[cProperties].vValue.dblVal = dblValue;
cProperties++;
return true;
}
// Add the passed property to the property set
bool AddProperty(DWORD dwPropertyID, CY cyValue)
{
if (!Add())
return false;
rgProperties[cProperties].dwPropertyID = dwPropertyID;
rgProperties[cProperties].vValue.vt = VT_CY;
rgProperties[cProperties].vValue.cyVal = cyValue;
cProperties++;
return true;
}
// Implementation
// Create memory to add a new property
bool Add()
{
rgProperties = (DBPROP*)CoTaskMemRealloc(rgProperties, (cProperties + 1) * sizeof(DBPROP));
if (rgProperties != NULL)
{
rgProperties[cProperties].dwOptions = DBPROPOPTIONS_REQUIRED;
rgProperties[cProperties].colid = DB_NULLID;
rgProperties[cProperties].vValue.vt = VT_EMPTY;
return true;
}
else
return false;
}
// Copies in the passed value now it this value been cleared
void InternalCopy(const CDBPropSet& propset)
{
cProperties = propset.cProperties;
guidPropertySet = propset.guidPropertySet;
rgProperties = (DBPROP*)CoTaskMemAlloc(cProperties * sizeof(DBPROP));
if (rgProperties != NULL)
{
for (ULONG i = 0; i < cProperties; i++)
{
rgProperties[i].dwPropertyID = propset.rgProperties[i].dwPropertyID;
rgProperties[i].dwOptions = DBPROPOPTIONS_REQUIRED;
rgProperties[i].colid = DB_NULLID;
rgProperties[i].vValue.vt = VT_EMPTY;
VariantCopy(&rgProperties[i].vValue, &propset.rgProperties[i].vValue);
}
}
else
{
// The memory allocation failed so set the count
// of properties to zero
cProperties = 0;
}
}
};
///////////////////////////////////////////////////////////////////////////
// class CDBPropIDSet
class CDBPropIDSet : public tagDBPROPIDSET
{
// Constructors and Destructors
public:
CDBPropIDSet()
{
rgPropertyIDs = NULL;
cPropertyIDs = 0;
}
CDBPropIDSet(const GUID& guid)
{
rgPropertyIDs = NULL;
cPropertyIDs = 0;
guidPropertySet = guid;
}
CDBPropIDSet(const CDBPropIDSet& propidset)
{
InternalCopy(propidset);
}
~CDBPropIDSet()
{
if (rgPropertyIDs != NULL)
free(rgPropertyIDs);
}
CDBPropIDSet& operator=(CDBPropIDSet& propset)
{
this->~CDBPropIDSet();
InternalCopy(propset);
return *this;
}
// Set the GUID of the property ID set
void SetGUID(const GUID& guid)
{
guidPropertySet = guid;
}
// Add a property ID to the set
bool AddPropertyID(DBPROPID propid)
{
if (!Add())
return false;
rgPropertyIDs[cPropertyIDs] = propid;
cPropertyIDs++;
return true;
}
// Implementation
bool Add()
{
rgPropertyIDs = (DBPROPID*)realloc(rgPropertyIDs, (cPropertyIDs + 1) * sizeof(DBPROPID));
return (rgPropertyIDs != NULL) ? true : false;
}
void InternalCopy(const CDBPropIDSet& propidset)
{
cPropertyIDs = propidset.cPropertyIDs;
guidPropertySet = propidset.guidPropertySet;
rgPropertyIDs = (DBPROPID*)malloc(cPropertyIDs * sizeof(DBPROPID));
if (rgPropertyIDs != NULL)
{
for (ULONG i = 0; i < cPropertyIDs; i++)
rgPropertyIDs[i] = propidset.rgPropertyIDs[i];
}
else
{
// The memory allocation failed so set the count
// of properties to zero
cPropertyIDs = 0;
}
}
};
///////////////////////////////////////////////////////////////////////////
// class CBookmarkBase
class ATL_NO_VTABLE CBookmarkBase
{
public:
virtual ULONG GetSize() const = 0;
virtual BYTE* GetBuffer() const = 0;
};
///////////////////////////////////////////////////////////////////////////
// class CBookmark
template <ULONG nSize = 0>
class CBookmark : public CBookmarkBase
{
public:
virtual ULONG GetSize() const { return nSize; }
virtual BYTE* GetBuffer() const { return (BYTE*)m_rgBuffer; }
// Implementation
BYTE m_rgBuffer[nSize];
};
// Size of 0 means that the memory for the bookmark will be allocated
// at run time.
template <>
class CBookmark<0> : public CBookmarkBase
{
public:
CBookmark()
{
m_nSize = 0;
m_pBuffer = NULL;
}
CBookmark(ULONG nSize)
{
m_pBuffer = NULL;
ATLTRY(m_pBuffer = new BYTE[nSize]);
m_nSize = (m_pBuffer == NULL) ? 0 : nSize;
}
~CBookmark()
{
delete [] m_pBuffer;
}
CBookmark& operator=(const CBookmark& bookmark)
{
SetBookmark(bookmark.GetSize(), bookmark.GetBuffer());
return *this;
}
virtual ULONG GetSize() const { return m_nSize; }
virtual BYTE* GetBuffer() const { return m_pBuffer; }
// Sets the bookmark to the passed value
HRESULT SetBookmark(ULONG nSize, BYTE* pBuffer)
{
ATLASSERT(pBuffer != NULL);
delete [] m_pBuffer;
m_pBuffer = NULL;
ATLTRY(m_pBuffer = new BYTE[nSize]);
if (m_pBuffer != NULL)
{
memcpy(m_pBuffer, pBuffer, nSize);
m_nSize = nSize;
return S_OK;
}
else
{
m_nSize = 0;
return E_OUTOFMEMORY;
}
}
ULONG m_nSize;
BYTE* m_pBuffer;
};
///////////////////////////////////////////////////////////////////////////
// class CAccessorBase
class CAccessorBase
{
public:
CAccessorBase()
{
m_pAccessorInfo = NULL;
m_nAccessors = 0;
m_pBuffer = NULL;
}
void Close()
{
// If Close is called then ReleaseAccessors must have been
// called first
ATLASSERT(m_nAccessors == 0);
ATLASSERT(m_pAccessorInfo == NULL);
}
// Get the number of accessors that have been created
ULONG GetNumAccessors() const { return m_nAccessors; }
// Get the handle of the passed accessor (offset from 0)
HACCESSOR GetHAccessor(ULONG nAccessor) const
{
ATLASSERT(nAccessor<m_nAccessors);
return m_pAccessorInfo[nAccessor].hAccessor;
};
// Called during Close to release the accessor information
HRESULT ReleaseAccessors(IUnknown* pUnk)
{
ATLASSERT(pUnk != NULL);
HRESULT hr = S_OK;
if (m_nAccessors > 0)
{
CComPtr<IAccessor> spAccessor;
hr = pUnk->QueryInterface(IID_IAccessor, (void**)&spAccessor);
if (SUCCEEDED(hr))
{
ATLASSERT(m_pAccessorInfo != NULL);
for (ULONG i = 0; i < m_nAccessors; i++)
spAccessor->ReleaseAccessor(m_pAccessorInfo[i].hAccessor, NULL);
}
m_nAccessors = 0;
delete [] m_pAccessorInfo;
m_pAccessorInfo = NULL;
}
return hr;
}
// Returns true or false depending upon whether data should be
// automatically retrieved for the passed accessor.
bool IsAutoAccessor(ULONG nAccessor) const
{
ATLASSERT(nAccessor < m_nAccessors);
ATLASSERT(m_pAccessorInfo != NULL);
return m_pAccessorInfo[nAccessor].bAutoAccessor;
}
// Implementation
// Used by the rowset class to find out where to place the data
BYTE* GetBuffer() const
{
return m_pBuffer;
}
// Set the buffer that is used to retrieve the data
void SetBuffer(BYTE* pBuffer)
{
m_pBuffer = pBuffer;
}
// Allocate internal memory for the passed number of accessors
HRESULT AllocateAccessorMemory(int nAccessors)
{
// Can't be called twice without calling ReleaseAccessors first
ATLASSERT(m_pAccessorInfo == NULL);
m_nAccessors = nAccessors;
m_pAccessorInfo = NULL;
ATLTRY(m_pAccessorInfo = new _ATL_ACCESSOR_INFO[nAccessors]);
if (m_pAccessorInfo == NULL)
return E_OUTOFMEMORY;
else
return S_OK;
}
// BindParameters will be overriden if parameters are used
HRESULT BindParameters(HACCESSOR*, ICommand*, void**) { return S_OK; }
// Create an accessor for the passed binding information. The created accessor is
// returned through the pHAccessor parameter.
static HRESULT BindEntries(DBBINDING* pBindings, int nColumns, HACCESSOR* pHAccessor,
ULONG nSize, IAccessor* pAccessor)
{
ATLASSERT(pBindings != NULL);
ATLASSERT(pHAccessor != NULL);
ATLASSERT(pAccessor != NULL);
HRESULT hr;
int i;
DWORD dwAccessorFlags = (pBindings->eParamIO == DBPARAMIO_NOTPARAM) ?
DBACCESSOR_ROWDATA : DBACCESSOR_PARAMETERDATA;
#ifdef _DEBUG
// In debug builds we will retrieve the status flags and trace out
// any errors that may occur.
DBBINDSTATUS* pStatus = NULL;
ATLTRY(pStatus = new DBBINDSTATUS[nColumns]);
hr = pAccessor->CreateAccessor(dwAccessorFlags, nColumns,
pBindings, nSize, pHAccessor, pStatus);
if (FAILED(hr) && pStatus != NULL)
{
for (i=0; i<nColumns; i++)
{
if (pStatus[i] != DBBINDSTATUS_OK)
ATLTRACE2(atlTraceDBClient, 0, _T("Binding entry %d failed. Status: %d\n"), i, pStatus[i]);
}
}
delete [] pStatus;
#else
hr = pAccessor->CreateAccessor(dwAccessorFlags, nColumns,
pBindings, nSize, pHAccessor, NULL);
#endif
for (i=0; i<nColumns; i++)
delete pBindings[i].pObject;
return hr;
}
// Set up the binding structure pointed to by pBindings based upon
// the other passed parameters.
static void Bind(DBBINDING* pBinding, ULONG nOrdinal, DBTYPE wType,
ULONG nLength, BYTE nPrecision, BYTE nScale, DBPARAMIO eParamIO,
ULONG nDataOffset, ULONG nLengthOffset = NULL, ULONG nStatusOffset = NULL,
DBOBJECT* pdbobject = NULL)
{
ATLASSERT(pBinding != NULL);
// If we are getting a pointer to the data then let the provider
// own the memory
if (wType & DBTYPE_BYREF)
pBinding->dwMemOwner = DBMEMOWNER_PROVIDEROWNED;
else
pBinding->dwMemOwner = DBMEMOWNER_CLIENTOWNED;
pBinding->pObject = pdbobject;
pBinding->eParamIO = eParamIO;
pBinding->iOrdinal = nOrdinal;
pBinding->wType = wType;
pBinding->bPrecision = nPrecision;
pBinding->bScale = nScale;
pBinding->dwFlags = 0;
pBinding->obValue = nDataOffset;
pBinding->obLength = 0;
pBinding->obStatus = 0;
pBinding->pTypeInfo = NULL;
pBinding->pBindExt = NULL;
pBinding->cbMaxLen = nLength;
pBinding->dwPart = DBPART_VALUE;
if (nLengthOffset != NULL)
{
pBinding->dwPart |= DBPART_LENGTH;
pBinding->obLength = nLengthOffset;
}
if (nStatusOffset != NULL)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -