我正在尝试运行一些特定于 postgres 的 sql,并希望在 Exposed 中重用事务管理。
问问题
12750 次
5 回答
12
Exposed 有Transaction.exec(String)
可能做你想要的方法。见https://github.com/JetBrains/Exposed/blob/master/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/mysql/MysqlTests.kt
于 2016-12-06T00:19:48.333 回答
10
在 Kiskae 的回答的帮助下,我能够运行原始 sql:
transaction {
val conn = TransactionManager.current().connection
val statement = conn.createStatement()
val query = "REFRESH MATERIALIZED VIEW someview"
statement.execute(query)
}
于 2016-12-11T23:39:59.920 回答
3
import java.sql.*
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.*
fun getAccId(db : Database, acc_domain: String) {
transaction(db) {
addLogger(StdOutSqlLogger)
var acc_id: Long = 0
exec("select id from accounts where domain = '${acc_domain}'") { rs ->
while(rs.next()) {
acc_id = rs.getLong("id")
}
}
println("Acc Id is ${acc_id}")
}
}
val database = Database.connect(datasource)
getAccId(database,"mytest.test.io")
于 2020-06-16T10:49:03.140 回答
3
这是一个带参数的示例:
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()
}
于 2020-08-17T13:14:42.930 回答
1
您可以创建一个简单的辅助函数,例如:
String.execAndMap(transform : (ResultSet) -> T) : List { val 结果 = arrayListOf() TransactionManager.current().exec(this) { rs -> 而(rs.next()){ 结果 += 变换(rs) } } 返回结果 } "select u.name, c.name from user u inner join city c where blah blah".execAndMap { rs -> rs.getString("u.name") 到 rs.getString("c.name") }
取自:https ://github.com/JetBrains/Exposed/wiki/FAQ#q-is-it-possible-to-use-native-sql--sql-as-a-string
于 2020-02-20T03:52:08.207 回答