YD_DBOperator.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #pragma once
  2. /*******************************************************************************
  3. 描 述 : 数据库读写类
  4. 其 他 : 未主控系统提供
  5. 历史记录:
  6. 1. 时间: 2020-10-13
  7. 作者: 姚建军
  8. 修改描述: 新创建
  9. 2. ...
  10. *******************************************************************************/
  11. #if defined(_MSC_VER) || defined(WIN64) || defined(__WIN64__) || defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
  12. #ifdef YDDBOPERATOR_EXPORTS
  13. #define YDDBOPERATOR_API __declspec(dllexport)
  14. #else
  15. #define YDDBOPERATOR_API __declspec(dllimport)
  16. #endif
  17. #ifndef YDDBOPERATOR_EXPORTS
  18. #ifndef _WIN64
  19. #ifndef _DEBUG
  20. #pragma comment(lib,"YD.Public.DbOperator.x86.lib")
  21. #else
  22. #pragma comment(lib,"YD.Public.DbOperator.x86d.lib")
  23. #endif
  24. #else
  25. #ifndef _DEBUG
  26. #pragma comment(lib,"YD.Public.DbOperator.x64.lib")
  27. #else
  28. #pragma comment(lib,"YD.Public.DbOperator.x64d.lib")
  29. #endif
  30. #endif
  31. #endif
  32. #else //linux
  33. #define YDDBOPERATOR_API __attribute__ ((visibility("default")))
  34. #define memcpy_s(dest,dest_size,src,src_size) memcpy(dest,src,src_size)
  35. #endif
  36. #include <string>
  37. #include <vector>
  38. #include <memory>
  39. class iDBReadSet
  40. {
  41. public:
  42. virtual ~iDBReadSet() {}
  43. //获取行数
  44. virtual int GetRowCount() = 0;
  45. //是否读取完成
  46. virtual bool IsEOF() = 0;
  47. //下一条
  48. virtual bool MoveNext() = 0;
  49. //上一条
  50. virtual bool MovePrevious() = 0;
  51. //第一条
  52. virtual bool MoveFirst() = 0;
  53. //最后一条
  54. virtual bool MoveLast() = 0;
  55. virtual std::string GetFieldValue(const std::string& strFieldName, const std::string& strDefault = "") = 0;
  56. virtual int GetFieldValue(const std::string& strFieldName, int nDefault = 0) = 0;
  57. virtual long long GetFieldValue(const std::string& strFieldName, long long dwDefault = 0) = 0;
  58. virtual float GetFieldValue(const std::string& strFieldName, float dDefault = 0) = 0;
  59. virtual double GetFieldValue(const std::string& strFieldName, double dDefault = 0) = 0;
  60. //获取列数
  61. virtual int GetColCount() = 0;
  62. //获取列名
  63. virtual void GetColNames(std::vector<std::string> & vrFieldName) = 0;
  64. virtual std::string GetFieldValue(int nIndex, const std::string& strDefault = "") = 0;
  65. virtual int GetFieldValue(int nIndex, int nDefault = 0) = 0;
  66. virtual long long GetFieldValue(int nIndex, long long dwDefault = 0) = 0;
  67. virtual float GetFieldValue(int nIndex, float dDefault = 0) = 0;
  68. virtual double GetFieldValue(int nIndex, double fDefault = 0.0f) = 0;
  69. virtual bool GetFieldValue(int nIndex, std::vector<unsigned char>& vucOutValue) = 0;
  70. virtual bool GetFieldValue(const std::string& strFieldName, std::vector<unsigned char>& vucOutValue) = 0;
  71. };
  72. class I_DBOperator;
  73. class YDDBOPERATOR_API YD_DBOperator
  74. {
  75. public:
  76. //YD_DBOperator();
  77. YD_DBOperator(int mode=0);//0 sqlserver,1 mysql
  78. ~YD_DBOperator();
  79. public:
  80. enum db_ret_code
  81. {
  82. cd_gbk,//返回gbk编码
  83. cd_utf8,//返回utf8编码
  84. cd_auto //windows 下返回GBK ,linux 下返回utf8
  85. };
  86. public:
  87. int ConnectDB(const std::string& strIP, const std::string& strUserName, const std::string& strPassword, const std::string& strDbName,int port =0);
  88. std::shared_ptr<iDBReadSet> DirectQuery(const std::string& strSql, YD_DBOperator::db_ret_code inCode = YD_DBOperator::db_ret_code::cd_gbk);
  89. bool DirectExecute(const std::string& strSql,YD_DBOperator::db_ret_code inCode = YD_DBOperator::db_ret_code::cd_gbk);
  90. bool isOpened();
  91. int CloseDB();
  92. /// <summary>
  93. /// 设置返回字符串的编码
  94. /// </summary>
  95. /// <param name="code">gbk;utf8;cd_auto(windows下返回GBK; linux下返回utf8)</param>
  96. void SetReturnCode(YD_DBOperator::db_ret_code code);
  97. YD_DBOperator::db_ret_code GetReturnCode();
  98. int GetDBType();
  99. void SetDBType(int dbtype);
  100. private:
  101. I_DBOperator* m_pOperator;
  102. };