OLEDBTextProviderSess.h
上传用户:benben_wyd
上传日期:2010-02-26
资源大小:1229k
文件大小:5k
源码类别:

书籍源码

开发平台:

Visual C++

  1. // Session.h : Declaration of the COLEDBTextProviderSession
  2. #ifndef __COLEDBTextProviderSession_H_
  3. #define __COLEDBTextProviderSession_H_
  4. #include "resource.h"       // main symbols
  5. #include "OLEDBTextProviderRS.h"
  6. class COLEDBTextProviderSessionTRSchemaRowset;
  7. class COLEDBTextProviderSessionColSchemaRowset;
  8. class COLEDBTextProviderSessionPTSchemaRowset;
  9. /////////////////////////////////////////////////////////////////////////////
  10. // COLEDBTextProviderSession
  11. class ATL_NO_VTABLE COLEDBTextProviderSession : 
  12. public CComObjectRootEx<CComSingleThreadModel>,
  13. public IGetDataSourceImpl<COLEDBTextProviderSession>,
  14. public IOpenRowsetImpl<COLEDBTextProviderSession>,
  15. public ISessionPropertiesImpl<COLEDBTextProviderSession>,
  16. public IObjectWithSiteSessionImpl<COLEDBTextProviderSession>,
  17. public IDBSchemaRowsetImpl<COLEDBTextProviderSession>,
  18. public IDBCreateCommandImpl<COLEDBTextProviderSession, COLEDBTextProviderCommand>
  19. {
  20. public:
  21. COLEDBTextProviderSession()
  22. {
  23. }
  24. HRESULT FinalConstruct()
  25. {
  26. return FInit();
  27. }
  28. STDMETHOD(OpenRowset)(IUnknown *pUnk, DBID *pTID, DBID *pInID, REFIID riid,
  29.    ULONG cSets, DBPROPSET rgSets[], IUnknown **ppRowset)
  30. {
  31. COLEDBTextProviderRowset* pRowset;
  32. return CreateRowset(pUnk, pTID, pInID, riid, cSets, rgSets, ppRowset, pRowset);
  33. }
  34. BEGIN_PROPSET_MAP(COLEDBTextProviderSession)
  35. BEGIN_PROPERTY_SET(DBPROPSET_SESSION)
  36. PROPERTY_INFO_ENTRY(SESS_AUTOCOMMITISOLEVELS)
  37. END_PROPERTY_SET(DBPROPSET_SESSION)
  38. END_PROPSET_MAP()
  39. BEGIN_COM_MAP(COLEDBTextProviderSession)
  40. COM_INTERFACE_ENTRY(IGetDataSource)
  41. COM_INTERFACE_ENTRY(IOpenRowset)
  42. COM_INTERFACE_ENTRY(ISessionProperties)
  43. COM_INTERFACE_ENTRY(IObjectWithSite)
  44. COM_INTERFACE_ENTRY(IDBCreateCommand)
  45. COM_INTERFACE_ENTRY(IDBSchemaRowset)
  46. END_COM_MAP()
  47. BEGIN_SCHEMA_MAP(COLEDBTextProviderSession)
  48. SCHEMA_ENTRY(DBSCHEMA_TABLES, COLEDBTextProviderSessionTRSchemaRowset)
  49. SCHEMA_ENTRY(DBSCHEMA_COLUMNS, COLEDBTextProviderSessionColSchemaRowset)
  50. SCHEMA_ENTRY(DBSCHEMA_PROVIDER_TYPES, COLEDBTextProviderSessionPTSchemaRowset)
  51. END_SCHEMA_MAP()
  52. };
  53. class COLEDBTextProviderSessionTRSchemaRowset : 
  54. public CRowsetImpl< COLEDBTextProviderSessionTRSchemaRowset, CTABLESRow, COLEDBTextProviderSession>
  55. {
  56. public:
  57. HRESULT Execute(LONG* pcRowsAffected, ULONG, const VARIANT*)
  58. {
  59. USES_CONVERSION;
  60. CTABLESRow trData;
  61. lstrcpyW(trData.m_szType, OLESTR("TABLE"));
  62. /* ************************** */
  63. //Error if file does not exist
  64. WIN32_FIND_DATA wf;
  65. HANDLE fileHandle;
  66. fileHandle = FindFirstFile(
  67. COLEDBTextProviderRowset::GetFileName(),
  68. &wf);
  69. if (fileHandle == INVALID_HANDLE_VALUE)
  70. return E_FAIL; // The file does not exist
  71. FindClose(fileHandle);
  72. lstrcpyW(trData.m_szDesc, 
  73. T2OLE(COLEDBTextProviderRowset::GetFileName()));
  74.         lstrcpyW(trData.m_szTable, T2OLE("Text File"));
  75. /* ************************** */
  76. if (!m_rgRowData.Add(trData))
  77. return E_OUTOFMEMORY;
  78. *pcRowsAffected = 1;
  79. return S_OK;
  80. }
  81. };
  82. class COLEDBTextProviderSessionColSchemaRowset : 
  83. public CRowsetImpl< COLEDBTextProviderSessionColSchemaRowset, CCOLUMNSRow, COLEDBTextProviderSession>
  84. {
  85. public:
  86. HRESULT Execute(LONG* pcRowsAffected, ULONG, const VARIANT*)
  87. {
  88. USES_CONVERSION;
  89. //******
  90. static const struct { char *szName; int maxLength; }
  91. rgcolumns[] = 
  92.    {{"Class", 50},
  93. {"SectionID", 11},
  94. {"Year", 6},
  95. {"Term", 30},
  96. {"Instructor", 50},
  97. {"Assignment", 255},
  98. {"Score", 22},
  99. {"Student", 35}};
  100. const int NUMBERCOLUMNS = sizeof(rgcolumns) / sizeof(rgcolumns[0]);
  101. WIN32_FIND_DATA wf;
  102. HANDLE fileHandle;
  103. //Error if file does not exist
  104. fileHandle = FindFirstFile(
  105. COLEDBTextProviderRowset::GetFileName(),
  106. &wf);
  107. if (fileHandle == INVALID_HANDLE_VALUE)
  108. return E_FAIL; // The file does not exist
  109. FindClose(fileHandle);
  110. // Fill out all the CCOLUMNSRow records
  111. for (int column=0; column<NUMBERCOLUMNS; column++) {
  112. CCOLUMNSRow crColumnInfo;
  113. lstrcpyW(crColumnInfo.m_szTableName, 
  114. T2OLE(COLEDBTextProviderRowset::GetFileName()));
  115. crColumnInfo.m_ulOrdinalPosition = column+1;
  116. crColumnInfo.m_bIsNullable = VARIANT_FALSE;
  117. crColumnInfo.m_bColumnHasDefault = VARIANT_FALSE;
  118. crColumnInfo.m_ulColumnFlags = 0;
  119. crColumnInfo.m_nDataType = DBTYPE_STR;
  120. lstrcpyW(crColumnInfo.m_szColumnName, T2OLE(rgcolumns[column].szName));
  121. lstrcpyW(crColumnInfo.m_szDescription, T2OLE(rgcolumns[column].szName));
  122. crColumnInfo.m_ulCharMaxLength = rgcolumns[column].maxLength;
  123. // Declare new schema row
  124. m_rgRowData.Add(crColumnInfo);
  125. }
  126. *pcRowsAffected = NUMBERCOLUMNS;  // for the four columns
  127. //******
  128. return S_OK;
  129. }
  130. };
  131. class COLEDBTextProviderSessionPTSchemaRowset : 
  132. public CRowsetImpl< COLEDBTextProviderSessionPTSchemaRowset, CPROVIDER_TYPERow, COLEDBTextProviderSession>
  133. {
  134. public:
  135. HRESULT Execute(LONG* pcRowsAffected, ULONG, const VARIANT*)
  136. {
  137. return S_OK;
  138. }
  139. };
  140. #endif //__COLEDBTextProviderSession_H_