- 新增 msg_log 原始帧表,RX/TX 双向记录原始字节与校验结果 - SQLite 封装改为描述符驱动的泛化 insert,删除 10 个手写 SQL - 新增 ccuSetParmCmd/ccuSetParmFb 表,补齐 CCU 参数设定反馈落库 - 错误防崩溃:统一加锁、fail-open 降级、损坏自动重建、析构顺序修复 - WAL/迁移/预编译语句路径,数据库路径支持 mission 配置 dbpath - 清理未使用的 shell.c/sqlite3ext.h/SQLite.zip
78 lines
2.4 KiB
C++
78 lines
2.4 KiB
C++
#ifndef SQLITE_H
|
|
#define SQLITE_H
|
|
#include "sqlite3.h"
|
|
#include <string>
|
|
#include <mutex>
|
|
#include <vector>
|
|
#include <cstdint>
|
|
#include "../pmSysvariable.h"
|
|
#include "../udpcomm/ccuUdpMsg.h"
|
|
#include "../udpcomm/CommLogger.h"
|
|
|
|
class SQLite : public ICommLogger
|
|
{
|
|
public:
|
|
// 数据库健康状态
|
|
enum class Status { OK, Degraded, Disabled };
|
|
|
|
explicit SQLite(const std::string& dbPath);
|
|
~SQLite();
|
|
|
|
// 连接是否有效(无锁的指针判断,仅用于快速探活)
|
|
bool isConnected() const { return m_db != nullptr; }
|
|
Status status() const { return m_status; }
|
|
std::string lastError() const;
|
|
|
|
// 建表(幂等,含迁移)
|
|
bool createTable();
|
|
bool createTable(const std::string& tableName, const std::string& columns);
|
|
|
|
// 状态反馈数据
|
|
bool insertData(ccuState data);
|
|
bool insertData(disHighVolBusState data);
|
|
bool insertData(disHighAVolBusState data);
|
|
bool insertData(disLowMainBusState data);
|
|
bool insertData(disLowBusState data);
|
|
bool insertData(const msg_CcuSetParmFbMsg& fb);
|
|
|
|
// 下发指令数据
|
|
bool insertData(ccuColCmd cmd);
|
|
bool insertData(ccuSetParmCmd cmd);
|
|
bool insertData(disHighVolBusCmd cmd);
|
|
bool insertData(disHighAVolBusCmd cmd);
|
|
bool insertData(disLowMainBusCmd cmd);
|
|
bool insertData(disLowBusCmd cmd);
|
|
|
|
// 原始收发帧落库(实现 ICommLogger,供 udpComm 调用)
|
|
void onFrame(int direction, unsigned short msgId, const char* name,
|
|
const unsigned char* data, unsigned int len,
|
|
bool checksumOk) override;
|
|
|
|
// 查询接口
|
|
long long count(const std::string& table);
|
|
bool queryInt(const std::string& sql, long long& out);
|
|
|
|
std::string m_dbPath;
|
|
|
|
private:
|
|
sqlite3* m_db;
|
|
mutable std::mutex m_dbMutex;
|
|
Status m_status;
|
|
std::string m_lastError;
|
|
|
|
bool setError(int rc, const char* op);
|
|
bool applyPragmas();
|
|
bool migrate();
|
|
bool createTableInternal(const std::string& tableName, const std::string& columns);
|
|
bool createAllTablesUnlocked();
|
|
|
|
// 泛化插入:表名 + 列描述(名称 + 绑定器),所有类型共用这一条路径
|
|
struct Column {
|
|
const char* name;
|
|
void (*bind)(sqlite3_stmt*, int& idx, const void* obj);
|
|
};
|
|
bool insertGeneric(const std::string& table, const std::vector<Column>& cols, const void* obj);
|
|
};
|
|
|
|
#endif // SQLITE_H
|