sql语法兼容问题修复
This commit is contained in:
@@ -100,22 +100,42 @@ public class InterviewInvitationServiceImpl implements InterviewInvitationServic
|
|||||||
log.info("interview_invitation 表创建成功(序列自增)!");
|
log.info("interview_invitation 表创建成功(序列自增)!");
|
||||||
} else {
|
} else {
|
||||||
log.info("interview_invitation 表结构正常,检查新增列...");
|
log.info("interview_invitation 表结构正常,检查新增列...");
|
||||||
// 为已有数据库添加新列(使用 IF NOT EXISTS,兼容 PostgreSQL / HighGo)
|
// 为已有数据库添加新列:先查 information_schema 判断列是否存在,存在则跳过
|
||||||
try {
|
// 不使用 IF NOT EXISTS / DO $$ 语法,因为 Druid 1.2.23 StatFilter 无法解析这些 PG 扩展语法
|
||||||
stmt.execute("ALTER TABLE interview_invitation ADD COLUMN IF NOT EXISTS company_name varchar(200) NULL");
|
addColumnIfNotExists(stmt, "interview_invitation", "company_name", "varchar(200) NULL");
|
||||||
} catch (Exception e) { log.warn("添加 company_name 列失败: {}", e.getMessage()); }
|
addColumnIfNotExists(stmt, "interview_invitation", "job_name", "varchar(200) NULL");
|
||||||
try {
|
addColumnIfNotExists(stmt, "interview_invitation", "interviewer_name", "varchar(100) NULL");
|
||||||
stmt.execute("ALTER TABLE interview_invitation ADD COLUMN IF NOT EXISTS job_name varchar(200) NULL");
|
|
||||||
} catch (Exception e) { log.warn("添加 job_name 列失败: {}", e.getMessage()); }
|
|
||||||
try {
|
|
||||||
stmt.execute("ALTER TABLE interview_invitation ADD COLUMN IF NOT EXISTS interviewer_name varchar(100) NULL");
|
|
||||||
} catch (Exception e) { log.warn("添加 interviewer_name 列失败: {}", e.getMessage()); }
|
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("初始化表失败: {}", e.getMessage());
|
log.error("初始化表失败: {}", e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 幂等添加列:先查 information_schema 判断列是否存在,不存在才执行 ALTER TABLE ADD COLUMN
|
||||||
|
* 不使用 IF NOT EXISTS / DO $$ 语法,避免 Druid StatFilter mergeSql 报错
|
||||||
|
*/
|
||||||
|
private void addColumnIfNotExists(Statement stmt, String tableName, String columnName, String columnDef) {
|
||||||
|
try {
|
||||||
|
String checkSql = "SELECT COUNT(*) FROM information_schema.columns WHERE table_name='" + tableName + "' AND column_name='" + columnName + "'";
|
||||||
|
java.sql.ResultSet rs = stmt.executeQuery(checkSql);
|
||||||
|
boolean exists = false;
|
||||||
|
if (rs.next()) {
|
||||||
|
exists = rs.getInt(1) > 0;
|
||||||
|
}
|
||||||
|
rs.close();
|
||||||
|
if (!exists) {
|
||||||
|
String alterSql = "ALTER TABLE " + tableName + " ADD COLUMN " + columnName + " " + columnDef;
|
||||||
|
stmt.execute(alterSql);
|
||||||
|
log.info("成功添加列: {}.{}", tableName, columnName);
|
||||||
|
} else {
|
||||||
|
log.info("列已存在,跳过: {}.{}", tableName, columnName);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("添加列 {}.{} 失败: {}", tableName, columnName, e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<InterviewInvitation> getInterviewInvitationList(InterviewInvitation interviewInvitation) {
|
public List<InterviewInvitation> getInterviewInvitationList(InterviewInvitation interviewInvitation) {
|
||||||
return interviewInvitationMapper.getInterviewInvitationList(interviewInvitation);
|
return interviewInvitationMapper.getInterviewInvitationList(interviewInvitation);
|
||||||
|
|||||||
Reference in New Issue
Block a user