1
This commit is contained in:
parent
530f682f17
commit
b951e02740
@ -175,5 +175,8 @@ class DefaultDbMigration : DbMigration {
|
||||
SqlMigrationGenerator.setModelPath("${pathToResources}/${migrationPath}/${modelPath}")
|
||||
SqlMigrationGenerator.setModelSuffix(modelSuffix)
|
||||
SqlMigrationGenerator.setLogToSystemOut(logToSystemOut)
|
||||
|
||||
// 同时设置SqlAnnotationMapperGenerator的日志配置
|
||||
SqlAnnotationMapperGenerator.setLogToSystemOut(logToSystemOut)
|
||||
}
|
||||
}
|
||||
@ -11,6 +11,25 @@ import kotlin.reflect.KClass
|
||||
class SqlAnnotationMapperGenerator {
|
||||
|
||||
companion object {
|
||||
// 是否输出日志到控制台
|
||||
private var LOG_TO_SYSTEM_OUT = true
|
||||
|
||||
/**
|
||||
* 设置是否输出日志到控制台
|
||||
*/
|
||||
fun setLogToSystemOut(log: Boolean) {
|
||||
LOG_TO_SYSTEM_OUT = log
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录日志信息
|
||||
*/
|
||||
private fun log(message: String) {
|
||||
if (LOG_TO_SYSTEM_OUT) {
|
||||
println("DbMigration> $message")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从实体类获取SQL信息
|
||||
* @param entityClass 实体类
|
||||
@ -165,7 +184,7 @@ class SqlAnnotationMapperGenerator {
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
// 忽略获取默认值失败的情况
|
||||
println("获取枚举默认值失败: ${e.message}")
|
||||
log("获取枚举默认值失败: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
@ -401,12 +420,12 @@ class SqlAnnotationMapperGenerator {
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
// 处理单个索引注解失败时记录错误但继续处理其他索引
|
||||
println("处理索引注解时出错: ${e.message}")
|
||||
log("处理索引注解时出错: ${e.message}")
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
// 处理索引注解整体失败时记录错误
|
||||
println("处理表 ${sqlInfo.tableName} 的索引注解时出错: ${e.message}")
|
||||
log("处理表 ${sqlInfo.tableName} 的索引注解时出错: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
@ -456,6 +475,7 @@ class SqlAnnotationMapperGenerator {
|
||||
it.annotationClass.qualifiedName == mapper.enumValueMapping!!.annotationClass.qualifiedName
|
||||
}
|
||||
}
|
||||
|
||||
if (enumValueMethod != null) {
|
||||
// 使用EnumValue标注的方法获取枚举值
|
||||
enumConstants.map { enumValueMethod.invoke(it).toString() }
|
||||
@ -485,10 +505,31 @@ class SqlAnnotationMapperGenerator {
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
// 忽略枚举值提取失败的情况
|
||||
println("提取枚举值失败: ${e.message}")
|
||||
log("提取枚举值失败: ${e.message}")
|
||||
}
|
||||
}
|
||||
"VARCHAR(50)"
|
||||
|
||||
// 根据枚举值的类型决定SQL类型
|
||||
if (columnInfo != null && columnInfo.enumValues?.isNotEmpty() == true) {
|
||||
val firstValue = columnInfo.enumValues!!.first()
|
||||
val enumType = when {
|
||||
// 尝试将值转换为数字,判断是否是数值型枚举
|
||||
firstValue.toIntOrNull() != null -> "INTEGER"
|
||||
firstValue.toLongOrNull() != null -> "BIGINT"
|
||||
firstValue.toDoubleOrNull() != null -> "DOUBLE PRECISION"
|
||||
// 如果值很短,使用CHAR
|
||||
firstValue.length <= 1 -> "CHAR(1)"
|
||||
// 找出最长的枚举值,并基于此设置VARCHAR长度
|
||||
else -> {
|
||||
val maxLength = columnInfo.enumValues!!.maxBy { it.length }.length
|
||||
val safeLength = maxLength + 10 // 增加一些余量
|
||||
"VARCHAR($safeLength)"
|
||||
}
|
||||
}
|
||||
enumType
|
||||
} else {
|
||||
"VARCHAR(50)" // 默认回退
|
||||
}
|
||||
}
|
||||
|
||||
else -> "VARCHAR(255)"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user