【问题标题】:How does Exposed handle bind parameters?Exposed 如何处理绑定参数?
【发布时间】:2021-08-11 16:28:08
【问题描述】:

大家好,大家好

我最近了解到the importance of bind parameters in sql queries for optimization

我在 DSL 设置中使用 kotlin-exposed,当我像这样关注 basic query example 时:

TExportJobs
    .select { TExportJobs.id eq request.id.toInt() }.firstOrNull()

打印出来的查询没有使用绑定参数:

SELECT export_jobs.id, ... FROM export_jobs WHERE export_jobs.id = 4

是否在后台使用绑定参数公开?我的设置可能格式不正确吗?

这是我的示例表对象:

object TExportJobs: IntIdTable("export_jobs") {
    val userEmail = varchar("user_email", 128).index()
}

【问题讨论】:

    标签: sql kotlin kotlin-exposed


    【解决方案1】:

    找到了!

    sqlLogger 接口 (org.jetbrains.exposed.sql.SqlLogger) 使用 statementContext (org.jetbrains.exposed.sql.statements.StatementContext) 中的 expandArgs 方法替换 '?'参数。

    所以它是在引擎盖下完成的

    buildString {
            val quoteStack = Stack<Char>()
            var lastPos = 0
            for (i in 0..sql.length - 1) {
                val char = sql[i]
                if (char == '?') {
                    if (quoteStack.isEmpty()) {
                        append(sql.substring(lastPos, i))
                        lastPos = i + 1
                        val (col, value) = iterator.next()
                        append(col.valueToString(value))
                    }
                    continue
                }
    
                if (char == '\'' || char == '\"') {
                    if (quoteStack.isEmpty()) {
                        quoteStack.push(char)
                    } else {
                        val currentQuote = quoteStack.peek()
                        if (currentQuote == char)
                            quoteStack.pop()
                        else
                            quoteStack.push(char)
                    }
                }
            }
    
            if (lastPos < sql.length)
                append(sql.substring(lastPos))
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-02
      • 1970-01-01
      • 2017-07-18
      相关资源
      最近更新 更多