增加:f_insertrecord 类实现
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
/*
|
||||
* @Description: file content
|
||||
* @Author: why
|
||||
* @version:
|
||||
* @Date: 2025-02-21 14:03:54
|
||||
* @LastEditors: why
|
||||
* @LastEditTime: 2025-02-21 17:48:44
|
||||
*/
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include "F_InsertRecord.h"
|
||||
using namespace std;
|
||||
using json = nlohmann::json // for convenience
|
||||
|
||||
typedef struct topic{
|
||||
|
||||
std::string src;
|
||||
std::string des;
|
||||
std::string sid;
|
||||
std::string fid;
|
||||
} topic_type;
|
||||
InsertRecord::InsertRecord(void){
|
||||
handpara = nullptr;
|
||||
initpara = nullptr;
|
||||
}
|
||||
|
||||
InsertRecord::~InsertRecord(void){
|
||||
}
|
||||
|
||||
void InsertRecord::Init(initparam_t *param){
|
||||
initpara = param;
|
||||
}
|
||||
int InsertRecord::SQLBatch_exc(std::vector<std::string> &sql_list){
|
||||
sqlite3 *db = handpara;
|
||||
char *errMsg = nullptr;
|
||||
const char *sql;
|
||||
int rc;
|
||||
|
||||
// 开启事务
|
||||
if (sqlite3_exec(db, "BEGIN TRANSACTION;", nullptr, nullptr, &errMsg) != SQLITE_OK) {
|
||||
std::cerr << "事务开启失败: " << errMsg << std::endl;
|
||||
sqlite3_free(errMsg);
|
||||
|
||||
return 1;
|
||||
}
|
||||
// 执行批量插入操作
|
||||
sqlite3_stmt *stmt;
|
||||
for(const auto &k:sql_list){
|
||||
std::cout << "cmd:"<<k.c_str()<<std::endl;
|
||||
if (sqlite3_prepare_v2(db, k.c_str(), -1, &stmt, nullptr) != SQLITE_OK) {
|
||||
std::cerr << "准备语句失败: " << sqlite3_errmsg(db) << std::endl;
|
||||
continue;
|
||||
}
|
||||
rc = sqlite3_step(stmt);
|
||||
if (rc != SQLITE_DONE) {
|
||||
std::cerr << "插入失败: " << sqlite3_errmsg(db) << std::endl;
|
||||
}
|
||||
sqlite3_reset(stmt); // 重置语句
|
||||
}
|
||||
// 提交事务
|
||||
if (sqlite3_exec(db, "COMMIT;", nullptr, nullptr, &errMsg) != SQLITE_OK) {
|
||||
std::cerr << "事务提交失败: " << errMsg << std::endl;
|
||||
sqlite3_free(errMsg);
|
||||
}
|
||||
// 清理
|
||||
sqlite3_finalize(stmt);
|
||||
return 0;
|
||||
}
|
||||
int InsertRecord::HandleMsg(const msg_t msg, handparam_t *param){
|
||||
|
||||
topic.clear();
|
||||
topic_split(msg.topic,'/');
|
||||
json value,resp;
|
||||
int ret = 0;
|
||||
std::vector<std::string> sql_list;
|
||||
sql_list.clear();
|
||||
std::string table_name;
|
||||
try{
|
||||
value = json::prase(msg.payload)
|
||||
}
|
||||
catch(const std::expection &e){
|
||||
std::cout << "topic "<<msg.topic.c_str()<<" prase error:"<<e.what();
|
||||
return -1;//format failfail
|
||||
}
|
||||
if(!value.is_object())return -1;
|
||||
if(value.contains("token")){
|
||||
resp["token"] = value["token"];
|
||||
}
|
||||
if(value.contains("time")){
|
||||
// value["time"]
|
||||
}
|
||||
if(value.contains("data") && value["data"].contains("dataarray")){
|
||||
json subvalue = value["data"]["dataarray"];
|
||||
if(subvalue.is_array()){
|
||||
for(const auto &k:subvalue){
|
||||
std::stringstream ss;
|
||||
k["metid"]
|
||||
k["afn"]
|
||||
k["dataid"]
|
||||
k["frozentime"]
|
||||
k["crcaux"]
|
||||
k["databuff"]
|
||||
ss << "insert into " <<table_name.c_str() <<" ";
|
||||
sql_list.pushback(ss.str());
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
SQLBatch_exc(sql_list);
|
||||
resp["time"]=0;
|
||||
resp["ack"]=true;
|
||||
|
||||
}
|
||||
const std::string GetFID(){
|
||||
|
||||
}
|
||||
void InsertRecord::topic_split(const std::string& str, char delimiter) {
|
||||
std::string token; // 临时存储每个子字符串
|
||||
std::istringstream tokenStream(str); // 使用字符串流处理输入字符串
|
||||
// 按分隔符逐个读取子字符串
|
||||
while (std::getline(tokenStream, token, delimiter)) {
|
||||
topic.push_back(token); // 将子字符串添加到结果中
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* @Description: file content
|
||||
* @Author: why
|
||||
* @version:
|
||||
* @Date: 2025-02-21 14:04:30
|
||||
* @LastEditors: why
|
||||
* @LastEditTime: 2025-02-21 17:30:21
|
||||
*/
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "bizservice.h"
|
||||
|
||||
class InsertRecord :public IBizService
|
||||
{
|
||||
public:
|
||||
void Init(initparam_t *param);
|
||||
int HandleMsg(const msg_t msg, handparam_t *param = NULL);
|
||||
const std::string GetFID();
|
||||
InsertRecord();
|
||||
~InsertRecord();
|
||||
private:
|
||||
handparam_t * handpara;
|
||||
initparam_t * initpara;
|
||||
std::vector<std::string> topic;//0:dst 1:src 2:sid 3:fid
|
||||
void topic_split(const std::string& str, char delimiter);
|
||||
int InsertRecord::SQLBatch_exc(std::vector<std::string> &sql_list);
|
||||
};
|
||||
@@ -0,0 +1,63 @@
|
||||
|
||||
/**
|
||||
* @brief 使用方法
|
||||
* 1、设计类时请继承于IBizService类
|
||||
* 2、在HandleMsg中将结果信息通过WriteResponseMsg进行输出
|
||||
*/
|
||||
#ifndef _DCAPP_CENTER_INTERFACE_H_
|
||||
#define _DCAPP_CENTER_INTERFACE_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
typedef struct
|
||||
{
|
||||
void *pDbHandle; // 数据库句柄参数
|
||||
} handparam_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
void *resove;
|
||||
} initparam_t; // 其他参数
|
||||
|
||||
typedef struct msg
|
||||
{
|
||||
std::string topic; // 消息主题
|
||||
std::string payload; // 消息内容
|
||||
long timestamp; // 消息时间戳
|
||||
} msg_t; // 消息体结构
|
||||
|
||||
/**
|
||||
* @brief 指令响应(将响应的信息体进行输出)
|
||||
*
|
||||
* @param msg //待输出信息
|
||||
* @return int 是否发送成功 0:成功 -1:失败
|
||||
*/
|
||||
int WriteResponseMsg(const msg_t &msg);
|
||||
|
||||
class IBizService
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @brief 初始化
|
||||
*
|
||||
* @param param 输入初始化参数
|
||||
*/
|
||||
virtual void Init(initparam_t *param = NULL) = 0;
|
||||
|
||||
/**
|
||||
* @brief 业务处理
|
||||
*
|
||||
* @param msg 输入消息体
|
||||
* @param param 可用数据库连接句柄
|
||||
* @return int 返回值 成功为0 失败<0 (-1待处理数据格式错误 -2操作数据库失败 -3其它错误)
|
||||
*/
|
||||
virtual int HandleMsg(const msg_t msg, handparam_t *param = NULL) = 0; // 业务处理
|
||||
|
||||
/**
|
||||
* @brief 获取FID服务标识
|
||||
*
|
||||
* @return FID服务标识
|
||||
*/
|
||||
virtual const std::string GetFID() = 0; /* 如"F-GetVersion" */
|
||||
};
|
||||
#endif
|
||||
+25447
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user