每次调用时查询中的重复ORDER BY数据和条件

时间:2016-04-18 17:40:19

标签: jooq

我之前和很久以前都注意到这一点已经停止让我的Condition成为私人静态决赛,但是我在这个问题上摸不着头脑,因为它有点问题,特别是如果我正在调试一个查询。这是一个例子(我之前在3.3.x中看过这个,虽然我目前在3.7.3):

        final SelectJoinStep<Record13<String, String, String, String, String, BigDecimal, BigDecimal, BigDecimal, String, BigDecimal, String, String, Byte>> query = getSelect()
                .from(getFrom(coConditions,
                        ConditionUtils.buildCondition(cortConditions, removeCortBySpeciality),
                        ConditionUtils.buildCondition(cosConditions, removeCosBySensitivity), tuConditions,
                        uaConditions, enableBlackMajik));
        final SortField<?>[] orders = new SortField[] {DSL.inline(Integer.valueOf(2)).asc(),
                DSL.inline(Integer.valueOf(1)).asc(), DSL.inline(Integer.valueOf(6)).asc()};

        if (cosConditions.isPresent()) {
            User.logger.error(builder.renderInlined(query.where(cosConditions.get()).orderBy(orders)));
            return query.where(cosConditions.get()).orderBy(orders);
        }

        User.logger.error(builder.renderInlined(query.orderBy(orders)));
        return query.orderBy(orders);

以下是记录器调用中显示ORDER BY

的SQL代码段
order by 
  2 asc, 
  1 asc, 
  6 asc

这是发送到SQL服务器的ORDER BY的SQL片段:

order by 
  2 asc, 
  1 asc, 
  6 asc, 
  2 asc, 
  1 asc, 
  6 asc

现在,为了进一步显示乐趣,这里是另一个代码片段,仅用于演示问题:

        User.logger.error(builder.renderInlined(query.orderBy(orders)));
        User.logger.error(builder.renderInlined(query.orderBy(orders)));
        User.logger.error(builder.renderInlined(query.orderBy(orders)));
        return query.orderBy(orders);

第一次记录器调用:

order by 
  2 asc, 
  1 asc, 
  6 asc

第二次记录器调用:

order by 
  2 asc, 
  1 asc, 
  6 asc, 
  2 asc, 
  1 asc, 
  6 asc

第三次记录器呼叫:

order by 
  2 asc, 
  1 asc, 
  6 asc, 
  2 asc, 
  1 asc, 
  6 asc, 
  2 asc, 
  1 asc, 
  6 asc

DB看到了什么:

order by 
  2 asc, 
  1 asc, 
  6 asc, 
  2 asc, 
  1 asc, 
  6 asc, 
  2 asc, 
  1 asc, 
  6 asc, 
  2 asc, 
  1 asc, 
  6 asc

现在,我实际上已经注意到了Condition之前的这种行为,每次我调用Condition时,它都被复制到我构建条件的位置并且仅参考他们一次(静态的,为了一些乐趣)。有谁知道我为什么会看到这种行为(看看有条件的行为)?

1 个答案:

答案 0 :(得分:1)

这是由于jOOQ中的API设计缺陷导致jOOQ已经存在了很长一段时间,并且仅使用jOOQ 4.0修复(#2198)。

通常,您不能安全地假设DSL API是不可变的(尽管它应该是)。因此,您对orderBy()的连续调用实际上每次都会添加ORDER BY列集,但您只打印第一列,因此您看不到。

此处解释了当前行为(滚动到“可变性”):

http://www.jooq.org/doc/latest/manual/sql-building/sql-statements/dsl-and-non-dsl

相关问题