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