有没有办法用Kotlin的Exposed库运行原始sql

pon*_*tic 5 kotlin kotlin-exposed

我正在尝试运行一些特定于postgres的sql,并希望在Exposed中重用事务管理.

pon*_*tic 9

在Kiskae的回答的帮助下,我能够运行原始sql:

transaction {
     val conn = TransactionManager.current().connection
     val statement = conn.createStatement()
     val query = "REFRESH MATERIALIZED VIEW someview"
     statement.execute(query)
}
Run Code Online (Sandbox Code Playgroud)

  • 过时的答案。“createStatement()”在“exubed 0.31.1”上不存在。 (4认同)

Mar*_*bst 7

这是带有参数的示例:

transaction(database) {
   val conn = TransactionManager.current().connection
   val query = "update user set name = ? where id = ?";
   val statement = conn.prepareStatement(query, false)
   statement.fillParameters(listOf(Pair(VarCharColumnType(), "Laura"),
      Pair(IntegerColumnType(), 3)));
   statement.executeUpdate()
}
Run Code Online (Sandbox Code Playgroud)