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.

169 lines
5.5 KiB

  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Microsoft WMIOLE DB Provider
  4. //
  5. // (C) Copyright 1999 Microsoft Corporation. All Rights Reserved.
  6. //
  7. // IIndexDef.CPP IIndexDefinition interface implementation
  8. //
  9. ///////////////////////////////////////////////////////////////////////////
  10. #include "headers.h"
  11. /////////////////////////////////////////////////////////////////////////////////////////////////
  12. //
  13. // Creates a index on a property of a class
  14. //
  15. // Returns one of the following values:
  16. // S_OK Method Succeeded
  17. // E_FAIL Generic Provider specific failure
  18. // DB_S_ERRORSOCCURED creating index was successful with some errors
  19. // DB_E_ERRORSOCCURRED Could not create index
  20. // E_INVALIDARG One or more arguments was invalid
  21. // OTHER Other HRESULTs returned by called functions
  22. // DB_E_NOTABLE The table specified is not present
  23. // DB_SEC_E_PERMISSIONDENIED User does not have permission to create index
  24. //
  25. // Note: pIndexID - Should be pointing to the columnname for which the index need to be added
  26. // ppIndexID - will be pointing to the Index qualifier. ie. <ColumnName>/"Index"
  27. /////////////////////////////////////////////////////////////////////////////////////////////////
  28. STDMETHODIMP CImplIIndexDefinition::CreateIndex(DBID * pTableID,
  29. DBID * pIndexID,
  30. DBORDINAL cIndexColumnDescs,
  31. const DBINDEXCOLUMNDESC rgIndexColumnDescs[],
  32. ULONG cPropertySets,
  33. DBPROPSET rgPropertySets[],
  34. DBID ** ppIndexID)
  35. {
  36. HRESULT hr = E_FAIL;
  37. CSetStructuredExceptionHandler seh;
  38. TRY_BLOCK;
  39. // Serialize the obect
  40. CAutoBlock cab(m_pObj->GetCriticalSection());
  41. LONG lSize = -1;
  42. // Clear Error information
  43. g_pCError->ClearErrorInfo();
  44. //===================================================================================
  45. // Check the connection is valid
  46. //===================================================================================
  47. if ( m_pObj->m_pCDataSource->m_pWbemWrap->ValidConnection() ){
  48. if( pIndexID->eKind != DBKIND_NAME || 0 == wcslen(pIndexID->uName.pwszName) ||
  49. pTableID->eKind != DBKIND_NAME || 0 == wcslen(pTableID->uName.pwszName))
  50. {
  51. hr = E_INVALIDARG;
  52. }
  53. else
  54. {
  55. CWmiOleDBMap Map;
  56. if(SUCCEEDED(hr =Map.FInit(NO_QUALIFIERS,pTableID->uName.pwszName,m_pObj->m_pCDataSource->m_pWbemWrap)))
  57. {
  58. if(S_OK == (hr = Map.AddIndex(pIndexID)) && ppIndexID != NULL)
  59. {
  60. try
  61. {
  62. // Allocate memory for DBID
  63. (*ppIndexID) = (DBID *)g_pIMalloc->Alloc(sizeof(DBID));
  64. }
  65. catch(...)
  66. {
  67. if(*ppIndexID)
  68. {
  69. g_pIMalloc->Free(*ppIndexID);
  70. (*ppIndexID) = NULL;
  71. }
  72. throw;
  73. }
  74. if(*ppIndexID == NULL)
  75. {
  76. hr = E_OUTOFMEMORY;
  77. }
  78. else
  79. {
  80. (*ppIndexID)->eKind = DBKIND_NAME;
  81. // Allocate memory for then tablename
  82. lSize = sizeof(WCHAR) *(wcslen(pIndexID->uName.pwszName) + \
  83. wcslen(strIndex) + \
  84. wcslen(SEPARATOR) + 1);
  85. // Allocate memory for qualifer name which gives the indexID
  86. (*ppIndexID)->uName.pwszName = (WCHAR *)g_pIMalloc->Alloc(lSize);
  87. wcscpy((*ppIndexID)->uName.pwszName,pIndexID->uName.pwszName);
  88. wcscat((*ppIndexID)->uName.pwszName,SEPARATOR);
  89. wcscat((*ppIndexID)->uName.pwszName,strIndex);
  90. }
  91. }
  92. }
  93. }
  94. }
  95. hr = hr == S_OK ? hr :g_pCError->PostHResult(hr,&IID_IIndexDefinition);
  96. CATCH_BLOCK_HRESULT(hr,L"IIndexDefinition::CreateIndex");
  97. return hr;
  98. }
  99. /////////////////////////////////////////////////////////////////////////////////////////////////
  100. //
  101. // Creates a index on a property of a class
  102. //
  103. // Returns one of the following values:
  104. // S_OK Method Succeeded
  105. // E_FAIL Generic Provider specific failure
  106. // DB_S_ERRORSOCCURED creating index was successful with some errors
  107. // DB_E_ERRORSOCCURRED Could not create index
  108. // E_INVALIDARG One or more arguments was invalid
  109. // DB_E_NOINDEX index not present
  110. // DB_E_NOTABLE The table specified is not present
  111. // DB_SEC_E_PERMISSIONDENIED User does not have permission to create index
  112. //
  113. // pIndexID - will be pointing to the index qualifier ie.<columnName>/"Index"
  114. /////////////////////////////////////////////////////////////////////////////////////////////////
  115. STDMETHODIMP CImplIIndexDefinition::DropIndex( DBID * pTableID, DBID * pIndexID)
  116. {
  117. HRESULT hr = E_FAIL;
  118. LONG lSize = -1;
  119. CSetStructuredExceptionHandler seh;
  120. TRY_BLOCK;
  121. // Serialize the obect
  122. CAutoBlock cab(m_pObj->GetCriticalSection());
  123. //===================================================================================
  124. // Check the connection is valid
  125. //===================================================================================
  126. // Clear Error information
  127. g_pCError->ClearErrorInfo();
  128. if ( m_pObj->m_pCDataSource->m_pWbemWrap->ValidConnection() ){
  129. if( pIndexID->eKind != DBKIND_NAME || 0 == wcslen(pIndexID->uName.pwszName) ||
  130. pTableID->eKind != DBKIND_NAME || 0 == wcslen(pTableID->uName.pwszName))
  131. {
  132. hr = E_INVALIDARG;
  133. }
  134. else
  135. {
  136. CWmiOleDBMap Map;
  137. if(SUCCEEDED(hr = Map.FInit(NO_QUALIFIERS,pTableID->uName.pwszName,m_pObj->m_pCDataSource->m_pWbemWrap)))
  138. {
  139. hr = Map.DropIndex(pIndexID);
  140. }
  141. }
  142. }
  143. hr = hr == S_OK ? hr :g_pCError->PostHResult(hr,&IID_IIndexDefinition);
  144. CATCH_BLOCK_HRESULT(hr,L"IIndexDefinition::CreateIndex");
  145. return hr;
  146. }