JOOQ FLUENT API中间结果是不可变的吗?

发布于 2025-01-26 22:49:36 字数 461 浏览 1 评论 0 原文

如果我从流利的API查询构建器中保存中间结果,以后会调用它吗? (换句话说,中间结果是不可变的吗?)例如,

final var a = create.select().from(AUTHOR);
final var before1920 = a.where(AUTHOR.YEAR_OF_BIRTH.gt(1920).fetch();
final var authorSql = a.where(AUTHOR.FIRST_NAME.eq("Paulo")).getSql();

我可以确信 a 未通过以后的语句修改吗?

如果不是这种情况,我的主要愿望是有时将查询中的SQL记录到审核日志或调试日志。我可以在调用 fetch*()方法之前或之后安全地调用 getsql()吗?

If I save an intermediate result from the fluent API query builder, will later calls modify it? (In other words, are the intermediate results immutable?) For example,

final var a = create.select().from(AUTHOR);
final var before1920 = a.where(AUTHOR.YEAR_OF_BIRTH.gt(1920).fetch();
final var authorSql = a.where(AUTHOR.FIRST_NAME.eq("Paulo")).getSql();

Can I be confident that a was not modified by the later statements?

If this is not the case, my main desire is to sometimes log the SQL from queries either to audit logs or debugging logs. Can I safely call getSQL() before or after calling fetch*() methods?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北音执念 2025-02-02 22:49:36

JOOQ DSL的可变性

为JOOQ 3.16,是的,这些特定的方法可暴露出可变的行为,因此您可以像这样动态构建查询。另请参见,暗示了这种可能在(远)未来的变化,而有利于更可预测的不可预测的DSL API。

但是,即使可以利用这一点,您也不应构建这样。使用推荐方法:

Condition condition = AUTHOR.YEAR_OF_BIRTH.gt(1920);

if (...)
    condition = condition.and(AUTHOR.FIRST_NAME.eq("Paulo"));

create.select()
      .from(AUTHOR)
      .where(condition)
      .fetch();

这对您的实际问题无济于事,但是它回答了您问的问题的一部分,即,您不应该依靠这种可变的行为。

具体问题

我可以相信A不会被以后的陈述所修改吗?

不,你不能。 A 已修改,请参见有关Mustability 的部分。

我的主要愿望是有时将查询中的SQL记录到审核日志或调试日志

您可以使用内置的执行日志记录,将所有查询记录到 debug 级别。

我可以安全地调用 getsql()在调用 fetch*()方法之前或之后

是的,这些电话是安全的。这也是上述执行记录也是如此,以应用格式化,将绑定值插入登录目的,等等。

Mutability of the jOOQ DSL

As of jOOQ 3.16, yes, those particular methods expose mutable behaviour, so you can dynamically construct the query as you did. See also the jOOQ manual's section about mutability, which hints at this possibly changing in the (far) future, in favour of more predictable immutable DSL APIs.

But even if this can be exploited, you should not construct dynamic SQL queries like this. Use the recommended approach:

Condition condition = AUTHOR.YEAR_OF_BIRTH.gt(1920);

if (...)
    condition = condition.and(AUTHOR.FIRST_NAME.eq("Paulo"));

create.select()
      .from(AUTHOR)
      .where(condition)
      .fetch();

This doesn't help you with your actual question, but it answers parts of the question you have asked, namely, you shouldn't rely on this mutable behaviour.

Specific questions

Can I be confident that a was not modified by the later statements?

No, you cannot. a was modified, see the section about mutability.

my main desire is to sometimes log the SQL from queries either to audit logs or debugging logs

You can either use the built in execute logging, which logs all queries to the DEBUG level.

Can I safely call getSQL() before or after calling fetch*() methods?

Yes, those calls are safe. That's what the above execute logging does, too, in order to apply formatting, inlining of bind values for logging purposes, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文