package com.gyf.csams import com.zaxxer.hikari.HikariConfig import com.zaxxer.hikari.HikariDataSource import io.ktor.application.* import org.jetbrains.exposed.sql.Database import org.jetbrains.exposed.sql.SchemaUtils import org.jetbrains.exposed.sql.transactions.transaction import javax.sql.DataSource class MySQL private constructor(val dataSource: DataSource? = null) { companion object { @Volatile private var instance: MySQL? = null fun getInstance(dataSource: DataSource?) = instance ?: synchronized(this) { instance ?: MySQL(dataSource).also { instance = it } } } } fun Application.MySQL(testing: Boolean = false){ val config = HikariConfig().apply { jdbcUrl = environment.config.property("ktor.deployment.mysql.jdbcUrl").getString() driverClassName = environment.config.property("ktor.deployment.mysql.driverClassName").getString() username = environment.config.property("ktor.deployment.mysql.username").getString() password = environment.config.property("ktor.deployment.mysql.password").getString() maximumPoolSize = 10 } val dataSource = HikariDataSource(config) Database.connect(dataSource) MySQL.getInstance(dataSource) initTable() } fun initTable(){ transaction { val tableList= arrayOf(Users,UserTokens) SchemaUtils.createMissingTablesAndColumns(*tableList) updateComment(*tableList) } }