Ktorm 集成例子

刚刚写的,顺手发上来,希望能帮上点忙。


import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
import org.ktorm.database.Database
import org.ktorm.database.SqlDialect
import org.ktorm.expression.QueryExpression
import org.ktorm.expression.SqlFormatter

// 下面的是 Hikari 封装

val dataSource by lazy {
    HikariDataSource(HikariConfig().apply {
        jdbcUrl = "jdbc:sinodbms-sqli://localhost:18411/healthms:SINODBMSSERVER=sinodb_demo"
        username = "sinodbms"
        password = "sinodbms"
        driverClassName = com.sinodbms.jdbc.IfxDriver::class.java.name
        maximumPoolSize = 10
        isAutoCommit = true
    })
}

val db by lazy {
    Database.connect(dataSource, dialect = SinodbDialect())
}

// 下面的是 Ktorm 方言支持

class SinodbDialect : SqlDialect {
    override fun createSqlFormatter(database: Database, beautifySql: Boolean, indentSize: Int): SqlFormatter {
        return SinodbFormatter(database, beautifySql, indentSize)
    }
}

class SinodbFormatter(database: Database, beautifySql: Boolean, indentSize: Int) :
    SqlFormatter(database, beautifySql, indentSize) {

    override fun writePagination(expr: QueryExpression) {
        newLine(Indentation.SAME)

        // sinodbms 的 FIRST 和 SKIP 子句不能用参数绑定
        if (expr.limit != null) {
            writeKeyword("FIRST ${expr.limit} ")
        }
        if (expr.offset != null) {
            writeKeyword("SKIP ${expr.offset} ")
        }
    }
}