ADORecordset.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*************************************************************
  2. /* Copyright (C), 2014-2020, lyfz. Co., Ltd.
  3. /* 文件名: ADORecordset.h
  4. /* 作者: Jeff.w
  5. /* 创建日期: 2014-08-16
  6. /* 版本号: V1.0
  7. /* 描述: 核心层底,数据库ADO编程
  8. /* 其它:
  9. /* 主要类模块:
  10. /* 历史修改记录:
  11. /* 作者 时间 版本 描述
  12. /* Jeff.w 14/08/18 1.0 创建这个模块
  13. ***************************************************************/
  14. #ifndef __ADO_RECORDSET_HEADER__
  15. #define __ADO_RECORDSET_HEADER__
  16. #pragma once
  17. #include "AdoDef.h"
  18. #include "ADODatabase.h"
  19. #include "ADOCommand.h"
  20. class CADORecordset
  21. {
  22. public:
  23. BOOL Clone(CADORecordset& pRs);
  24. enum cadoOpenEnum{
  25. openUnknown = 0,
  26. openQuery = 1,
  27. openTable = 2,
  28. openStoredProc = 3,
  29. openCmdText = 4
  30. };
  31. enum cadoEditEnum{
  32. dbEditNone = 0,
  33. dbEditNew = 1,
  34. dbEdit = 2
  35. };
  36. enum cadoPositionEnum{
  37. positionUnknown = -1,
  38. positionBOF = -2,
  39. positionEOF = -3
  40. };
  41. enum cadoSearchEnum {
  42. searchForward = 1,
  43. searchBackward = -1
  44. };
  45. enum cadoDataType {
  46. typeEmpty = adEmpty,
  47. typeTinyInt = adTinyInt,
  48. typeSmallInt = adSmallInt,
  49. typeInteger = adInteger,
  50. typeBigInt = adBigInt,
  51. typeUnsignedTinyInt = adUnsignedTinyInt,
  52. typeUnsignedSmallInt = adUnsignedSmallInt,
  53. typeUnsignedInt = adUnsignedInt,
  54. typeUnsignedBigInt = adUnsignedBigInt,
  55. typeSingle = adSingle,
  56. typeDouble = adDouble,
  57. typeCurrency = adCurrency,
  58. typeDecimal = adDecimal,
  59. typeNumeric = adNumeric,
  60. typeBoolean = adBoolean,
  61. typeError = adError,
  62. typeUserDefined = adUserDefined,
  63. typeVariant = adVariant,
  64. typeIDispatch = adIDispatch,
  65. typeIUnknown = adIUnknown,
  66. typeGUID = adGUID,
  67. typeDate = adDate,
  68. typeDBDate = adDBDate,
  69. typeDBTime = adDBTime,
  70. typeDBTimeStamp = adDBTimeStamp,
  71. typeBSTR = adBSTR,
  72. typeChar = adChar,
  73. typeVarChar = adVarChar,
  74. typeLongVarChar = adLongVarChar,
  75. typeWChar = adWChar,
  76. typeVarWChar = adVarWChar,
  77. typeLongVarWChar = adLongVarWChar,
  78. typeBinary = adBinary,
  79. typeVarBinary = adVarBinary,
  80. typeLongVarBinary = adLongVarBinary,
  81. typeChapter = adChapter,
  82. typeFileTime = adFileTime,
  83. typePropVariant = adPropVariant,
  84. typeVarNumeric = adVarNumeric,
  85. typeArray = adVariant
  86. };
  87. enum cadoSchemaType
  88. {
  89. schemaSpecific = adSchemaProviderSpecific,
  90. schemaAsserts = adSchemaAsserts,
  91. schemaCatalog = adSchemaCatalogs,
  92. schemaCharacterSet = adSchemaCharacterSets,
  93. schemaCollections = adSchemaCollations,
  94. schemaColumns = adSchemaColumns,
  95. schemaConstraints = adSchemaCheckConstraints,
  96. schemaConstraintColumnUsage = adSchemaConstraintColumnUsage,
  97. schemaConstraintTableUsage = adSchemaConstraintTableUsage,
  98. shemaKeyColumnUsage = adSchemaKeyColumnUsage,
  99. schemaTableConstraints = adSchemaTableConstraints,
  100. schemaColumnsDomainUsage = adSchemaColumnsDomainUsage,
  101. schemaIndexes = adSchemaIndexes,
  102. schemaColumnPrivileges = adSchemaColumnPrivileges,
  103. schemaTablePrivileges = adSchemaTablePrivileges,
  104. schemaUsagePrivileges = adSchemaUsagePrivileges,
  105. schemaProcedures = adSchemaProcedures,
  106. schemaTables = adSchemaTables,
  107. schemaProviderTypes = adSchemaProviderTypes,
  108. schemaViews = adSchemaViews,
  109. schemaViewTableUsage = adSchemaViewTableUsage,
  110. schemaProcedureParameters = adSchemaProcedureParameters,
  111. schemaForeignKeys = adSchemaForeignKeys,
  112. schemaPrimaryKeys = adSchemaPrimaryKeys,
  113. schemaProcedureColumns = adSchemaProcedureColumns,
  114. schemaDBInfoKeywords = adSchemaDBInfoKeywords,
  115. schemaDBInfoLiterals = adSchemaDBInfoLiterals,
  116. schemaCubes = adSchemaCubes,
  117. schemaDimensions = adSchemaDimensions,
  118. schemaHierarchies = adSchemaHierarchies,
  119. schemaLevels = adSchemaLevels,
  120. schemaMeasures = adSchemaMeasures,
  121. schemaProperties = adSchemaProperties,
  122. schemaMembers = adSchemaMembers,
  123. };
  124. BOOL SetFieldValue(int nIndex, int nValue);
  125. BOOL SetFieldValue(LPCTSTR lpFieldName, int nValue);
  126. BOOL SetFieldValue(int nIndex, long lValue);
  127. BOOL SetFieldValue(LPCTSTR lpFieldName, long lValue);
  128. BOOL SetFieldValue(int nIndex, unsigned long lValue);
  129. BOOL SetFieldValue(LPCTSTR lpFieldName, unsigned long lValue);
  130. BOOL SetFieldValue(int nIndex, double dblValue);
  131. BOOL SetFieldValue(LPCTSTR lpFieldName, double dblValue);
  132. BOOL SetFieldValue(int nIndex, CString strValue);
  133. BOOL SetFieldValue(LPCTSTR lpFieldName, CString strValue);
  134. BOOL SetFieldValue(int nIndex, COleDateTime time);
  135. BOOL SetFieldValue(LPCTSTR lpFieldName, COleDateTime time);
  136. BOOL SetFieldValue(int nIndex, bool bValue);
  137. BOOL SetFieldValue(LPCTSTR lpFieldName, bool bValue);
  138. BOOL SetFieldValue(int nIndex, COleCurrency cyValue);
  139. BOOL SetFieldValue(LPCTSTR lpFieldName, COleCurrency cyValue);
  140. BOOL SetFieldValue(int nIndex, _variant_t vtValue);
  141. BOOL SetFieldValue(LPCTSTR lpFieldName, _variant_t vtValue);
  142. BOOL SetFieldEmpty(int nIndex);
  143. BOOL SetFieldEmpty(LPCTSTR lpFieldName);
  144. void CancelUpdate();
  145. BOOL Update();
  146. void Edit();
  147. BOOL AddNew();
  148. BOOL AddNew(CADORecordBinding &pAdoRecordBinding);
  149. BOOL Find(LPCTSTR lpFind, int nSearchDirection = CADORecordset::searchForward);
  150. BOOL FindFirst(LPCTSTR lpFind);
  151. BOOL FindNext();
  152. CADORecordset();
  153. CADORecordset(CADODatabase* pAdoDatabase);
  154. virtual ~CADORecordset()
  155. {
  156. Close();
  157. if(m_pRecordset)
  158. m_pRecordset.Release();
  159. if(m_pCmd)
  160. m_pCmd.Release();
  161. m_pRecordset = NULL;
  162. m_pCmd = NULL;
  163. m_pRecBinding = NULL;
  164. m_strQuery = _T("");
  165. m_strLastError = _T("");
  166. m_dwLastError = 0;
  167. m_nEditStatus = dbEditNone;
  168. }
  169. inline CString GetQuery() {return m_strQuery;};
  170. inline void SetQuery(LPCSTR strQuery) {m_strQuery = strQuery;};
  171. BOOL RecordBinding(CADORecordBinding &pAdoRecordBinding);
  172. DWORD GetRecordCount();
  173. BOOL IsOpen();
  174. void Close();
  175. BOOL Open(_ConnectionPtr mpdb, LPCTSTR lpstrExec = _T(""), const int& nOption = CADORecordset::openUnknown, const int& nCursorLocation = adUseServer);
  176. BOOL Open(LPCTSTR lpstrExec = _T(""), const int& nOption = CADORecordset::openUnknown, const int& nCursorLocation = adUseServer);
  177. BOOL OpenSchema(int nSchema, LPCTSTR SchemaID = _T(""));
  178. inline long GetFieldCount() {return m_pRecordset->Fields->GetCount();};
  179. BOOL GetFieldValue(LPCTSTR lpFieldName, int& nValue);
  180. BOOL GetFieldValue(int nIndex, int& nValue);
  181. BOOL GetFieldValue(LPCTSTR lpFieldName, long& lValue);
  182. BOOL GetFieldValue(int nIndex, long& lValue);
  183. BOOL GetFieldValue(LPCTSTR lpFieldName, unsigned long& ulValue);
  184. BOOL GetFieldValue(int nIndex, unsigned long& ulValue);
  185. BOOL GetFieldValue(LPCTSTR lpFieldName, double& dbValue);
  186. BOOL GetFieldValue(int nIndex, double& dbValue);
  187. BOOL GetFieldValue(LPCTSTR lpFieldName, CString& strValue, CString strDateFormat = _T(""));
  188. BOOL GetFieldValue(int nIndex, CString& strValue, CString strDateFormat = _T(""));
  189. BOOL GetFieldValue(LPCTSTR lpFieldName, COleDateTime& time);
  190. BOOL GetFieldValue(int nIndex, COleDateTime& time);
  191. BOOL GetFieldValue(int nIndex, bool& bValue);
  192. BOOL GetFieldValue(LPCTSTR lpFieldName, bool& bValue);
  193. BOOL GetFieldValue(int nIndex, COleCurrency& cyValue);
  194. BOOL GetFieldValue(LPCTSTR lpFieldName, COleCurrency& cyValue);
  195. BOOL GetFieldValue(int nIndex, _variant_t& vtValue);
  196. BOOL GetFieldValue(LPCTSTR lpFieldName, _variant_t& vtValue);
  197. BOOL IsFieldNull(LPCTSTR lpFieldName);
  198. BOOL IsFieldNull(int nIndex);
  199. BOOL IsFieldEmpty(LPCTSTR lpFieldName);
  200. BOOL IsFieldEmpty(int nIndex);
  201. inline BOOL IsEof(){return m_pRecordset->EndOfFile == VARIANT_TRUE;};
  202. inline BOOL IsEOF(){return m_pRecordset->EndOfFile == VARIANT_TRUE;};
  203. inline BOOL IsBof(){return m_pRecordset->BOF == VARIANT_TRUE;};
  204. inline BOOL IsBOF(){return m_pRecordset->BOF == VARIANT_TRUE;};
  205. inline void MoveFirst() {m_pRecordset->MoveFirst();};
  206. inline void MoveNext() {m_pRecordset->MoveNext();};
  207. inline void MovePrevious() {m_pRecordset->MovePrevious();};
  208. inline void MoveLast() {m_pRecordset->MoveLast();};
  209. inline long GetAbsolutePage(){return m_pRecordset->GetAbsolutePage();};
  210. inline void SetAbsolutePage(int nPage) {m_pRecordset->PutAbsolutePage((enum PositionEnum)nPage);};
  211. inline long GetPageCount() {return m_pRecordset->GetPageCount();};
  212. inline long GetPageSize() {return m_pRecordset->GetPageSize();};
  213. inline void SetPageSize(int nSize) {m_pRecordset->PutPageSize(nSize);};
  214. inline long GetAbsolutePosition() {return m_pRecordset->GetAbsolutePosition();};
  215. inline void SetAbsolutePosition(int nPosition) {m_pRecordset->PutAbsolutePosition((enum PositionEnum)nPosition);};
  216. //inline void Move(ADO_LONGPTR NumRecords){m_pRecordset->Move(NumRecords);}
  217. BOOL GetFieldInfo(LPCTSTR lpFieldName, CADOFieldInfo* fldInfo);
  218. BOOL GetFieldInfo(int nIndex, CADOFieldInfo* fldInfo);
  219. BOOL AppendChunk(LPCTSTR lpFieldName, LPVOID lpData, UINT nBytes);
  220. BOOL AppendChunk(int nIndex, LPVOID lpData, UINT nBytes);
  221. BOOL AppendChunkFromFile(LPCTSTR lpFieldName, TCHAR* strFileName);
  222. BOOL AppendChunkFromFile(int nIndex, TCHAR* strFileName);
  223. BOOL GetChunk(LPCTSTR lpFieldName, CString& strValue);
  224. BOOL GetChunk(int nIndex, CString& strValue);
  225. BOOL GetChunk(LPCTSTR lpFieldName, LPVOID pData);
  226. BOOL GetChunk(int nIndex, LPVOID pData);
  227. LONG GetChunkEx(LPCTSTR lpFieldName, HGLOBAL *hglobal);
  228. LONG GetChunkEx(int nIndex, HGLOBAL *hglobal);
  229. LONG GetChunkToFile(LPCTSTR lpFieldName, TCHAR* strFileName);
  230. LONG GetChunkToFile(int nIndex, TCHAR* strFileName);
  231. CString GetString(LPCTSTR lpCols, LPCTSTR lpRows, LPCTSTR lpNull, long numRows = 0);
  232. inline CString GetLastErrorString() {return m_strLastError;};
  233. inline DWORD GetLastError(){return m_dwLastError;};
  234. inline void GetBookmark() {m_varBookmark = m_pRecordset->Bookmark;};
  235. BOOL SetBookmark();
  236. BOOL Delete();
  237. inline BOOL IsConnectionOpen() {return m_pConnection != NULL && m_pConnection->GetState() != adStateClosed;};
  238. inline _RecordsetPtr GetRecordset(){return m_pRecordset;};
  239. inline _ConnectionPtr GetActiveConnection() {return m_pConnection;};
  240. BOOL SetFilter(LPCTSTR strFilter);
  241. BOOL SetSort(LPCTSTR lpstrCriteria);
  242. BOOL SaveAsXML(LPCTSTR lpstrXMLFile);
  243. BOOL OpenXML(LPCTSTR lpstrXMLFile);
  244. BOOL Execute(CADOCommand* pCommand);
  245. BOOL Requery();
  246. public:
  247. _RecordsetPtr m_pRecordset;
  248. _CommandPtr m_pCmd;
  249. protected:
  250. _ConnectionPtr m_pConnection;
  251. int m_nSearchDirection;
  252. CString m_strFind;
  253. _variant_t m_varBookFind;
  254. _variant_t m_varBookmark;
  255. int m_nEditStatus;
  256. CString m_strLastError;
  257. DWORD m_dwLastError;
  258. void dump_com_error(_com_error &e);
  259. IADORecordBinding *m_pRecBinding;
  260. CString m_strQuery;
  261. protected:
  262. BOOL PutFieldValue(LPCTSTR lpFieldName, _variant_t vtFld);
  263. BOOL PutFieldValue(_variant_t vtIndex, _variant_t vtFld);
  264. BOOL GetFieldInfo(FieldPtr pField, CADOFieldInfo* fldInfo);
  265. BOOL GetChunk(FieldPtr pField, CString& strValue);
  266. BOOL GetChunk(FieldPtr pField, LPVOID lpData);
  267. BOOL AppendChunk(FieldPtr pField, LPVOID lpData, UINT nBytes);
  268. };
  269. #endif