如何在jOOQ中使用Scala的字符串插值? [英] How to use Scala's String Interpolation in jOOQ?
问题描述
我想在Scala中使用jOOQ的字符串插值功能,例如resultQuery"SELECT * FROM objects"
:
I would like to use the string interpolation feature of jOOQ in Scala, for example resultQuery"SELECT * FROM objects"
:
// setup connection
val con = DriverManager.getConnection(url, userName, password)
// create DSLContext
val dsl = DSL.using(con, SQLDialect.POSTGRES_9_4)
// normal use of DSLContext
dsl.resultQuery("SELECT * FROM objects")
// intented use of string interpolation
val q = resultQuery"SELECT * FROM objects"
val result = q.fetch()
println(result)
运行此代码(没有任何外部配置等)会导致以下异常:
Running this code (without any outside configuration and the like) results in the following exception:
Exception in thread "main" org.jooq.exception.DetachedException: Cannot execute query. No Connection configured
at org.jooq.impl.AbstractQuery.execute(AbstractQuery.java:312)
at org.jooq.impl.AbstractResultQuery.fetch(AbstractResultQuery.java:305)
...
SQLInterpolation
似乎正在使用默认设置.我试图通过将DSLContext
和Configuration
设置为隐式来提供这些设置,但是我仍然收到相同的异常:
It seems that the SQLInterpolation
is using the default settings. I tried to provide these settings by setting DSLContext
as well as Configuration
as implicits but I still received the same exception:
implicit val dsl = DSL.using(con, SQLDialect.POSTGRES_9_4)
implicit val config = new DefaultConfiguration().derive(con)
.derive(SQLDialect.POSTGRES_9_4)
如何正确地将我的设置(连接,方言等)提供给字符串插值?
How do I correctly provide my settings (connection, dialect, etc.) to the string interpolation?
推荐答案
使用字符串插值创建的ResultQuery
对象不是附加的",即不能单独执行.
The ResultQuery
object that you've created using string interpolation is not "attached", i.e. it cannot be executed on its own.
换句话说,您应该像这样运行查询,
In other words, you should run the query like this, instead:
val result = dsl.fetch(q)
这篇关于如何在jOOQ中使用Scala的字符串插值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!