重新设计oj_model
MySQL :: MySQL Product Archives
![![[Pasted image 20250227094526.png]]](https://i-blog.csdnimg.cn/direct/7e6537af79cc483385a41250a4d8115c.png)
选择mysql connector c
![![[Pasted image 20250227094611.png]]](https://i-blog.csdnimg.cn/direct/cfd269eb22db4bc7bab353e53866107f.png)
点击dowload
将文件导入并改名为mysql-connector
![![[Pasted image 20250227095550.png]]](https://i-blog.csdnimg.cn/direct/69b1aa7d6af2491da2acd5c72bca3f0f.png)
ln -s ./comm/mysql-connector/include include
建立软链接
ln -s ./comm/mysql-connector/lib lib
![![[Pasted image 20250227100035.png]]](https://i-blog.csdnimg.cn/direct/fde98e6801a64666b600f7c9406dd5c1.png)
![![[Pasted image 20250227100245.png]]](https://i-blog.csdnimg.cn/direct/de3fe732f6d24e799c66f6062d70f5a7.png)
链接到oj_server里
复制一个oj_model
![![[Pasted image 20250227100521.png]]](https://i-blog.csdnimg.cn/direct/d73a8fc46fa2499d8c5a5f4845e94ee0.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]]](https://i-blog.csdnimg.cn/direct/d24b84204303471694d3b4de14a4ad32.png)
如果到时候mysql找不到系统库
ls /etc/ld.so.conf.d/
cd /etc/ld.so.conf.d/
sudo touch oj_lib_search.conf
![![[Pasted image 20250227105307.png]]](https://i-blog.csdnimg.cn/direct/cc31f4c836df45c78956907000fba677.png)
/root/OnlineJudge/oj_server/lib
把路径粘贴到oj_lib_search.conf里
在执行下条命令
ldconfig
![![[Pasted image 20250227112230.png]]](https://i-blog.csdnimg.cn/direct/7aa04fddd38f4773b6fd05d3afa57fea.png)
再录题
![![[Pasted image 20250227113425.png]]](https://i-blog.csdnimg.cn/direct/ba8c8622bca042d9b1d9b452c3cc3009.png)
![![[Pasted image 20250227113449.png]]](https://i-blog.csdnimg.cn/direct/94e40cc38dcb4eb59350347e844beecf.png)
直接刷新题库,就有了第二道题
![![[Pasted image 20250227113511.png]]](https://i-blog.csdnimg.cn/direct/e55a83ffea864aa3aa9b78270cb357fd.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;
1206

被折叠的 条评论
为什么被折叠?



