简体   繁体   English

表达式过滤器解析器

[英]Parser for Expression Filter

I have table column with expression filter applied.我有应用了表达式过滤器的表列。 Is there any existing parser that would allow me to parse this column?是否有任何现有的解析器可以让我解析此列? the column can contain simple conditions such as该列可以包含简单的条件,例如

PROPERTY = 'name' AND PROPERTY2 = 'something else'

or more advanced like或更高级的

(PROPERTY IN ('foo', 'bar') 
  AND (PROPERTY IN ('foo1', 'bar2') OR OTHER_PROPERTY IN ('etc'))

It would totally suffice if it would return list of properties in expression and either value (in case of = operator) or list of all values (even if one property appears more than once in expression).如果它返回表达式中的属性列表和值(在 = 运算符的情况下)或所有值的列表(即使一个属性在表达式中出现多次),这完全足够了。 Is there such parser available or do I have to write it myself?有这样的解析器可用还是我必须自己写?

I don't know how you are developing your project. 我不知道您是如何开发项目的。 But if your are using hibernate you can use criteria queries. 但是,如果您使用的是休眠模式,则可以使用条件查询。 Cirteria queries Cirteria查询

You could usejOOQ's parser .你可以使用jOOQ 的 parser Given this requirement:鉴于此要求:

It would totally suffice if it would return list of properties in expression and either value (in case of = operator) or list of all values (even if one property appears more than once in expression)如果它返回表达式中的属性列表和值(在 = 运算符的情况下)或所有值的列表(即使一个属性在表达式中出现多次),这完全足够了

You could run this piece of code:你可以运行这段代码:

Map<Field<?>, List<QueryPart>> result =
DSL.using(SQLDialect.DEFAULT)
   .parser()
   // Parse the SQL string to a jOOQ expression tree
   .parseCondition(
       """
       (PROPERTY IN ('foo', 'bar')
           AND (PROPERTY IN ('foo1', 'bar2') OR OTHER_PROPERTY IN ('etc')))
       """)
   // Traverse the jOOQ expression tree using Traversers and Collectors
   // You can also traverse the tree using imperative APIs, of course
   .$traverse(Traversers.collecting(
       Collectors.filtering(p -> p instanceof InList 
                              || p instanceof CompareCondition,
           Collectors.groupingBy(p ->
                 p instanceof InList<?> i
               ? i.$arg1()
               : p instanceof CompareCondition<?> c
               ? c.$arg1()
               : null,
               Collectors.mapping(p ->
                     p instanceof InList<?> i
                   ? i.$arg2()
                   : p instanceof CompareCondition<?> c
                   ? c.$arg2()
                   : null,
                   Collectors.toList()
               )
           )
       )
   ));
System.out.println(result);

It produces this output:它产生这个 output:

{PROPERTY=['foo', 'bar', 'foo1', 'bar2'], OTHER_PROPERTY=['etc']}

Not sure if this exactly matches your requirements, but it should be straightforward to tweak the code accordingly不确定这是否完全符合您的要求,但相应地调整代码应该很简单

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

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