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.

329 lines
11 KiB

  1. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Microsoft WMIOLE DB Provider
  4. // (C) Copyright 1999 Microsoft Corporation. All Rights Reserved.
  5. //
  6. //
  7. // ITableDef.cpp - ITableDefination interface implementation
  8. //
  9. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  10. #include "headers.h"
  11. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  12. //
  13. // Create a new table and return a pointer to an empty rowset
  14. //
  15. // HRESULT
  16. // S_OK - Success
  17. // DB_S_ERRORSOCCURRED - General failure
  18. // E_INVALIDARG - one of the arguments is invalid
  19. // DB_E_NOTABLE - the table defined ( the parent class referred is not present)
  20. // DB_E_BADCOLUMNID - the column id is bad
  21. // DB_SEC_E_PERMISSIONDENIED - does not have permission to create the table
  22. // E_NOINTERFACE - does not implement that is asked for
  23. // DB_E_BADTABLEID - The tableID is bad
  24. //
  25. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  26. HRESULT STDMETHODCALLTYPE CImpITableDefinition::CreateTable( IUnknown __RPC_FAR *pUnkOuter,
  27. DBID __RPC_FAR *pTableID,
  28. DBORDINAL cColumnDescs,
  29. const DBCOLUMNDESC __RPC_FAR rgColumnDescs[ ],
  30. REFIID riid, ULONG cPropertySets,
  31. DBPROPSET __RPC_FAR rgPropertySets[ ],
  32. DBID __RPC_FAR *__RPC_FAR *ppTableID,
  33. IUnknown __RPC_FAR *__RPC_FAR *ppRowset) // OUT
  34. {
  35. HRESULT hr = E_FAIL;
  36. CRowset* pRowset = NULL;
  37. WCHAR *pwcsTableName = NULL;
  38. CSetStructuredExceptionHandler seh;
  39. TRY_BLOCK;
  40. //===================================================================
  41. // Initialize incoming parameters
  42. //===================================================================
  43. if(ppRowset != NULL)
  44. {
  45. *ppRowset = NULL;
  46. }
  47. //==========================
  48. // Seriliaze the object
  49. //==========================
  50. CAutoBlock cab(m_pObj->GetCriticalSection());
  51. //==========================
  52. // Clear Error information
  53. //==========================
  54. g_pCError->ClearErrorInfo();
  55. if(pTableID->uName.pwszName == NULL)
  56. {
  57. hr = DB_E_BADTABLEID;
  58. }
  59. else
  60. if(wcslen(pTableID->uName.pwszName) == 0 || pTableID->eKind != DBKIND_NAME)
  61. {
  62. hr = DB_E_BADTABLEID;
  63. }
  64. else
  65. //===================================================================================
  66. // Check the connection is valid
  67. //===================================================================================
  68. if ( m_pObj->m_pCDataSource->m_pWbemWrap->ValidConnection() ){
  69. //===============================================================
  70. // construct a new CWmiOleDBMap class
  71. //===============================================================
  72. CWmiOleDBMap Map;
  73. if(SUCCEEDED(hr = Map.FInit(NO_QUALIFIERS,(LPWSTR)pTableID->uName.pwszName,m_pObj->m_pCDataSource->m_pWbemWrap)))
  74. {
  75. hr = Map.CreateTable(cColumnDescs, rgColumnDescs, cPropertySets, rgPropertySets);
  76. if ( SUCCEEDED(hr) && ppRowset != NULL)
  77. {
  78. pwcsTableName = Map.GetClassName();
  79. try
  80. {
  81. //===========================================================
  82. // Now, return the rowset
  83. //===========================================================
  84. pRowset = new CRowset(pUnkOuter,m_pObj);
  85. }
  86. catch(...)
  87. {
  88. SAFE_DELETE_PTR(pRowset);
  89. throw;
  90. }
  91. if ( pRowset ){
  92. //=======================================================
  93. //Set session of this rowset
  94. //=======================================================
  95. // m_pObj->m_fRowsetCreated = TRUE;
  96. if ( SUCCEEDED(hr) ){
  97. //=======================================================
  98. // Store the newly created table in pRowset & get ptr
  99. //=======================================================
  100. hr = pRowset->InitRowset(cPropertySets, rgPropertySets,(LPWSTR)pwcsTableName);
  101. if( SUCCEEDED(hr)){
  102. hr = pRowset->QueryInterface(IID_IRowset, (void**)ppRowset);
  103. }
  104. }
  105. }
  106. }
  107. }
  108. if ( !SUCCEEDED(hr) && ppRowset != NULL) {
  109. SAFE_DELETE_PTR(pRowset);
  110. *ppRowset = NULL;
  111. }
  112. if(ppTableID != NULL && SUCCEEDED( hr))
  113. {
  114. //====================================
  115. // Allocate memory for DBID
  116. //====================================
  117. *ppTableID = (DBID *)g_pIMalloc->Alloc(sizeof(DBID));
  118. (*ppTableID)->eKind = DBKIND_NAME;
  119. //====================================
  120. // Allocate memory for then tablename
  121. //====================================
  122. (*ppTableID)->uName.pwszName = (WCHAR *)g_pIMalloc->Alloc(sizeof(WCHAR) *(wcslen(pwcsTableName) + 1));
  123. wcscpy((*ppTableID)->uName.pwszName,Map.GetClassName());
  124. }
  125. }
  126. hr = hr == S_OK ? hr :g_pCError->PostHResult(hr,&IID_ITableDefinition);
  127. CATCH_BLOCK_HRESULT(hr,L"ITableDefinition::CreateTable");
  128. return hr;
  129. }
  130. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  131. // DropTable
  132. // S_OK - Success
  133. // DB_S_ERRORSOCCURRED - General failure
  134. // E_INVALIDARG - one of the arguments is invalid
  135. // DB_E_NOTABLE - the table defined in invalid
  136. // DB_SEC_E_PERMISSIONDENIED - does not have permission to drop the table
  137. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  138. HRESULT STDMETHODCALLTYPE CImpITableDefinition::DropTable( DBID __RPC_FAR *pTableID) // IN
  139. {
  140. HRESULT hr = E_FAIL;
  141. CBSTR strClassName;
  142. CSetStructuredExceptionHandler seh;
  143. TRY_BLOCK;
  144. CAutoBlock cab(m_pObj->GetCriticalSection());
  145. //====================================
  146. // Clear Error information
  147. //====================================
  148. g_pCError->ClearErrorInfo();
  149. //===================================================================================
  150. // Check the connection is valid
  151. //===================================================================================
  152. if ( m_pObj->m_pCDataSource->m_pWbemWrap->ValidConnection() ){
  153. if(pTableID->uName.pwszName == NULL)
  154. {
  155. hr = DB_E_BADTABLEID;
  156. }
  157. else
  158. if ( wcslen(pTableID->uName.pwszName) == 0 || pTableID->eKind != DBKIND_NAME){
  159. hr = DB_E_BADTABLEID;
  160. }
  161. else{
  162. strClassName.SetStr(pTableID->uName.pwszName);
  163. //=========================================================================
  164. // Call this function of the connection wrapper class to delete the class
  165. //=========================================================================
  166. hr = m_pObj->m_pCDataSource->m_pWbemWrap->DeleteClass((BSTR)strClassName);
  167. }
  168. }
  169. hr = hr == S_OK ? hr :g_pCError->PostHResult(hr,&IID_ITableDefinition);
  170. CATCH_BLOCK_HRESULT(hr,L"ITableDefinition::DropTable");
  171. return hr;
  172. }
  173. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  174. //A method of ITableDefinition interface to add a column to a table
  175. // Return Values :
  176. // S_OK - Success
  177. // DB_S_ERRORSOCCURRED - General failure
  178. // E_INVALIDARG - one of the arguments is invalid
  179. // DB_E_NOTABLE - the table defined in invalid
  180. // DB_E_BADCOLUMNID - the column id is bad
  181. // DB_SEC_E_PERMISSIONDENIED - does not have permission to add a column
  182. //
  183. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  184. HRESULT STDMETHODCALLTYPE CImpITableDefinition::AddColumn( DBID __RPC_FAR *pTableID, // IN
  185. DBCOLUMNDESC __RPC_FAR *pColumnDesc, //IN|OUT
  186. DBID __RPC_FAR *__RPC_FAR *ppColumnID)
  187. {
  188. HRESULT hr = E_FAIL;
  189. CSetStructuredExceptionHandler seh;
  190. TRY_BLOCK;
  191. CAutoBlock cab(m_pObj->GetCriticalSection());
  192. //===============================
  193. // Clear Error information
  194. //===============================
  195. g_pCError->ClearErrorInfo();
  196. //===================================================================================
  197. // Check the connection is valid
  198. //===================================================================================
  199. if ( m_pObj->m_pCDataSource->m_pWbemWrap->ValidConnection() ){
  200. if(pTableID->uName.pwszName == NULL)
  201. {
  202. hr = DB_E_BADTABLEID;
  203. }
  204. else
  205. if( NULL == pColumnDesc ||
  206. pTableID->eKind != DBKIND_NAME || 0 == wcslen(pTableID->uName.pwszName))
  207. {
  208. hr = E_INVALIDARG;
  209. }
  210. else
  211. {
  212. CWmiOleDBMap Map;
  213. if(SUCCEEDED(hr =Map.FInit(NO_QUALIFIERS,pTableID->uName.pwszName,m_pObj->m_pCDataSource->m_pWbemWrap)))
  214. {
  215. if(S_OK == (hr = Map.AddColumn(pColumnDesc, ppColumnID)) && ppColumnID != NULL )
  216. {
  217. //=====================================
  218. // Allocate memory for DBID
  219. //=====================================
  220. *ppColumnID = (DBID *)g_pIMalloc->Alloc(sizeof(DBID));
  221. (*ppColumnID)->eKind = DBKIND_NAME;
  222. //=====================================
  223. // Allocate memory for then tablename
  224. //=====================================
  225. (*ppColumnID)->uName.pwszName = (WCHAR *)g_pIMalloc->Alloc(sizeof(WCHAR) *(wcslen(pColumnDesc->dbcid.uName.pwszName) + 1));
  226. wcscpy((*ppColumnID)->uName.pwszName,pColumnDesc->dbcid.uName.pwszName);
  227. }
  228. }
  229. }
  230. }
  231. hr = hr == S_OK ? hr :g_pCError->PostHResult(hr,&IID_ITableDefinition);
  232. CATCH_BLOCK_HRESULT(hr,L"ITableDefinition::AddColumn");
  233. return hr;
  234. }
  235. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  236. //A method of ITableDefinition interface to drop a column
  237. // Return Values :
  238. // S_OK - Success
  239. // DB_S_ERRORSOCCURRED - General failure
  240. // E_INVALIDARG - one of the arguments is invalid
  241. // DB_E_NOTABLE - the table defined in invalid
  242. // DB_E_BADCOLUMNID - the column id is bad or not exists
  243. // DB_SEC_E_PERMISSIONDENIED - does not have permission to drop the column
  244. //
  245. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  246. HRESULT STDMETHODCALLTYPE CImpITableDefinition::DropColumn(
  247. DBID __RPC_FAR *pTableID, // IN Name of WBEM Class
  248. DBID __RPC_FAR *pColumnID) // IN Name of WBEM Property
  249. {
  250. HRESULT hr = E_FAIL;
  251. CSetStructuredExceptionHandler seh;
  252. TRY_BLOCK;
  253. CAutoBlock cab(m_pObj->GetCriticalSection());
  254. //=====================================
  255. // Clear Error information
  256. //=====================================
  257. g_pCError->ClearErrorInfo();
  258. //===================================================================================
  259. // Check the connection is valid
  260. //===================================================================================
  261. if ( m_pObj->m_pCDataSource->m_pWbemWrap->ValidConnection() ){
  262. if(pTableID->uName.pwszName == NULL)
  263. {
  264. hr = DB_E_BADTABLEID;
  265. }
  266. else
  267. if( pTableID->eKind != DBKIND_NAME || 0 == wcslen(pTableID->uName.pwszName) ||
  268. pColumnID->eKind != DBKIND_NAME || 0 == wcslen(pColumnID->uName.pwszName))
  269. {
  270. hr = E_INVALIDARG;
  271. }
  272. else
  273. {
  274. CWmiOleDBMap Map;
  275. if(SUCCEEDED(hr = Map.FInit(NO_QUALIFIERS,(LPWSTR)pTableID->uName.pwszName,m_pObj->m_pCDataSource->m_pWbemWrap)))
  276. {
  277. hr = Map.DropColumn(pColumnID);
  278. }
  279. }
  280. }
  281. hr = hr == S_OK ? hr :g_pCError->PostHResult(hr,&IID_ITableDefinition);
  282. CATCH_BLOCK_HRESULT(hr,L"ITableDefinition::DropColumn");
  283. return hr;
  284. }