diff --git a/src/Dao.kt b/src/Dao.kt index f2411a4..0ba8d10 100644 --- a/src/Dao.kt +++ b/src/Dao.kt @@ -8,22 +8,16 @@ import org.jetbrains.exposed.sql.Column import org.jetbrains.exposed.sql.`java-time`.datetime import java.time.LocalDateTime -/** - * 用户 - */ + +@TableComment("用户") object Users: IntIdTable(){ - /** - * 学号 - */ + @TableComment("学号") val studentId:Column = varchar(name="student_id",length = 8).uniqueIndex() - /** - * 姓名 - */ + + @TableComment("姓名") val name:Column = varchar(name="name",length = 10) - /** - * 密码,hash加密 - */ + @TableComment("密码") val password:Column = varchar(name="password",length = 32) } @@ -34,29 +28,22 @@ class User(id:EntityID):IntEntity(id){ var password by Users.password } -/** - * 用户授权令牌 - */ +@TableComment("用户授权令牌") object UserTokens: IntIdTable(){ - /** - * 授权学号 - */ + + @TableComment("授权学号") val studentId:Column = reference("student_id",Users.studentId) - /** - * 令牌 - */ + + @TableComment("令牌") val token:Column = varchar(name="token",length = 32) - /** - * 授权ip地址 - */ + + @TableComment("授权ip地址") val ip:Column = varchar(name="ip",length = 32) - /** - * 令牌创建时间 - */ + + @TableComment("令牌创建时间") val createTime:Column = datetime("create_time").default(LocalDateTime.now()) - /** - * 授权设备 - */ + + @TableComment("授权设备") val device:Column = varchar(name="device",length = 256) } diff --git a/src/MySQL.kt b/src/MySQL.kt index c407b08..04bca2c 100644 --- a/src/MySQL.kt +++ b/src/MySQL.kt @@ -40,7 +40,8 @@ fun Application.MySQL(testing: Boolean = false){ fun initTable(){ transaction { - SchemaUtils.createMissingTablesAndColumns(Users) - SchemaUtils.createMissingTablesAndColumns(UserTokens) + val tableList= arrayOf(Users,UserTokens) + SchemaUtils.createMissingTablesAndColumns(*tableList) + updateComment(*tableList) } } \ No newline at end of file diff --git a/src/Util.kt b/src/Util.kt index e5c1549..f6ffa00 100644 --- a/src/Util.kt +++ b/src/Util.kt @@ -1,5 +1,10 @@ package com.gyf.csams +import org.jetbrains.exposed.dao.id.IntIdTable +import org.jetbrains.exposed.sql.Column +import org.jetbrains.exposed.sql.transactions.transaction +import org.jetbrains.exposed.sql.vendors.MysqlDialect +import org.jetbrains.exposed.sql.vendors.currentDialect import java.math.BigInteger import java.security.MessageDigest @@ -19,3 +24,50 @@ fun String.md5(): String { return BigInteger(1, md.digest(toByteArray())).toString(16).padStart(32, '0') } +@Target(AnnotationTarget.CLASS,AnnotationTarget.FIELD) +annotation class TableComment(val comment:String) + +/** + * 表、表字段添加注释 + * + * @param T + * @param tables + * @return + */ +fun updateComment(vararg tables:T){ + transaction { + if (currentDialect is MysqlDialect) { + tables.forEach { + val tableClass = it::class.java + + var statementList: List = listOf() + .run { + val comment = tableClass.getAnnotation(TableComment::class.java)?.comment ?: "" + plus("ALTER TABLE ${it.tableName} COMMENT '$comment'") + } + tableClass.declaredFields.filter { it.name != "INSTANCE" } + .forEach { + it.isAccessible = true + val column = it.get(null) + if (column is Column<*>) { + val ddl = column.ddl + if (ddl.size == 1) { + val comment = it.getAnnotation(TableComment::class.java)?.comment ?: "" + statementList = statementList.plus( + ddl[0].plus(" COMMENT '$comment'") + .replace("ADD", "MODIFY") + .replace("PRIMARY KEY", "") + ) + } + } + } + + statementList.forEach { + exec(it) + } + } + } + } +} + + diff --git a/test/ApplicationTest.kt b/test/ApplicationTest.kt index 3c8076a..90807a7 100644 --- a/test/ApplicationTest.kt +++ b/test/ApplicationTest.kt @@ -62,17 +62,28 @@ class ApplicationTest { } + + @Test + fun testUpdateComment(){ + initApp { +// updateComment(Users,UserTokens) + transaction { + UserToken.new { + studentId = "6666" + token = "22" + ip = "sdf" + device = "hahha" + } + } + } + } + @Test fun testEntryId() { initApp { transaction { -// User.new { -// studentId="20210101" -// name="222" -// password="12345678".md5() -// } -// alter table Users comment '123'; - exec("alter table Users comment '6666'") + val c=updateComment(UserTokens) + println(c) } } } @@ -88,6 +99,7 @@ class ApplicationTest { @Test fun documentGeneration() { initApp { + val fileName="数据库设计文档" //生成配置 val engineConfig = EngineConfig.builder() //生成文件路径 @@ -95,11 +107,11 @@ class ApplicationTest { //打开目录 .openOutputDir(true) //文件类型 - .fileType(EngineFileType.HTML) + .fileType(EngineFileType.WORD) //生成模板实现 .produceType(EngineTemplateType.freemarker) //自定义文件名称 - .fileName("777").build(); + .fileName(fileName).build(); //忽略表 // val ignoreTableName = ArrayList() @@ -123,7 +135,7 @@ class ApplicationTest { //配置 val config: Configuration = Configuration.builder() //版本 .version("1.0.0") //描述 - .description("数据库设计文档生成") //数据源 + .description("${fileName}生成") //数据源 .dataSource(MySQL.getInstance(null).dataSource) //生成配置 .engineConfig(engineConfig) //生成配置 .produceConfig(processConfig) @@ -136,3 +148,4 @@ class ApplicationTest { } } +