Filter是MOQL提供的主要功能之一,它支持SQL语法中Where部分的语法描述,能够通过编写条件语句对内存中的数据进行条件匹配。它可以用于对从数据库中查询回的数据集进行二次查询的功能;还能够用于数据处理时对数据流的实时过滤等。
Filter功能建立在Operand的基础上,Operand决定了能对哪些数据进行过滤。简单来说包括:常量、变量、数组、集合、对象等(详见文章MOQL—Operand(操作数))。这些数据必须封装在一个名为EntityMap的数据对象中,Filter就可以按照预定义的语法对数据进行匹配了。
Filter支持的运算符包括:关系运算符与逻辑运算符两大类。关系运算符包括:大于(>)、小于(<)、等于(=)、大于等于(>=)、小于等于(<=)、不等于(<>)、between..and、like、in、is等。逻辑运算符包括:逻辑与(and)、逻辑或(or)以及逻辑非(not)。关系运算符的运算优先级高于逻辑运算符,但所有关系运算符的优先级一致,没有差别。逻辑运算符的优先级中逻辑非(not)>逻辑与(and)>逻辑或 (or)。Filter也支持小括号”()”,可以通过小括号改变算符的执行顺序。
关系运算符的代码示例如下:
try { EntityMap entityMap = new EntityMapImpl(); entityMap.putEntity("num", 123); entityMap.putEntity("bean", new BeanA("bean", 100) Filter filter = MoqlUtils.createFilter("num = 123"); System.out.println(filter.isMatch(entityMap)); filter = MoqlUtils.createFilter("num < 160"); System.out.println(filter.isMatch(entityMap)); filter = MoqlUtils.createFilter("bean.num > 0 "); System.out.println(filter.isMatch(entityMap)); filter = MoqlUtils.createFilter("num <> 123"); System.out.println(filter.isMatch(entityMap)); filter = MoqlUtils.createFilter("num >= 123"); System.out.println(filter.isMatch(entityMap)); filter = MoqlUtils.createFilter("num <= 100"); System.out.println(filter.isMatch(entityMap)); // between..and filter = MoqlUtils.createFilter("bean.num between 0 and 200"); System.out.println(filter.isMatch(entityMap)); filter = MoqlUtils.createFilter("bean.num between 0 and 100"); System.out.println(filter.isMatch(entityMap)); // like filter = MoqlUtils.createFilter("bean.name like '%ean'"); System.out.println(filter.isMatch(entityMap)); filter = MoqlUtils.createFilter("bean.name like '%ea.'"); System.out.println(filter.isMatch(entityMap)); filter = MoqlUtils.createFilter("bean.name like '%e%'"); System.out.println(filter.isMatch(entityMap)); filter = MoqlUtils.createFilter("bean.name like '%c%'"); System.out.println(filter.isMatch(entityMap)); // in filter = MoqlUtils.createFilter("bean.name in ('Abean', 'Bbean')"); System.out.println(filter.isMatch(entityMap)); filter = MoqlUtils.createFilter("bean.name in ('Cbean', 'Bbean')"); System.out.println(filter.isMatch(entityMap)); filter = MoqlUtils.createFilter("50 in (bean.getArray())"); System.out.println(filter.isMatch(entityMap)); filter = MoqlUtils.createFilter("150 in (bean.getArray())"); System.out.println(filter.isMatch(entityMap)); // is filter = MoqlUtils.createFilter("bean.name is null"); System.out.println(filter.isMatch(entityMap)); filter = MoqlUtils.createFilter("bean.name is not null"); System.out.println(filter.isMatch(entityMap)); } catch (MoqlException e) { e.printStackTrace(); } |
逻辑运算符的代码示例如下:
try { EntityMap entityMap = new EntityMapImpl(); entityMap.putEntity("num", 123); entityMap.putEntity("bean", new BeanA("bean", 100)); Filter filter = MoqlUtils.createFilter("num > 100 and bean.num < 200 and bean.name like'%ean'"); System.out.println(filter.isMatch(entityMap)); filter = MoqlUtils.createFilter("bean.num > 0 and bean.num < 100"); System.out.println(filter.isMatch(entityMap)); // or filter = MoqlUtils.createFilter("num > 150 or bean.num = 100"); System.out.println(filter.isMatch(entityMap)); filter = MoqlUtils.createFilter("num > 150 or bean.num <> 100"); System.out.println(filter.isMatch(entityMap)); // not filter = MoqlUtils.createFilter("not bean.num <> 100"); System.out.println(filter.isMatch(entityMap)); filter = MoqlUtils.createFilter("not num > 150 or bean.num = 100"); // paren System.out.println(filter.isMatch(entityMap)); filter = MoqlUtils.createFilter("not (num > 150 or bean.num = 100)"); System.out.println(filter.isMatch(entityMap)); filter = MoqlUtils.createFilter("bean.num < 100 and num > 100 or bean.num = 100"); System.out.println(filter.isMatch(entityMap)); filter = MoqlUtils.createFilter("bean.num < 100 and (num > 100 or bean.num = 100)"); System.out.println(filter.isMatch(entityMap)); } catch (MoqlException e) { e.printStackTrace(); } |
以上两段代码的执行输出结果非常简单,在此不作赘述。示例中的Bean对象的定义可以通过下载MOQL的jar包获得,也可以随意自定义一个。MOQL的相关路径如下:
项目地址:http://sourceforge.net/projects/moql/
代码路径:svn://svn.code.sf.net/p/moql/code/trunk