mrDarker
2025-05-14 0d2da4a2507a5a8d34bf7f736727917d791df0d2
1. 添加数据库事务提交,减少频繁执行 SQL 写操作
已修改3个文件
59 ■■■■ 文件已修改
SourceCode/Bond/Servo/PageRecipe.cpp 35 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
SourceCode/Bond/Servo/SECSRuntimeManager.cpp 22 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
SourceCode/Bond/Servo/SECSRuntimeManager.h 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
SourceCode/Bond/Servo/PageRecipe.cpp
@@ -32,15 +32,21 @@
    pListCtrl->DeleteAllItems();
    // 遍历数据并插入到CListCtrl中
    int nIndex = 0;
    for (const auto& row : vecData) {
        int nNewItem = pListCtrl->InsertItem(nIndex, _T(""));
    for (int i = 0; i < static_cast<int>(vecData.size()); ++i) {
        // 插入行
        pListCtrl->InsertItem(i, _T(""));
        CString str;
        str.Format(_T("%d"), nIndex++);
        pListCtrl->SetItemText(nNewItem, 1, str); // Recipe No
        str.Format(_T("%s"), row.c_str());
        pListCtrl->SetItemText(nNewItem, 2, str); // PPID
        // 设置 Recipe No(第1列)
        CString strRecipeNo;
        strRecipeNo.Format(_T("%d"), i);
        pListCtrl->SetItemText(i, 1, strRecipeNo);
        // 设置 PPID(第2列)
        CString strPPID = CA2T(vecData[i].c_str());
        if (strPPID.CompareNoCase(_T("NULL")) == 0) {
            strPPID.Empty();
        }
        pListCtrl->SetItemText(i, 2, strPPID);
    }
    // 获取列数
@@ -147,18 +153,13 @@
void CPageRecipe::OnBnClickedButtonSave()
{
    // TODO: 在此添加控件通知处理程序代码
    std::vector<std::string> vecPPID;
    int nCount = m_listPPID.GetItemCount();
    for (int i = 0; i < nCount; ++i) {
        std::string strPPID = CT2A(m_listPPID.GetItemText(i, 2));
        SECSRuntimeManager::getInstance().updatePPIDForRecipe(i, strPPID);
        CString str = m_listPPID.GetItemText(i, 2);
        vecPPID.emplace_back(CT2A(str));
    }
    //if (!SECSRuntimeManager::getInstance().saveAllPPID(vecData)) {
    //    AfxMessageBox(_T("保存失败"));
    //}
    //else {
    //    AfxMessageBox(_T("保存成功"));
    //}
    SECSRuntimeManager::getInstance().setAllPPID(vecPPID);
}
void CPageRecipe::OnBnClickedButtonRefresh()
SourceCode/Bond/Servo/SECSRuntimeManager.cpp
@@ -1436,6 +1436,28 @@
    return vecResult;
}
void SECSRuntimeManager::setAllPPID(const std::vector<std::string>& vecPPIDList) {
    std::lock_guard<std::mutex> lock(m_mutex);
    if (m_pDB == nullptr) return;
    // 开启事务
    m_pDB->executeQuery("BEGIN TRANSACTION;");
    for (size_t i = 0; i < vecPPIDList.size(); ++i) {
        std::string safePPID = vecPPIDList[i];
        size_t pos = 0;
        while ((pos = safePPID.find('\'', pos)) != std::string::npos) {
            safePPID.insert(pos, 1, '\'');
            pos += 2;
        }
        std::string sql = "UPDATE EqpPPID SET PPID = '" + safePPID + "' WHERE RecipeNo = " + std::to_string(i) + ";";
        m_pDB->executeQuery(sql);
    }
    // 提交事务
    m_pDB->executeQuery("COMMIT;");
}
bool SECSRuntimeManager::updatePPIDForRecipe(int nRecipeNo, const std::string& strPPID) {
    std::lock_guard<std::mutex> lock(m_mutex);
    if (m_pDB == nullptr) {
SourceCode/Bond/Servo/SECSRuntimeManager.h
@@ -391,6 +391,8 @@
    std::vector<std::string> getAllPPID();
    void setAllPPID(const std::vector<std::string>& vecPPIDList);
    bool updatePPIDForRecipe(int nRecipeNo, const std::string& strPPID);
    std::string getPPIDForRecipe(int nRecipeNo);