可用版本: Dev (3.21) | 最新 (3.20) | 3.19 | 3.18 | 3.17

这是一个实验性功能,因此可能会发生变化。使用风险自负!

监听替换器

适用于 ❌ 开源版   ✅ Express 版   ✅ 专业版   ✅ 企业版

这个替换器本身不会替换任何东西,但可以帮助调试其他替换器中的问题。例如,一个替换器可以删除冗余的 NOT(NOT(x)) 表达式

// The input condition
Condition c = ctx.parser().parseCondition("not not not book.id != 1");

// The replacer doing the replacement
Replacer r = Replacer.of(q -> {
    if (q instanceof QOM.Not n1 && n1.$arg1() instanceof QOM.Not n2)
        return n2.$arg1();
    else if (q instanceof QOM.Not n1 && n1.$arg1() instanceof QOM.Ne<?> n2)
        return n2.$arg1().eq((Field) n2.$arg2());
    else
        return q;
});

// A proxy to the above replacer, doing some logging
r = Replacers.listening(r, (p1, p2) -> System.out.println("Replacing: " + p1 + " => " + p2));

// The application of the replacement
QueryPart result = c.$replace(r);
System.out.println("Result: " + result);

现在,以上代码将打印深度优先替换算法应用于表达式树的每个替换,自下而上

Replacing: not (book.id <> 1) => book.id = 1
Replacing: not (not (book.id = 1)) => book.id = 1
Result: book.id = 1

反馈

您对此页面有任何反馈吗? 我们很乐意听取您的意见!

The jOOQ Logo