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.

175 lines
3.5 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1997, Microsoft Corp. All rights reserved.
  4. //
  5. // FILE
  6. //
  7. // Dictionary.cpp
  8. //
  9. // SYNOPSIS
  10. //
  11. // This file implements the class Dictionary.
  12. //
  13. // MODIFICATION HISTORY
  14. //
  15. // 12/19/1997 Original version.
  16. // 01/15/1998 Removed IasDataSource aggregate.
  17. // 04/17/1998 Added reset() method and allow reinitialization.
  18. // 08/10/1998 Streamlined IDictionary interface.
  19. // 08/12/1998 Lookup attribute ID's with a SQL query.
  20. //
  21. ///////////////////////////////////////////////////////////////////////////////
  22. #include <iascore.h>
  23. #include <iasutil.h>
  24. #include <Dictionary.h>
  25. #include <SimTable.h>
  26. Dictionary::Dictionary() throw ()
  27. : dataSource(NULL)
  28. { }
  29. Dictionary::~Dictionary() throw ()
  30. {
  31. if (dataSource)
  32. {
  33. dataSource->Release();
  34. }
  35. }
  36. STDMETHODIMP Dictionary::get_DataSource(IIasDataSource** pVal)
  37. {
  38. if (pVal == NULL) { return E_POINTER; }
  39. Lock();
  40. if (dataSource) { dataSource->AddRef(); }
  41. *pVal = dataSource;
  42. Unlock();
  43. return S_OK;
  44. }
  45. STDMETHODIMP Dictionary::put_DataSource(IIasDataSource* newVal)
  46. {
  47. Lock();
  48. if (dataSource) { dataSource->Release(); }
  49. dataSource = newVal;
  50. if (dataSource) { dataSource->AddRef(); }
  51. Unlock();
  52. return S_OK;
  53. }
  54. STDMETHODIMP Dictionary::get_AttributesTable(IUnknown** ppAttrTable)
  55. {
  56. return openTable(L"Attributes", ppAttrTable);
  57. }
  58. STDMETHODIMP Dictionary::get_EnumeratorsTable(IUnknown** ppEnumTable)
  59. {
  60. return openTable(L"Enumerators", ppEnumTable);
  61. }
  62. STDMETHODIMP Dictionary::get_SyntaxesTable(IUnknown** ppSyntaxTable)
  63. {
  64. return openTable(L"Syntaxes", ppSyntaxTable);
  65. }
  66. STDMETHODIMP Dictionary::ExecuteCommand(LPCWSTR szCommandText,
  67. IUnknown** ppRowset)
  68. {
  69. Lock();
  70. HRESULT hr;
  71. if (dataSource)
  72. {
  73. hr = dataSource->ExecuteCommand(DBGUID_DBSQL, szCommandText, ppRowset);
  74. }
  75. else
  76. {
  77. hr = E_POINTER;
  78. }
  79. Unlock();
  80. return hr;
  81. }
  82. // Command for mapping an attribute name to an attribute ID.
  83. const WCHAR CMD_PREFIX[] = L"SELECT ID FROM Attributes WHERE Name = \"";
  84. const WCHAR CMD_SUFFIX[] = L"\";";
  85. HRESULT Dictionary::GetAttributeID(LPCWSTR szAttrName, DWORD* plAttrID)
  86. {
  87. // Allocate a buffer to hold the command text.
  88. size_t nbyte = sizeof(CMD_PREFIX) +
  89. sizeof(CMD_SUFFIX) +
  90. wcslen(szAttrName) * sizeof(WCHAR);
  91. PWSTR command = (PWSTR)_alloca(nbyte);
  92. // Build the command.
  93. wcscpy(command, CMD_PREFIX);
  94. wcscat(command, szAttrName);
  95. wcscat(command, CMD_SUFFIX);
  96. //////////
  97. // Execute the command.
  98. //////////
  99. CComPtr<IUnknown> unk;
  100. RETURN_ERROR(ExecuteCommand(command, &unk));
  101. //////////
  102. // Process the returned rowset.
  103. //////////
  104. CComPtr<IRowset> rowset;
  105. RETURN_ERROR(unk->QueryInterface(__uuidof(IRowset), (PVOID*)&rowset));
  106. CSimpleTable table;
  107. RETURN_ERROR(table.Attach(rowset));
  108. DBORDINAL idCol;
  109. if (!table.GetOrdinal(L"ID", &idCol) ||
  110. table.GetColumnType(idCol) != DBTYPE_I4)
  111. {
  112. return HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
  113. }
  114. if (table.MoveNext() != S_OK)
  115. {
  116. // If the rowset is empty, the name was invalid.
  117. return E_INVALIDARG;
  118. }
  119. *plAttrID = (DWORD)*(long*)table.GetValue(idCol);
  120. return S_OK;
  121. }
  122. HRESULT Dictionary::openTable(LPCWSTR szTableName, IUnknown** ppTable) throw ()
  123. {
  124. Lock();
  125. HRESULT hr;
  126. if (dataSource)
  127. {
  128. hr = dataSource->OpenTable(szTableName, ppTable);
  129. }
  130. else
  131. {
  132. hr = E_POINTER;
  133. }
  134. Unlock();
  135. return hr;
  136. }