mirror of
https://gitee.com/js-yhsec/energy_storage.git
synced 2026-05-27 18:59:26 +08:00
113 lines
3.0 KiB
C++
113 lines
3.0 KiB
C++
#ifndef _DaoBase_H_
|
||
#define _DaoBase_H_
|
||
|
||
#include "MysqlClient.h"
|
||
|
||
class DaoEntity
|
||
{
|
||
public:
|
||
DaoEntity(string tableName);
|
||
~DaoEntity();
|
||
|
||
static MysqlOption& mysqlOption();
|
||
static void setOption(std::string host, int port, std::string user, std::string pwd, std::string dbname);
|
||
|
||
static std::shared_ptr<DaoEntity> create(string tableName);
|
||
|
||
/**
|
||
* 执行sql语句
|
||
* @param: sql 要执行的完整 sql 语句
|
||
*/
|
||
static bool execOnce(string sql);
|
||
|
||
/**
|
||
* 执行sql语句,返回结果数据集
|
||
* @param: sql 要执行的完整 sql 语句
|
||
* @param: result 返回的结果数据集
|
||
*/
|
||
static bool execOnce(string sql, vector<Fields>& result);
|
||
|
||
/**
|
||
* 设置数据库表名称
|
||
* @param: tableName 数据表名
|
||
*/
|
||
void setTableName(string tableName);
|
||
|
||
/**
|
||
* 设置数据库表名称
|
||
* @return: bool true: 连接成功;false:未连接/连接失败
|
||
*/
|
||
bool isConnected();
|
||
|
||
/**
|
||
* 执行sql语句
|
||
*/
|
||
int exec(string sql);
|
||
|
||
/**
|
||
* 执行sql语句并返回执行(查询)的结果集
|
||
*/
|
||
int exec(string sql, vector<Fields>& result);
|
||
|
||
|
||
/**
|
||
* 数据库插入一条数据, 需要先指定数据表名称
|
||
* @param: fields 写入的数据字段和值
|
||
*/
|
||
int insertFields(Fields& vecFields);
|
||
|
||
/**
|
||
* 数据库插入多条数据, 需要先指定数据表名称
|
||
* @param: vecFields 写入的数据字段和值的集合
|
||
*/
|
||
int insertFields(vector<Fields>& vecFields);
|
||
|
||
/**
|
||
* 数据库插入多条数据,UNIQUE索引或PRIMARY KEY重复时执行更新数据, 需要先指定数据表名称
|
||
* @param: vecFields 写入的数据字段和值的集合
|
||
* @param: keys 数据重复时需要更新的字段
|
||
*/
|
||
int duplicateUpdate(Fields& vecFields, const vector<string>& keys);
|
||
|
||
/**
|
||
* 数据库查询,需要先指定数据表名称
|
||
* @param: sql_c 查询条件,例:"where id='1'"
|
||
* @param: result 查询的数据结果集
|
||
*/
|
||
int queryFields(string keys, const string& sql_c, vector<Fields>& result);
|
||
|
||
/**
|
||
* 数据库查询,需要先指定数据表名称
|
||
* @param: sql_c 查询条件,例:"where id='1'"
|
||
* @param: pageinfo 分页信息
|
||
* @param: result 查询的数据结果集
|
||
*/
|
||
int queryFields(string keys, const string& sql_c, PageInfo& pageinfo, vector<Fields>& result);
|
||
|
||
/**
|
||
* 数据库更新,需要先指定数据表名称
|
||
* @param: fields 要更新的数据字段和值
|
||
* @param: sql_c 更新条件
|
||
*/
|
||
int updateFields(Fields& fields, const string& sql_c);
|
||
|
||
/**
|
||
* 数据库更新,需要先指定数据表名称
|
||
* @param: fields 要更新的数据值
|
||
* @param: vecKeys 要更新的字段名称
|
||
* @param: cond 更新条件
|
||
*/
|
||
int updateFields(Fields& fields, vector<string> vecKeys, const string& cond);
|
||
|
||
protected:
|
||
static MysqlOption option;
|
||
|
||
// mysql 数据库操作对象
|
||
std::shared_ptr<MysqlClient> db = nullptr;
|
||
|
||
// 数据库表名称
|
||
string tableName;
|
||
};
|
||
|
||
#endif // !!! _DaoBase_H_
|