XNSim/XNInterfaceGenServer/GetInterfaceData.cpp

177 lines
5.4 KiB
C++
Raw Normal View History

#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 tableName
* @return
*/
AllInterfaceData GetInterfaceData::getInterfaceData(int configrationID, 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;
}
std::string tableName = "DataInterface_" + std::to_string(configrationID);
std::string sql = "SELECT * FROM " + tableName;
sqlite3_stmt *stmt = nullptr;
rc = sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, nullptr);
if (rc != SQLITE_OK) {
errorMsg = "SQL预处理失败: " + std::string(sqlite3_errmsg(db));
sqlite3_close(db);
return allData;
}
while (sqlite3_step(stmt) == SQLITE_ROW) {
std::string currentSystemName;
std::string currentPlaneName;
std::string currentATAName;
std::string currentModelStructName;
InterfaceData currentInterfaceData;
int colCount = sqlite3_column_count(stmt);
for (int i = 0; i < colCount; ++i) {
const char *colName = sqlite3_column_name(stmt, i);
if (!colName)
continue;
std::string value = (const char *)sqlite3_column_text(stmt, i);
if (strcmp(colName, "SystemName") == 0) {
currentSystemName = value;
} else if (strcmp(colName, "PlaneName") == 0) {
currentPlaneName = value;
} else if (strcmp(colName, "ATAName") == 0) {
currentATAName = value;
} else if (strcmp(colName, "ModelStructName") == 0) {
currentModelStructName = value;
} else if (strcmp(colName, "InterfaceName") == 0) {
currentInterfaceData.interfaceName = value;
} else if (strcmp(colName, "InterfaceType") == 0) {
currentInterfaceData.interfaceType = value;
} else if (strcmp(colName, "InterfaceIsArray") == 0) {
currentInterfaceData.interfaceIsArray = safe_stoi(value);
} else if (strcmp(colName, "InterfaceArraySize_1") == 0) {
currentInterfaceData.interfaceArraySize_1 = safe_stoi(value);
} else if (strcmp(colName, "InterfaceArraySize_2") == 0) {
currentInterfaceData.interfaceArraySize_2 = safe_stoi(value);
} else if (strcmp(colName, "InterfaceNotes") == 0) {
currentInterfaceData.interfaceNotes = value;
}
}
// 初始化systemName和planeName
if (allData.systemName.empty()) {
allData.systemName = currentSystemName;
allData.planeName = currentPlaneName;
}
// 检查systemName和planeName是否匹配
if (currentSystemName != allData.systemName || currentPlaneName != allData.planeName) {
continue;
}
// 检查ATA是否已存在
bool ataFound = false;
for (auto &ata : allData.ataInterfaceData) {
if (ata.ataName == currentATAName) {
bool structFound = false;
for (auto &structData : ata.structInterfaceData) {
if (structData.modelStructName == currentModelStructName) {
bool interfaceFound = false;
for (auto &interfaceData : structData.interfaceData) {
if (interfaceData.interfaceName == currentInterfaceData.interfaceName) {
interfaceFound = true;
break;
}
}
if (!interfaceFound) {
structData.interfaceData.push_back(currentInterfaceData);
}
structFound = true;
break;
}
}
if (!structFound) {
StructInterfaceData structData;
structData.modelStructName = currentModelStructName;
structData.interfaceData.push_back(currentInterfaceData);
ata.structInterfaceData.push_back(structData);
}
ataFound = true;
break;
}
}
if (!ataFound) {
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);
}
}
sqlite3_finalize(stmt);
sqlite3_close(db);
return allData;
}
std::string GetInterfaceData::getConfigName(int configrationID, std::string &errorMsg)
{
std::string dbPath = GetXNCoreEnv() + "/database/XNSim.db";
sqlite3 *db = nullptr;
std::string confName;
int rc = sqlite3_open(dbPath.c_str(), &db);
if (rc != SQLITE_OK) {
errorMsg = "无法打开数据库: " + std::string(sqlite3_errmsg(db));
return "";
}
std::string sql = "SELECT ConfName FROM Configuration WHERE ConfID = ?";
sqlite3_stmt *stmt = nullptr;
rc = sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, nullptr);
if (rc != SQLITE_OK) {
errorMsg = "SQL预处理失败: " + std::string(sqlite3_errmsg(db));
sqlite3_close(db);
return "";
}
sqlite3_bind_int(stmt, 1, configrationID);
rc = sqlite3_step(stmt);
if (rc == SQLITE_ROW) {
const unsigned char *text = sqlite3_column_text(stmt, 0);
if (text) {
confName = reinterpret_cast<const char *>(text);
}
} else if (rc != SQLITE_DONE) {
errorMsg = "SQL执行失败: " + std::string(sqlite3_errmsg(db));
}
sqlite3_finalize(stmt);
sqlite3_close(db);
return confName;
}