java - 复杂的字段条件判断解析
PHPz
PHPz 2017-04-18 10:52:25
0
3
484

应用场景简化描述

现在有个用户Person,包含int age, BigDecimal money,java.util.Date accessTime等字段,对应于用户表person:

年龄(age : int) 资产 (money : number) 入网日期 (accessTime : date)
20 100.00 2017-03-24
20 150.00 2016-05-07
21 300.00 2015-04-03
21 240.00 2015-07-15
22 300.00 2014-12-21
21 300.00 2014-12-21

另外,有一张条件表,condition:

字段名(fieldName : varchar) 运算符(oper : varchar) 阈值 (threshold : varchar )
age = 21
money > 280.05
accessTime > 2015-05-31

条件表condition用来配置过滤用户person的条件,表示要过滤出

  • 年龄等于21岁

  • 资产大于280.05

  • 入网日期在 2015-06-01之后

的所有用户。

其中,oper可取的值有 = , < , > , >= , <= , in , between

如果oper为inbetween, 则阈值为多个,用逗号隔开。

问题

现在,对于不同的字段,在条件表condition里都是varchar类型,而在person表中,却有不同的类型。

而且,条件表里的条件是从web系统页面上,由用户配置的;也就是说,条件的个数不确定,配置的字段,运算符,阈值也都是不确定的。

问: 如何才能使用java代码,先将所有用户以及条件查出来,然后遍历每个用户,对于每个用户,遍历每个条件,怎样才能正确的判断该用户通过所有的条件检查,得出通过条件筛选的用户列表?(上边的场景是实际场景简化后的,请不要给出拼接sql,通过sql过滤的答案)


是不是可以把person的记录生成xml文件,条件生成xsd文件,用xsd去校验xml ??

PHPz
PHPz

学习是最好的投资!

reply all(3)
小葫芦

If you don’t use SQL, you can consider using the chain of responsibility model to get all the data and put it in the linkedlist.
Then write a filter to filter the content in the collection. Each condition can be equivalent to a filter

刘奇

How big is your user table? Find out all of it.

The formal method is to use sql to check. . .

A more advanced approach should be to customize the domain-specific language and then convert it to sql. It is impossible to use xml filtering anyway.

左手右手慢动作

I feel better about youcondition表没什么用啊?仅仅是存储数据?如果临时的直接用json传过来就好了,如果你要持久存储的话,起码要个admin_id什么的,来标记下是谁的筛选条件。感觉效率不如直接扔到redis.

See you are using Java, but I am used to using PHP, so I will briefly describe my idea using PHP...

Write oneFilter类处理condition转换为sql.

class Filter {
    public function doFilter() {
        //1. 获取登录的admin_id
        //2. 获取筛选条件
        /**
         * 数据格式例子为: [
         *    'field' => 'age';, 
         *    'type'  => 'compare',
         *    'ext'   => '>',
         *    'value' => '2'
         * ]
         */
        foreach($conditions as $condition) {
            // 根据type获取对应的$conditionHandle
            $conditionHandle::parse($condition);
        }
    }
}

// Condition接口
interface Condition {
    // 解析数据,返回sql片段和需要填充的数据(此处使用PDO预处理,减少SQL注入)
    static function parse($data);
}

// ConditionHandle
namespace Condition;
class Compare implements Condition {
    // 执行流程
    public static function parse($data) {
        ...
        return [
            'sql' => 'age > ?',
            'params' => [ 2 ]
        ];
    }
}

Handwritten, please ignore grammatical errors...

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!