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.

192 lines
5.8 KiB

  1. //***************************************************************************
  2. //
  3. // (c) 1999-2001 by Microsoft Corp. All Rights Reserved.
  4. //
  5. // sqlutils.h
  6. //
  7. // cvadai 6-May-1999 created.
  8. //
  9. //***************************************************************************
  10. #ifndef _SQLCACHE_H_
  11. #define _SQLCACHE_H_
  12. // This file defines a class that maintains a cache of
  13. // SQL connections. Each instance of this class only
  14. // contains one set of logon credentials.
  15. // Each controller by default is only entitled to
  16. // a maximum of 20 connections.
  17. // ===================================================
  18. #define ROOTNAMESPACEID -1411745643584611171
  19. #define NAMESPACECLASSID 2372429868687864876
  20. #define MAPPEDNSCLASSID -7061265575274197401
  21. #define INSTANCESCLASSID 3373910491091605771
  22. #define CONTAINERASSOCID -7316356768687527881
  23. #define CLASSSECURITYID -6648033040106090009
  24. #define CLASSESID 864779262989765648
  25. #define CLASSINSTSECID 3879906028937869252
  26. #define THISNAMESPACEID 8909346217325131296
  27. #include <std.h>
  28. #include <seqstream.h>
  29. #include <comutil.h>
  30. #include <objcache.h>
  31. typedef struct InsertQualifierValues
  32. {
  33. int iPropID; // internal property ID
  34. wchar_t *pValue; // Value
  35. wchar_t *pRefKey; // The key string, if a reference property.
  36. int iPos; // Array position
  37. int iQfrID; // ID of qualifier or method dependency
  38. int iFlavor; // Qualifier flavor, if applicable.
  39. bool bLong; // TRUE if this is too long for this op sys
  40. int iStorageType; // our internal storage type
  41. bool bIndexed; // TRUE if indexed
  42. SQL_ID dClassId; // The actual Class ID of this property
  43. InsertQualifierValues()
  44. {
  45. pValue = NULL, pRefKey = NULL, iPos = 0, iQfrID = 0, iFlavor = 0,
  46. bLong = FALSE, iStorageType = 0, bIndexed = FALSE, dClassId = 0;
  47. }
  48. } InsertQfrValues;
  49. typedef InsertQfrValues InsertPropValues;
  50. // Structs to facilitate passing OLE DB parameters
  51. typedef struct tagBATCHPARAMS
  52. {
  53. DB_NUMERIC dObjectId; // @ObjectId
  54. DB_NUMERIC dScopeId; // @ScopeID
  55. DB_NUMERIC dClassId; // @ClassID
  56. int iQfrId[5]; // @QfrId[0-4]
  57. BSTR sPropValue[5]; // @QfrValue[0-4]
  58. int iPos[5]; // @QfrPos[0-4]
  59. int iPropId[5]; // @PropID[0-4]
  60. int iFlavor[5]; // @Flavor[0-4]
  61. } BATCHPARAMS;
  62. // SQL procedures
  63. #define SQL_POS_INSERT_CLASS 0x0
  64. #define SQL_POS_INSERT_CLASSDATA 0x1
  65. #define SQL_POS_INSERT_BATCH 0x2
  66. #define SQL_POS_INSERT_PROPBATCH 0x4
  67. #define SQL_POS_INSERT_BLOBDATA 0x8
  68. #define SQL_POS_HAS_INSTANCES 0x10
  69. #define SQL_POS_OBJECTEXISTS 0x20
  70. #define SQL_POS_GETCLASSOBJECT 0x40
  71. // Jet tables
  72. #define SQL_POS_OBJECTMAP 0x0
  73. #define SQL_POS_CLASSMAP 0x1
  74. #define SQL_POS_PROPERTYMAP 0x2
  75. #define SQL_POS_CLASSDATA 0x4
  76. #define SQL_POS_CLASSIMAGES 0x8
  77. #define SQL_POS_INDEXSTRING 0x10
  78. #define SQL_POS_INDEXNUMERIC 0x20
  79. #define SQL_POS_INDEXREAL 0x40
  80. #define SQL_POS_INDEXREF 0x80
  81. #define SQL_POS_ICOMMANDTEXT 0x00010000
  82. #define SQL_POS_ICOMMANDWITHPARAMS 0x00020000
  83. #define SQL_POS_IACCESSOR 0x00040000
  84. #define SQL_POS_HACCESSOR 0x00080000
  85. #define SQL_POS_IOPENROWSET 0x00100000
  86. #define SQL_POS_IROWSETCHANGE 0x00200000
  87. #define SQL_POS_IROWSETINDEX 0x00400000
  88. #define SQL_POS_IROWSET 0x00800000
  89. #define SQL_POS_PKINDEX 0x0
  90. #define SQL_POS_INDEX2 0x100
  91. #define SQL_POS_INDEX3 0x200
  92. typedef struct
  93. {
  94. HACCESSOR hAcc;
  95. IUnknown *pUnk;
  96. } WmiDBObject;
  97. class CDBObjectManager
  98. {
  99. public:
  100. CDBObjectManager();
  101. ~CDBObjectManager();
  102. void Empty();
  103. void * GetObject(DWORD type);
  104. void SetObject(DWORD type, void * pNew);
  105. void DeleteObject(DWORD type);
  106. private:
  107. CHashCache<WmiDBObject*> m_Objs;
  108. };
  109. class _declspec( dllexport ) CSQLConnection
  110. {
  111. public:
  112. CSQLConnection() {m_dwThreadId = GetCurrentThreadId(), m_bIsDistributed = FALSE;};
  113. virtual ~CSQLConnection () {};
  114. DWORD m_dwThreadId;
  115. BOOL m_bIsDistributed;
  116. };
  117. //***************************************************************************
  118. //
  119. // CSQLConnCache
  120. //
  121. //***************************************************************************
  122. class CSQLConnCache
  123. {
  124. typedef std::vector <CSQLConnection *> ConnVector;
  125. typedef std::vector <HANDLE> HandleVector;
  126. public:
  127. CSQLConnCache(DBPROPSET *pPropSet = NULL, DWORD dwMaxConns = 15,
  128. DWORD dwTimeout = 600);
  129. ~CSQLConnCache();
  130. HRESULT SetCredentials(DBPROPSET *pPropSet);
  131. HRESULT SetMaxConnections (DWORD dwMax);
  132. HRESULT SetTimeoutSecs(DWORD dwSecs);
  133. HRESULT SetDatabase(LPCWSTR lpDBName);
  134. HRESULT GetConnection(CSQLConnection **ppConn, BOOL bTransacted = FALSE,
  135. BOOL bDistributed = FALSE,DWORD dwTimeOutSecs = 600);
  136. HRESULT ReleaseConnection(CSQLConnection *pConn,
  137. HRESULT retcode = WBEM_S_NO_ERROR, BOOL bDistributed = FALSE);
  138. HRESULT DeleteUnusedConnections(BOOL bDeadOnly=FALSE);
  139. // Distrubuted only
  140. HRESULT FinalRollback(CSQLConnection *pConn);
  141. HRESULT FinalCommit(CSQLConnection *pConn);
  142. HRESULT GetCredentials(DBPROPSET **ppPropSet);
  143. HRESULT GetMaxConnections (DWORD &dwMax);
  144. HRESULT GetTimeoutSecs (DWORD &dwSecs);
  145. HRESULT GetDatabase(_bstr_t &sName);
  146. HRESULT ClearConnections();
  147. HRESULT Shutdown();
  148. private:
  149. HRESULT ExecInitialQueries(IDBInitialize *pDBInit, CSQLConnection *pConn);
  150. CRITICAL_SECTION m_cs;
  151. ConnVector m_Conns;
  152. DBPROPSET *m_pPropSet;
  153. DWORD m_dwMaxNumConns;
  154. DWORD m_dwTimeOutSecs;
  155. _bstr_t m_sDatabaseName;
  156. HandleVector m_WaitQueue;
  157. BOOL m_bInit;
  158. DWORD m_dwStatus;
  159. };
  160. #endif // _SQLCACHE_H_