简体   繁体   English

jOOQ:重用/复制查询

[英]jOOQ: reusing / copying queries

In order to avoid re-creating the same part of a dynamic query over and over again, I was going to build the main part once and then reuse this part in different parts of the application.为了避免一遍又一遍地重新创建动态查询的同一部分,我将构建一次主要部分,然后在应用程序的不同部分重用这部分。 Since building the query is somewhat involved (see question jOOQ: best way to get aliased fields (from #as(alias, aliasFunction)) for one aspect of why this is the case), this should benefit performance...由于构建查询有点涉及(请参阅问题jOOQ: best way to get aliased fields (from #as(alias, aliasFunction))的一个方面),这应该有利于性能...

Unfortunately, I've had to realize that the different "steps" in the builder pattern don't return amended copies, but modify underlying state.不幸的是,我不得不意识到构建器模式中的不同“步骤”不会返回修改后的副本,而是修改底层状态。 Thus, I've looked for a way to create an immutable copy of a specific "step" (eg SelectWhereStep ) from which to initialize and subsequently amend a (Select-)query each time I need it.因此,我一直在寻找一种方法来创建特定“步骤”(例如SelectWhereStep )的不可变副本,每次我需要它时都会从中初始化并随后修改(Select-)查询。 Unfortunately, I haven't been able to identify any ("legal") way of achieving this.不幸的是,我无法确定实现这一目标的任何(“合法”)方式。

Can it be done?可以做到吗? If not, what's the best alternative?如果没有,最好的选择是什么?

Unfortunately, I've had to realize that the different "steps" in the builder pattern don't return amended copies, but modify underlying state不幸的是,我不得不意识到构建器模式中的不同“步骤”不返回修改后的副本,而是修改底层状态

This is indeed a very unfortunate limitation in the current design of the jOOQ DSL / model APIs .这确实是jOOQ DSL/模型 API当前设计中的一个非常不幸的限制。 The DSL API should be immutable whereas the model API is the mutable one. DSL API应该是不可变的,而模型 API 是可变的。 But this isn't always the case as you've noticed.但正如您所注意到的,情况并非总是如此。

There is currently no way to clone a jOOQ query as mostly, this is not really necessary.目前没有办法像大多数人那样克隆 jOOQ 查询,这并不是真正必要的。 Regardless if you're operating with a mutable or immutable API, the cleanest way to achieve what you want to do is to compose jOOQ queries in a functional way.无论您使用的是可变 API 还是不可变 API,实现您想要做的事情的最简洁方法是以函数方式编写 jOOQ 查询。 Ie instead of即代替

I was going to build the main part once and then reuse this part in different parts of the application我打算构建一次主要部分,然后在应用程序的不同部分重用这部分

You could just do the same thing in a functional way rather than an imperative way.你可以用函数式的方式而不是命令式的方式来做同样的事情。 Rather than assigning "the main part" to some local or global variable, you could make a theMainPart() function that returns that part on the fly.与其将“主要部分”分配给某个局部或全局变量,您还可以创建一个theMainPart()函数来即时返回该部分。 More about this in this blog post . 在这篇博文中详细介绍了这一点

As a side-note, when writing dynamic SQL, there are usually better ways than referencing the XYZStep types directly . 附带说明一下,在编写动态 SQL 时,通常有比直接引用XYZStep类型更好的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM