Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

331 lines
7.9 KiB

  1. //***************************************************************************
  2. //
  3. // (c) 1999-2001 by Microsoft Corp. All Rights Reserved.
  4. //
  5. // sqlcache.cpp
  6. //
  7. // cvadai 6-May-1999 created.
  8. //
  9. //***************************************************************************
  10. #define _SQLCACHE_CPP_
  11. #pragma warning( disable : 4786 ) // identifier was truncated to 'number' characters in the
  12. #pragma warning( disable : 4251 ) // needs to have dll-interface to be used by clients of class
  13. #include "precomp.h"
  14. #include <std.h>
  15. #include <sqlcache.h>
  16. #include <arena.h>
  17. #include <reputils.h>
  18. #include <smrtptr.h>
  19. #if defined _WIN64
  20. #define ULONG unsigned __int64
  21. #define LONG __int64
  22. #endif
  23. typedef std::vector <CSQLConnection *> ConnVector;
  24. CDBObjectManager::CDBObjectManager()
  25. {
  26. }
  27. CDBObjectManager::~CDBObjectManager()
  28. {
  29. }
  30. void CDBObjectManager::Empty()
  31. {
  32. DWORD dwVals1[] =
  33. {
  34. SQL_POS_HACCESSOR,
  35. SQL_POS_ICOMMANDTEXT,
  36. SQL_POS_ICOMMANDWITHPARAMS,
  37. SQL_POS_IACCESSOR,
  38. SQL_POS_IOPENROWSET,
  39. SQL_POS_IROWSETCHANGE,
  40. SQL_POS_IROWSETINDEX,
  41. SQL_POS_IROWSET
  42. };
  43. DWORD dwVals2[] =
  44. {
  45. SQL_POS_INSERT_CLASS,
  46. SQL_POS_INSERT_CLASSDATA ,
  47. SQL_POS_INSERT_BATCH ,
  48. SQL_POS_INSERT_PROPBATCH,
  49. SQL_POS_INSERT_BLOBDATA ,
  50. SQL_POS_HAS_INSTANCES ,
  51. SQL_POS_OBJECTEXISTS ,
  52. SQL_POS_GETCLASSOBJECT
  53. };
  54. DWORD dwNumVals1 = sizeof(dwVals1) / sizeof(DWORD);
  55. DWORD dwNumVals2 = sizeof(dwVals2) / sizeof(DWORD);
  56. for (int i = 0; i < dwNumVals1; i++)
  57. {
  58. for (int j = 0; j < dwNumVals2; j++)
  59. {
  60. DeleteObject(dwVals1[i]|dwVals2[j]);
  61. }
  62. }
  63. }
  64. void * CDBObjectManager::GetObject(DWORD type)
  65. {
  66. WmiDBObject *pObj = NULL;
  67. void *pRet = NULL;
  68. m_Objs.Get(type, &pObj);
  69. if (pObj)
  70. {
  71. if (type & SQL_POS_HACCESSOR)
  72. pRet = (void *)pObj->hAcc;
  73. else
  74. pRet = pObj->pUnk;
  75. }
  76. return pRet;
  77. }
  78. void CDBObjectManager::SetObject(DWORD type, void *pNew)
  79. {
  80. WmiDBObject *pObj = new WmiDBObject;
  81. if (pObj)
  82. {
  83. if (!(type & SQL_POS_HACCESSOR))
  84. {
  85. void *pTemp = GetObject(type);
  86. if (pTemp)
  87. ((IUnknown *)pTemp)->Release();
  88. pObj->pUnk = (IUnknown *)pNew;
  89. pObj->hAcc = NULL;
  90. }
  91. else
  92. {
  93. void *pTemp = GetObject(SQL_POS_IACCESSOR | (type & 0xFFF));
  94. void *pTemp2 = GetObject(type);
  95. if (pTemp2 && pTemp)
  96. ((IAccessor *)pTemp)->ReleaseAccessor((HACCESSOR)pTemp, NULL);
  97. pObj->hAcc = (HACCESSOR)pNew;
  98. pObj->pUnk = NULL;
  99. }
  100. if (pNew)
  101. m_Objs.Insert((SQL_ID)type, pObj);
  102. }
  103. }
  104. void CDBObjectManager::DeleteObject(DWORD type)
  105. {
  106. WmiDBObject *pObj = NULL;
  107. m_Objs.Get(type, &pObj);
  108. if (pObj)
  109. {
  110. void *pTemp = GetObject(type);
  111. if (pTemp)
  112. {
  113. if (!(type & SQL_POS_HACCESSOR))
  114. {
  115. ((IUnknown *)pTemp)->Release();
  116. }
  117. else
  118. {
  119. void *pTemp2 = GetObject(SQL_POS_IACCESSOR | (type & 0xFFF));
  120. if (pTemp2 && pTemp)
  121. ((IAccessor *)pTemp2)->ReleaseAccessor((HACCESSOR)pTemp, NULL);
  122. }
  123. }
  124. }
  125. }
  126. //***************************************************************************
  127. //
  128. // CSQLConnCache::CSQLConnCache
  129. //
  130. //***************************************************************************
  131. CSQLConnCache::CSQLConnCache(DBPROPSET *pPropSet, DWORD MaxConns, DWORD Timeout)
  132. {
  133. InitializeCriticalSection(&m_cs);
  134. m_pPropSet = pPropSet;
  135. m_dwMaxNumConns = MaxConns;
  136. m_dwTimeOutSecs = Timeout;
  137. m_sDatabaseName = L"master";
  138. m_bInit = FALSE;
  139. m_dwStatus = 0;
  140. }
  141. //***************************************************************************
  142. //
  143. // CSQLConnCache::~CSQLConnCache
  144. //
  145. //***************************************************************************
  146. CSQLConnCache::~CSQLConnCache()
  147. {
  148. for (int i = 0; i < m_Conns.size(); i++)
  149. {
  150. CSQLConnection *pConn = m_Conns.at(i);
  151. delete pConn;
  152. }
  153. m_Conns.clear();
  154. DeleteCriticalSection(&m_cs);
  155. }
  156. //***************************************************************************
  157. //
  158. // CSQLConnCache::SetCredentials
  159. //
  160. //***************************************************************************
  161. HRESULT CSQLConnCache::SetCredentials(DBPROPSET *pPropSet)
  162. {
  163. CRepdrvrCritSec r(&m_cs);
  164. HRESULT hr = WBEM_S_NO_ERROR;
  165. if (!pPropSet)
  166. hr = WBEM_E_INVALID_PARAMETER;
  167. else
  168. {
  169. if (m_pPropSet)
  170. delete m_pPropSet;
  171. m_pPropSet = pPropSet;
  172. }
  173. return hr;
  174. }
  175. //***************************************************************************
  176. //
  177. // CSQLConnCache::SetMaxConnections
  178. //
  179. //***************************************************************************
  180. HRESULT CSQLConnCache::SetMaxConnections (DWORD dwMax)
  181. {
  182. CRepdrvrCritSec r (&m_cs);
  183. HRESULT hr = WBEM_S_NO_ERROR;
  184. if (!dwMax)
  185. hr = WBEM_E_INVALID_PARAMETER;
  186. else
  187. m_dwMaxNumConns = dwMax;
  188. return hr;
  189. }
  190. //***************************************************************************
  191. //
  192. // CSQLConnCache::SetTimeoutMinutes
  193. //
  194. //***************************************************************************
  195. HRESULT CSQLConnCache::SetTimeoutSecs(DWORD dwSecs)
  196. {
  197. CRepdrvrCritSec r (&m_cs);
  198. m_dwTimeOutSecs = dwSecs;
  199. return WBEM_S_NO_ERROR;
  200. }
  201. //***************************************************************************
  202. //
  203. // CSQLConnCache::SetDatabase
  204. //
  205. //***************************************************************************
  206. HRESULT CSQLConnCache::SetDatabase(LPCWSTR lpDBName)
  207. {
  208. CRepdrvrCritSec r (&m_cs);
  209. m_sDatabaseName = lpDBName;
  210. return WBEM_S_NO_ERROR;
  211. }
  212. //***************************************************************************
  213. //
  214. // CSQLConnCache::GetCredentials
  215. //
  216. //***************************************************************************
  217. HRESULT CSQLConnCache::GetCredentials(DBPROPSET **ppPropSet)
  218. {
  219. CRepdrvrCritSec r (&m_cs);
  220. HRESULT hr = WBEM_S_NO_ERROR;
  221. if (!ppPropSet)
  222. hr = WBEM_E_INVALID_PARAMETER;
  223. else
  224. *ppPropSet = m_pPropSet;
  225. return hr;
  226. }
  227. //***************************************************************************
  228. //
  229. // CSQLConnCache::GetMaxConnections
  230. //
  231. //***************************************************************************
  232. HRESULT CSQLConnCache::GetMaxConnections (DWORD &dwMax)
  233. {
  234. CRepdrvrCritSec r (&m_cs);
  235. HRESULT hr = WBEM_S_NO_ERROR;
  236. dwMax = m_dwMaxNumConns;
  237. return hr;
  238. }
  239. //***************************************************************************
  240. //
  241. // CSQLConnCache::GetTimeoutSecs
  242. //
  243. //***************************************************************************
  244. HRESULT CSQLConnCache::GetTimeoutSecs (DWORD &dwSecs)
  245. {
  246. CRepdrvrCritSec r (&m_cs);
  247. HRESULT hr = WBEM_S_NO_ERROR;
  248. dwSecs = m_dwTimeOutSecs;
  249. return hr;
  250. }
  251. //***************************************************************************
  252. //
  253. // CSQLConnCache::GetDatabase
  254. //
  255. //***************************************************************************
  256. HRESULT CSQLConnCache::GetDatabase (_bstr_t &sName)
  257. {
  258. CRepdrvrCritSec r (&m_cs);
  259. HRESULT hr = WBEM_S_NO_ERROR;
  260. sName = m_sDatabaseName;
  261. return hr;
  262. }
  263. //***************************************************************************
  264. //
  265. // CSQLConnCache::ClearConnections
  266. //
  267. //***************************************************************************
  268. HRESULT CSQLConnCache::ClearConnections()
  269. {
  270. HRESULT hr = WBEM_S_NO_ERROR;
  271. hr = Shutdown();
  272. for (int i = m_Conns.size()-1; i >= 0; i--)
  273. {
  274. CSQLConnection *pConn = m_Conns.at(i);
  275. delete pConn;
  276. }
  277. m_Conns.clear();
  278. return hr;
  279. }