#include "GetInterfaceData.h" /** * @brief 安全地将字符串转换为整数 * @param str 要转换的字符串 * @param defaultValue 转换失败时的默认值 * @return 转换后的整数值 */ static int safe_stoi(const std::string &str, int defaultValue = 0) { if (str.empty()) { return defaultValue; } try { return std::stoi(str); } catch (const std::exception &) { return defaultValue; } } /** * @brief 回调函数,用于处理查询结果 * @param data 用户数据指针 * @param argc 列数 * @param argv 列值数组 * @param azColName 列名数组 * @return 0表示成功 */ static int callback(void *data, int argc, char **argv, char **azColName) { if (!data || !argv || !azColName) { return 0; } auto *allData = static_cast(data); static bool isFirstRecord = true; std::string currentSystemName; std::string currentPlaneName; std::string currentATAName; std::string currentModelStructName; InterfaceData currentInterfaceData; //先把数据都读出来 for (int i = 0; i < argc; i++) { std::string colName = azColName[i]; std::string value = argv[i] ? argv[i] : ""; if (colName == "SystemName") { currentSystemName = value; } else if (colName == "PlaneName") { currentPlaneName = value; } else if (colName == "ATAName") { currentATAName = value; } else if (colName == "ModelStructName") { currentModelStructName = value; } else if (colName == "InterfaceName") { currentInterfaceData.interfaceName = value; } else if (colName == "InterfaceType") { currentInterfaceData.interfaceType = value; } else if (colName == "InterfaceIsArray") { currentInterfaceData.interfaceIsArray = safe_stoi(value); } else if (colName == "InterfaceArraySize_1") { currentInterfaceData.interfaceArraySize_1 = safe_stoi(value); } else if (colName == "InterfaceArraySize_2") { currentInterfaceData.interfaceArraySize_2 = safe_stoi(value); } else if (colName == "InterfaceNotes") { currentInterfaceData.interfaceNotes = value; } } // 如果是第一条记录,初始化系统名称和飞机名称 if (isFirstRecord) { allData->systemName = currentSystemName; allData->planeName = currentPlaneName; isFirstRecord = false; } // 检查系统名称和飞机名称是否匹配 if (currentSystemName != allData->systemName || currentPlaneName != allData->planeName) { return 0; } //检查ATA是否已存在 for (auto &ata : allData->ataInterfaceData) { if (ata.ataName == currentATAName) { for (auto &structData : ata.structInterfaceData) { if (structData.modelStructName == currentModelStructName) { for (auto &interfaceData : structData.interfaceData) { if (interfaceData.interfaceName == currentInterfaceData.interfaceName) { return 0; } } structData.interfaceData.push_back(currentInterfaceData); return 0; } } StructInterfaceData structData; structData.modelStructName = currentModelStructName; structData.interfaceData.push_back(currentInterfaceData); ata.structInterfaceData.push_back(structData); return 0; } } ATAInterfaceData ataData; ataData.ataName = currentATAName; StructInterfaceData structData; structData.modelStructName = currentModelStructName; structData.interfaceData.push_back(currentInterfaceData); ataData.structInterfaceData.push_back(structData); allData->ataInterfaceData.push_back(ataData); return 0; } /** * @brief 从指定表中读取接口数据 * @param tableName 数据库表名 * @return 接口数据 */ AllInterfaceData GetInterfaceData::getInterfaceData(const std::string &tableName, std::string &errorMsg) { AllInterfaceData allData; std::string dbPath = GetXNCoreEnv() + "/database/XNSim.db"; sqlite3 *db; int rc = sqlite3_open(dbPath.c_str(), &db); if (rc != SQLITE_OK) { errorMsg = "无法打开数据库: " + std::string(sqlite3_errmsg(db)); return allData; } char *errMsg = nullptr; std::string sql = "SELECT * FROM " + tableName; rc = sqlite3_exec(db, sql.c_str(), callback, &allData, &errMsg); if (rc != SQLITE_OK) { std::string error = errMsg; sqlite3_free(errMsg); errorMsg = "SQL错误: " + error; return allData; } sqlite3_close(db); return allData; }