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.

216 lines
4.8 KiB

  1. #include "stdinc.h"
  2. #include <string>
  3. #include <vector>
  4. #include <fstream>
  5. #include <iostream>
  6. #include "SxApwHandle.h"
  7. #include "SxApwCreate.h"
  8. #include "SxApwComPtr.h"
  9. #include "chost.h"
  10. #define NUMBER_OF(x) (sizeof(x)/sizeof((x)[0]))
  11. const static
  12. PCWSTR s_dbQueries[] =
  13. {
  14. L"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\\\fusiontest\\sxs\\appweek\\nwind.mdb;"
  15. L"|SELECT * from employees",
  16. L"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\\\fusiontest\\sxs\\appweek\\nwind.mdb;"
  17. L"|SELECT title from employees",
  18. };
  19. std::wstring MultiByteToWideChar( const std::string& source )
  20. {
  21. std::vector<WCHAR> wch;
  22. wch.resize( MultiByteToWideChar( CP_ACP, 0, source.data(), source.size(), NULL, 0 ) );
  23. MultiByteToWideChar( CP_ACP, 0, source.data(), source.size(), &wch.front(), wch.size() );
  24. return std::wstring( wch.begin(), wch.end() );
  25. }
  26. STDMETHODIMP
  27. CSxApwHost::OnRowCountEstimateAvailable(
  28. int nRows
  29. )
  30. {
  31. /*
  32. just multiplex/broadcast the data across all the views..
  33. */
  34. for (Views_t::const_iterator i = m_views.begin(); i != m_views.end() ; ++i)
  35. {
  36. (*i)->OnRowCountEstimateAvailable(nRows);
  37. }
  38. return S_OK;
  39. }
  40. STDMETHODIMP
  41. CSxApwHost::OnNextRow(
  42. int nColumns,
  43. const PCWSTR* columns
  44. )
  45. {
  46. /*
  47. just multiplex/broadcast the data across all the views..
  48. */
  49. for (Views_t::const_iterator i = m_views.begin(); i != m_views.end() ; ++i)
  50. {
  51. (*i)->OnNextRow(nColumns, columns);
  52. }
  53. return S_OK;
  54. }
  55. STDMETHODIMP
  56. CSxApwHost::InformSchema(
  57. const SxApwColumnInfo rgColumnInfo[],
  58. int nColumnCount
  59. )
  60. {
  61. /*
  62. just multiplex/broadcast the data across all the views..
  63. */
  64. for (Views_t::const_iterator i = m_views.begin(); i != m_views.end() ; ++i)
  65. {
  66. (*i)->InformSchema(rgColumnInfo, nColumnCount);
  67. }
  68. return S_OK;
  69. }
  70. STDMETHODIMP
  71. CSxApwHost::SetDataSource(
  72. LPCWSTR datasource
  73. )
  74. {
  75. HRESULT hr;
  76. if (FAILED(hr = SxApwCreateObject(datasource, SXAPW_CREATEOBJECT_NOWRAP, m_dataSource)))
  77. goto Exit;
  78. Exit:
  79. return hr;
  80. }
  81. STDMETHODIMP
  82. CSxApwHost::CreateView(
  83. LPCWSTR view
  84. )
  85. {
  86. HRESULT hr;
  87. CSxApwComPtr<ISxApwUiView> iview;
  88. HWND hWnd = NULL /* UNDONE */;
  89. if (FAILED(hr = SxApwCreateObject(view, SXAPW_CREATEOBJECT_NOWRAP, iview)))
  90. goto Exit;
  91. if (FAILED(hr = iview->CreateWindow(hWnd)))
  92. goto Exit;
  93. m_views.push_back(iview);
  94. Exit:
  95. return hr;
  96. }
  97. STDMETHODIMP
  98. CSxApwHost::RunQuery(
  99. LPCWSTR query
  100. )
  101. {
  102. HRESULT hr;
  103. if (FAILED(hr = m_dataSource->RunQuery(query)))
  104. goto Exit;
  105. Exit:
  106. return hr;
  107. }
  108. HRESULT CSxApwHost::Main()
  109. {
  110. std::ifstream inStream;
  111. HRESULT hr;
  112. CSxApwComPtr<ISxApwDataSource> dirSource;
  113. CSxApwComPtr<ISxApwDataSource> dbSource;
  114. if (FAILED(hr = SetDataSource(CLSID_CSxApwDirDataSource_brace_stringW)))
  115. goto Exit;
  116. if (FAILED(hr = SetDataSource(CLSID_CSxApwDbDataSource_brace_stringW)))
  117. goto Exit;
  118. if (FAILED(hr = CreateView(CLSID_CSxApwStdoutView_brace_stringW)))
  119. goto Exit;
  120. if (FAILED(hr = dirSource->SetSite(this)))
  121. goto Exit;
  122. if (FAILED(hr = dirSource->RunQuery(L"C:\\*")))
  123. goto Exit;
  124. inStream.open("dbqueries.txt");
  125. if (!inStream.is_open())
  126. {
  127. hr = E_FAIL;
  128. goto Exit;
  129. }
  130. while (!inStream.eof())
  131. {
  132. std::string line;
  133. std::getline(inStream, line);
  134. if (line.empty())
  135. {
  136. break;
  137. }
  138. std::wstring wLine = MultiByteToWideChar(line);
  139. if (FAILED(hr = dbSource->SetSite(this)))
  140. goto Exit;
  141. if (FAILED(hr = dbSource->RunQuery(wLine.c_str())))
  142. goto Exit;
  143. }
  144. Exit:
  145. return hr;
  146. }
  147. //
  148. // DSQuery
  149. ///////////////////////////////////////////////////////////////////////////
  150. HRESULT CSxApwHost::DSQuery(int nDataSourceType, int nViewType, PCWSTR query, HWND hWnd)
  151. {
  152. HRESULT hr;
  153. const static PCWSTR dataSourceClsIds[] =
  154. { CLSID_CSxApwDirDataSource_brace_stringW, CLSID_CSxApwDbDataSource_brace_stringW };
  155. const static PCWSTR viewClsIds[] =
  156. { CLSID_CSxApwGDIPlusView_brace_stringW, CLSID_CSxApwEditView_brace_stringW, CLSID_CSxApwStdoutView_brace_stringW, CLSID_CSxApwComctl32View_brace_stringW };
  157. nDataSourceType -= 1;
  158. if (nDataSourceType >= 0 && nDataSourceType < NUMBER_OF(dataSourceClsIds))
  159. {
  160. if (FAILED(hr = SetDataSource(dataSourceClsIds[nDataSourceType])))
  161. {
  162. goto Exit;
  163. }
  164. }
  165. else
  166. {
  167. hr = E_INVALIDARG;
  168. goto Exit;
  169. }
  170. nViewType -= 1;
  171. if ( (nViewType >= 0) && (nViewType < NUMBER_OF(viewClsIds)) )
  172. {
  173. if (FAILED(hr = CreateView(viewClsIds[nViewType])))
  174. goto Exit;
  175. }
  176. else
  177. {
  178. hr = E_INVALIDARG;
  179. goto Exit;
  180. }
  181. if (FAILED(hr = m_dataSource->SetSite(this)))
  182. goto Exit;
  183. if (FAILED(hr = m_dataSource->RunQuery(query)))
  184. goto Exit;
  185. Exit:
  186. return hr;
  187. }