From b951e027403474544f4ad1047843b5b272df21bd Mon Sep 17 00:00:00 2001 From: AiKrai Date: Fri, 21 Mar 2025 15:32:34 +0800 Subject: [PATCH] 1 --- .../vertx/db/migration/DefaultDbMigration.kt | 3 ++ .../migration/SqlAnnotationMapperGenerator.kt | 51 +++++++++++++++++-- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/vertx-fw/src/main/kotlin/org/aikrai/vertx/db/migration/DefaultDbMigration.kt b/vertx-fw/src/main/kotlin/org/aikrai/vertx/db/migration/DefaultDbMigration.kt index 50d4560..983ec90 100644 --- a/vertx-fw/src/main/kotlin/org/aikrai/vertx/db/migration/DefaultDbMigration.kt +++ b/vertx-fw/src/main/kotlin/org/aikrai/vertx/db/migration/DefaultDbMigration.kt @@ -175,5 +175,8 @@ class DefaultDbMigration : DbMigration { SqlMigrationGenerator.setModelPath("${pathToResources}/${migrationPath}/${modelPath}") SqlMigrationGenerator.setModelSuffix(modelSuffix) SqlMigrationGenerator.setLogToSystemOut(logToSystemOut) + + // 同时设置SqlAnnotationMapperGenerator的日志配置 + SqlAnnotationMapperGenerator.setLogToSystemOut(logToSystemOut) } } \ No newline at end of file diff --git a/vertx-fw/src/main/kotlin/org/aikrai/vertx/db/migration/SqlAnnotationMapperGenerator.kt b/vertx-fw/src/main/kotlin/org/aikrai/vertx/db/migration/SqlAnnotationMapperGenerator.kt index 8dedac7..91f0da9 100644 --- a/vertx-fw/src/main/kotlin/org/aikrai/vertx/db/migration/SqlAnnotationMapperGenerator.kt +++ b/vertx-fw/src/main/kotlin/org/aikrai/vertx/db/migration/SqlAnnotationMapperGenerator.kt @@ -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)"