You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
2.3 KiB
74 lines
2.3 KiB
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
|
|
|
|
const val ApiPathPrefix="/api"
|
|
/**
|
|
* 从0-9生成随机字符串
|
|
*/
|
|
fun randomNum(length:Int=8):String{
|
|
return List(length) {('0' .. '9').random()}.joinToString("")
|
|
}
|
|
|
|
/**
|
|
* md5加密字符串
|
|
*/
|
|
fun String.md5(): String {
|
|
val md = MessageDigest.getInstance("MD5")
|
|
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 <T : IntIdTable> updateComment(vararg tables:T){
|
|
transaction {
|
|
if (currentDialect is MysqlDialect) {
|
|
tables.forEach {
|
|
val tableClass = it::class.java
|
|
|
|
var statementList: List<String> = listOf<String>()
|
|
.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)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|