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.
csamsserver/src/MySQL.kt

47 lines
1.5 KiB

4 years ago
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 }
}
}
}
4 years ago
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)
4 years ago
initTable()
}
fun initTable(){
transaction {
val tableList= arrayOf(Users,UserTokens)
SchemaUtils.createMissingTablesAndColumns(*tableList)
updateComment(*tableList)
4 years ago
}
}