当前位置: 首页 > article >正文

13.重新设计oj_model|综合测试|顶层makefile(C++)

重新设计oj_model

MySQL :: MySQL Product Archives
![[Pasted image 20250227094526.png]]

选择mysql connector c
![[Pasted image 20250227094611.png]]

点击dowload
将文件导入并改名为mysql-connector
![[Pasted image 20250227095550.png]]

ln -s ./comm/mysql-connector/include include

建立软链接

ln -s ./comm/mysql-connector/lib lib

![[Pasted image 20250227100035.png]]

![[Pasted image 20250227100245.png]]

链接到oj_server里

复制一个oj_model
![[Pasted image 20250227100521.png]]

#pragma once
//MySQL 版本
#include "../comm/util.hpp"
#include "../comm/log.hpp"
#include "include/mysql.h"
  
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <fstream>
#include <cstdlib>
#include <cassert>
  
// 根据题目list文件,加载所有的题目信息到内存中
// model: 主要用来和数据进行交互,对外提供访问数据的接口
  
namespace ns_model
{
    using namespace std;
    using namespace ns_log;
    using namespace ns_util;
  
    struct Question
    {
        std::string number; //题目编号,唯一
        std::string title;  //题目的标题
        std::string star;   //难度: 简单 中等 困难
        std::string desc;   //题目的描述
        std::string header; //题目预设给用户在线编辑器的代码
        std::string tail;   //题目的测试用例,需要和header拼接,形成完整代码
        int cpu_limit;      //题目的时间要求(S)
        int mem_limit;      //题目的空间要去(KB)
    };
  
    const std::string oj_questions = "oj_questions";
    const std::string host = "127.0.0.1";
    const std::string user = "oj_client";
    const std::string passwd = "123456";
    const std::string db = "oj";
    const int port = 3306;
  
    class Model
    {
    public:
        Model()
        {}
        bool QueryMySql(const std::string &sql, vector<Question> *out)
        {
            // 创建mysql句柄
            MYSQL *my = mysql_init(nullptr);
  
            // 连接数据库
            if(nullptr == mysql_real_connect(my, host.c_str(), user.c_str(), passwd.c_str(),db.c_str(),port, nullptr, 0)){
                LOG(FATAL) << "连接数据库失败!" << "\n";
                return false;
            }
  
            // 一定要设置该链接的编码格式, 要不然会出现乱码问题
            mysql_set_character_set(my, "utf8");
            LOG(INFO) << "连接数据库成功!" << "\n";
  
            // 执行sql语句
            if(0 != mysql_query(my, sql.c_str()))
            {
                LOG(WARNING) << sql << " execute error!" << "\n";
                return false;
            }
  
            // 提取结果
            MYSQL_RES *res = mysql_store_result(my);
  
            // 分析结果
            int rows = mysql_num_rows(res); //获得行数量
            int cols = mysql_num_fields(res); //获得列数量
  
            Question q;
  
            for(int i = 0; i < rows; i++)
            {
                MYSQL_ROW row = mysql_fetch_row(res);
                q.number = row[0];
                q.title = row[1];
                q.star = row[2];
                q.desc = row[3];
                q.header = row[4];
                q.tail = row[5];
                q.cpu_limit = atoi(row[6]);
                q.mem_limit = atoi(row[7]);
  
                out->push_back(q);
            }
            // 释放结果空间
            free(res);
            // 关闭mysql连接
            mysql_close(my);
  
            return true;
        }
        bool GetAllQuestions(vector<Question> *out)
        {
            std::string sql = "select * from ";
            sql += oj_questions;
            return QueryMySql(sql, out);
        }
        bool GetOneQuestion(const std::string &number, Question *q)
        {
            bool res = false;
            std::string sql = "select * from ";
            sql += oj_questions;
            sql += " where number=";
            sql += number;
            vector<Question> result;
            if(QueryMySql(sql, &result))
            {
                if(result.size() == 1){
                    *q = result[0];
                    res = true;
                }
            }
            return res;
        }
        ~Model()
        {}
    };
} // namespace ns_model

makefile

oj_server:oj_server.cc
    g++ -o $@ $^ -I./include -L./lib -std=c++11 -lpthread -lctemplate -ljsoncpp -lmysqlclient
  
.PHONY:clean
clean:
    rm -f oj_server

综合测试

ldd oj_server

![[Pasted image 20250227104721.png]]

如果到时候mysql找不到系统库

ls /etc/ld.so.conf.d/
cd /etc/ld.so.conf.d/
sudo touch oj_lib_search.conf

![[Pasted image 20250227105307.png]]

/root/OnlineJudge/oj_server/lib

把路径粘贴到oj_lib_search.conf里
在执行下条命令

ldconfig

![[Pasted image 20250227112230.png]]

再录题
![[Pasted image 20250227113425.png]]

![[Pasted image 20250227113449.png]]

直接刷新题库,就有了第二道题
![[Pasted image 20250227113511.png]]

顶层makefile

.PHONY: all
all:
    @cd compile_server;\
    make;\
    cd -;\
    cd oj_server;\
    make;\
    cd -;
  
.PHONY:output
output:
    @mkdir -p output/compile_server;\
    mkdir -p output/oj_server;\
    cp -rf compile_server/compile_server output/compile_server;\
    cp -rf compile_server/temp output/compile_server;\
    cp -rf oj_server/conf output/oj_server/;\
    cp -rf oj_server/lib output/oj_server/;\
    cp -rf oj_server/questions output/oj_server/;\
    cp -rf oj_server/template_html output/oj_server/;\
    cp -rf oj_server/wwwroot output/oj_server/;\
    cp -rf oj_server/oj_server output/oj_server/;
  
.PHONY:clean
clean:
    @cd compile_server;\
    make clean;\
    cd -;\
    cd oj_server;\
    make clean;\
    cd -;\
    rm -rf output;

http://www.kler.cn/a/566842.html

相关文章:

  • SAP-ABAP:SAP数据库视图(Database View)详解-创建
  • 学习dify第二天-web前篇
  • 典型相关分析:原理、检验与Matlab实战
  • 领域驱动设计:事件溯源架构简介
  • DeepSeek再次重磅开源DeepEP:开源世界里的 AI 通信 “新引擎”
  • 自动化测试无法启动(java.net.SocketException)
  • 清理docker资源
  • 构建逻辑思维链(CoT)为金融AI消除幻觉(保险赔付篇)
  • STM32——HAL库开发笔记24(定时器5—超声波测距)(参考来源:b站铁头山羊)
  • React底层常见的设计模式
  • Vue的data配置项
  • Go红队开发—语法补充
  • 迁移过程中,hive元数据字段校对
  • 在kubernetes集群中持续压测 SpringCloud 应用,pod 的 memory cache 持续增长问题
  • Mysql .idb文件 恢复
  • Windows10 Xming6 + Xshell7 实现远程 ubuntu-24.04.1-desktop gui 界面本地展示
  • Redis 同步机制详解
  • Docker 部署 Spring Cloud 项目:实战指南与经验分享
  • Djiang 5实用指南(八)后台管理系统
  • JSON-to-Excel v2.0.0发布,可以在Excel内部,把JSON转换成Excel格式,嵌套的JSON也能转