| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- #pragma once
- /*******************************************************************************
- 描 述 : 数据库读写类
- 其 他 : 未主控系统提供
- 历史记录:
- 1. 时间: 2020-10-13
- 作者: 姚建军
- 修改描述: 新创建
- 2. ...
- *******************************************************************************/
- #if defined(_MSC_VER) || defined(WIN64) || defined(__WIN64__) || defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
- #ifdef YDDBOPERATOR_EXPORTS
- #define YDDBOPERATOR_API __declspec(dllexport)
- #else
- #define YDDBOPERATOR_API __declspec(dllimport)
- #endif
- #ifndef YDDBOPERATOR_EXPORTS
- #ifndef _WIN64
- #ifndef _DEBUG
- #pragma comment(lib,"YD.Public.DbOperator.x86.lib")
- #else
- #pragma comment(lib,"YD.Public.DbOperator.x86d.lib")
- #endif
- #else
- #ifndef _DEBUG
- #pragma comment(lib,"YD.Public.DbOperator.x64.lib")
- #else
- #pragma comment(lib,"YD.Public.DbOperator.x64d.lib")
- #endif
- #endif
- #endif
- #else //linux
- #define YDDBOPERATOR_API __attribute__ ((visibility("default")))
- #define memcpy_s(dest,dest_size,src,src_size) memcpy(dest,src,src_size)
- #endif
- #include <string>
- #include <vector>
- #include <memory>
- class iDBReadSet
- {
- public:
- virtual ~iDBReadSet() {}
- //获取行数
- virtual int GetRowCount() = 0;
- //是否读取完成
- virtual bool IsEOF() = 0;
- //下一条
- virtual bool MoveNext() = 0;
- //上一条
- virtual bool MovePrevious() = 0;
- //第一条
- virtual bool MoveFirst() = 0;
- //最后一条
- virtual bool MoveLast() = 0;
- virtual std::string GetFieldValue(const std::string& strFieldName, const std::string& strDefault = "") = 0;
- virtual int GetFieldValue(const std::string& strFieldName, int nDefault = 0) = 0;
- virtual long long GetFieldValue(const std::string& strFieldName, long long dwDefault = 0) = 0;
- virtual float GetFieldValue(const std::string& strFieldName, float dDefault = 0) = 0;
- virtual double GetFieldValue(const std::string& strFieldName, double dDefault = 0) = 0;
- //获取列数
- virtual int GetColCount() = 0;
- //获取列名
- virtual void GetColNames(std::vector<std::string> & vrFieldName) = 0;
- virtual std::string GetFieldValue(int nIndex, const std::string& strDefault = "") = 0;
- virtual int GetFieldValue(int nIndex, int nDefault = 0) = 0;
- virtual long long GetFieldValue(int nIndex, long long dwDefault = 0) = 0;
- virtual float GetFieldValue(int nIndex, float dDefault = 0) = 0;
- virtual double GetFieldValue(int nIndex, double fDefault = 0.0f) = 0;
- virtual bool GetFieldValue(int nIndex, std::vector<unsigned char>& vucOutValue) = 0;
- virtual bool GetFieldValue(const std::string& strFieldName, std::vector<unsigned char>& vucOutValue) = 0;
- };
- class I_DBOperator;
- class YDDBOPERATOR_API YD_DBOperator
- {
- public:
- //YD_DBOperator();
- YD_DBOperator(int mode=0);//0 sqlserver,1 mysql
- ~YD_DBOperator();
- public:
- enum db_ret_code
- {
- cd_gbk,//返回gbk编码
- cd_utf8,//返回utf8编码
- cd_auto //windows 下返回GBK ,linux 下返回utf8
- };
- public:
- int ConnectDB(const std::string& strIP, const std::string& strUserName, const std::string& strPassword, const std::string& strDbName,int port =0);
- std::shared_ptr<iDBReadSet> DirectQuery(const std::string& strSql, YD_DBOperator::db_ret_code inCode = YD_DBOperator::db_ret_code::cd_gbk);
- bool DirectExecute(const std::string& strSql,YD_DBOperator::db_ret_code inCode = YD_DBOperator::db_ret_code::cd_gbk);
- bool isOpened();
- int CloseDB();
- /// <summary>
- /// 设置返回字符串的编码
- /// </summary>
- /// <param name="code">gbk;utf8;cd_auto(windows下返回GBK; linux下返回utf8)</param>
- void SetReturnCode(YD_DBOperator::db_ret_code code);
- YD_DBOperator::db_ret_code GetReturnCode();
- int GetDBType();
- void SetDBType(int dbtype);
- private:
- I_DBOperator* m_pOperator;
- };
|