【修改】mysql细节修改
This commit is contained in:
@@ -128,6 +128,8 @@ public class IMysqlLocalServiceImpl implements IMysqlLocalService {
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
appendToFile(answerLogPath, "未找到答题表:" +stuDataName+ ","+e.getMessage());
|
||||
judgementStr = HtmlAppender.appendHtmlLine(judgementStr, "未找到答题表:" +stuDataName+","+ e.getMessage());
|
||||
}
|
||||
|
||||
// // **建立连接到新创建的数据库**
|
||||
@@ -267,8 +269,7 @@ public class IMysqlLocalServiceImpl implements IMysqlLocalService {
|
||||
String answerId= examQuestionAnswer.getAnswerId();
|
||||
List<ExamMysqlKeyword> examMysqlKeywordList = examMysqlKeywordMapper.selectListByAnswerId(answerId);
|
||||
|
||||
|
||||
//把select语句 转化成验证语句sql
|
||||
// 传入 INSERT SQL 和主键字段
|
||||
String yanzheng = convertInsertToSelect(sql);
|
||||
|
||||
String stuSql=null;
|
||||
@@ -894,6 +895,71 @@ public class IMysqlLocalServiceImpl implements IMysqlLocalService {
|
||||
judgementStr = studentScorePojo.getText();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (sql.trim().toUpperCase().startsWith("ALTER TABLE")) {
|
||||
appendToFile(answerLogPath, "=================修改表语句==================");
|
||||
judgementStr = HtmlAppender.appendHtmlLine(judgementStr, "==================修改表语句==================");
|
||||
appendToFile(answerLogPath, "答案语句: " + sql);
|
||||
judgementStr = HtmlAppender.appendHtmlLine(judgementStr, "答案语句: ");
|
||||
judgementStr = HtmlAppender.appendHtmlLine(judgementStr, sql);
|
||||
// 匹配成功
|
||||
String answerId = examQuestionAnswer.getAnswerId();
|
||||
List<ExamMysqlKeyword> examMysqlKeywordList= examMysqlKeywordMapper.selectListByAnswerId(answerId);
|
||||
//转换成select语句
|
||||
String selectSql = convertAlertToSelectWhere(sql);
|
||||
|
||||
List<List<String>> answerList= new ArrayList<>();
|
||||
List<List<String>> answerListStu = new ArrayList<>();
|
||||
try (Connection connanswer = DriverManager.getConnection(newDbUrl, user, password);
|
||||
Statement stmtan = connanswer.createStatement()) {
|
||||
try (ResultSet answer = stmtan.executeQuery(selectSql)) {
|
||||
answerList = getAnswerList(answer);
|
||||
appendToFile(answerLogPath, "查找语句标准答案");
|
||||
judgementStr = HtmlAppender.appendHtmlLine(judgementStr, "查找语句标准答案");
|
||||
printResult(answerList,judgementStr);
|
||||
}catch (SQLException e) {
|
||||
appendToFile(answerLogPath, "答案表执行验证语句"+selectSql+"时发生错误: " + e.getMessage());
|
||||
judgementStr = HtmlAppender.appendHtmlLine(judgementStr, "答案表执行验证语句"+selectSql+"时发生错误: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
String stuSql =null;
|
||||
try (Connection connstu = DriverManager.getConnection(stuDbUrl, user, password);
|
||||
Statement stmtstu = connstu.createStatement()) {
|
||||
try (ResultSet answer = stmtstu.executeQuery(selectSql)) {
|
||||
answerListStu = getAnswerList(answer);
|
||||
appendToFile(answerLogPath, "运行学生语句的查询结果:");
|
||||
judgementStr = HtmlAppender.appendHtmlLine(judgementStr, "运行学生语句的查询结果:");
|
||||
judgementStr= printResult(answerListStu,judgementStr);
|
||||
} catch (SQLException e) {
|
||||
appendToFile(answerLogPath, "学生表执行验证语句"+selectSql+"时发生错误:" + e.getMessage());
|
||||
judgementStr = HtmlAppender.appendHtmlLine(judgementStr, "学生表执行验证语句"+selectSql+"时发生错误: " + e.getMessage());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
boolean isEquivalent =false;
|
||||
|
||||
if (answerListStu!=null&&answerListStu.size()>0){
|
||||
isEquivalent = compareResultsSelect(answerList, answerListStu);
|
||||
}
|
||||
//
|
||||
if (isEquivalent) {
|
||||
SourceAndText studentScorePojo = accumulateScoreAndLog(examMysqlKeywordList,total,answerLogPath,sql,totalKeyScore,score,answerId,scoreTotal,judgementStr);
|
||||
scoreTotal += studentScorePojo.getScore();
|
||||
judgementStr = studentScorePojo.getText();
|
||||
|
||||
} else {
|
||||
SourceAndText studentScorePojo= calculateTotalScoreRate(sql, examMysqlKeywordList,totalKeyScore,score,answerId,scoreTotal,judgementStr);
|
||||
scoreTotal += studentScorePojo.getScore();
|
||||
judgementStr = studentScorePojo.getText();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//删除临时创建的数据库databaseName
|
||||
@@ -917,6 +983,23 @@ public class IMysqlLocalServiceImpl implements IMysqlLocalService {
|
||||
sourceAndText.setText(judgementStr);
|
||||
return sourceAndText;
|
||||
}
|
||||
public static String convertAlertToSelectWhere(String sql) {
|
||||
if (sql == null || sql.trim().isEmpty()) return null;
|
||||
|
||||
// 正则提取表名和字段名(支持反引号)
|
||||
Pattern pattern = Pattern.compile(
|
||||
"ALTER\\s+TABLE\\s+`?(\\w+)`?\\s+MODIFY\\s+COLUMN\\s+`?(\\w+)`?",
|
||||
Pattern.CASE_INSENSITIVE);
|
||||
Matcher matcher = pattern.matcher(sql);
|
||||
|
||||
if (matcher.find()) {
|
||||
String tableName = matcher.group(1);
|
||||
String columnName = matcher.group(2);
|
||||
return "SHOW COLUMNS FROM " + tableName + " LIKE '" + columnName + "';";
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delMysqlConnect(List<String> tNames) {
|
||||
@@ -1526,49 +1609,57 @@ public class IMysqlLocalServiceImpl implements IMysqlLocalService {
|
||||
|
||||
return callStatements;
|
||||
}
|
||||
public static String convertInsertToSelect(String insertSql) {
|
||||
// 匹配表名和插入值
|
||||
Pattern pattern = Pattern.compile("INSERT INTO\\s+`?(\\w+)`?\\s*\\(.*?\\)\\s*VALUES\\s*(.+);?", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
|
||||
Matcher matcher = pattern.matcher(insertSql);
|
||||
|
||||
if (!matcher.find()) {
|
||||
throw new IllegalArgumentException("无法解析 INSERT 语句");
|
||||
}
|
||||
public static String convertInsertToSelect(String insertSql) {
|
||||
Pattern pattern = Pattern.compile(
|
||||
"INSERT INTO\\s+`?(\\w+)`?\\s*\\((.*?)\\)\\s*VALUES\\s*\\((.*?)\\)",
|
||||
Pattern.CASE_INSENSITIVE | Pattern.DOTALL
|
||||
);
|
||||
Matcher matcher = pattern.matcher(insertSql);
|
||||
if (!matcher.find()) throw new IllegalArgumentException("无法解析 INSERT 语句");
|
||||
|
||||
String tableName = matcher.group(1);
|
||||
String valuesPart = matcher.group(2);
|
||||
String columnStr = matcher.group(2);
|
||||
String valueStr = matcher.group(3);
|
||||
|
||||
// 匹配所有 ('xxx', ...) 或者 ('xxx')
|
||||
Pattern valPattern = Pattern.compile("\\((.*?)\\)");
|
||||
Matcher valMatcher = valPattern.matcher(valuesPart);
|
||||
String[] columns = columnStr.split("\\s*,\\s*");
|
||||
String[] values = valueStr.split("\\s*,\\s*");
|
||||
|
||||
Set<String> values = new LinkedHashSet<>();
|
||||
while (valMatcher.find()) {
|
||||
String valueStr = valMatcher.group(1).trim();
|
||||
// 如果是单字段插入,可能只有一个值
|
||||
if (!valueStr.contains(",")) {
|
||||
values.add(valueStr.replaceAll("^'|'$", "").replace("'", "''"));
|
||||
if (columns.length != values.length) {
|
||||
throw new IllegalArgumentException("字段和值数量不匹配");
|
||||
}
|
||||
|
||||
// 构造 column-value 映射
|
||||
Map<String, String> columnValueMap = new LinkedHashMap<>();
|
||||
for (int i = 0; i < columns.length; i++) {
|
||||
String col = columns[i].trim();
|
||||
String val = values[i].trim();
|
||||
|
||||
// 处理引号
|
||||
if ((val.startsWith("'") && val.endsWith("'")) || (val.startsWith("\"") && val.endsWith("\""))) {
|
||||
val = val.substring(1, val.length() - 1).replace("''", "'");
|
||||
}
|
||||
|
||||
columnValueMap.put(col, val);
|
||||
}
|
||||
|
||||
if (values.isEmpty()) {
|
||||
throw new IllegalArgumentException("未提取到插入值");
|
||||
// 构造 SELECT 语句
|
||||
StringBuilder sb = new StringBuilder("SELECT * FROM ");
|
||||
sb.append(tableName).append(" WHERE ");
|
||||
|
||||
List<String> conditions = new ArrayList<>();
|
||||
for (Map.Entry<String, String> entry : columnValueMap.entrySet()) {
|
||||
conditions.add(entry.getKey() + " = '" + entry.getValue().replace("'", "''") + "'");
|
||||
}
|
||||
|
||||
// 拼接验证 SQL
|
||||
StringBuilder selectBuilder = new StringBuilder("SELECT * FROM ");
|
||||
selectBuilder.append(tableName).append(" WHERE teamName IN (");
|
||||
int i = 0;
|
||||
for (String val : values) {
|
||||
if (i++ > 0) selectBuilder.append(", ");
|
||||
selectBuilder.append("'").append(val).append("'");
|
||||
}
|
||||
selectBuilder.append(")");
|
||||
|
||||
return selectBuilder.toString();
|
||||
sb.append(String.join(" AND ", conditions));
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 比较两个查询的结果
|
||||
*/
|
||||
|
Reference in New Issue
Block a user